# Load necessary libraries
library(ggplot2)
library(stats)
simulate_student_features <- function(n = 100, clusters = 3) {
# Set the random seed
set.seed(260923)
# Generate unique student IDs
student_ids <- seq(1, n)
# Simulate student engagement
student_engagement <- rnorm(n, mean = 50, sd = 10)
# Simulate student performance
student_performance <- rnorm(n, mean = 60, sd = 15)
# Combine the data into a data frame
student_features <- data.frame(
student_id = student_ids,
student_engagement = student_engagement,
student_performance = student_performance
)
# Perform PCA for dimensionality reduction
pca_result <- prcomp(student_features[, -1], scale. = TRUE)
student_features_pca <- data.frame(student_id = student_ids, pca_result$x)
colnames(student_features_pca) <- c("student_id", paste0("PC", 1:ncol(pca_result$x)))
# Perform K-means clustering
kmeans_result <- kmeans(pca_result$x, centers = clusters)
# Add cluster labels to the data
student_features_pca$cluster <- as.factor(kmeans_result$cluster)
# Return the data frame
return(student_features_pca)
}
# Generate student features data with dimensionality reduction and clustering
student_data <- simulate_student_features()
head(student_data)
## student_id PC1 PC2 cluster
## 1 1 -0.4415374 -1.64338014 2
## 2 2 0.2737353 -0.08332113 1
## 3 3 1.9391244 -0.26845756 1
## 4 4 -1.0635415 -1.05927303 2
## 5 5 1.0139706 0.23352365 1
## 6 6 0.8785119 0.03787004 1
# Plot clusters
ggplot(student_data, aes(x = PC1, y = PC2, color = cluster)) +
geom_point() +
labs(title = "K-means Clustering of Student Features with PCA",
x = "Principal Component 1",
y = "Principal Component 2",
color = "Cluster") +
theme_minimal()
