Divide indices from 1 to n into subsets for k-fold cross validation. These functions are potentially useful when creating the cvfits and cvfun arguments for init_refmodel. The returned value is different for these two methods, see below for details.

cvfolds(n, k, seed = NULL)

cvind(n, k, out = "foldwise", seed = NULL)



Number of data points.


Number of folds.


Random seed so that the same division could be obtained again if needed.


Format of the output, either 'foldwise' (default) or 'indices'. See below for details.


cvfolds returns a vector of length n such that each element is an integer between 1 and k denoting which fold the corresponding data point belongs to. The returned value of cvind depends on the out-argument. If out='foldwise', the returned value is a list with k elements, each having fields tr and ts which give the training and test indices, respectively, for the corresponding fold. If out='indices', the returned value is a list with fields tr and ts each of which is a list with k elements giving the training and test indices for each fold.


### compute sample means within each fold n <- 100 y <- rnorm(n) cv <- cvind(n, k=5) cvmeans <- lapply(cv, function(fold) mean(y[fold$tr]))