# Load necessary libraries
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(FactoMineR)
## Warning: package 'FactoMineR' was built under R version 4.3.3
library(cluster)
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
# 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
)

# Determine the optimal number of clusters using the elbow method
wss <- (nrow(student_features) - 1) * sum(apply(student_features[, c("student_engagement", "student_performance")], 2, var))
for (i in 2:15) {
  wss[i] <- sum(kmeans(student_features[, c("student_engagement", "student_performance")], centers = i)$withinss)
}

# Plot the elbow method result
plot(1:15, wss, type = "b", xlab = "Number of Clusters", ylab = "Within groups sum of squares",
     main = "Elbow Method for Optimal Number of Clusters")

# From the plot, identify the optimal number of clusters (e.g., elbow point)
# Here, let's assume the optimal number of clusters is 3

# Cluster the data using KMeans
set.seed(123)
kmeans_result <- kmeans(student_features[, c("student_engagement", "student_performance")], centers = 3) # Change the number of centers as per the elbow plot

# Add the cluster results to the data frame
student_features$cluster <- as.factor(kmeans_result$cluster)

# Plot the clusters
ggplot(student_features, aes(student_engagement, student_performance, color = cluster)) +
  geom_point() +
  labs(title = "Clusters of Students",
       x = "Student Engagement",
       y = "Student Performance")

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.