simulate_student_features <- function(n = 100, mean_engagement = 50, sd_engagement = 10, mean_performance = 60, sd_performance = 15) {
# Set the random seed
set.seed(260923)
# Generate unique student IDs
student_ids <- seq(1, n)
# Simulate student engagement
student_engagement <- rnorm(n, mean = mean_engagement, sd = sd_engagement)
# Simulate student performance
student_performance <- rnorm(n, mean = mean_performance, sd = sd_performance)
# Combine the data into a data frame
student_features <- data.frame(
student_id = student_ids,
student_engagement = student_engagement,
student_performance = student_performance
)
# Return the data frame
return(student_features)
}
# Call the simulate_student_features function to generate the data
student_features <- simulate_student_features()
# Select the columns for clustering (excluding student_id)
clustering_data <- student_features[, c("student_engagement", "student_performance")]
# Perform k-means clustering
k <- 3 # Number of clusters
set.seed(123) # Set seed for reproducibility
kmeans_result <- kmeans(clustering_data, centers = k)
# Add cluster labels to the student features data frame
student_features$cluster <- kmeans_result$cluster
# View the clustered data
head(student_features)
## student_id student_engagement student_performance cluster
## 1 1 35.47855 50.52231 2
## 2 2 51.79512 58.88396 3
## 3 3 62.41012 40.56755 3
## 4 4 35.20679 62.46033 2
## 5 5 59.37552 54.69326 3
## 6 6 57.00109 54.09745 3
# Call the simulate_student_features function to generate the data
student_features <- simulate_student_features()
# Select the columns for dimensionality reduction (excluding student_id and cluster)
reduction_data <- student_features[, c("student_engagement", "student_performance")]
# Perform PCA
pca_result <- prcomp(reduction_data, scale. = TRUE)
# Get the principal component scores
pc_scores <- as.data.frame(predict(pca_result))
# Add cluster labels to the principal component scores
pc_scores$cluster <- student_features$cluster
# View the data after dimensionality reduction
print(head(pc_scores))
## PC1 PC2
## 1 -0.4415374 -1.64338014
## 2 0.2737353 -0.08332113
## 3 1.9391244 -0.26845756
## 4 -1.0635415 -1.05927303
## 5 1.0139706 0.23352365
## 6 0.8785119 0.03787004
library(ggplot2)
# Call the simulate_student_features function to generate the data
student_features <- simulate_student_features()
# Select the columns for clustering (excluding student_id)
clustering_data <- student_features[, c("student_engagement", "student_performance")]
# Perform k-means clustering
k <- 3 # Number of clusters
set.seed(123) # Set seed for reproducibility
kmeans_result <- kmeans(clustering_data, centers = k)
# Add cluster labels to the student features data frame
student_features$cluster <- as.factor(kmeans_result$cluster)
# Plot using ggplot
ggplot(student_features, aes(x = student_engagement, y = student_performance, color = cluster)) +
geom_point(size = 3) +
labs(title = "Student Engagement vs. Performance",
x = "Student Engagement",
y = "Student Performance",
color = "Cluster") +
theme_minimal()

# Cluster density plot(A cluster density plot is a visual representation of the density distribution of data points in a two-dimensional space, where each point is colored according to its cluster membership. It's a useful tool for understanding the distribution of data within clusters and identifying any patterns or overlaps.)
ggplot(student_features, aes(x = student_engagement, y = student_performance)) +
geom_density_2d(aes(color = cluster)) +
labs(title = "Student Engagement vs. Performance",
x = "Student Engagement",
y = "Student Performance",
color = "Cluster") +
theme_minimal()
