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

updated priors vignette so that it works, exported required functions #2439

Merged
merged 18 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ For more information about this file see also [Keep a Changelog](http://keepacha
- Replace deprecated `rlang::UQ` syntax with the recommended `!!`
- Explicitly use `PEcAn.uncertainty::read.ensemble.output` in `PEcAn.utils::get.results`. Otherwise, it would sometimes use the deprecated `PEcAn.utils::read.ensemble.output` version.
- History page would not pass the hostname parameter when showing a running workflow, this would result in the running page showing an error.
- Fixed Priors vignette (#2439)

### Changed
- Updated modules/rtm PROSPECT docs
Expand Down
3 changes: 2 additions & 1 deletion modules/priors/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Depends:
PEcAn.utils
Imports:
PEcAn.logger,
ggplot2
ggplot2,
MASS
Suggests:
testthat
Encoding: UTF-8
Expand Down
8 changes: 5 additions & 3 deletions modules/priors/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,posterior.density)
S3method(plot,prior.density)
S3method(plot,trait)
infotroph marked this conversation as resolved.
Show resolved Hide resolved
export(create.density.df)
export(fit.dist)
export(get.quantiles.from.density)
export(get.sample)
export(plot_densities)
export(plot_posterior.density)
export(plot_prior.density)
export(plot_trait)
export(pr.dens)
export(pr.samp)
export(prior.fn)
export(priorfig)
importFrom(ggplot2,aes)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_text)
Expand Down
76 changes: 39 additions & 37 deletions modules/priors/R/plots.R
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
##--------------------------------------------------------------------------------------------------#
##' Plots a prior density from a parameterized probability distribution
##'
##' @name plot.prior.density
##' @name plot_prior.density
##' @title Add Prior Density
##' @param prior.density
##' @param base.plot a ggplot object (grob), created by \code{\link{create.base.plot}} if none provided
##' @param prior.color color of line to be plotted
##' @return plot with prior density added
##' @seealso \code{\link{pr.dens}}
##' @author David LeBauer
##' @export
##' @export plot_prior.density
##' @examples
##' \dontrun{
##' plot.prior.density(pr.dens('norm', 0, 1))
##' plot_prior.density(pr.dens('norm', 0, 1))
##' }
plot.prior.density <- function(prior.density, base.plot = NULL, prior.color = "black") {
plot_prior.density <- function(prior.density, base.plot = NULL, prior.color = "black") {
if (is.null(base.plot)) {
base.plot <- create.base.plot()
}
new.plot <- base.plot + geom_line(data = prior.density, aes(x = x, y = y), color = prior.color)
return(new.plot)
} # plot.prior.density
} # plot_prior.density


##--------------------------------------------------------------------------------------------------#
##' Add posterior density to a plot
##'
##' @name plot.posterior.density
##' @name plot_posterior.density
##' @title Add posterior density.
##' @param posterior.density
##' @param base.plot a ggplot object (grob), created by \code{\link{create.base.plot}} if none provided
##' @return plot with posterior density line added
##' @export
##' @export plot_posterior.density
##' @importFrom ggplot2 geom_line aes
##' @author David LeBauer
plot.posterior.density <- function(posterior.density, base.plot = NULL) {
plot_posterior.density <- function(posterior.density, base.plot = NULL) {
if (is.null(base.plot)) {
base.plot <- create.base.plot()
}
new.plot <- base.plot + geom_line(data = posterior.density, aes(x = x, y = y))
return(new.plot)
} # plot.posterior.density
} # plot_posterior.density


##--------------------------------------------------------------------------------------------------#
Expand All @@ -54,6 +54,7 @@ plot.posterior.density <- function(posterior.density, base.plot = NULL) {
##' @param xlim limits for x axis
##' @author David LeBauer
##' @return plot / grob of prior distribution with data used to inform the distribution
##' @export
##' @importFrom ggplot2 ggplot aes theme_bw scale_x_continuous scale_y_continuous element_blank element_text geom_rug geom_line geom_point
priorfig <- function(priordata = NA, priordensity = NA, trait = "", xlim = "auto", fontsize = 18) {
if (is.data.frame(priordata)) {
Expand All @@ -78,7 +79,7 @@ priorfig <- function(priordata = NA, priordensity = NA, trait = "", xlim = "auto
axis.text.x = element_text(size = fontsize),
axis.title.y = element_blank(), ## hide y axis label
axis.title.x = element_text(size = fontsize * 0.9),
plot.title = element_text(size = fontsize * 1.1))
plot_title = element_text(size = fontsize * 1.1))
dlebauer marked this conversation as resolved.
Show resolved Hide resolved

if (is.data.frame(priordata)) {
priordata <- subset(priordata, subset = !is.na(x))
Expand All @@ -101,7 +102,7 @@ priorfig <- function(priordata = NA, priordensity = NA, trait = "", xlim = "auto
##--------------------------------------------------------------------------------------------------#
##' Plot trait density and data
##'
##' @name plot.trait
##' @name plot_trait
##' @title Plot trait density
##' @param trait character, name of trait to be plotted
##' @param prior named distribution with parameters
Expand All @@ -111,18 +112,18 @@ priorfig <- function(priordata = NA, priordensity = NA, trait = "", xlim = "auto
##' @return plot (grob) object
##' @author David LeBauer
##' @importFrom ggplot2 theme_bw aes scale_x_continuous labs element_text element_blank
##' @export
##' @export plot_trait
##' @examples
##' \dontrun{
##' prior1 <- data.frame(distn = 'norm',
##' parama = 20,
##' paramb = 5)
##' data1 <- data.frame(Y = c(19, 21), se = c(1,1))
##' plot.trait(trait = 'Vcmax',
##' plot_trait(trait = 'Vcmax',
##' prior = prior1,
##' trait.df = data1)
##' }
plot.trait <- function(trait,
plot_trait <- function(trait,
prior = NULL,
posterior.sample = NULL,
trait.df = NULL,
Expand All @@ -132,23 +133,23 @@ plot.trait <- function(trait,
logx = FALSE) {

## Determine plot components
plot.posterior <- !is.null(posterior.sample)
plot.prior <- !is.null(prior)
plot.data <- !is.null(trait.df)
plot_posterior <- !is.null(posterior.sample)
plot_prior <- !is.null(prior)
plot_data <- !is.null(trait.df)

## get units for plot title
units <- trait.lookup(trait)$units

if(plot.data) trait.df <- jagify(trait.df)
if(plot_data) trait.df <- jagify(trait.df)

if (plot.prior) {
prior.color <- ifelse(plot.posterior, "grey", "black")
if (plot_prior) {
prior.color <- ifelse(plot_posterior, "grey", "black")
prior.density <- create.density.df(distribution = prior)
prior.density <- prior.density[prior.density$x > 0, ]
} else {
prior.density <- data.frame(x = NA, y = NA)
}
if (plot.posterior) {
if (plot_posterior) {
posterior.density <- create.density.df(samps = posterior.sample)
posterior.density <- posterior.density[posterior.density$x > 0, ]
} else {
Expand All @@ -170,13 +171,13 @@ plot.trait <- function(trait,
x.ticks <<- pretty(c(0, x.lim[2]))

base.plot <- create.base.plot() + theme_bw()
if (plot.prior) {
base.plot <- plot.prior.density(prior.density, base.plot = base.plot, prior.color = prior.color)
if (plot_prior) {
base.plot <- plot_prior.density(prior.density, base.plot = base.plot, prior.color = prior.color)
}
if (plot.posterior) {
base.plot <- plot.posterior.density(posterior.density, base.plot = base.plot)
if (plot_posterior) {
base.plot <- plot_posterior.density(posterior.density, base.plot = base.plot)
}
if (plot.data) {
if (plot_data) {
base.plot <- plot_data(trait.df, base.plot = base.plot, ymax = y.lim[2])
}

Expand All @@ -191,38 +192,39 @@ plot.trait <- function(trait,
axis.ticks.y = element_blank(),
axis.line.y = element_blank(),
legend.position = "none",
plot.title = element_text(size = fontsize$title),
plot_title = element_text(size = fontsize$title),
dlebauer marked this conversation as resolved.
Show resolved Hide resolved
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
return(trait.plot)
} # plot.trait
} # plot_trait


##--------------------------------------------------------------------------------------------------#
##' Plot probability density and data
##'
##' @name plot.densities
##' @name plot_densities
##' @title Plot Trait Probability Densities
##' @param sensitivity.results list containing sa.samples and sa.splines
##' @param sensitivity.results list containing sa.samples and sa.splines
##' @export plot_densities
##' @param outdir directory in which to generate figure as pdf
##' @author David LeBauer
##' @return outputs plots in outdir/sensitivity.analysis.pdf file
plot.densities <- function(density.plot.inputs, outdir, ...) {
trait.samples <- density.plot.inputs$trait.samples
trait.df <- density.plot.inputs$trait.df
prior.trait.samples <- density.plot.inputs$trait.df
plot_densities <- function(density.plot_inputs, outdir, ...) {
trait.samples <- density.plot_inputs$trait.samples
trait.df <- density.plot_inputs$trait.df
prior.trait.samples <- density.plot_inputs$trait.df

traits <- names(trait.samples)
pdf(paste0(outdir, "trait.densities.pdf"), height = 12, width = 20)

for (trait in traits) {
density.plot <- plot.density(trait.sample = trait.samples[, trait],
density.plot <- plot_density(trait.sample = trait.samples[, trait],
trait.df = trait.df[[trait]], ...)
print(sensitivity.plot)
}
dev.off()
} # plot.densities
} # plot_densities


##--------------------------------------------------------------------------------------------------#
Expand All @@ -233,7 +235,7 @@ plot.densities <- function(density.plot.inputs, outdir, ...) {
##' @param priordensity density dataframe generated by \code{\link{create.density.df}}
##' @param quantiles default is the median and 95\% CI;
##' @author David LeBauer
##' @export
##' @export get.quantiles.from.density
##' @examples
##' prior.df <- create.density.df(distribution = list('norm',0,1))
##' get.quantiles.from.density(prior.df)
Expand Down
12 changes: 6 additions & 6 deletions modules/priors/R/priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ fit.dist <- function(trait.data, trait = colnames(trait.data),
}
## warning(immediate. = TRUE)
nostart.dists <- dists[dists %in% c("weibull", "lognormal", "gamma", "normal")]
a <- lapply(nostart.dists, function(x) suppressWarnings(fitdistr(trait.data, x)))
a <- lapply(nostart.dists, function(x) suppressWarnings(MASS::fitdistr(trait.data, x)))
names(a) <- nostart.dists
if ("f" %in% dists) {
print(trait)
if (trait == "tt") {
a[["f"]] <- suppressWarnings(fitdistr(trait.data, "f",
a[["f"]] <- suppressWarnings(MASS::fitdistr(trait.data, "f",
start = list(df1 = 100, df2 = 200)))
} else if (trait == "sla") {
a[["f"]] <- suppressWarnings(fitdistr(trait.data, "f",
a[["f"]] <- suppressWarnings(MASS::fitdistr(trait.data, "f",
start = list(df1 = 6, df2 = 1)))
} else if (trait == "rrr") {
a[["f"]] <- suppressWarnings(fitdistr(trait.data, "f",
a[["f"]] <- suppressWarnings(MASS::fitdistr(trait.data, "f",
start = list(df1 = 6, df2 = 1)))
} else if (trait == "q") {
a[["f"]] <- suppressWarnings(fitdistr(trait.data, "f",
a[["f"]] <- suppressWarnings(MASS::fitdistr(trait.data, "f",
start = list(df1 = 1, df2 = 2)))
} else {
PEcAn.logger::logger.severe(paste(trait, "not supported!"))
}
}
if ("beta" %in% dists) {
a[["beta"]] <- suppressWarnings(fitdistr(trait.data, "beta",
a[["beta"]] <- suppressWarnings(MASS::fitdistr(trait.data, "beta",
start = list(shape1 = 2, shape2 = 1)))
}
aicvalues <- lapply(a, AIC)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.