Back to Course
Machine Learning

Machine Learning Workflows

Beyond classical statistics, R offers a full machine learning toolkit. This lesson covers unsupervised clustering, the Random Forest ensemble method, and how to properly evaluate any classification model.

1. K-Means Clustering (Unsupervised Learning)

K-means groups similar observations into k clusters without any labeled outcome variable.

set.seed(42)
df_scaled <- scale(df[, c("age", "salary")])  # standardize features first!

km_model <- kmeans(df_scaled, centers = 3, nstart = 25)
df$cluster <- km_model$cluster

table(df$cluster)
km_model$centers   # coordinates of each cluster center
Production Reality: Always scale() your numeric features before k-means — variables on larger numeric scales (like salary) would otherwise dominate the distance calculation.

2. Choosing the Number of Clusters — Elbow Method

wss <- sapply(1:10, function(k) {{
  kmeans(df_scaled, centers = k, nstart = 10)$tot.withinss
}})

plot(1:10, wss, type = "b", pch = 19,
     xlab = "Number of Clusters (k)", ylab = "Total Within-Cluster Sum of Squares")

3. Random Forest — Supervised Classification/Regression

Random Forest builds many decision trees and combines their votes — robust, accurate, and requires minimal tuning.

install.packages("randomForest")
library(randomForest)
library(caret)

set.seed(42)
train_index <- createDataPartition(df$churned, p = 0.8, list = FALSE)
train_data <- df[train_index, ]
test_data  <- df[-train_index, ]

rf_model <- randomForest(as.factor(churned) ~ tenure + monthly_charges + support_calls,
                          data = train_data, ntree = 500, importance = TRUE)

print(rf_model)
importance(rf_model)          # which variables matter most
varImpPlot(rf_model)          # visualize variable importance

4. Making Predictions with Random Forest

predictions <- predict(rf_model, newdata = test_data)

5. Model Evaluation — Confusion Matrix

library(caret)

conf_matrix <- confusionMatrix(predictions, as.factor(test_data$churned))
print(conf_matrix)
MetricMeaning
AccuracyOverall proportion of correct predictions
Sensitivity (Recall)Proportion of actual positives correctly identified
SpecificityProportion of actual negatives correctly identified
PrecisionProportion of predicted positives that are correct
KappaAgreement beyond chance

6. Model Evaluation — ROC and AUC

install.packages("pROC")
library(pROC)

prob_predictions <- predict(rf_model, newdata = test_data, type = "prob")[, 2]
roc_obj <- roc(test_data$churned, prob_predictions)

plot(roc_obj, main = "ROC Curve")
auc(roc_obj)   # Area Under the Curve — closer to 1 is better
Common Issues: Accuracy alone can be misleading on imbalanced datasets (e.g., 95% non-churners). Always check sensitivity, specificity, and AUC together.

7. Train/Test Split Best Practice

  • Always split data before any preprocessing that "learns" from the data (like scaling parameters).
  • A common split is 70–80% training, 20–30% testing.
  • Use set.seed() so your split — and results — are reproducible.

8. Production-Ready Checklist

  • ✅ Scale features before k-means — and choose k with the elbow method.
  • ✅ Use Random Forest for robust classification — minimal tuning, strong baseline.
  • ✅ Split data into train/test — before evaluating any supervised model.
  • ✅ Evaluate with confusion matrix + ROC/AUC — not accuracy alone.
Pro Tip: Machine learning in R (via caret, randomForest, tidymodels) is production-capable — many real analytics teams run it directly in R without ever touching Python.

Ready to master R Programming?

Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.

Explore Course