library(ggplot2) library(FactoMineR) library(cluster) library(factoextra)
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) }
student_features <- simulate_student_features(n = 100)
res.pca <- PCA(student_features[, c(“student_engagement”, “student_performance”)], graph = FALSE)
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 )
set.seed(123) kmeans_result <- kmeans(student_features[, c(“student_engagement”, “student_performance”)], centers = 3)
student_features\(kmeans_cluster <- as.factor(kmeans_result\)cluster)
ggplot(student_features, aes(student_engagement, student_performance, color = kmeans_cluster)) + geom_point() + labs(title = “K-Means Clustering of Students”, x = “Student Engagement”, y = “Student Performance”)
dist_matrix <- dist(student_features[, c(“student_engagement”, “student_performance”)], method = “euclidean”) hclust_result <- hclust(dist_matrix, method = “ward.D2”)
hierarchical_clusters <- cutree(hclust_result, k = 3)
student_features$hierarchical_cluster <- as.factor(hierarchical_clusters)
ggplot(student_features, aes(student_engagement, student_performance, color = hierarchical_cluster)) + geom_point() + labs(title = “Hierarchical Clustering of Students”, x = “Student Engagement”, y = “Student Performance”)