crossv_kfold: Generate cross-validated K-fold test-training pairs

Description Usage Arguments Value Methods (by class) References See Also Examples

Description

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.

Usage

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, ...)

Arguments

data

A data frame

K

The number of folds

...

Arguments passed to methods

shuffle

If TRUE, randomly assign observations to folds. Otherwise, observations are sequentially assigned to folds.

stratify

If TRUE, within each group observations are split into folds, and those folds combined. If FALSE, groups are assigned into folds.

Value

A data frame with K rows and the following columns:

sample

A list of resample objects. Training sets.

.id

An integer vector of identifiers.

Methods (by class)

References

See Also

This function has more features than the modelr function crossv_kfold.

Examples

 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))

jrnold/resamplr documentation built on May 20, 2019, 1:05 a.m.