Package 'PEcAn.benchmark'

Title: PEcAn Functions Used for Benchmarking
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. The PEcAn.benchmark package provides utilities for comparing models and data, including a suite of statistical metrics and plots.
Authors: Michael Dietze, Betsy Cowdery, David LeBauer, Rob Kooper, Toni Viskari
Maintainer: Mike Dietze <[email protected]>
License: BSD_3_clause + file LICENSE
Version: 1.7.5.9000
Built: 2026-07-21 08:10:00 UTC
Source: https://github.com/PecanProject/pecan

Help Index


Add workflow specific info to settings list for benchmarking

Description

Add workflow specific info to settings list for benchmarking

Usage

add_workflow_info(settings, bety)

Arguments

settings

settings or multisettings object

bety

connection to the database

Author(s)

Betsy Cowdery


align_first_observation

Description

align_first_observation

Usage

align_by_first_observation(observation_one, observation_two, custom_table)

Arguments

observation_one

a vector of plant functional types, or species. Provides species/pft names.

observation_two

another vector of plant functional types, or species. Provides the order.

custom_table

a table that either maps two pft's to one another or maps custom species codes to bety id codes. In the second case, must be passable to match_species_id.

Value

vector Returns a vector of PFT's/species from observation_one that matches the order of observation_two

Author(s)

Tempest McCabe

Examples

observation_one<-c("AMCA3","AMCA3","AMCA3","AMCA3")
observation_two<-c("a", "b", "a", "a")

table<-list()
table$plant_functional_type_one<- c("AMCA3","AMCA3","ARHY", "ARHY")
table$plant_functional_type_two<- c('a','a','b', 'b') # PFT groupings
table<-as.data.frame(table)

aligned <- align_by_first_observation(
  observation_one = observation_one,
  observation_two = observation_two,
  custom_table = table)

# aligned should be a vector '[1] "AMCA3" "ARHY"  "AMCA3" "AMCA3"'

Align model and observation data frames by nearest time

Description

Align model and observation data frames by nearest time

Usage

align_by_time(model_df, obs_df, tolerance_secs = 3600)

Arguments

model_df

data.frame with columns: time (POSIXct), value

obs_df

data.frame with columns: time (POSIXct), value

tolerance_secs

max allowed time difference in seconds

Value

data.frame with columns: model, obvs, time


Align timeseries data

Description

Align timeseries data

Usage

align_data(model.calc, obvs.calc, var, align_method = "match_timestep")

Arguments

model.calc

data.frame

obvs.calc

data.frame

var

data.frame

align_method

name of function to use for alignment

Value

dat

Author(s)

Betsy Cowdery


align_data_to_data_pft

Description

align_data_to_data_pft

Usage

align_data_to_data_pft(
  con,
  observation_one,
  observation_two,
  custom_table = NULL,
  format_one,
  format_two,
  subset_is_ok = FALSE
)

Arguments

con

database connection

observation_one

a vector of plant functional types, or species

observation_two

another vector of plant functional types, or species

custom_table

a table that either maps two pft's to one another or maps custom species codes to bety id codes. In the second case, must be passable to match_species_id.

format_one

The output of query.format.vars() of observation one of the form output$vars$bety_names

format_two

The output of query.format.vars() of observation two of the form output$vars$bety_names

subset_is_ok

When aligning two species lists, this allows for alignment when species lists aren't identical. set to FALSE by default.

Details

Aligns vectors of Plant Fucntional Typed and species. Can align: - two vectors of plant functional types (pft's) if a custom map is provided - a list of species (usda, fia, or latin_name format) to a plant functional type - a list of species in a custom format, with a table mapping it to bety_species_id's

Will return a list of what was originally provided, bety_species_codes if possible, and an aligned output. Because some alignement is order-sensitive, alignment based on observation_one and observation_two are both provided.

Value

list containing the following columns:

$original

Will spit back out original vectors pre-alignment

$aligned$aligned_by_observation_one

Where possible, will return a vector of observation_one pft's/species in the order of observation_two

species

Where possible, will return a vector of observation_two's pft's/species in the order of observation_one

$bety_species_id

Where possible, will return the bety_species_id's for one or both observations

$bety_species_intersection

Where possible, will return the intersection of two aligned lists of species. subset_is_ok must be set to TRUE.

Author(s)

Tempest McCabe

Examples

## Not run: 

con <- db.open(list(host = "postgres", user = "bety", password = "carya"))

observation_one <- c("AMCA3","AMCA3","AMCA3","AMCA3")
observation_two <- c("a", "b", "a", "a")

table <- list()
table$plant_functional_type_one <- c("AMCA3","AMCA3","ARHY", "ARHY")
table$plant_functional_type_two <- c('a','a','b', 'b') # PFT groupings
table <- as.data.frame(table)

format_one <- "species_USDA_symbol"
format_two <- "plant_functional_type"

aligned <- align_data_to_data_pft(
 con = con,
 observation_one = observation_one, observation_two = observation_two,
 format_one = format_one, format_two = format_two,
 custom_table = table)

## End(Not run)

Align vectors of Plant Functional Type and species.

Description

Align vectors of Plant Functional Type and species.

Usage

align_pft(
  con,
  observation_one,
  observation_two,
  custom_table = NULL,
  format_one,
  format_two,
  subset_is_ok = FALSE,
  comparison_type = "data_to_data",
  ...
)

Arguments

con

database connection

observation_one

a vector of plant fucntional types, or species

observation_two

anouther vector of plant fucntional types, or species

custom_table

a table that either maps two pft's to one anouther or maps custom species codes to bety id codes. In the second case, must be passable to match_species_id.

format_one

The output of query.format.vars() of observation one of the form output$vars$bety_names

format_two

The output of query.format.vars() of observation two of the form output$vars$bety_names

subset_is_ok

When aligning two species lists, this allows for alignement when species lists aren't identical. set to FALSE by default.

comparison_type

one of "data_to_model", "data_to_data", or "model_to_model"

...

other arguments, currently ignored

Details

Can align: - two vectors of plant fucntional types (pft's) if a custom map is provided - a list of species (usda, fia, or latin_name format) to a plant fucntional type - a list of species in a custom format, with a table mapping it to bety_species_id's

Will return a list of what was originally provided, bety_speceis_codes if possible, and an aligned output. Becuase some alignement is order-sensitive, alignment based on observation_one and observation_two are both provided.

comparison_type can be one of the following:

data_to_data

Will align lists of pfts and species. Must be assosiated with inputs.

data_to_model

Not yet implemented

model_to_model

Not yet implemented

Value

list containing the following columns:

$original

Will spit back out original vectors pre-alignment

$aligned$aligned_by_observation_one

Where possible, will return a vector of observation_one pft's/species in the order of observation_two

species

Where possible, will return a vector of observation_two's pft's/species in the order of observation_one

$bety_species_id

Where possible, will return the bety_species_id's for one or both observations

Author(s)

Tempest McCabe

Examples

## Not run: 

con <- db.open(list(host = "postgres", user = "bety", password = "carya"))

#------------ A species to PFT alignment -----------
observation_one <- c("AMCA3","AMCA3","AMCA3","AMCA3")
observation_two <- c("a", "b", "a", "a")

format_one <- "species_USDA_symbol"
format_two <- "plant_functional_type"

table <- list()
table$plant_functional_type_one <- c("AMCA3","AMCA3","ARHY", "ARHY")
table$plant_functional_type_two <- c('a','a','b', 'b') # PFT groupings
table <- as.data.frame(table)


aligned <- align_pft(
  con = con,
  observation_one = observation_one, observation_two = observation_two,
  custom_table = table,
  format_one = format_one, format_two = format_two)

## End(Not run)

Move benchmarking settings back in to original pecan settings object

Description

Move benchmarking settings back in to original pecan settings object

Usage

bm_settings2pecan_settings(bm.settings)

Arguments

bm.settings

settings or multisettings object

Author(s)

Betsy Cowdery


Validate benchmark input dataframes

Description

Validate benchmark input dataframes

Usage

bm_validate(model_df, obs_df)

Arguments

model_df

data.frame with columns: time (POSIXct), value (numeric)

obs_df

data.frame with columns: time (POSIXct), value (numeric)

Value

invisible(TRUE)


Calculate benchmarking statistics

Description

For each benchmark id, calculate metrics and update benchmarks_ensemble_scores

Usage

calc_benchmark(settings, bety, start_year = NA, end_year = NA)

Arguments

settings

settings object describing the run to calculate

bety

database connection

start_year, end_year

time range to read. If NA, these are taken from 'settings'

Author(s)

Betsy Cowdery


calc_metrics

Description

calc_metrics

Usage

calc_metrics(model.calc, obvs.calc, var, metrics, ensemble.id, bm_dir)

Arguments

model.calc

model data

obvs.calc

observational data

var

variables to be used

metrics

metrics to be used

ensemble.id

id of ensemble run

bm_dir

directory where benchmarking outputs will be saved

Author(s)

Betsy Cowdery


Check whether a run has been registered as a reference run in BETY

Description

Check whether a run has been registered as a reference run in BETY

Usage

check_BRR(settings_xml, con)

Arguments

settings_xml

cleaned settings to be compared with BRR in the database

con

database connection

Author(s)

Betsy Cowdery


check_if_list_of_pfts

Description

Checks if format contains a variable named "plant_functional_type"

Usage

check_if_list_of_pfts(vars)

Arguments

vars

names to check

Value

boolean

Author(s)

Tempest McCabe


check_if_species_list

Description

check_if_species_list

Usage

check_if_species_list(vars, custom_table = NULL)

Arguments

vars

format

custom_table

a table that either maps two pft's to one anouther or maps custom species codes to bety id codes. In the second case, must be passable to match_species_id.

Details

Checks if format contains a species list in a known format, or a declared custom format.

Value

boolean

Author(s)

Tempest McCabe


Cleans PEcAn settings file and prepares the settings to be saved in a reference run record in BETY

Description

Cleans PEcAn settings file and prepares the settings to be saved in a reference run record in BETY

Usage

clean_settings_BRR(inputfile)

Arguments

inputfile

the PEcAn settings file to be used.

Author(s)

Betsy Cowdery


Compute benchmark metrics

Description

Compute benchmark metrics

Usage

compute_metrics(aligned, metrics = c("RMSE", "MAE", "R2"))

Arguments

aligned

data.frame with columns: model, obvs, time

metrics

character vector of metric names

Value

data.frame with columns: metric, value


Create benchmark reference run and ensemble

Description

For each benchmark id, calculate metrics and update benchmarks_ensemble_scores

Usage

create_BRR(ens_wf, con, user_id = "")

Arguments

ens_wf

table made from joining ensemble and workflow tables

con

database connection

user_id

Optional user id to use for this record in reference_runs table

Author(s)

Betsy Cowdery


Benchmark Definition: Retrieve or Create Bety Benchmarking Records

Description

Creates records for benchmarks, benchmarks_benchmarks_reference_runs, benchmarks_metrics

Usage

define_benchmark(settings, bety)

Arguments

settings

settings list

bety

database connection

Value

updated settings list

Author(s)

Betsy Cowdery


Function to convert wide format to long format

Description

Function to convert wide format to long format

Usage

format_wide2long(out, format, vars_used, time.row)

Arguments

out

wide format data

format

as returned by query.format.vars

vars_used

data frame mapping 'input_name' to 'bety_name'

time.row

ignored; value in output is set from 'format$vars$storage_type'

Value

list of updated values

Author(s)

Istem Fer


get_species_list_standard

Description

Returns the format type for convience of use with match_species_id

Usage

get_species_list_standard(vars)

Arguments

vars

format to be matched

Value

character Returns "usda", "latin_name", "fia" or "custom"

Author(s)

Tempest McCabe


Load and standardize arbitrary tabular data using a YAML mapping configuration

Description

Load and standardize arbitrary tabular data using a YAML mapping configuration

Usage

load_and_map_data(data.path, mapping.path)

Arguments

data.path

character, file path to the tabular data (e.g. .csv)

mapping.path

character, file path to the YAML mapping configuration

Value

A standardized data frame with column names mapped to PEcAn standard vocabulary


load_csv

Description

load_csv

Usage

load_csv(data.path, format, site, vars = NULL)

Arguments

data.path

character, file path to the .csv file

format

list, config list containing

  • header, numeric for number of header rows in file

  • skip, numeric for skip argument of utils::read.csv

  • na.strings, character vector for na.strings argument of utils::read.csv

site

list, currently ignored

vars

column names to return. If NULL, returns all columns

Value

a data frame with the processed data of the .csv file

Author(s)

Betsy Cowdery


load data

Description

Generic function to convert input files containing observational data to a common PEcAn format.

Usage

load_data(
  data.path,
  format,
  start_year = NA,
  end_year = NA,
  site = NA,
  vars.used.index = NULL,
  ...
)

Arguments

data.path

character

format

list

start_year

numeric

end_year

numeric

site

list

vars.used.index

which variables to use? If NULL, these are taken from 'format'

...

further arguments, currently ignored

Author(s)

Betsy Cowdery, Istem Fer, Joshua Mantooth


load_rds

Description

load_rds

Usage

load_rds(data.path, format, site, vars = NULL)

Arguments

data.path

character

format

list, not used, for compatibility

site

not used, for compatibility

vars

optional variable names to load. if NULL, returns all variables in file

Author(s)

Istem Fer


Load files with mime-type 'text/tab-separated-values'

Description

Load files with mime-type 'text/tab-separated-values'

Usage

load_tab_separated_values(data.path, format, site = NULL, vars = NULL)

Arguments

data.path

character

format

list

site

list

vars

variable names to load. If NULL, loads all columns

Author(s)

Betsy Cowdery, Mike Dietze


Load from netCDF

Description

Load from netCDF

Usage

load_x_netcdf(data.path, format, site, vars = NULL)

Arguments

data.path

character vector or list of paths to NetCDF files

format

list containing format info (e.g. na.strings)

site

list site info (optional)

vars

character vector of variables to load

Value

A data.frame with the requested variables and a 'time' column (POSIXct)

Author(s)

Istem Fer, Anshul Jain


Match time step

Description

Match time step

Usage

match_timestep(date.coarse, date.fine, data.fine)

Arguments

date.coarse

numeric

date.fine

numeric

data.fine

matrix

Author(s)

Istem Fer


Calculate benchmarking statistics

Description

Calculate benchmarking statistics

Usage

mean_over_larger_timestep(date.coarse, date.fine, data.fine)

Arguments

date.coarse

numeric

date.fine

numeric

data.fine

data.frame

Author(s)

Betsy Cowdery, Michael Dietze


Absolute Maximum Error

Description

Absolute Maximum Error

Usage

metric_AME(dat, ...)

Arguments

dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Correlation Coefficient

Description

Correlation Coefficient

Usage

metric_cor(dat, ...)

Arguments

dat

dataframe

...

ignored

Author(s)

Mike Dietze


Prediction Interval Coverage

Description

Prediction Interval Coverage

Usage

metric_Coverage(dat, ...)

Arguments

dat

dataframe with columns 'model_q025' and 'model_q975'

...

ignored

Details

Measures the fraction of observations that fall within the model's stated 95

Value

A numeric value representing the fraction of observations that fall within the 95


Frechet Distance

Description

Frechet Distance

Usage

metric_Frechet(metric_dat, ...)

Arguments

metric_dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Linear Regression Diagnostic Plot

Description

Linear Regression Diagnostic Plot

Usage

metric_lmDiag_plot(metric_dat, var, filename = NA, draw.plot = FALSE)

Arguments

metric_dat

data.frame

var

ignored

filename

path to save plot, or NA to not save

draw.plot

logical: return plot object?

Author(s)

Betsy Cowdery


Mean Absolute Error

Description

Mean Absolute Error

Usage

metric_MAE(dat, ...)

Arguments

dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Mean Square Error

Description

Mean Square Error

Usage

metric_MSE(dat, ...)

Arguments

dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Predictive Model Uncertainty (PMU)

Description

Predictive Model Uncertainty (PMU)

Usage

metric_PMU(dat, ...)

Arguments

dat

dataframe with columns 'obs_se' and 'obs_n'

...

ignored

Details

Calculates the pooled measurement uncertainty. Requires 'obs_se' (standard error) and 'obs_n' (replicate counts) in the observation data.

Value

A numeric value representing the pooled measurement uncertainty.


Pearson Product Moment Correlation

Description

Pearson Product Moment Correlation

Usage

metric_PPMC(metric_dat, ...)

Arguments

metric_dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Coefficient of Determination (R2)

Description

Coefficient of Determination (R2)

Usage

metric_R2(metric_dat, ...)

Arguments

metric_dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Relative Absolute Error

Description

Relative Absolute Error

Usage

metric_RAE(metric_dat, ...)

Arguments

metric_dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Residual Plot

Description

Residual Plot

Usage

metric_residual_plot(
  metric_dat,
  var,
  filename = NA,
  draw.plot = is.na(filename)
)

Arguments

metric_dat

dataframe to plot, with at least columns 'time', 'model', 'obvs'

var

variable name, used as plot title

filename

path to save plot, or NA to not save

draw.plot

logical: Return the plot object?

Author(s)

Betsy Cowdery


Root Mean Square Error

Description

Root Mean Square Error

Usage

metric_RMSE(dat, ...)

Arguments

dat

dataframe

...

ignored

Author(s)

Betsy Cowdery


Model Run Check

Description

Model Run Check

Usage

metric_run(settings)

Arguments

settings

list

Author(s)

Betsy Cowdery


Scatter Plot

Description

Scatter Plot

Usage

metric_scatter_plot(
  metric_dat,
  var,
  filename = NA,
  draw.plot = is.na(filename)
)

Arguments

metric_dat

dataframe to plot, with at least columns 'model' and 'obvs'

var

ignored

filename

path to save plot, or NA to not save

draw.plot

logical: Return the plot object?

Author(s)

Betsy Cowdery


Timeseries Plot

Description

Timeseries Plot

Usage

metric_timeseries_plot(
  metric_dat,
  var,
  filename = NA,
  draw.plot = is.na(filename)
)

Arguments

metric_dat

dataframe to plot, with at least columns 'time', 'model', 'obvs'

var

variable name, used as plot title

filename

path to save plot, or NA to not save

draw.plot

logical: Return the plot object?

Author(s)

Betsy Cowdery


Metric Registry for PEcAn.benchmark

Description

Metric Registry for PEcAn.benchmark

Usage

pecan_metric_registry

Format

An object of class environment of length 7.


Plot model vs observations time series

Description

Plot model vs observations time series

Usage

plot_time_series(aligned)

Arguments

aligned

data.frame with columns: model, obvs, time

Value

ggplot object


Read settings from database using reference run id

Description

For each benchmark entry in a (multi)settings object, get run settings using reference run id and add to the settings object

Usage

read_settings_BRR(settings)

Arguments

settings

settings or multisettings object

Author(s)

Betsy Cowdery


Register a new benchmark metric

Description

Register a new benchmark metric

Usage

register_metric(name, func)

Arguments

name

Character name of the metric

func

Function that takes an aligned dataframe and returns a numeric value


Run a simple benchmark pipeline

Description

Takes two validated dataframes, aligns by time, computes metrics, and returns a results table with a plot.

Usage

run_benchmark(
  model_df,
  obs_df,
  metrics = c("RMSE", "MAE"),
  tolerance_secs = 3600,
  method = "nearest"
)

Arguments

model_df

data.frame with columns: time (POSIXct), value (numeric)

obs_df

data.frame with columns: time (POSIXct), value (numeric)

metrics

character vector of metrics to compute. Options: "RMSE", "MAE"

tolerance_secs

nearest-neighbor time tolerance in seconds (default 1 hour)

method

alignment method: "nearest" or "interpolate"

Value

list with: metrics (data.frame), aligned (data.frame), plot (ggplot)

Author(s)

Anshul Jain