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.
Usage
cvfolds(n, K, seed = NULL)
cv_ids(n, K, out = c("foldwise", "indices"), seed = NULL)
Arguments
n
Number of data points.
K
Number of folds. Must be at least 2 and not exceed n.
seed
Random seed so that the same division could be obtained again if
needed.
out
Format of the output, either 'foldwise' (default) or 'indices'.
See below for details.
Value
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 cv_ids
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.
# NOT RUN {### compute sample means within each foldn <- 100
y <- rnorm(n)
cv <- cv_ids(n, K=5)
cvmeans <- lapply(cv, function(fold) mean(y[fold$tr]))
# }# NOT RUN {# }