# Load necessary libraries
library(dbscan)
##
## Attaching package: 'dbscan'
## The following object is masked from 'package:stats':
##
## as.dendrogram
library(NbClust)
library(ellipse)
##
## Attaching package: 'ellipse'
## The following object is masked from 'package:graphics':
##
## pairs
library(mclust)
## Package 'mclust' version 6.1
## Type 'citation("mclust")' for citing this R package in publications.
# Function to simulate student data
simulate_student_data <- function(n = 200) {
set.seed(789)
student_interaction <- rnorm(n, mean = 70, sd = 15)
student_scores <- rnorm(n, mean = 80, sd = 20)
# Combine into a data frame
data.frame(student_interaction = student_interaction, student_scores = student_scores)
}
# Simulate the data
student_data <- simulate_student_data(n = 200)
# Perform dimensionality reduction on the data using PCA
pca_result <- prcomp(student_data[, c("student_interaction", "student_scores")], scale = TRUE)
# Plot the PCA results
plot(pca_result$x, col = 'blue', pch = 19, cex = 0.6)
# Determine the optimal number of clusters using the silhouette method
nb <- NbClust(student_data, distance = "euclidean", min.nc = 2, max.nc = 15, method = "ward.D2")
## *** : The Hubert index is a graphical method of determining the number of clusters.
## In the plot of Hubert index, we seek a significant knee that corresponds to a
## significant increase of the value of the measure i.e the significant peak in Hubert
## index second differences plot.
##
## *** : The D index is a graphical method of determining the number of clusters.
## In the plot of D index, we seek a significant knee (the significant peak in Dindex
## second differences plot) that corresponds to a significant increase of the value of
## the measure.
##
## *******************************************************************
## * Among all indices:
## * 5 proposed 2 as the best number of clusters
## * 8 proposed 3 as the best number of clusters
## * 3 proposed 5 as the best number of clusters
## * 1 proposed 9 as the best number of clusters
## * 2 proposed 12 as the best number of clusters
## * 1 proposed 13 as the best number of clusters
## * 1 proposed 14 as the best number of clusters
## * 2 proposed 15 as the best number of clusters
##
## ***** Conclusion *****
##
## * According to the majority rule, the best number of clusters is 3
##
##
## *******************************************************************
# Cluster the data using DBSCAN
set.seed(789)
dbscan_result <- dbscan(student_data[, c("student_interaction", "student_scores")], eps = 0.3, minPts = 5)
# Add the cluster results to the data frame
student_data$cluster <- as.factor(dbscan_result$cluster)
# Plot the clusters
pairs(student_data, col = dbscan_result$cluster)
legend("topright", legend = levels(as.factor(dbscan_result$cluster)), fill = 1:max(dbscan_result$cluster))
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:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
```
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.