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 )

K-means clustering

set.seed(123) kmeans_result <- kmeans(student_features[, c(“student_engagement”, “student_performance”)], centers = 3)

Add the cluster results to the data frame

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

Plot the clusters

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”)

Hierarchical clustering

dist_matrix <- dist(student_features[, c(“student_engagement”, “student_performance”)], method = “euclidean”) hclust_result <- hclust(dist_matrix, method = “ward.D2”)

Cut the dendrogram to get clusters

hierarchical_clusters <- cutree(hclust_result, k = 3)

Add the hierarchical cluster results to the data frame

student_features$hierarchical_cluster <- as.factor(hierarchical_clusters)

Plot the 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”)