Skip to contents

Performs k-fold cross validation with the Mixed Model Repeated Measures model (see MMRM).

Usage

mmrm_cv(
  formula,
  time,
  subjects,
  data,
  k = 10,
  seed = NULL,
  in_loop = NULL,
  ...
)

Arguments

formula

formula for the fixed effects structure of the model

time

the time variable of the model

subjects

the variable indicating unique subjects

data

the data structure

k

the number of splits for k-fold cross-validation, default = 10

seed

random seed used to generating splits, If NULL does not set the seed

in_loop

function to process each split of training set data. See details

...

arguments passed to in_loop and MMRM

Value

mmrmCV object: a list of outputs with length k, each element is the output of a call to mmrm. See mmrm and mmrmObject for details

Details

Please see MMRM for details on the model.

mmrm_cv supports fitting each of the k-folds in parallel using foreach::foreach loops. To use multiple cores, please register a parallel backend prior to calling mmrm_cv. Here is an example:

cl = parallel::makeCluster(n_cores)
doParallel::registerDoParallel(cl)

The in_loop argument allows users to provide a function that transforms training set data. This function must:

  • accept the training set data as its first argument

  • accept pass through arguments (...)

  • return the processed data structure with all variables specified in the supplied formula Here is an example in_loop that performs no transformations:

in_loop = function(data, ...) {
  # do stuff here
  return(data)
}

Examples

if (FALSE) {
mmrm_cv(
  outcome ~ baseline + group + time +
    baseline:time + group:time,
  time = "time",
  subjects = "subjectid",
  data = my_data,
  k = 10
)
}