kfold.hsstan: K-fold cross-validation

Description Usage Arguments Value Examples

View source: R/stan.R

Description

Perform K-fold cross-validation using the same settings used when fitting the model on the whole data.

Usage

1
2
3
4
5
6
7
8
9
## S3 method for class 'hsstan'
kfold(
  x,
  folds,
  chains = 1,
  store.fits = TRUE,
  cores = getOption("mc.cores", 1),
  ...
)

Arguments

x

An object of class hsstan.

folds

Integer vector with one element per observation indicating the cross-validation fold in which the observation should be withdrawn.

chains

Number of Markov chains to run. By default this is set to 1, independently of the number of chains used for x.

store.fits

Whether the fitted models for each fold should be stored in the returned object (TRUE by default).

cores

Number of cores to use for parallelization (the value of options("mc.cores") by default). The cross-validation folds will be distributed to the available cores, and the Markov chains for each model will be run sequentially.

...

Further arguments passed to rstan::sampling().

Value

An object with classes kfold and loo that has a similar structure as the objects returned by loo() and waic() and is compatible with the loo_compare function for comparing models. The object contains the following fields:

estimates

a matrix containing point estimates and standard errors of the expected log pointwise predictive density ("elpd_kfold"), the effective number of parameters ("p_kfold", always NA) and the K-fold information criterion "kfoldic" (which is -2 * elpd_kfold, i.e., converted to the deviance scale).

pointwise

a matrix containing the pointwise contributions of "elpd_kfold", "p_kfold" and "kfoldic".

fits

a matrix with two columns and number of rows equal to the number of cross-validation folds. Column fit contains the fitted hsstan objects for each fold, and column test.idx contains the indices of the withdrawn observations for each fold. This is not present if store.fits=FALSE.

data

the dataset used in fitting the model (before withdrawing observations). This is not present if store.fits=FALSE.

Examples

1
2
3
4
# continued from ?hsstan
# only 2 folds for speed of example
folds <- rep(1:2, length.out=length(df$Y))
cv.biom <- kfold(hs.biom, folds=folds, cores=2)

hsstan documentation built on Sept. 16, 2021, 9:11 a.m.