Skip to contents

This function performs k-fold cross-validation using the appropriate model development function based on the class of the ermod object.

Usage

run_kfold_cv(ermod, newdata = NULL, k = 5, seed = NULL)

Arguments

ermod

An ermod object containing the model and data.

newdata

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

k

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

seed

Random seed for reproducibility. Default is NULL.

Value

A kfold_cv_ermod class object containing the fitted models and holdout predictions for each fold.

Examples

# \donttest{
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 <- run_kfold_cv(ermod_bin, k = 3, seed = 123)

print(cv_results)
#> 
#> ── k-fold Cross-Validation for ermod object ────────────────────────────────────
#>  Number of folds:  3
#> 
#> ── Structure of the object: ──
#> 
#> • $l_ermod: list of ermod objects
#> • $d_truth: data frame with true response values
#> • $d_sim: data frame with holdout predictions
# }