cvFolds: Cross-validation folds

Description Usage Arguments Value Author(s) See Also Examples

View source: R/cvFolds.R

Description

Split n observations into K groups to be used for (repeated) K-fold cross-validation. K should thereby be chosen such that all groups are of approximately equal size.

Usage

1
2
  cvFolds(n, K = 5, R = 1,
    type = c("random", "consecutive", "interleaved"))

Arguments

n

an integer giving the number of observations to be split into groups.

K

an integer giving the number of groups into which the observations should be split (the default is five). Setting K equal to n yields leave-one-out cross-validation.

R

an integer giving the number of replications for repeated K-fold cross-validation. This is ignored for for leave-one-out cross-validation and other non-random splits of the data.

type

a character string specifying the type of folds to be generated. Possible values are "random" (the default), "consecutive" or "interleaved".

Value

An object of class "cvFolds" with the following components:

n

an integer giving the number of observations.

K

an integer giving the number of folds.

R

an integer giving the number of replications.

subsets

an integer matrix in which each column contains a permutation of the indices.

which

an integer vector giving the fold for each permuted observation.

Author(s)

Andreas Alfons

See Also

cvFit, cvSelect, cvTuning

Examples

1
2
3
4
5
set.seed(1234)  # set seed for reproducibility
cvFolds(20, K = 5, type = "random")
cvFolds(20, K = 5, type = "consecutive")
cvFolds(20, K = 5, type = "interleaved")
cvFolds(20, K = 5, R = 10)

Example output

Loading required package: lattice
Loading required package: robustbase

5-fold CV:    
Fold   Index
   1       3
   2      12
   3      11
   4      18
   5      14
   1      10
   2       1
   3       4
   4       8
   5       6
   1       7
   2       5
   3      20
   4      15
   5       2
   1       9
   2      17
   3      16
   4      19
   5      13

5-fold CV:    
Fold   Index
   1       1
   1       2
   1       3
   1       4
   2       5
   2       6
   2       7
   2       8
   3       9
   3      10
   3      11
   3      12
   4      13
   4      14
   4      15
   4      16
   5      17
   5      18
   5      19
   5      20

5-fold CV:    
Fold   Index
   1       1
   2       2
   3       3
   4       4
   5       5
   1       6
   2       7
   3       8
   4       9
   5      10
   1      11
   2      12
   3      13
   4      14
   5      15
   1      16
   2      17
   3      18
   4      19
   5      20

Repeated 5-fold CV with 10 replications:    
Fold     1  2  3  4  5  6  7  8  9 10
   1     7 12 18 19  1 15 11  9 10 14
   2     6 13  1  9 11 18 19 11  7 11
   3     3  6  6  3  6 19  6  8 11  6
   4     1 11 19 10  4 17  9  4  2 20
   5     4 18  4  4  3  8 18  2 16  9
   1    13  8 11 14  5  5 10 10  1 19
   2     8 10  5  6 16  4 20  7 12  5
   3    12  7  7  5  2  7  8  1  9 18
   4    10  3 17  2 18  6 12 15  4  3
   5    17  9 13 17 20 14  7 17 13 15
   1     5  1  2 12  8 10  5 18 19 13
   2    18 20  9 20 19 12  3  6 18 12
   3     9 16 12 11 10  2  1 20 17  2
   4    16  4 20  1  9  3 16  3 20 10
   5     2 14  8  7 13  9  2 14  5  4
   1    20 17  3 18 15 13 15 19  6 17
   2    11  2 10  8 17 11 17 16 14 16
   3    15  5 15 15  7 16  4  5  3  7
   4    19 15 14 13 14 20 13 13  8  1
   5    14 19 16 16 12  1 14 12 15  8

cvTools documentation built on May 2, 2019, 12:53 a.m.