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) }
student_features <- simulate_student_features(n = 100)
head(student_features)
pca_result <- prcomp(student_features[, -1], scale. = TRUE)
summary(pca_result)
plot(pca_result, type = “l”, main = “Scree Plot for PCA”)
wss <- numeric(10) for (i in 1:10) { kmeans_result <- kmeans(pca_result\(x, centers = i, nstart = 25) wss[i] <- kmeans_result\)tot.withinss }
plot(1:10, wss, type = “b”, xlab = “Number of Clusters”, ylab = “Within Sum of Squares”, main = “Elbow Method for Optimal K”)
optimal_k <- 3 # Replace with the optimal number determined from the elbow method kmeans_result <- kmeans(pca_result$x, centers = optimal_k, nstart = 25)
student_features\(cluster_kmeans <- kmeans_result\)cluster
table(student_features$cluster_kmeans)
hc <- hclust(dist(pca_result$x))
cut_tree <- cutree(hc, k = optimal_k)
student_features$cluster_hierarchical <- cut_tree
table(student_features$cluster_hierarchical)
plot(hc, main = “Dendrogram for Hierarchical Clustering”)
comparison_table <- table(student_features\(cluster_kmeans, student_features\)cluster_hierarchical) print(“Comparison of Clustering Algorithms:”) print(comparison_table)
summary_table <- aggregate(. ~ cluster_kmeans, data = student_features[, -1], mean) print(“Summary Statistics for Each Cluster Based on KMeans:”) print(summary_table)
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)