We’ll demonstrate a basic machine learning workflow using the caret
package for building a predictive model.
Step 1: Install and Load Caret
If you don’t have caret
installed, you can do so with:
rCopy codeinstall.packages("caret")
Then, load the library:
rCopy codelibrary(caret)
Step 2: Create a Sample Dataset
We’ll use the same dataset but add a binary outcome variable to predict:
rCopy code# Adding a binary outcome variable
set.seed(789)
data$outcome <- ifelse(data$weight > 70, "Heavy", "Light")
Step 3: Split the Dataset
Split the data into training and testing sets:
rCopy code# Set seed for reproducibility
set.seed(123)
# Create a training index
train_index <- createDataPartition(data$outcome, p = 0.7, list = FALSE)
# Split data into training and testing sets
train_data <- data[train_index, ]
test_data <- data[-train_index, ]
Step 4: Train a Model
We’ll train a simple logistic regression model:
rCopy code# Train a logistic regression model
model <- train(outcome ~ height + weight, data = train_data, method = "glm", family = "binomial")
# Print the model summary
summary(model)
Step 5: Make Predictions
Use the model to make predictions on the test set:
rCopy code# Make predictions on the test set
predictions <- predict(model, newdata = test_data)
# Confusion matrix to evaluate performance
confusionMatrix(predictions, test_data$outcome)
Leave a Reply