Learn R Programming

origami (version 0.8.0)

cross_validate: Main Cross-Validation Function

Description

Applies cvfun to the folds using future_lapply and combines the results across folds using combine_results.

Usage

cross_validate(cv_fun, folds, ..., .combine = TRUE,
  .combine_control = list(), .old_results = NULL)

Arguments

cv_fun

a function that takes a 'fold' as it's first argument and returns a list of results from that fold.

folds

a list of folds to loop over generated using make_folds.

...

other arguments passed to cvfun.

.combine

(logical) - should combine_results be called.

.combine_control

(list) - arguments to combine_results.

.old_results

(list) - the returned result from a previous call to This function. Will be combined with the current results. This is useful for adding additional CV folds to a results object.

Value

A list of results, combined across folds.

Examples

Run this code
data(mtcars)

# resubstitution MSE
r <- lm(mpg ~ ., data = mtcars)
mean(resid(r)^2)

# function to calculate cross-validated squared error
cvlm <- function(fold) {
    train_data <- training(mtcars)
    valid_data <- validation(mtcars)
    
    r <- lm(mpg ~ ., data = train_data)
    preds <- predict(r, newdata = valid_data)
    list(coef = data.frame(t(coef(r))), SE = ((preds - valid_data$mpg)^2))
    
}

# replicate the resubstitution estimate
resub <- make_folds(mtcars, fold_fun = folds_resubstitution)[[1]]
resub_results <- cvlm(resub)
mean(resub_results$SE)

# cross-validated estimate
folds <- make_folds(mtcars)
results <- cross_validate(cvlm, folds)
mean(results$SE)

Run the code above in your browser using DataLab