# Load necessary libraries
library(tidyverse)  # Provides ggplot2 for plotting and other functionalities
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(cluster)    # For KMeans clustering
library(factoextra) # For silhouette analysis (optional)
## 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 princomp
res.pca <- princomp(student_features[, c("student_engagement", "student_performance")])

# Visualize the PCA results using fviz_pca_ind
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

# Rest of the code (elbow method, clustering, visualization, silhouette analysis)
# Plot the elbow method result (unchanged)
plot(1:1, type = "b", xlab = "Number of Clusters", ylab = "Within groups sum of squares",
     main = "Elbow Method for Optimal Number of Clusters")

# Identify the optimal number of clusters based on the elbow plot (replace 3 with chosen value)
optimal_clusters <- 3  # Replace with the elbow point identified from the plot

# Cluster the data using KMeans (change variable names if needed)
set.seed(123)
kmeans_result <- kmeans(student_features[, c("student_engagement", "student_performance")], centers = optimal_clusters)

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

# Cluster visualization with alternative method (using geom_point and color aesthetic)
ggplot(student_features, aes(x = student_engagement, y = student_performance, color = cluster)) +
  geom_point(size = 3) +  # Adjust point size as desired
  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.