Learn R Programming

pspline.inference (version 1.0.4)

pspline.estimate.timeseries: Calculates confidence intervals for time series sampled from generalized additive (mixed) model of an outbreak

Description

This function performs a series of Monte Carlo simulations of a GAM/GAMM outbreak model. For each simulated outbreak, it calls outcome to calculate a time series for the simulated outbreak (for example, the number of cumulative cases vs time). It then calculates and returns the confidence interval of the simulated time series at each time point across all simulations

Usage

pspline.estimate.timeseries(
  model,
  predictors,
  outcome,
  samples = 1000,
  level = 0.95
)

Arguments

model

model returned by gam or gamm, with a single parameter (time)

predictors

data frame of predictor values at which the model will be evaluated

outcome

function returning calculated outcome time series, as described above

samples

number of simulations to run

level

confidence level for returned estimates

Value

data frame of estimates, as described above

Details

The outcome function must accept (model, params, time) and return a vector containing the outcome time series obtained by evaluating the model at the time points given in time and using the model parameters given in params.

A typical implementation of the outcome function would call predict on model and time to obtain the linear predictor matrix, and then post-multiply that matrix by params. Having thus obtained model prediction at every time point, it would calculate the desired time series outcome and return it in a vector.

For example, to calculate the time series of the first derivative of incidence, you might use this function for outcome:

calc_deriv = function(model, params, time) { eps = 0.001 predictors = predict(model, data.frame(time=time), type="lpmatrix") fit = model$family$linkinv(predictors predictors_eps = predict(model, data.frame(time=time + eps), type="lpmatrix") fit_eps = model$family$linkinv(predictors_eps (fit_eps - fit) / eps }

The data frame returned by pspline.estimate.timeseries contains three columns and one row for each time point in time. The columns are lower, median, and upper, containing the median and the confidence interval for the computed outcome time series at each time point.