Simulate the data

simulate_student_features <- function(n = 100) { set.seed(260923) student_ids <- seq(1, n) student_engagement <- rnorm(n, mean = 50, sd = 10) student_performance <- rnorm(n, mean = 60, sd = 15) student_features <- data.frame( student_id = student_ids, student_engagement = student_engagement, student_performance = student_performance ) return(student_features) }

Generate simulated data

student_features <- simulate_student_features(n = 100)

Display the first few rows of the data

head(student_features)

Perform PCA

pca_result <- prcomp(student_features[, -1], scale. = TRUE)

Summary of PCA

summary(pca_result)

Plotting the variance explained

plot(pca_result, type = “l”, main = “Scree Plot for PCA”)

Determine the optimal number of clusters using the elbow method

wss <- numeric(10) for (i in 1:10) { kmeans_result <- kmeans(pca_result\(x, centers = i, nstart = 25) wss[i] <- kmeans_result\)tot.withinss }

Elbow plot

plot(1:10, wss, type = “b”, xlab = “Number of Clusters”, ylab = “Within Sum of Squares”, main = “Elbow Method for Optimal K”)

Perform KMeans clustering with the optimal number of clusters

optimal_k <- 3 # Replace with the optimal number determined from the elbow method kmeans_result <- kmeans(pca_result$x, centers = optimal_k, nstart = 25)

Add cluster labels to the original data

student_features\(cluster_kmeans <- kmeans_result\)cluster

Display the cluster assignments for KMeans

table(student_features$cluster_kmeans)

Perform hierarchical clustering

hc <- hclust(dist(pca_result$x))

Cut the dendrogram to get clusters

cut_tree <- cutree(hc, k = optimal_k)

Add cluster labels to the original data

student_features$cluster_hierarchical <- cut_tree

Display the cluster assignments for hierarchical clustering

table(student_features$cluster_hierarchical)

Plot the dendrogram to visualize hierarchical clustering

plot(hc, main = “Dendrogram for Hierarchical Clustering”)

Comparison of clustering algorithms

comparison_table <- table(student_features\(cluster_kmeans, student_features\)cluster_hierarchical) print(“Comparison of Clustering Algorithms:”) print(comparison_table)

Summary statistics for each cluster

summary_table <- aggregate(. ~ cluster_kmeans, data = student_features[, -1], mean) print(“Summary Statistics for Each Cluster Based on KMeans:”) print(summary_table)

Summary statistics for each cluster based on hierarchical clustering

summary_table_hierarchical <- aggregate(. ~ cluster_hierarchical, data = student_features[, -1], mean) print(“Summary Statistics for Each Cluster Based on Hierarchical Clustering:”) print(summary_table_hierarchical)