Learn R Programming

nestedcv

Nested cross-validation (CV) for the glmnet and caret packages. With glmnet this includes cross-validation of elastic net alpha parameter. A number of feature selection filter functions (t-test, Wilcoxon test, ANOVA, Pearson/Spearman correlation, random forest, ReliefF) for feature selection are provided and can be embedded within the outer loop of the nested CV. Nested CV can be also be performed with the caret package giving access to the large number of prediction methods available in caret.

Installation

Install from CRAN

install.packages("nestedcv")

Install from Github

devtools::install_github("myles-lewis/nestedcv")

Example

In this example using iris dataset (multinomial, 3 classes), we fit a glmnet model, tuning both lambda and alpha with 10 x 10-fold nested CV.

library(nestedcv)
data(iris)
y <- iris$Species
x <- as.matrix(iris[, -5])

cores <- parallel::detectCores(logical = FALSE)  # detect physical cores

res <- nestcv.glmnet(y, x, family = "multinomial", cv.cores = cores)
summary(res)

Use summary() to see full information from the nested model fitting. coef() can be used to show the coefficients of the final fitted model. Filters can be used by setting the filterFUN argument. Options for the filter function are passed as a list through filter_options.

Output from the nested CV with glmnet can be plotted to show how deviance is affected by alpha and lambda.

plot_alphas(res)
plot_lambdas(res)

The tuning of lambda and alpha for each outer CV fold can be plotted. Here we inspect outer CV fold 1.

plot(res$outer_result[[1]]$cvafit)

ROC curves from left-out folds from both outer and inner CV can be plotted for binary comparisons (see vignette).

Nested CV can also be performed using the caret package framework. Here we use caret for tuning random forest using the ranger package.

res <- nestcv.train(y, x, method = "ranger", cv.cores = cores)
summary(res)

Copy Link

Version

Install

install.packages('nestedcv')

Monthly Downloads

541

Version

0.9.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Myles Lewis

Last Published

July 14th, 2026

Functions in nestedcv (0.9.0)

one_hot

One-hot encode
nestcv.explain

Generate SHAP values from nestedcv models using shapr
nestcv.SuperLearner

Outer cross-validation of SuperLearner model
nestcv.train

Nested cross-validation for caret
model.hsstan

hsstan model for cross-validation
outercv

Outer cross-validation of selected models
nestcv.glmnet

Nested cross-validation with glmnet
lm_filter

Linear model filter
metrics

Model performance metrics
mcc

Matthews correlation coefficient
plot.prc

Plot precision-recall curve
plot_caret

Plot caret tuning
plot_varImp

Variable importance plot
plot_var_ranks

Plot variable importance rankings
plot_var_stability

Plot variable stability
plot_lambdas

Plot cross-validated glmnet lambdas across outer folds
plot.cva.glmnet

Plot lambda across range of alphas
plot_shap_bar

SHAP importance bar plot
plot_shap_beeswarm

SHAP importance beeswarm plot
plot_alphas

Plot cross-validated glmnet alpha
predSummary

Summarise prediction performance metrics
ranger_filter

Random forest ranger filter
predict.hsstan

Predict from hsstan model fitted within cross-validation
pred_nestcv_glmnet

Prediction wrappers to use shapr with nestedcv
predict.cva.glmnet

Predict method for cva.glmnet models
randomsample

Oversampling and undersampling
predict.nestcv.glmnet

Predict method for nestcv.glmnet fits
prc

Build precision-recall curve
pls_filter

Partial Least Squares filter
relieff_filter

ReliefF filter
supervisedPCA

Supervised PCA plot
repeatfolds

Create folds for repeated nested CV
slim

Slim nestedcv models
train_preds

Outer training fold predictions
repeatcv

Repeated nested CV
stat_filter

Univariate filter for binary classification with mixed predictor datatypes
summary_vars

Summarise variables
train_roc

Build ROC curve from outer CV training folds
rf_filter

Random forest filter
smote

SMOTE
ttest_filter

Univariate filters
var_direction

Variable directionality
txtProgressBar2

Text Progress Bar 2
var_stability

Variable stability
train_summary

Summarise performance on outer training folds
weight

Calculate weights for class imbalance
boxplot_expression

Boxplot expression levels of model predictors
barplot_var_stability

Barplot variable stability
coef.cva.glmnet

Extract coefficients from a cva.glmnet object
class_balance

Check class balance in training folds
boot_ttest

Bootstrap univariate filters
collinear

Filter to reduce collinearity in predictors
combo_filter

Combo filter
boot_filter

Bootstrap for filter functions
boruta_filter

Boruta filter
coef.nestcv.glmnet

Extract coefficients from nestcv.glmnet object
cv_varImp

Extract variable importance from outer CV caret models
innercv_preds

Inner CV predictions
correls2

Correlation between a vector and a matrix
innercv_roc

Build ROC curve from left-out folds from inner CV
innercv_summary

Summarise performance on inner CV test folds
glmnet_coefs

glmnet coefficients
cva.glmnet

Cross-validation of alpha for glmnet
cv_coef

Coefficients from outer CV glmnet models
glmnet_filter

glmnet filter
lines.prc

Add precision-recall curve to a plot