Introduction
The caret package provides a unified interface for machine learning. It simplifies model training and evaluation.
Basic Usage
library(caret)
# Split data
trainIndex <- createDataPartition(y, p = 0.8, list = FALSE)
train <- data[trainIndex, ]
test <- data[-trainIndex, ]
# Train model
model <- train(y ~ ., data = train, method = "lm")
# Predict
predictions <- predict(model, test)
# Evaluate
postResample(predictions, test$y)
Model Training
# Various methods
train(y ~ x, data = train, method = "rf") # Random Forest
train(y ~ x, data = train, method = "glm") # Logistic
train(y ~ x, data = train, method = "svm") # SVM
train(y ~ x, data = train, method = "knn") # KNN
# With cross-validation
trainControl(method = "cv", number = 10)
train(y ~ x, data = train, trControl = ctrl)
Summary
caret provides consistent ML interface. Use it for efficient model building.