Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

cv-indices

Create cross-validation indices


Description

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.

Examples

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

projpred

Projection Predictive Feature Selection

v2.0.2
GPL-3
Authors
Juho Piironen [aut], Markus Paasiniemi [aut], Alejandro Catalina [cre, aut], Aki Vehtari [aut], Jonah Gabry [ctb], Marco Colombo [ctb], Paul-Christian Bürkner [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.