Skip to contents

cv_predict() extracts out-of-sample predictions from a “cv” or related object, cv_resid() extracts out-of-sample residuals. Both are generic.

Usage

cv_predict(x, ...)

# S3 method for cv
cv_predict(x, simplify = TRUE, ...)

# S3 method for model
cv_predict(x, simplify = TRUE, ...)

# S3 method for multimodel
cv_predict(x, simplify = TRUE, ...)

# S3 method for default
cv_predict(x, simplify = TRUE, ...)

cv_resid(x, ...)

# S3 method for cv
cv_resid(x, simplify = TRUE, ...)

# S3 method for model
cv_resid(x, ...)

# S3 method for multimodel
cv_resid(x, simplify = TRUE, ...)

# S3 method for default
cv_resid(x, simplify = TRUE, ...)

Arguments

x

A “cv” object or an object of another class. See “Methods”.

...

Not used in *.cv methods, passed to cv in all other methods.

simplify

Logical: If FALSE, a list of vectors will be returned in case of multiple models, if TRUE the output will be converted to a matrix.

Value

A vector, or if there is more than one model, a matrix (if simplify=TRUE) or list of vectors (simplify=FALSE).

Details

The output will depend on the folds used in cv(). If the folds do not include all observations in the model data, the output vector will be shorter than the number of rows in the model data.

Methods

  • cv_predict.cv(x, ...) is the core method described above.

  • cv_predict.model(x, ...) and cv_predict.multimodel execute x %>% cv(...) %>% cv_predict.

  • cv_predict.default(x, ...) is applied to a fitted model; it executes x %>% model %>% cv(...) %>% cv_predict.

cv_resid has the same methods as cv_predict.

Examples

# Simulate data 
set.seed(123)
n <- 30
p <- 20
x <- matrix(rnorm(p*n), nrow = n)
y <- rowSums(x) + rnorm(n)
mymodel <- model(lm(y~x))

# Compare correlation of in-sample and out-of-sample predictions
# with response
preds <- data.frame(
  training = predict(mymodel), 
  test = drop(cv_predict(mymodel)))
cor(response(mymodel), preds)
#>      training      test
#> [1,] 0.987604 0.8395298