kfold.ermod {BayesERtools}R Documentation

Run k-fold cross-validation

Description

This function performs k-fold cross-validation using the appropriate model development function based on the class of the ermod object. It is internally used by eval_ermod(). The output is compatible with loo ecosystem, e.g. it can be used for loo::loo_compare() function. See loo::kfold() for details.

Usage

## S3 method for class 'ermod'
kfold(x, k = 5, newdata = NULL, seed = NULL, ...)

kfold(x, ...)

extract_kfold_loo(kfold_ermod)

Arguments

x

An ermod object containing the model and data.

k

The number of folds for cross-validation. Default is 5.

newdata

Optional new dataset to use instead of the original data. Default is NULL.

seed

Random seed for reproducibility. Default is NULL.

...

Currently not used.

kfold_ermod

An object of class kfold_ermod from kfold()

Value

kfold() returns kfold_ermod class object containing the fitted models and holdout predictions for each fold.

extract_kfold_loo() returns c("kfold", "loo") class object that works well with loo ecosystem

Examples



data(d_sim_binom_cov_hgly2)

ermod_bin <- dev_ermod_bin(
  data = d_sim_binom_cov_hgly2,
  var_resp = "AEFLAG",
  var_exposure = "AUCss_1000",
  var_cov = "BHBA1C_5",
  # Settings to make the example run faster
  chains = 2,
  iter = 1000
)

cv_results <- kfold(ermod_bin, k = 3, seed = 123)

print(cv_results)



[Package BayesERtools version 0.2.3 Index]