Cross-validation with caret in R

Here are various examples of performing cross-validation in R using the caret package for different types of regression models:

Linear Regression

# Load required libraries
library(caret)
# Load the mtcars dataset
data(mtcars)
# Set up trainControl for cross-validation
mtcars.scale <- data.frame(mtcars[,1],scale(mtcars[,-1]))
train_control <- trainControl(method = "repeatedcv", number = 10, repeats = 100)
# Train the model using linear regression
model <- train(mpg ~ ., data = mtcars.scale, method = "lm", trControl = train_control)
# Plot the results
plot(model_ridge)
# Print the results
print(model)
# Check cross-validated metrics
print(model$results)

Ridge Regression

# Train the model using Ridge regression (alpha = 0)
model_ridge <- train(mpg ~ ., data = mtcars.scale, method = "glmnet", tuneGrid = expand.grid(alpha = 0, lambda = seq(2, 5, by = 0.1)), trControl = train_control)
# Plot the results
plot(model_ridge)
# Print the results
print(model_ridge)

Lasso Regression

# Train the model using Lasso regression (alpha = 1)
model_lasso <- train(mpg ~ ., data = mtcars.scale, method = "glmnet", tuneGrid = expand.grid(alpha = 1, lambda = seq(0.2, 2, by = 0.1)), trControl = train_control)
# Plot the results
plot(model_lasso)
# Print the results
print(model_lasso)

K-Nearest Neighbors Regression

# Train the model using KNN regression
set.seed(123)
model_knn <- train(mpg ~ ., data = mtcars, method = "knn", tuneGrid = data.frame(k = 1:10) , trControl = train_control)
# Plot the results
plot(model_lasso)
# Print the results
print(model_knn)

Modifié le: vendredi 27 septembre 2024, 15:07