Load necessary libraries

library(ggplot2) library(FactoMineR) library(cluster) library(factoextra)

Function to simulate student data

simulate_student_features <- function(n = 100) { set.seed(123) student_engagement <- rnorm(n, mean = 50, sd = 10) student_performance <- rnorm(n, mean = 50, sd = 10)

# Combine into a data frame data.frame(student_engagement = student_engagement, student_performance = student_performance) }

Simulate the data

student_features <- simulate_student_features(n = 100)

Perform dimensionality reduction on the data using PCA

res.pca <- PCA(student_features[, c(“student_engagement”, “student_performance”)], graph = FALSE)

Plot the PCA results

fviz_pca_ind(res.pca, col.ind = “cos2”, # Color by the quality of representation gradient.cols = c(“#00AFBB”, “#E7B800”, “#FC4E07”), repel = TRUE # Avoid text overlapping )

Determine the optimal number of clusters using the elbow method

wss <- (nrow(student_features) - 1) * sum(apply(student_features[, c(“student_engagement”, “student_performance”)], 2, var)) for (i in 2:15) { wss[i] <- sum(kmeans(student_features[, c(“student_engagement”, “student_performance”)], centers = i)$withinss) }

Plot the elbow method result

plot(1:15, wss, type = “b”, xlab = “Number of Clusters”, ylab = “Within groups sum of squares”, main = “Elbow Method for Optimal Number of Clusters”)

From the plot, identify the optimal number of clusters (e.g., elbow point)

Here, let’s assume the optimal number of clusters is 3

Cluster the data using KMeans

set.seed(123) kmeans_result <- kmeans(student_features[, c(“student_engagement”, “student_performance”)], centers = 3) # Change the number of centers as per the elbow plot

Add the cluster results to the data frame

student_features\(cluster <- as.factor(kmeans_result\)cluster)

Plot the clusters

ggplot(student_features, aes(student_engagement, student_performance, color = cluster)) + geom_point() + labs(title = “Clusters of Students”, x = “Student Engagement”, y = “Student Performance”)