Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some warnings and notes from PEcAn.MA package. #2956

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1098a35
Cleared Some notes and warnings of PEcAn.MA package.
nanu1605 Jul 12, 2022
20e49b0
some changes in global variables.
nanu1605 Jul 12, 2022
7a89622
Binding variables locally to the function "pecan.ma.summary"
nanu1605 Jul 12, 2022
7e94677
Merge branch 'meta_analysis' of https://github.com/nanu1605/pecan int…
nanu1605 Jul 12, 2022
c874c0c
Update modules/meta.analysis/R/meta.analysis.summary.R
nanu1605 Jul 12, 2022
c885215
Update modules/meta.analysis/DESCRIPTION
nanu1605 Jul 12, 2022
df5c7a1
Update modules/meta.analysis/R/meta.analysis.R
nanu1605 Jul 12, 2022
4a37f07
Fixed undefined global function.
nanu1605 Jul 13, 2022
8304559
Resolved R2WinBUGS dependency.
nanu1605 Jul 13, 2022
aca2329
Changed Documentation.
nanu1605 Jul 13, 2022
9bcf367
Changed Documentation.
nanu1605 Jul 13, 2022
eb4ac88
Merge branch 'meta_analysis' of https://github.com/nanu1605/pecan int…
nanu1605 Jul 13, 2022
4f972ca
added ggplot2 to DESCRIPTION
nanu1605 Jul 13, 2022
c4c7827
Added ggplot2 in Suggests.
nanu1605 Jul 13, 2022
4933786
updated pecan.depends.R
nanu1605 Jul 15, 2022
bc37d10
Update modules/meta.analysis/R/meta.analysis.summary.R
nanu1605 Jul 16, 2022
7c3526f
changed Makefile.depends
nanu1605 Jul 16, 2022
8f331b2
minor changes
nanu1605 Jul 16, 2022
a4145e1
Fixed Vignette Warning
nanu1605 Jul 16, 2022
ddb1e44
minor changes
nanu1605 Jul 16, 2022
8ad821e
Update modules/meta.analysis/DESCRIPTION
nanu1605 Jul 16, 2022
5344903
Merge branch 'develop' into meta_analysis
dlebauer Jul 18, 2022
c55f90e
Merge branch 'develop' into meta_analysis
nanu1605 Jul 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions modules/meta.analysis/R/approx.posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ approx.posterior <- function(trait.mcmc, priors, trait.data = NULL, outdir = NUL
posteriors <- priors
do.plot <- !is.null(outdir)
if (do.plot == TRUE) {
pdf(file.path(outdir, paste("posteriors", filename.flag, ".pdf", sep = "")))
grDevices::pdf(file.path(outdir, paste("posteriors", filename.flag, ".pdf", sep = "")))
}

## loop over traits
Expand All @@ -55,21 +55,21 @@ approx.posterior <- function(trait.mcmc, priors, trait.data = NULL, outdir = NUL
zerobound <- c("exp", "gamma", "lnorm", "weibull")
if (pdist %in% "beta") {
m <- mean(dat)
v <- var(dat)
v <- stats::var(dat)
k <- (1 - m)/m
a <- (k / ((1 + k) ^ 2 * v) - 1) / (1 + k)
b <- a * k
fit <- try(suppressWarnings(MASS::fitdistr(dat, "beta", list(shape1 = a, shape2 = b))), silent = TRUE)

if (do.plot) {
x <- seq(0, 1, length = 1000)
plot(density(dat), col = 2, lwd = 2, main = trait)
plot(stats::density(dat), col = 2, lwd = 2, main = trait)
if (!is.null(trait.data)) {
rug(trait.data[[trait]]$Y, lwd = 2, col = "purple")
graphics::rug(trait.data[[trait]]$Y, lwd = 2, col = "purple")
}
lines(x, dbeta(x, fit$estimate[1], fit$estimate[2]), lwd = 2, type = "l")
lines(x, dbeta(x, pparm[1], pparm[2]), lwd = 3, type = "l", col = 3)
legend("topleft",
graphics::lines(x, dbeta(x, fit$estimate[1], fit$estimate[2]), lwd = 2, type = "l")
graphics::lines(x, dbeta(x, pparm[1], pparm[2]), lwd = 3, type = "l", col = 3)
graphics::legend("topleft",
legend = c("data", "prior", "post", "approx"),
col = c("purple", 3, 2, 1), lwd = 2)
}
Expand Down Expand Up @@ -111,15 +111,15 @@ approx.posterior <- function(trait.mcmc, priors, trait.data = NULL, outdir = NUL
## default: NORMAL
posteriors[trait, "distn"] <- "norm"
posteriors[trait, "parama"] <- mean(dat)
posteriors[trait, "paramb"] <- sd(dat)
posteriors[trait, "paramb"] <- stats::sd(dat)
if (do.plot) {
.dens_plot(posteriors, priors, ptrait, dat, trait, trait.data)
}
}
} ## end trait loop

if (do.plot) {
dev.off()
imguR::dev.off()
nanu1605 marked this conversation as resolved.
Show resolved Hide resolved
}

return(posteriors)
Expand Down Expand Up @@ -159,13 +159,13 @@ approx.posterior <- function(trait.mcmc, priors, trait.data = NULL, outdir = NUL
rng <- range(trait.data[[trait]]$Y)
}

plot(density(dat), col = 2, lwd = 2, main = trait, xlim = rng)
plot(stats::density(dat), col = 2, lwd = 2, main = trait, xlim = rng)
if (!is.null(trait.data)) {
rug(trait.data[[trait]]$Y, lwd = 2, col = "purple")
graphics::rug(trait.data[[trait]]$Y, lwd = 2, col = "purple")
}
lines(x, f(x), lwd = 2, type = "l")
lines(x, fp(x), lwd = 3, type = "l", col = 3)
legend("topleft",
graphics::lines(x, f(x), lwd = 2, type = "l")
graphics::lines(x, fp(x), lwd = 3, type = "l", col = 3)
graphics::legend("topleft",
legend = c("data", "prior", "post", "approx"),
col = c("purple", 3, 2, 1), lwd = 2)
}
6 changes: 3 additions & 3 deletions modules/meta.analysis/R/meta.analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ pecan.ma <- function(trait.data, prior.distns,
"\nmean:", signif(mean(data$Y, na.rm = TRUE), 3),
"\nn:", length(data$Y)))
writeLines("stem plot of data points")
writeLines(paste(stem(data$Y)))
writeLines(paste(graphics::stem(data$Y)))
if (any(!is.na(data$obs.prec)) && all(!is.infinite(data$obs.prec))) {
writeLines("stem plot of obs.prec:")
writeLines(paste(stem(data[["obs.prec"]] ^ 2)))
writeLines(paste(graphics::stem(data[["obs.prec"]] ^ 2)))
} else {
writeLines(paste("no estimates of SD for", trait.name))
}
Expand All @@ -155,7 +155,7 @@ pecan.ma <- function(trait.data, prior.distns,
print(summary(jags.out))
}

jags.out.trunc <- window(jags.out, start = j.iter / 2)
jags.out.trunc <- stats::window(jags.out, start = j.iter / 2)

mcmc.object[[trait.name]] <- jags.out.trunc
}
Expand Down
17 changes: 8 additions & 9 deletions modules/meta.analysis/R/meta.analysis.summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
##' }
##' @author David LeBauer, Shawn Serbin
pecan.ma.summary <- function(mcmc.object, pft, outdir, threshold = 1.2, gg = FALSE) {
ggmcmc <- is.mcmc.list <- theme_set <- theme_bw <- ggs <- pdf<- box <- dev.off <- NULL
fail <- rep(FALSE, length(mcmc.object))
names(fail) <- names(mcmc.object)
not.converged <- data.frame()
Expand All @@ -45,9 +44,9 @@ pecan.ma.summary <- function(mcmc.object, pft, outdir, threshold = 1.2, gg = FAL
}
## new diagnostic plots. But very slow & !any(grepl('^gg', dir(outdir)))){
if (gg) {
if (is.mcmc.list(mcmc.object[[trait]])) {
theme_set(theme_bw())
ggmcmc(ggs(mcmc.object[[trait]]),
if (coda::is.mcmc.list(mcmc.object[[trait]])) {
ggplot2::theme_set(ggplot2::theme_bw())
ggmcmc::ggmcmc(ggmcmc::ggs(mcmc.object[[trait]]),
plot = c("ggs_density", "ggs_traceplot", "ggs_autocorrelation", "ggs_Rhat", "ggs_geweke"),
file.path(outdir, paste0("gg.ma.summaryplots.", trait, ".pdf")))
}
Expand All @@ -59,23 +58,23 @@ pecan.ma.summary <- function(mcmc.object, pft, outdir, threshold = 1.2, gg = FAL
maparms <- .maparms[c(which(.maparms %in% .parms), which(!.maparms %in% .parms))]

## plots for mcmc diagnosis
pdf(file.path(outdir, paste0("ma.summaryplots.", trait, ".pdf")))
grDevices::pdf(file.path(outdir, paste0("ma.summaryplots.", trait, ".pdf")))

for (i in maparms) {
plot(mcmc.object[[trait]][, i],
trace = FALSE,
density = TRUE,
main = paste("summary plots of", i, "for", pft, trait))
box(lwd = 2)
graphics::box(lwd = 2)
plot(mcmc.object[[trait]][, i], density = FALSE)
box(lwd = 2)
graphics::box(lwd = 2)
coda::autocorr.plot(mcmc.object[[trait]][, i][1], xlim = c(1, 50))
box(lwd = 2)
graphics::box(lwd = 2)
}
lattice::xyplot(mcmc.object[[trait]])
lattice::densityplot(mcmc.object[[trait]])
coda::acfplot(mcmc.object[[trait]])
dev.off()
imguR::dev.off()

## G-R diagnostics to ensure convergence
gd <- coda::gelman.diag(mcmc.object[[trait]])
Expand Down
10 changes: 5 additions & 5 deletions modules/meta.analysis/R/run.meta.analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ run.meta.analysis.pft <- function(pft, iterations, random = TRUE, threshold = 1.

## Check that data is consistent with prior
for (trait in names(jagged.data)) {
data.median <- median(jagged.data[[trait]]$Y)
data.median <- stats::median(jagged.data[[trait]]$Y)
prior <- prior.distns[trait, ]
check <- check_consistent(data.median, prior, trait, "data")
if (is.na(check)) {
Expand Down Expand Up @@ -123,7 +123,7 @@ run.meta.analysis.pft <- function(pft, iterations, random = TRUE, threshold = 1.

### Check that meta-analysis posteriors are consistent with priors
for (trait in names(trait.mcmc)) {
post.median <- median(as.matrix(trait.mcmc[[trait]][, "beta.o"]))
post.median <- stats::median(as.matrix(trait.mcmc[[trait]][, "beta.o"]))
prior <- prior.distns[trait, ]
check <- check_consistent(post.median, prior, trait, "data")
if (is.na(check)) {
Expand Down Expand Up @@ -157,7 +157,7 @@ run.meta.analysis.pft <- function(pft, iterations, random = TRUE, threshold = 1.
}
filename <- file.path(pathname, file)
file.copy(file.path(pft$outdir, file), filename)
dbfile.insert(pathname, file, "Posterior", pft$posteriorid, dbcon)
PEcAn.DB::dbfile.insert(pathname, file, "Posterior", pft$posteriorid, dbcon)
}
} # run.meta.analysis.pft

Expand Down Expand Up @@ -185,8 +185,8 @@ run.meta.analysis.pft <- function(pft, iterations, random = TRUE, threshold = 1.
##' @author Shawn Serbin, David LeBauer
run.meta.analysis <- function(pfts, iterations, random = TRUE, threshold = 1.2, dbfiles, database, use_ghs = TRUE) {
# process all pfts
dbcon <- db.open(database)
on.exit(db.close(dbcon), add = TRUE)
dbcon <- PEcAn.DB::db.open(database)
on.exit(PEcAn.DB::db.close(dbcon), add = TRUE)

result <- lapply(pfts, run.meta.analysis.pft, iterations = iterations, random = random,
threshold = threshold, dbfiles = dbfiles, dbcon = dbcon, use_ghs = use_ghs)
Expand Down
4 changes: 3 additions & 1 deletion modules/meta.analysis/R/single.MA.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
##' full loop over traits and PFTs.
##' @title Single MA
##' @param j.chains number of chains in meta-analysis
##' @param data data frame generated by jagify function with indexed values for greenhouse, treatment, and site (ghs, trt, site) as well as Y, SE, and n for each observation or summary statistic
##' @param data data frame generated by jagify function
##' with indexed values for greenhouse, treatment, and site (ghs, trt, site)
##' as well as Y, SE, and n for each observation or summary statistic.
##' @param j.iter number of mcmc samples
##' @param tauA prior on variance parameters
##' @param tauB prior on variance parameters
Expand Down