| SDE | R Documentation |
R6 class for stochastic differential equation
R6 class for stochastic differential equation
Contains the model formulas and data.
This creates an attribute tmb_obj, which can be used to
evaluate the negative log-likelihood function.
This applies check_fn to the observed data (returned by
data() method) to obtain observed statistics. It then repeatedly
simulates a realisation from the fitted SDE (based on observed covariates),
and applies check_fn to the simulated data. The simulations use
the posterior = TRUE option from the codesimulate method, i.e.,
parameters of model used for simulation are generated from posterior
distribution.
new()Create a SDE object
SDE$new( formulas = NULL, data, type, response, par0 = NULL, fixpar = NULL, other_data = NULL )
formulasList of formulas for model parameters, with one element for each SDE parameter. Formulas can use standard R syntax, as well as mgcv-style syntax for splines and random effects.
dataData frame with covariates, response variable, time, and ID
typeType of SDE. Options are "BM" (Brownian motion), "OU" (Ornstein- Uhlenbeck process), "CTCRW" (continuous-time correlated random walk, a.k.a. integrated Ornstein-Uhlenbeck process), "CIR" (Cox-Ingersoll-Ross process), "BM_SSM" (BM with measurement error), "OU_SSM" (OU with measurement error), "BM_t" (BM with Student's t-distributed increments)
responseName of response variable, correspond to a column name in
data. Can be a vector of names if multiple response variables
par0Vector of initial values for SDE parameters, with one value for each SDE parameter. If not provided, parameters are initialised to zero on the link scale.
fixparVector of names of fixed SDE parameters
other_dataNamed list of data objects to pass to likelihood, only required for special models
A new SDE object
formulas()Formulas of SDE object
SDE$formulas()
data()Data of SDE object
SDE$data()
type()Type of SDE object
SDE$type()
response()Name(s) of response variable(s)
SDE$response()
fixpar()Name(s) of fixed parameter(s)
SDE$fixpar()
mats()List of model matrices (X_fe, X_re, and S)
SDE$mats()
other_data()Named list of additional data objects
SDE$other_data()
link()Link functions
SDE$link()
invlink()Inverse link functions
SDE$invlink()
coeff_fe()Fixed effect parameters
SDE$coeff_fe()
coeff_re()Random effect parameters
SDE$coeff_re()
lambda()Smoothness parameters
SDE$lambda()
sdev()Standard deviations of smooth terms
This function transforms the smoothness parameter of each smooth term into a standard deviation, given by SD = 1/sqrt(lambda). It is particularly helpful to get the standard deviations of independent normal random effects.
SDE$sdev()
rho()Decay parameter
SDE$rho()
terms()Terms of model formulas
SDE$terms()
out()Output of optimiser after model fitting
SDE$out()
tmb_obj()Model object created by TMB. This is the output of
the TMB function MakeADFun, and it is a list including elements
fnObjective function
grGradient function of fn
parVector of initial parameters on working scale
SDE$tmb_obj()
tmb_obj_joint()Model object created by TMB for the joint likelihood of
the fixed and random effects. This is the output of the TMB function
MakeADFun, and it is a list including elements
fnObjective function
grGradient function of fn
parVector of initial parameters on working scale
SDE$tmb_obj_joint()
tmb_rep()Output of the TMB function sdreport, which includes
estimates and standard errors for all model parameters.
SDE$tmb_rep()
obs()Data frame of observations (subset response variables out of full data frame)
SDE$obs()
eqn()Print equation for this model
SDE$eqn()
X_re_decay()Get design matrix for random effects in decay model
The design matrix is obtained by taking X_re (returned by make_mat), and multiplying the relevant columns by something like exp(-rho * time) to force the splines to decay to zero with a rate determined by rho.
SDE$X_re_decay()
Design matrix
update_coeff_fe()Update fixed effect coefficients
SDE$update_coeff_fe(new_coeff)
new_coeffNew coefficient vector
update_coeff_re()Update random effect coefficients
SDE$update_coeff_re(new_coeff)
new_coeffNew coefficient vector
update_lambda()Update smoothness parameters
SDE$update_lambda(new_lambda)
new_lambdaNew smoothness parameter vector
update_rho()Update decay parameter
SDE$update_rho(new_rho)
new_rhoNew decay parameter vector
make_mat()Create model matrices
SDE$make_mat(new_data = NULL)
new_dataOptional new data set, including covariates for which the design matrices should be created.
A list of
X_fe Design matrix for fixed effects
X_re Design matrix for random effects
S Smoothness matrix
ncol_fe Number of columns for X_fe for each parameter
ncol_re Number of columns of X_re and S for each random effect
Design matrices for grid of covariates
make_mat_grid()SDE$make_mat_grid(var, covs = NULL)
varName of variable
covsOptional data frame with a single row and one column for each covariate, giving the values that should be used. If this is not specified, the mean value is used for numeric variables, and the first level for factor variables.
A list with the same elements as the output of make_mat, plus a data frame of covariates values.
setup()TMB setup
SDE$setup(silent = TRUE, map = NULL)
silentLogical. If TRUE, all tracing outputs are hidden (default).
mapList passed to MakeADFun to fix parameters. (See TMB documentation.)
fit()Model fitting
The negative log-likelihood of the model is minimised using the
function optim. TMB uses the Laplace approximation to integrate
the random effects out of the likelihood.
After the model has been fitted, the output of optim can be
accessed using the method res.
SDE$fit(silent = TRUE, map = NULL)
silentLogical. If TRUE, all tracing outputs are hidden (default).
mapList passed to MakeADFun to fix parameters. See TMB documentation.
linear_predictor()Get linear predictor for SDE parameters
SDE$linear_predictor( new_data = NULL, t = "all", X_fe = NULL, X_re = NULL, coeff_fe = NULL, coeff_re = NULL, term = NULL )
new_dataOptional data set of covariates. If new_data,
X_fe and X_re are not provided, then the observed
covariates are used.
tTime points for which the parameters should be returned. If "all", returns parameters for all time steps (default).
X_feOptional design matrix for fixed effects, as returned
by make_mat. If new_data, X_fe and X_re
are not provided, then the observed covariates are used.
X_reOptional design matrix for random effects, as returned
by make_mat. If new_data, X_fe and X_re
are not provided, then the observed covariates are used.
coeff_feOptional vector of fixed effect parameters
coeff_reOptional vector of random effect parameters
termName of model term as character string, e.g., "time",
or "s(time)". Use $coeff_fe() and $coeff_re() methods
to find names of model terms. This uses fairly naive substring
matching, and may not work if one covariate's name is a
substring of another one.
Matrix of linear predictor (X_fe with one row for each time step and one column for each SDE parameter
par()Get SDE parameters
SDE$par( t = NULL, new_data = NULL, X_fe = NULL, X_re = NULL, coeff_fe = NULL, coeff_re = NULL, resp = TRUE, term = NULL )
tTime points for which the parameters should be returned. If "all", returns parameters for all time steps. Default: 1.
new_dataOptional data set of covariates. If new_data,
X_fe and X_re are not provided, then the observed
covariates are used.
X_feOptional design matrix for fixed effects, as returned
by make_mat. By default, uses design matrix from data.
X_reOptional design matrix for random effects, as returned
by make_mat. By default, uses design matrix from data.
coeff_feOptional vector of fixed effect parameters
coeff_reOptional vector of random effect parameters
respLogical (default: TRUE). Should the output be on the response scale? If FALSE, the output is on the linear predictor scale.
termName of model term as character string, e.g., "time",
or "s(time)". Use $coeff_fe() and $coeff_re() methods
to find names of model terms. This uses fairly naive substring
matching, and may not work if one covariate's name is a
substring of another one.
Matrix with one row for each time point in t, and one column for each SDE parameter
post_coeff()Posterior draws (coefficients)
SDE$post_coeff(n_post)
n_postNumber of posterior draws
Matrix with one column for each coefficient and one row for each posterior draw
post_par()Posterior draws of SDE parameters (for uncertainty quantification)
SDE$post_par(X_fe, X_re, n_post = 100, resp = TRUE, term = NULL)
X_feDesign matrix (fixed effects)
X_reDesign matrix (random effects)
n_postNumber of posterior draws (default: 100)
respLogical (default: TRUE). Should the output be on the response scale? If FALSE, the output is on the linear predictor scale.
termName of model term as character string, e.g., "time",
or "s(time)". Use $coeff_fe() and $coeff_re() methods
to find names of model terms. This uses fairly naive substring
matching, and may not work if one covariate's name is a
substring of another one.
Array with one row for each time step, one column for each SDE parameter, and one layer for each posterior draw
CI_pointwise()Pointwise confidence intervals for SDE parameters
SDE$CI_pointwise( t = NULL, new_data = NULL, X_fe = NULL, X_re = NULL, level = 0.95, n_post = 1000, resp = TRUE, term = NULL )
tTime points for which the parameters should be returned. If "all", returns parameters for all time steps. Defaults to 1 if new data not provided, or "all" if new data provided.
new_dataOptional data frame containing covariate values for which the CIs should be computed
X_feOptional design matrix for fixed effects, as returned
by make_mat. By default, uses design matrix from data.
X_reOptional design matrix for random effects, as returned
by make_mat. By default, uses design matrix from data.
levelConfidence level (default: 0.95 for 95% confidence intervals)
n_postNumber of posterior samples from which the confidence intervals are calculated. Larger values will reduce approximation error, but increase computation time. Defaults to 1000.
respLogical (default: TRUE). Should the output be on the response scale? If FALSE, the output is on the linear predictor scale.
termName of model term as character string, e.g., "time",
or "s(time)". Use $coeff_fe() and $coeff_re() methods
to find names of model terms. This uses fairly naive substring
matching, and may not work if one covariate's name is a
substring of another one.
This method generates pointwise confidence intervals
by simulation. That is, it generates n_post posterior samples
of the estimated parameters from a multivariate normal distribution,
where the mean is the vector of estimates and the covariance matrix
is provided by TMB (using post_par). Then, the SDE
parameters are derived for each set of posterior parameter values,
and pointwise confidence intervals are obtained as quantiles of the
posterior simulated SDE parameters.
List with elements:
lowMatrix of lower bounds of confidence intervals.
uppMatrix of upper bounds of confidence intervals.
CI_simultaneous()Simultaneous confidence intervals for SDE parameters
SDE$CI_simultaneous( t = NULL, new_data = NULL, X_fe = NULL, X_re = NULL, level = 0.95, n_post = 1000, resp = TRUE, term = NULL )
tTime points for which the parameters should be returned. If "all", returns parameters for all time steps. Defaults to 1 if new data not provided, or "all" if new data provided.
new_dataOptional data frame containing covariate values for which the CIs should be computed
X_feOptional design matrix for fixed effects, as returned
by make_mat. By default, uses design matrix from data.
X_reOptional design matrix for random effects, as returned
by make_mat. By default, uses design matrix from data.
levelConfidence level (default: 0.95 for 95% confidence intervals)
n_postNumber of posterior samples from which the confidence intervals are calculated. Larger values will reduce approximation error, but increase computation time. Defaults to 1000.
respLogical (default: TRUE). Should the output be on the response scale? If FALSE, the output is on the linear predictor scale.
termName of model term as character string, e.g., "time",
or "s(time)". Use $coeff_fe() and $coeff_re() methods
to find names of model terms. This uses fairly naive substring
matching, and may not work if one covariate's name is a
substring of another one.
This method closely follows the approach suggested by Gavin Simpson at fromthebottomoftheheap.net/2016/12/15/simultaneous-interval-revisited/, itself based on Section 6.5 of Ruppert et al. (2003).
List with elements:
lowMatrix of lower bounds of confidence intervals.
uppMatrix of upper bounds of confidence intervals.
residuals()Model residuals
SDE$residuals()
check_post()Posterior predictive checks
SDE$check_post(check_fn, n_sims = 100, silent = FALSE)
check_fnGoodness-of-fit function which accepts "data" as input and returns a statistic or a vector of statistics, to be compared between observed data and simulations.
n_simsNumber of simulations to perform
silentLogical. If FALSE, simulation progress is shown. (Default: FALSE)
List with elements:
obs_statVector of values of goodness-of-fit statistics for the observed data
statsMatrix of values of goodness-of-fit statistics for the simulated data sets (one row for each statistic, and one column for each simulation)
plotggplot object showing, for each statistic returned by check_fn, a histogram of simulated values and a vertical line for the observed value. An observed value in the tails of the simulated distribution suggests lack of fit.
Conditional Akaike Information Criterion
The conditional AIC is for example defined by Wood (2017), as AIC = - 2L + 2k where L is the maximum joint log-likelihood (of fixed and random effects), and k is the number of effective degrees of freedom of the model (accounting for flexibility in non-parametric terms implied by smoothing)
AIC_conditional()SDE$AIC_conditional()
Conditional AIC Marginal Akaike Information Criterion
The marginal AIC is for example defined by Wood (2017), as AIC = - 2L + 2k where L is the maximum marginal log-likelihood (of fixed effects), and k is the number of degrees of freedom of the fixed effect component of the model
AIC_marginal()SDE$AIC_marginal()
Marginal AIC
edf_conditional()Effective degrees of freedom
SDE$edf_conditional()
Number of effective degrees of freedom (accounting for flexibility in non-parametric terms implied by smoothing)
simulate()Simulate from SDE model
SDE$simulate(data, z0 = 0, posterior = FALSE)
dataData frame for input data. Should have at least one column 'time' for times of observations, and columns for covariates if necessary.
z0Optional value for first observation of simulated time series. Default: 0.
posteriorLogical. If TRUE, the parameters used for the simulation
are drawn from their posterior distribution using SDE$post_coeff,
therefore accounting for uncertainty.
Input data frame with extra column for simulated time series
plot_par()Plot observation parameters
SDE$plot_par( var, par_names = NULL, covs = NULL, n_post = 100, show_CI = "none", resp = TRUE, term = NULL )
varName of covariate as a function of which the parameters should be plotted
par_namesOptional vector for the names of SDE parameters to plot. If not specified, all parameters are plotted.
covsOptional data frame with a single row and one column for each covariate, giving the values that should be used. If this is not specified, the mean value is used for numeric variables, and the first level for factor variables.
n_postNumber of posterior draws to plot. Default: 100.
show_CIShould confidence bands be plotted rather than posterior
draws? Can takes values 'none' (default; no confidence bands),
'pointwise' (show pointwise confidence bands obtained with
CI_pointwise), or 'simultaneous' (show simultaneous
confidence bands obtained with CI_simultaneous)
respLogical (default: TRUE). Should the output be on the response scale? If FALSE, the output is on the linear predictor scale.
termName of model term as character string, e.g., "time",
or "s(time)". Use $coeff_fe() and $coeff_re() methods
to find names of model terms. This uses fairly naive substring
matching, and may not work if one covariate's name is a
substring of another one.
A ggplot object
ind_fixcoeff()Indices of fixed coefficients in coeff_fe
SDE$ind_fixcoeff()
message()Print SDE and parameter formulas
SDE$message()
print_par()Print parameter values for t = 1
SDE$print_par()
print()Print SDE object
SDE$print()
clone()The objects of this class are cloneable with this method.
SDE$clone(deep = FALSE)
deepWhether to make a deep clone.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.