Package 'PEcAn.priors'

Title: PEcAn Functions Used to Estimate Priors from Data
Description: Functions to estimate priors from data.
Authors: David LeBauer
Maintainer: David LeBauer <[email protected]>
License: BSD_3_clause + file LICENSE
Version: 1.7.2
Built: 2024-06-27 21:31:31 UTC
Source: https://github.com/PecanProject/pecan

Help Index


Create Density Data Frame from Sample

Description

Returns a data frame from stats::density function

Usage

create.density.df(
  samps = NULL,
  zero.bounded = FALSE,
  distribution = NULL,
  n = 1000,
  ...
)

Arguments

samps

a vector of samples from a distribution

zero.bounded
distribution

list with elements distn, parama, paramb, e.g. list('norm', 0, 1)

Value

data frame with x and y = dens(x)

Author(s)

David LeBauer

Examples

prior.df <- create.density.df(distribution = list('norm',0,1))
plot(prior.df)
samp.df <- create.density.df(samps = rnorm(100))
lines(samp.df)

Fit distribution to data

Description

Fit a distribution to data

Usage

fit.dist(
  trait.data,
  trait = colnames(trait.data),
  dists = c("weibull", "lognormal", "gamma"),
  n = NULL
)

Arguments

trait.data

data for distribution

dists

list of distribution names

Value

best fit distribution

Author(s)

David LeBauer


Get the quantiles from prior density

Description

Find quantiles on a density data frame

Usage

get.quantiles.from.density(density.df, quantiles = c(0.025, 0.5, 0.975))

Arguments

quantiles

default is the median and 95% CI;

priordensity

density dataframe generated by create.density.df

Author(s)

David LeBauer

Examples

prior.df <- create.density.df(distribution = list('norm',0,1))
get.quantiles.from.density(prior.df)
samp.df <- create.density.df(samps = rnorm(100))
get.quantiles.from.density(samp.df)

Get Samples

Description

Take n random samples from prior

Usage

get.sample(prior, n = NULL, p = NULL)

Arguments

prior

data.frame with distn, parama, and optionally paramb.

n

number of samples to return from a random sample of the rdistn family of functions (e.g. rnorm)

p

vector of quantiles from which to sample the distribution; typically pre-generated upstream in the workflow to be used by the qdistn family of functions (e.g. qnorm)

Details

Similar to the prior sample function pr.samp, except 1) it takes the prior as a named dataframe or list and it can return either a random sample of length n OR a sample from a quantile specified as p

Value

vector with n random samples from prior

See Also

pr.samp

Examples

## Not run: 
# return 1st through 99th quantile of standard normal distribution:
PEcAn.priors::get.sample(
   prior = data.frame(distn = 'norm', parama = 0, paramb = 1), 
   p = 1:99/100)
# return 100 random samples from standard normal distribution:
PEcAn.priors::get.sample(
   prior = data.frame(distn = 'norm', parama = 0, paramb = 1), 
   n = 100)

## End(Not run)

Plot probability density and data

Description

Plot probability density and data

Usage

plot_densities(density.plot_inputs, outdir, ...)

Arguments

density.plot_inputs

list containing trait.samples and trait.df

outdir

directory in which to generate figure as pdf

...

passed on to plot_density

Value

outputs plots in outdir/sensitivity.analysis.pdf file

Author(s)

David LeBauer


Add posterior density to a plot

Description

Add posterior density to a plot

Usage

plot_posterior.density(posterior.density, base.plot = NULL)

Arguments

posterior.density

data frame containing columns x and y

base.plot

a ggplot object (grob), created if none provided

Value

plot with posterior density line added

Author(s)

David LeBauer


Plots a prior density from a parameterized probability distribution

Description

Plots a prior density from a parameterized probability distribution

Usage

plot_prior.density(prior.density, base.plot = NULL, prior.color = "black")

Arguments

prior.density

data frame containing columns x and y

base.plot

a ggplot object (grob), created if none provided

prior.color

color of line to be plotted

Value

plot with prior density added

Author(s)

David LeBauer

See Also

pr.dens

Examples

## Not run: 
plot_prior.density(pr.dens('norm', 0, 1))

## End(Not run)

Plot trait density and data

Description

Plot trait density and data

Usage

plot_trait(
  trait,
  prior = NULL,
  posterior.sample = NULL,
  trait.df = NULL,
  fontsize = list(title = 18, axis = 14),
  x.lim = NULL,
  y.lim = NULL,
  logx = FALSE
)

Arguments

trait

dataframe with id, figid and units of the trait to be plotted

prior

named distribution with parameters

posterior.sample

samples from posterior distribution whose density should be plotted

trait.df

data to be plotted, in a format accepted by jagify

fontsize, x.lim, y.lim, logx

passed on to ggplot

Value

plot (grob) object

Author(s)

David LeBauer

Examples

## Not run: 
prior1 <- data.frame(distn = 'norm',
                     parama = 20,
                     paramb = 5)
data1  <- data.frame(Y = c(19, 21), se = c(1,1))
trait1 <- data.frame(id = 'Vcmax', figid = 'Vcmax', units = 'umol CO2 m-2 s-1')
plot_trait(trait = trait1,
          prior = prior1,
          trait.df = data1)

## End(Not run)

Calculate densities

Description

Calculates density at n points across the range of a parameter

Usage

pr.dens(distn, parama, paramb, n = 1000, alpha = 0.001)

Arguments

distn

distribution

parama

parameter

paramb

parameter

n

length of vector to be returned

alpha

sets range at which the distribution will be evaluated (e.g. from alpha to 1-alpha)

Details

For a distribution and parameters, return the density for values ranging from alpha to 1-alpha

Value

dataframe with equally spaced x values and the corresponding densities

Author(s)

David LeBauer


Sample from prior

Description

Take n random samples from prior

Usage

pr.samp(distn, parama, paramb, n)

Arguments

distn
parama
paramb
n

number of samples to return

Value

vector with n random samples from prior

See Also

{codeget.sample


prior.fn

Description

Prior fitting function for optimization

Usage

prior.fn(parms, x, alpha, distn, central.tendency = NULL, trait = NULL)

Arguments

parms

target for optimization

x

vector with c(lcl, ucl, ct) lcl / ucl = confidence limits, ct = entral tendency

alpha

quantile at which lcl/ucl are estimated (e.g. for a 95% CI, alpha = 0.5)

distn

named distribution, one of 'lnorm', 'gamma', 'weibull', 'beta'; support for other distributions not currently implemented

central.tendency

one of 'mode', 'median', and 'mean'

trait

name of trait, can be used for exceptions (currently used for trait == 'q')

Details

This function is used within DEoptim to parameterize a distribution to the central tendency and confidence interval of a parameter. This function is not very robust; currently it needs to be tweaked when distributions require starting values (e.g. beta, f)

Value

parms

Author(s)

David LeBauer

Examples

## Not run: 
  DEoptim(fn = prior.fn,
          lower = c(0, 0),
          upper = c(1000, 1000),
          x=c(2, 6, 3.3),
          alpha = 0.05,
          distn = 'lnorm')$optim$bestmem

## End(Not run)

Prior Figure

Description

Plot prior density and data

Usage

priorfig(
  priordata = NA,
  priordensity = NA,
  trait = NA,
  xlim = "auto",
  fontsize = 18
)

Arguments

priordata

observations to be plotted as points

priordensity

density of prior distribution, calculated by prior.density

trait

dataframe with id, figid and units of the trait

xlim

limits for x axis

Value

plot / grob of prior distribution with data used to inform the distribution

Author(s)

David LeBauer