This function cross-validates regression models in a user-supplied new sample or by using holdout (train-test), k-fold, or leave-one-out cross-validation.
performance_cv(
model,
data = NULL,
method = c("holdout", "k_fold", "loo"),
metrics = "all",
prop = 0.3,
k = 5,
stack = TRUE,
verbose = TRUE,
...
)
A data frame with columns for each metric requested, as well as k
if method = "holdout"
and the Method
used for cross-validation. If
method = "holdout"
and stack = TRUE
, the standard error (standard
deviation across holdout folds) for each metric is also included.
A regression model.
Optional. A data frame containing the same variables as model
that will be used as the cross-validation sample.
Character string, indicating the cross-validation method to use:
whether holdout ("holdout"
, aka train-test), k-fold ("k_fold"
), or
leave-one-out ("loo"
). If data
is supplied, this argument is ignored.
Can be "all"
, "common"
or a character vector of metrics to be
computed (some of c("ELPD", "Deviance", "MSE", "RMSE", "R2")
). "common" will
compute R2 and RMSE.
If method = "holdout"
, what proportion of the sample to hold
out as the test sample?
If method = "k_fold"
, the number of folds to use.
Logical. If method = "k_fold"
, should performance be computed
by stacking residuals from each holdout fold and calculating each metric on
the stacked data (TRUE
, default) or should performance be computed by
calculating metrics within each holdout fold and averaging performance
across each fold (FALSE
)?
Toggle warnings.
Not used.
model <- lm(mpg ~ wt + cyl, data = mtcars)
performance_cv(model)
Run the code above in your browser using DataLab