Description Usage Arguments Value Methods (by class) References See Also Examples
Generate cross-validated K-fold test-training pairs. In addition to ordinary
K-fold cross-validation, this supports stratified K-fold cross validation
if data
is a grouped data frame and stratify = TRUE
, and
Group K-fold if data
is a grouped data frame and stratify = FALSE
.
1 2 3 4 5 6 7 8 | crossv_kfold(data, K, ...)
## S3 method for class 'data.frame'
crossv_kfold(data, K = 5L, shuffle = TRUE, ...)
## S3 method for class 'grouped_df'
crossv_kfold(data, K = 5L, shuffle = TRUE,
stratify = FALSE, ...)
|
data |
A data frame |
K |
The number of folds |
... |
Arguments passed to methods |
shuffle |
If |
stratify |
If |
A data frame with K
rows and the following columns:
A list of resample
objects. Training sets.
An integer vector of identifiers.
data.frame
: Splits rows in a data frame into folds.
grouped_df
: Partitions rows within each group of a grouped data frame
into folds if stratify = FALSE
. This ensures that the test and training
sets will have approximately equal proportions of each group. If stratify = TRUE
,
then the groups are partitioned into folds.
Breiman, L., Friedman, J.H., Olshen, R.A. and Stone, C.J. (1984) Classification and Regression Trees. Wadsworth.
Burman, P. (1989) A comparative study of ordinary cross-validation, v-fold cross-validation and repeated learning-testing methods. Biometrika, 76, 503–514
Davison, A.C. and Hinkley, D.V. (1997) Bootstrap Methods and Their Application. Cambridge University Press.
Efron, B. (1986) How biased is the apparent error rate of a prediction rule? Journal of the American Statistical Association, 81, 461–470.
Stone, M. (1974) Cross-validation choice and assessment of statistical predictions (with Discussion). Journal of the Royal Statistical Society, B, 36, 111–147.
This function has more features than the modelr function
crossv_kfold
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Example originally from modelr::crossv_mc
library("purrr")
library("dplyr")
# 5-fold cross-validation
cv1 <- crossv_kfold(mtcars, K = 5)
models <- map(cv1$train, ~ lm(mpg ~ wt, data = .))
summary(map2_dbl(models, cv1$test, modelr::rmse))
# k-fold by group
cv2 <- crossv_kfold(group_by(mtcars, cyl), K = 2)
models <- map(cv2$train, ~ lm(mpg ~ wt, data = .))
summary(map2_dbl(models, cv2$test, modelr::rmse))
# stratified k-fold
cv3 <- crossv_kfold(group_by(mtcars, am), K = 3, stratify = TRUE)
models <- map(cv3$train, ~ lm(mpg ~ wt, data = .))
summary(map2_dbl(models, cv3$test, modelr::rmse))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.