metab_Kmodel: Combine a time series of K estimates to predict consistent...

View source: R/metab_Kmodel.R

metab_KmodelR Documentation

Combine a time series of K estimates to predict consistent values

Description

Takes daily estimates of K, usually from nighttime regression, and regresses against predictors such as discharge.daily. Returns a metab_Kmodel object that only predicts daily K, nothing else.

Usage

metab_Kmodel(
  specs = specs(mm_name("Kmodel")),
  data = mm_data(solar.time, discharge, velocity, optional = c("all")),
  data_daily = mm_data(date, K600.daily, K600.daily.lower, K600.daily.upper,
    discharge.daily, velocity.daily, optional = c("K600.daily.lower", "K600.daily.upper",
    "discharge.daily", "velocity.daily")),
  info = NULL
)

Arguments

specs

a list of model specifications and parameters for a model. Although this may be specified manually (it's just a list), it is easier and safer to use specs to generate the list, because the set of required parameters and their defaults depends on the model given in the model_name argument to specs. The help file for specs lists the necessary parameters, describes them in detail, and gives default values.

data

data.frame (not a tbl_df) of input data at the temporal resolution of raw observations (unit-value). Columns must have the same names, units, and format as the default. The solar.time column must also have a timezone code ('tzone' attribute) of 'UTC'. See the 'Formatting data' section below for a full description.

data_daily

data.frame containing inputs with a daily timestep. See the 'Formatting data_daily' section below for a full description.

info

any information, in any format, that you would like to store within the metab_model object

Details

Possible approaches:

  • "mean"Predict K as the mean of all K values

  • "weighted mean"Predict K as the mean of all K values, weighted by the inverse of the confidence intervals in the input K values

  • "KvQ"Regress K versus Q, tending toward overall mean in ranges of Q with sparse data

  • "weighted KvQ"Regress K versus Q, tending toward overall mean in ranges of Q with sparse data, weighting high-confidence K values more heavily

  • "T smoother"Predict K using a loess or spline smoother over time

  • "Q smoother"Predict K using a loess or spline smoother over discharge.daily

  • "TQ smoother"Predict K using a loess or spline smoother over both time and discharge.daily

Value

A metab_Kmodel object containing the fitted model. This object can be inspected with the functions in the metab_model_interface.

Author(s)

Alison Appling

See Also

Other metab_model: metab_bayes, metab_mle, metab_night, metab_sim

Examples

library(dplyr)
# create example data
set.seed(24842)
example_Ks <- data.frame(date=seq(as.Date("2012-08-15"),as.Date("2012-09-15"),
  as.difftime(1,units='days')), discharge.daily=exp(rnorm(32,2,1)), K600.daily=rnorm(32,30,4)) %>%
  mutate(K600.daily.lower=K600.daily-5, K600.daily.upper=K600.daily+6)

# mean
mm <- metab_Kmodel(
  specs(mm_name('Kmodel', engine='mean')),
  data_daily=example_Ks) # two warnings expected for engine='mean'
get_params(mm)
## Not run: 
plot(get_params(mm)$date, get_params(mm)$K600.daily)

## End(Not run)

# linear model
mm <- metab_Kmodel(
  specs(mm_name('Kmodel', engine='lm'), predictors='discharge.daily'),
  data_daily=example_Ks)
get_params(mm)
## Not run: 
plot(get_data_daily(mm)$discharge.daily, get_params(mm)$K600.daily)

## End(Not run)

# loess
mm <- metab_Kmodel(    ### breaks ###
  specs(mm_name('Kmodel', engine='loess'), predictors='date', other_args=list(span=0.4)),
  data_daily=example_Ks)
get_params(mm)
## Not run: 
plot(get_params(mm)$date, get_params(mm)$K600.daily)

## End(Not run)

## 3-phase workflow (sort of like complete pooling) for estimating K within
## days, then K across days, then GPP and ER within days

# 1. data and specifications for both of the MLE models
dat <- data_metab('10','15')
mle_specs <- specs(mm_name('mle'))

# fit a first-round MLE and extract the K estimates
mm1 <- metab_mle(mle_specs, data=dat)
K600_mm1 <- get_params(mm1, uncertainty='ci') %>%
  select(date, K600.daily, K600.daily.lower, K600.daily.upper)

# smooth the K600s
mm2 <- metab_Kmodel(specs(mm_name('Kmodel', engine='mean'),
  day_start=-1, day_end=23), data_daily=K600_mm1)
K600_mm2 <- get_params(mm2) %>% select(date, K600.daily)

# refit the MLE with fixed K
mm3 <- metab_mle(mle_specs, data=dat, data_daily=K600_mm2)
get_params(mm3, fixed='stars')
predict_metab(mm3)
## Not run: 
plot_metab_preds(mm1)
plot_metab_preds(mm3)

## End(Not run)

USGS-R/streamMetabolizer documentation built on Aug. 15, 2023, 7:50 a.m.