Introduction

Learning analytics is the use of data to understand and improve learning. Unsupervised learning is a type of machine learning that can be used to identify patterns and relationships in data without the need for labeled data.

In this case study, you will use unsupervised learning to analyze learning data from a Simulated School course. You will use dimensionality reduction to reduce the number of features in the data, and then use clustering to identify groups of students with similar learning patterns.

Data

The data for this case study is generated with the simulated function below. The data contains the following features:

Student ID: A unique identifier for each student Feature 1: A measure of student engagement Feature 2: A measure of student performance

simulate_student_features <- function(n = 100) {
  # 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
  )

  # Return the data frame
  return(student_features)
}

This function takes the number of students to simulate as an input and returns a data frame with three columns: student_id, student_engagement, and student_performance. The student_engagement and student_performance features are simulated using normal distributions with mean values of 50 and 60, respectively, and standard deviations of 10 and 15, respectively.

To use the simulate_student_features() function, we can simply pass the desired number of students to simulate as the argument:

student_features <- simulate_student_features(n = 100)

We can then use this data frame to perform unsupervised learning to identify groups of students with similar learning patterns,

Tasks

# Features selected for PCA
pca_features <- student_features[, c("student_engagement", "student_performance")]

# Scaling PCA features
scaled_pca_features <- scale(pca_features)

# Execute dimensionality reduction using PCA
final_pca <- prcomp(scaled_pca_features, scale = TRUE)

# Viewing summary of results
summary(final_pca)
## Importance of components:
##                           PC1    PC2
## Standard deviation     1.0104 0.9895
## Proportion of Variance 0.5104 0.4896
## Cumulative Proportion  0.5104 1.0000
library(ggplot2)

# Building a kmeans model with 3 centers
kmeans_model <- kmeans(scaled_pca_features, centers = 3, nstart = 20)

# Extracting cluster assignments
clust_kmeans <- kmeans_model$cluster

# Plotting student features
ggplot(student_features, aes(x = student_engagement, y = student_performance, color = factor(clust_kmeans))) +
  geom_point() + 
  labs(title = "Student Data with K-Means Clustering",
       x = "Student Engagement",
       y = "Student Performance")

library(ggplot2)

# Building a kmeans model with 2 centers
kmeans_model <- kmeans(scaled_pca_features, centers = 2, nstart = 20)

# Extracting cluster assignments
clust_kmeans <- kmeans_model$cluster

# Plotting student features
ggplot(student_features, aes(x = student_engagement, y = student_performance, color = factor(clust_kmeans))) +
  geom_point() + 
  labs(title = "Student Data with K-Means Clustering",
       x = "Student Engagement",
       y = "Student Performance")

library(purrr)
library(ggplot2)

# Confirming correct value of k with an elbow plot
tot_withinss <- map_dbl(1:10,  function(k){
  model <- kmeans(x = student_features, centers = k)
  model$tot.withinss
})

# Generating the data frame
elbow_df <- data.frame(
  k = 1:10 ,
  tot_withinss = tot_withinss
)
 
# Plotting the elbow plot
ggplot(elbow_df, aes(x = k, y = tot_withinss)) +
  geom_line() +
  scale_x_continuous(breaks = 1:10) 

library(cluster)

# Generating k-means using pam() function
pam_features <- pam(student_features, k = 3)

# Plotting to identify silhouette width for three centers
plot(silhouette(pam_features))

library(cluster)

# Generating k-means using pam() function
pam_features <- pam(student_features, k = 2)

# Plotting to identify silhouette width for two centers
plot(silhouette(pam_features))

# Displaying model results
kmeans_model
## K-means clustering with 2 clusters of sizes 53, 47
## 
## Cluster means:
##   student_engagement student_performance
## 1         -0.5644579            0.557169
## 2          0.6365164           -0.628297
## 
## Clustering vector:
##   [1] 1 2 2 1 2 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1 1 1 2 1 2 1 2 2 1 1 1 2 1 2 1 2 1
##  [38] 1 1 1 2 2 2 2 1 2 1 2 2 2 2 2 1 2 2 1 1 1 1 1 1 1 2 1 1 1 2 2 1 1 2 2 2 1
##  [75] 1 1 1 1 2 1 2 2 2 1 1 2 1 2 1 1 1 2 2 1 2 1 2 1 1 2
## 
## Within cluster sum of squares by cluster:
## [1] 74.60182 52.46275
##  (between_SS / total_SS =  35.8 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

Submission

Submit a report containing the following:

Your report should include your code. Submit the published RPubs link to Blackboard.