# Load necessary libraries
library(tidyverse)
## ── 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)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(ggfortify)

# 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
pca_result <- prcomp(student_features, scale. = TRUE)

# Plot the PCA results without specifying color
autoplot(pca_result, data = student_features, alpha = 0.7)

# Determine the optimal number of clusters using the silhouette method
sil_width <- c(NA)
for (i in 2:15) {
  diss <- dist(student_features)
  pam_fit <- pam(diss, k = i)
  sil_width[i] <- pam_fit$silinfo$avg.width
}

# Plot the silhouette method result
plot(1:15, sil_width, type = "b", xlab = "Number of Clusters", ylab = "Average Silhouette Width",
     main = "Silhouette Method for Optimal Number of Clusters")

# Here, let's assume the optimal number of clusters is 3

# Cluster the data using pam (Partitioning Around Medoids)
diss <- dist(student_features)
pam_fit <- pam(diss, k = 3)

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

# 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.