Package 'PEcAn.uncertainty'

Title: PEcAn Functions Used for Propagating and Partitioning Uncertainties in Ecological Forecasts and Reanalysis
Description: The Predictive Ecosystem Carbon Analyzer (PEcAn) is a scientific workflow management tool that is designed to simplify the management of model parameterization, execution, and analysis. The goal of PECAn is to streamline the interaction between data and models, and to improve the efficacy of scientific investigation.
Authors: David LeBauer, Mike Dietze, Xiaohui Feng, Dan Wang, Carl Davidson, Rob Kooper, Shawn Serbin
Maintainer: David LeBauer <[email protected]>
License: BSD_3_clause + file LICENSE
Version: 1.8.0.9000
Built: 2024-12-17 18:33:10 UTC
Source: https://github.com/PecanProject/pecan

Help Index


Generate ensemble filenames

Description

Generates a vector of filenames to be used for PEcAn ensemble output files. All paths start from directory 'settings$outdir', which will be created if it does not exist.

Usage

ensemble.filename(
  settings,
  prefix = "ensemble.samples",
  suffix = "Rdata",
  all.var.yr = TRUE,
  ensemble.id = settings$ensemble$ensemble.id,
  variable = settings$ensemble$variable,
  start.year = settings$ensemble$start.year,
  end.year = settings$ensemble$end.year
)

Arguments

settings

list of PEcAn settings.

prefix

string to appear at the beginning of the filename

suffix

file extension: string to appear at the end of the filename

all.var.yr

logical: does ensemble include all vars and years? If FALSE, filename will include years and vars

ensemble.id

ensemble ID(s)

variable

variable(s) included in the ensemble.

start.year, end.year

first and last year simulated.

Details

Typically used by passing only a settings object, but all values can be overridden for manual use.

If only a single variable or a subset of years are needed, the generated filename will identify these in the form If all vars and years are included, set 'all.yr.var' to TRUE to get a filename of the form 'prefix.ensemble_id.suffix'. All elements are recycled vectorwise.

Value

a vector of filenames, each in the form '[settings$outdir]/[prefix].[ensemble.ID].[variable].[start.year].[end.year][suffix]'.

Author(s)

Ryan Kelly


Plots an ensemble time-series from PEcAn for the selected target variable

Description

Plots an ensemble time-series from PEcAn for the selected target variable

Usage

ensemble.ts(ensemble.ts, observations = NULL, window = 1, ...)

Arguments

ensemble.ts

ensemble timeseries to be plotted, as returned by [read.ensemble.ts()]

observations

observed data to plot over timeseries predictions

window

number of timepoints to average across

...

additional arguments, currently ignored

Value

nothing, generates an ensemble time-series plot

Author(s)

Michael Dietze, Ryan Kelly


Calculate parameters for heteroskedastic flux uncertainty

Description

Calculate parameters for heteroskedastic flux uncertainty

Usage

flux.uncertainty(
  measurement,
  QC = 0,
  flags = TRUE,
  bin.num = 10,
  transform = identity,
  minBin = 5,
  ...
)

Arguments

measurement

= flux time-series

QC

= quality control flag time series (0 = best)

flags

= additional flags on flux filtering of PAIRS (length = 1/2 that of the time series, TRUE = use).

bin.num

= number of bins (default = 10)

transform

= transformation of magnitude (default = identity)

minBin

= minimum number of points allowed in a bin

...

additional arguments, currently ignored

Value

return.list List of parameters from the fitted uncertainty model

Author(s)

Mike Dietze, Carl Davidson


Get delta between sequential flux datapoints

Description

Get delta between sequential flux datapoints

Usage

get.change(measurement)

Arguments

measurement

numeric vector

Value

Difference between consecutive measurements

Author(s)

Mike Dietze, Carl Davidson


Get coefficient of variance

Description

Given a set of numbers (a numeric vector), this returns the set's coefficient of variance.

Usage

get.coef.var(set)

Arguments

set

numeric vector of trait values

Value

coeficient of variance


Generic function for the elasticity

Description

Given the sensitivity, samples, and outputs for a single trait, return elasticity

Usage

get.elasticity(sensitivity, samples, outputs)

Arguments

sensitivity

univariate sensitivity of model to a parameter, can be calculated by get.sensitivity

samples

samples from trait distribution

outputs

model output from ensemble runs

Value

elasticity = normalized sensitivity


Get Ensemble Samples

Description

Get parameter values used in ensemble

Usage

get.ensemble.samples(
  ensemble.size,
  pft.samples,
  env.samples,
  method = "uniform",
  param.names = NULL,
  ...
)

Arguments

ensemble.size

number of runs in model ensemble

pft.samples

random samples from parameter distribution, e.g. from a MCMC chain

env.samples

env samples

method

the method used to generate the ensemble samples. Random generators: uniform, uniform with latin hypercube permutation. Quasi-random generators: halton, sobol, torus. Random generation draws random variates whereas quasi-random generation is deterministic but well equidistributed. Default is uniform. For small ensemble size with relatively large parameter number (e.g ensemble size < 5 and # of traits > 5) use methods other than halton.

param.names

a list of parameter names that were fitted either by MA or PDA, important argument, if NULL parameters will be resampled independently

...

Other arguments passed on to the sampling method

Details

Returns a matrix of randomly or quasi-randomly sampled trait values to be assigned to traits over several model runs. given the number of model runs and a list of sample distributions for traits The model run is indexed first by model run, then by trait

Value

matrix of (quasi-)random samples from trait distributions

Author(s)

David LeBauer, Istem Fer


Get g_i(phi_i)

Description

Calculate distribution of function of a variable

Usage

get.gi.phii(splinefuns, trait.samples, maxn = NULL)

Arguments

splinefuns

univariate spline functions created for each trait, e.g. by the sensitivity.analysis function.

trait.samples

n x m matrix (or list with m vectors of length n) of n parameter sets, each with a sample from m traits

maxn

maximum number of parameter sets to evaluate

Details

Transform trait parameter through trait-specific univariate model emulator spline function Given a matrix of parameter sets, calculates a matrix of model output by applying appropriate spline function to each parameter. Output is used with plot_variance_decomposition and spline.ensemble

Value

matrix of spline estimates of model output for each of n parameter sets

Author(s)

David LeBauer


Convert priors / MCMC samples to chains that can be sampled for model parameters

Description

Convert priors / MCMC samples to chains that can be sampled for model parameters

Usage

get.parameter.samples(
  settings,
  posterior.files = rep(NA, length(settings$pfts)),
  ens.sample.method = "uniform"
)

Arguments

settings

PEcAn settings object

posterior.files

list of filenames to read from

ens.sample.method

one of "halton", "sobol", "torus", "lhc", "uniform"

Author(s)

David LeBauer, Shawn Serbin, Istem Fer


Reads model output and runs sensitivity and ensemble analyses

Description

Output is placed in model output directory (settings$outdir).

Usage

get.results(
  settings,
  sa.ensemble.id = NULL,
  ens.ensemble.id = NULL,
  variable = NULL,
  start.year = NULL,
  end.year = NULL
)

Arguments

settings

list, read from settings file (xml) using read.settings

sa.ensemble.id, ens.ensemble.id

ensemble IDs for the sensitivity analysis and ensemble analysis. If not provided, they are first looked up from 'settings', then if not found they are not used and the most recent set of results is read from samples.Rdata in directory settings$outdir

variable

variables to retrieve, as vector of names or expressions

start.year, end.year

first and last years to retrieve

Author(s)

David LeBauer, Shawn Serbin, Mike Dietze, Ryan Kelly


Calculate the sensitivity of a function at the median

Description

This function evaluates the sensitivity of a model to a parameter. This is done by evaluating the first derivative of the univariate spline estimate of the model response at the parameter median.

Usage

get.sensitivity(trait.samples, sa.splinefun)

Arguments

trait.samples

parameter values to evaluate at their median

sa.splinefun

fitted spline function. Must take two arguments.

Value

numeric estimate of model sensitivity to parameter


Function for generating samples based on sampling method, parent or etc

Description

Function for generating samples based on sampling method, parent or etc

Usage

input.ens.gen(settings, input, method = "sampling", parent_ids = NULL)

Arguments

settings

list of PEcAn settings

input

name of input to sample, e.g. "met", "veg", "pss"

method

Method for sampling - For now looping or sampling with replacement is implemented

parent_ids

This is basically the order of the paths that the parent is sampled.See Details.

Details

If for example met was a parent and it's sampling method resulted in choosing the first, third and fourth samples, these are the ids that need to be sent as parent_ids to this function.

Value

For a given input/tag in the pecan xml and a method, this function returns a list with $id showing the order of sampling and $samples with samples of that input.

Examples

## Not run: input.ens.gen(settings,"met","sampling")

Calculates the excess kurtosis of a vector

Description

Note that this calculates the 'excess kurtosis', which is defined as kurtosis - 3. This statistic is used in the calculation of the standard deviation of sample variance in the function sd.var.

Usage

kurtosis(x)

Arguments

x

vector of values

Value

numeric value of kurtosis

Author(s)

David LeBauer

References

NIST/SEMATECH e-Handbook of Statistical Methods, http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm, 2011-06-20.


Plot fit for heteroskedastic flux uncertainty

Description

Plot fit for heteroskedastic flux uncertainty

Usage

plot_flux_uncertainty(f, ...)

Arguments

f

output of flux.uncertainty functions

...

optional graphical paramters

Author(s)

Mike Dietze, Carl Davidson


Plot functions and quantiles used in sensitivity analysis

Description

Generates a plot using plot_sensitivity for multiple traits.

Usage

plot_sensitivities(
  sensitivity.plot.inputs,
  prior.sensitivity.plot.inputs = NULL,
  ...
)

Arguments

sensitivity.plot.inputs

inputs

prior.sensitivity.plot.inputs

priors

...

arguments passed to plot_sensitivity

Value

list of plots, one per trait


Sensitivity plot

Description

Plot univariate response of model output to a trait parameter.

Usage

plot_sensitivity(
  sa.sample,
  sa.spline,
  trait,
  y.range = c(0, 50),
  median.i = 4,
  prior.sa.sample = NULL,
  prior.sa.spline = NULL,
  fontsize = list(title = 12, axis = 8),
  linesize = 1,
  dotsize = 2
)

Arguments

sa.sample

trait quantiles used in sensitivity analysis

sa.spline

spline function estimated from sensitivity analysis

trait

trait name for title

y.range

limits for y axis of plot

median.i

index of median value in sa.sample; median.i == which(as.numeric(rownames(sa.sample)) == 50)

prior.sa.sample

similar to sa.sample, but for prior distribution. If given, plots sensitivity for prior run

prior.sa.spline

similar to sa.spline, but for prior trait distribution.

fontsize

(optional) list with three arguments that can be set to vary the fontsize of the title, axis labels, and axis title in the sensitivity plots

linesize

passed to ggplot to set line thickness

dotsize

passed to ggplot to set point size

Details

Plots for a single trait; called by plot_sensitivities to plot sensitivity plots for multiple traits.

Value

object of class ggplot


Variance Decomposition Plots

Description

Plots variance decomposition tryptich

Usage

plot_variance_decomposition(
  plot.inputs,
  fontsize = list(title = 18, axis = 14)
)

Arguments

plot.inputs

Output from a sensitivity analysis. Output must be of the form given by sensitivity.results$variance.decomposition.output in model output

fontsize

list specifying the font size of the titles and axes of the graph

Author(s)

David LeBauer, Carl Davidson

Examples

x <- list(trait.labels = c('a', 'b', 'c'),
          coef.vars = c(a=1,b=0.5, c=0.1),
          elasticities = c(a=1,b=2,c=0.5),
          variances    = c(a = 20, b=30, c = 10))
do.call(gridExtra::grid.arrange, c(plot_variance_decomposition(x), ncol = 4))

Read Ameriflux L2 Data

Description

Read Ameriflux L2 Data

Usage

read.ameriflux.L2(file.name, year)

Arguments

file.name

path to file

year

currently ignored

Value

Ameriflux L2 data read from file

Author(s)

Mike Dietze, Carl Davidson


Read ensemble output

Description

Reads output from model ensemble

Usage

read.ensemble.output(
  ensemble.size,
  pecandir,
  outdir,
  start.year,
  end.year,
  variable,
  ens.run.ids = NULL
)

Arguments

ensemble.size

the number of ensemble members run

pecandir

specifies where pecan writes its configuration files

outdir

directory with model output to use in ensemble analysis

start.year

first year to include in ensemble analysis

end.year

last year to include in ensemble analysis

variable

target variables for ensemble analysis

ens.run.ids

dataframe. Must contain a column named "id" giving the run IDs to be read. If NULL, will attempt to read IDs from a file named "samples.Rdata" in pecandir

Details

Reads output for an ensemble of length specified by ensemble.size and bounded by start.year and end.year

Value

a list of ensemble model output

Author(s)

Ryan Kelly, David LeBauer, Rob Kooper


Reads ensemble time-series from PEcAn for the selected target variable

Description

Reads ensemble time-series from PEcAn for the selected target variable

Usage

read.ensemble.ts(
  settings,
  ensemble.id = NULL,
  variable = NULL,
  start.year = NULL,
  end.year = NULL
)

Arguments

settings

PEcAn settings object

ensemble.id

database ID, taken from settings if not specified

variable

variable name to process. Required

start.year, end.year

taken from settings if not specified

Value

list

Author(s)

Michael Dietze, Ryan Kelly


Read Sensitivity Analysis output

Description

Reads output of sensitivity analysis runs

Usage

read.sa.output(
  traits,
  quantiles,
  pecandir,
  outdir,
  pft.name = "",
  start.year,
  end.year,
  variable,
  sa.run.ids = NULL,
  per.pft = FALSE
)

Arguments

traits

model parameters included in the sensitivity analysis

quantiles

quantiles selected for sensitivity analysis

pecandir

specifies where pecan writes its configuration files

outdir

directory with model output to use in sensitivity analysis

pft.name

name of PFT used in sensitivity analysis (Optional)

start.year

first year to include in sensitivity analysis

end.year

last year to include in sensitivity analysis

variable

variables to be read from model output

sa.run.ids

list of run ids to read. If NULL, will look in 'pecandir' for a file named 'samples.Rdata' and read from that

per.pft

flag to determine whether we want SA on pft-specific variables

Value

dataframe with one col per quantile analysed and one row per trait, each cell is a list of AGB over time

Author(s)

Ryan Kelly, David LeBauer, Rob Kooper, Mike Dietze, Istem Fer


run ensemble.analysis

Description

run ensemble.analysis

Usage

run.ensemble.analysis(
  settings,
  plot.timeseries = NA,
  ensemble.id = NULL,
  variable = NULL,
  start.year = NULL,
  end.year = NULL,
  ...
)

Arguments

settings

PEcAn settings object

plot.timeseries

if TRUE plots a modeled timeseries of target variable(s) with CIs

ensemble.id

database ID, taken from settings if not specified

variable

variable name to process, taken from settings if not specified

start.year, end.year

taken from settings if not specified

...

additional arguments passed to [ensemble.ts()]

Value

nothing, creates ensemble plots as ensemble.analysis.pdf

Author(s)

David LeBauer, Shawn Serbin, Ryan Kelly


run sensitivity.analysis

Description

Runs the sensitivity analysis module on a finished run

Usage

run.sensitivity.analysis(
  settings,
  plot = TRUE,
  ensemble.id = NULL,
  variable = NULL,
  start.year = NULL,
  end.year = NULL,
  pfts = NULL,
  ...
)

Arguments

settings

a PEcAn settings object

plot

logical. Option to generate sensitivity analysis and variance decomposition plots (plot=TRUE) or to turn these plots off (plot=FALSE).

ensemble.id

ensemble ID

variable

which varibable(s) to do sensitivity analysis for. Defaults to all specified in 'settings'

start.year

defaults to what is specified in 'settings'

end.year

defaults to what is specified in 'settings'

pfts

a vector of PFT names found in 'settings' to run sensitivity analysis on

...

currently unused

Value

nothing, saves sensitivity.results as sensitivity.results.Rdata, sensitivity plots as sensitivityanalysis.pdf, and variance decomposition 'popsicle plot' as variancedecomposition.pdf a side effect (OPTIONAL)

Author(s)

David LeBauer, Shawn Serbin, Ryan Kelly

Examples

## Not run: 
library(PEcAn.settings)
library(PEcAn.uncertainty)
settings <- read.settings("path/to/pecan.xml")
run.sensitivity.analysis(settings)

## End(Not run)

Apply get.results to each of a list of settings

Description

Apply get.results to each of a list of settings

Usage

runModule.get.results(settings)

Arguments

settings

a PEcAn Settings or MultiSettings object

See Also

get.results


Spline estimate of univariate relationship between parameter value and model output

Description

Creates a spline function using the splinefun function that estimates univariate response of parameter input to model output

Usage

sa.splinefun(quantiles.input, quantiles.output)

Arguments

quantiles.input

passed to 'x' argument of 'stats::splinefun'

quantiles.output

passed to 'y' argument of 'stats::splinefun'

Value

function


Calculates the standard deviation of the variance estimate

Description

Uses the equation σ4(2n1+κn)\sigma^4\left(\frac{2}{n-1}+\frac{\kappa}{n}\right)

Usage

sd.var(x)

Arguments

x

sample

Value

estimate of standard deviation of the sample variance

Author(s)

David LeBauer

References

Mood, Graybill, Boes 1974 'Introduction to the Theory of Statistics' 3rd ed. p 229; Casella and Berger 'Statistical Inference' p 364 ex. 7.45; 'Reference for Var(s^2)' CrossValidated http://stats.stackexchange.com/q/29905/1381, 'Calculating required sample size, precision of variance estimate' CrossValidated http://stats.stackexchange.com/q/7004/1381, 'Variance of Sample Variance?' Mathematics - Stack Exchange http://math.stackexchange.com/q/72975/3733


Performs univariate sensitivity analysis and variance decomposition

Description

This function estimates the univariate responses of a model to a parameter for a set of traits, calculates the model sensitivity at the median, and performs a variance decomposition. This function results in a set of sensitivity plots (one per variable) and plot_variance_decomposition.

Usage

sensitivity.analysis(trait.samples, sa.samples, sa.output, outdir)

Arguments

trait.samples

list of vectors, one per trait, representing samples of the trait value, with length equal to the mcmc chain length. Samples are taken from either the prior distribution or meta-analysis results

sa.samples

data.frame with one column per trait and one row for the set of quantiles used in sensitivity analysis.

sa.output

list of data.frames, similar to sa.samples, except cells contain the results of a model run with that trait x quantile combination and all other traits held at their median value

outdir

directory to which plots are written

Value

results of sensitivity analysis

Author(s)

David LeBauer

Examples

## Not run: 
sensitivity.analysis(
 trait.samples[[pft$name]],
 sa.samples[[pft$name]],
 sa.agb[[pft$name]],
 pft$outdir
)

## End(Not run)

Generate sensitivity analysis filenames

Description

Generate sensitivity analysis filenames

Usage

sensitivity.filename(
  settings,
  prefix = "sensitivity.samples",
  suffix = "Rdata",
  all.var.yr = TRUE,
  pft = NULL,
  ensemble.id = settings$sensitivity.analysis$ensemble.id,
  variable = settings$sensitivity.analysis$variable,
  start.year = settings$sensitivity.analysis$start.year,
  end.year = settings$sensitivity.analysis$end.year
)

Arguments

settings

list of PEcAn settings.

prefix

string to appear at the beginning of the filename

suffix

file extension: string to appear at the end of the filename

all.var.yr

logical: does ensemble include all vars and years? If FALSE, filename will include years and vars

pft

name of PFT used for analysis. If NULL, assumes all PFTs in run are used and does not add them to the filename

ensemble.id

ensemble ID(s)

variable

variable(s) included in the ensemble.

start.year, end.year

first and last year simulated.

Details

Generally uses values in settings, but can be overwritten for manual uses

Value

a filename

Author(s)

Ryan Kelly


Spline Ensemble

Description

Estimate model output based on univariate splines

Usage

spline.ensemble(gi.phii, median)

Arguments

gi.phii

matrix given as output from get.gi.phii

median

median value around which variance will be calculated

Details

Accepts output from get.gi.phii (the matrix g(ϕi)g(\phi_i)) and produces spline estimate of f(ϕ)f(\phi) for use in estimating closure term associated with spline approximation

Author(s)

David LeBauer


Truncate spline at zero if...

Description

Truncate spline at zero if P[x<0] < pnorm(-3) pnorm(-3) chosen as default value for min quantile because this is the default low end of range for the sensitivity analysis. This parameter could be determined based on minimum value in settings$sensitivity.analysis$quantiles

Usage

spline.truncate(x, min.quantile = stats::pnorm(-3))

Arguments

x

vector

min.quantile

threshold quantile for testing lower bound on variable

Value

either x or a vector with values < 0 converted to zero

Author(s)

David LeBauer

Examples

set.seed(0)
x <- c(rgamma(998,1,1), rnorm(10))
min(x) # -0.5238
min(PEcAn.uncertainty::spline.truncate(x))

variance statistics

Description

Variance and SD(variance)

Usage

variance.stats(x)

Arguments

x

numeric vector

Details

calculates variance and sd of variance using the var from base R and sd.var from PEcAn.

Value

list with variance and sd of variance

Author(s)

David LeBauer


Write ensemble config files

Description

Writes config files for use in meta-analysis and returns a list of run ids. Given a pft.xml object, a list of lists as supplied by get.sa.samples, a name to distinguish the output files, and the directory to place the files.

Usage

write.ensemble.configs(
  defaults,
  ensemble.samples,
  settings,
  model,
  clean = FALSE,
  write.to.db = TRUE,
  restart = NULL,
  rename = FALSE
)

Arguments

defaults

pft

ensemble.samples

list of lists supplied by get.ensemble.samples

settings

list of PEcAn settings

model

name of model to be run, e.g. "ED2" or "SIPNET"

clean

remove old output first?

write.to.db

logical: Record this run in BETY?

restart

In case this is a continuation of an old simulation. restart needs to be a list with name tags of runid, inputs, new.params (parameters), new.state (initial condition), ensemble.id (ensemble id), start.time and stop.time.See Details.

rename

Decide if we want to rename previous output files, for example convert from sipnet.out to sipnet.2020-07-16.out.

Details

The restart functionality is developed using model specific functions by calling write_restart.modelname function. First, you need to make sure that this function is already exist for your desired model.See here https://pecanproject.github.io/pecan-documentation/latest/pecan-models.html new state is a dataframe with a different column for each state variable. The number of the rows in this dataframe needs to be the same as the ensemble size. State variables that you can use for setting up the intial conditions differs for different models. You may check the documentation of the write_restart.modelname your model. The units for the state variables need to be in the PEcAn standard units which can be found in standard_vars. new.params also has similar structure to ensemble.samples which is sent as an argument.

Value

list, containing $runs = data frame of runids, $ensemble.id = the ensemble ID for these runs and $samples with ids and samples used for each tag. Also writes sensitivity analysis configuration files as a side effect

Author(s)

David LeBauer, Carl Davidson, Hamze Dokoohaki


Write sensitivity analysis config files

Description

Writes config files for use in sensitivity analysis.

Usage

write.sa.configs(
  defaults,
  quantile.samples,
  settings,
  model,
  clean = FALSE,
  write.to.db = TRUE
)

Arguments

defaults

named list with default parameter values

quantile.samples

list of lists supplied by get.sa.samples

settings

list of settings

model

name of model to be run

clean

logical: Delete any existing contents of the directory specified by settings$rundir before writing to it?

write.to.db

logical: Record this run to BETY? If TRUE, uses connection settings specified in settings$database

Value

list, containing $runs = data frame of runids, and $ensemble.id = the ensemble ID for these runs. Also writes sensitivity analysis configuration files as a side effect

Author(s)

David LeBauer, Carl Davidson