library(MASS)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.3
data <- read.csv("students_adaptability_level_online_education.csv")
head(data)
##   Gender   Age Education.Level Institution.Type IT.Student Location
## 1    Boy 21-25      University   Non Government         No      Yes
## 2   Girl 21-25      University   Non Government         No      Yes
## 3   Girl 16-20         College       Government         No      Yes
## 4   Girl 11-15          School   Non Government         No      Yes
## 5   Girl 16-20          School   Non Government         No      Yes
## 6    Boy 11-15          School   Non Government         No      Yes
##   Load.shedding Financial.Condition Internet.Type Network.Type Class.Duration
## 1           Low                 Mid          Wifi           4G            3-6
## 2          High                 Mid   Mobile Data           4G            1-3
## 3           Low                 Mid          Wifi           4G            1-3
## 4           Low                 Mid   Mobile Data           4G            1-3
## 5           Low                Poor   Mobile Data           3G              0
## 6           Low                Poor   Mobile Data           3G            1-3
##   Self.Lms Device Adaptivity.Level
## 1       No    Tab         Moderate
## 2      Yes Mobile         Moderate
## 3       No Mobile         Moderate
## 4       No Mobile         Moderate
## 5       No Mobile              Low
## 6       No Mobile              Low
str(data)
## 'data.frame':    1205 obs. of  14 variables:
##  $ Gender             : chr  "Boy" "Girl" "Girl" "Girl" ...
##  $ Age                : chr  "21-25" "21-25" "16-20" "11-15" ...
##  $ Education.Level    : chr  "University" "University" "College" "School" ...
##  $ Institution.Type   : chr  "Non Government" "Non Government" "Government" "Non Government" ...
##  $ IT.Student         : chr  "No" "No" "No" "No" ...
##  $ Location           : chr  "Yes" "Yes" "Yes" "Yes" ...
##  $ Load.shedding      : chr  "Low" "High" "Low" "Low" ...
##  $ Financial.Condition: chr  "Mid" "Mid" "Mid" "Mid" ...
##  $ Internet.Type      : chr  "Wifi" "Mobile Data" "Wifi" "Mobile Data" ...
##  $ Network.Type       : chr  "4G" "4G" "4G" "4G" ...
##  $ Class.Duration     : chr  "3-6" "1-3" "1-3" "1-3" ...
##  $ Self.Lms           : chr  "No" "Yes" "No" "No" ...
##  $ Device             : chr  "Tab" "Mobile" "Mobile" "Mobile" ...
##  $ Adaptivity.Level   : chr  "Moderate" "Moderate" "Moderate" "Moderate" ...
summary(data)
##     Gender              Age            Education.Level    Institution.Type  
##  Length:1205        Length:1205        Length:1205        Length:1205       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##   IT.Student          Location         Load.shedding      Financial.Condition
##  Length:1205        Length:1205        Length:1205        Length:1205        
##  Class :character   Class :character   Class :character   Class :character   
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character   
##  Internet.Type      Network.Type       Class.Duration       Self.Lms        
##  Length:1205        Length:1205        Length:1205        Length:1205       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##     Device          Adaptivity.Level  
##  Length:1205        Length:1205       
##  Class :character   Class :character  
##  Mode  :character   Mode  :character
data[] <- lapply(data, function(x) {
  if(is.character(x)) as.factor(x) else x
})
data$Adaptivity.Level <- factor(data$Adaptivity.Level,
                                levels = c("Low","Moderate","High"),
                                ordered = TRUE)
data <- na.omit(data)
set.seed(123)
index <- sample(1:nrow(data), 0.7 * nrow(data))
train_data <- data[index, ]
test_data <- data[-index, ]
model_lda <- lda(Adaptivity.Level ~ ., data = train_data)
print(model_lda)
## Call:
## lda(Adaptivity.Level ~ ., data = train_data)
## 
## Prior probabilities of groups:
##        Low   Moderate       High 
## 0.40450771 0.51126928 0.08422301 
## 
## Group means:
##          GenderGirl  Age11-15   Age16-20  Age21-25   Age26-30     Age6-10
## Low       0.4956012 0.2434018 0.30498534 0.2991202 0.08211144 0.043988270
## Moderate  0.4292343 0.3410673 0.17169374 0.3526682 0.03480278 0.009280742
## High      0.3098592 0.2957746 0.04225352 0.3943662 0.08450704 0.183098592
##          Education.LevelSchool Education.LevelUniversity
## Low                  0.3607038                 0.3870968
## Moderate             0.4663573                 0.3967517
## High                 0.5070423                 0.4788732
##          Institution.TypeNon Government IT.StudentYes LocationYes
## Low                           0.5131965     0.1994135   0.6510264
## Moderate                      0.8213457     0.3132251   0.8491879
## High                          0.8309859     0.3098592   0.9295775
##          Load.sheddingLow Financial.ConditionPoor Financial.ConditionRich
## Low             0.7888563               0.2668622              0.02052786
## Moderate        0.8654292               0.1392111              0.04176334
## High            0.8732394               0.1971831              0.47887324
##          Internet.TypeWifi Network.Type3G Network.Type4G Class.Duration1-3
## Low              0.3988270      0.3958944      0.5747801         0.6158358
## Moderate         0.3944316      0.3457077      0.6473318         0.7378190
## High             0.6760563      0.2112676      0.7887324         0.7887324
##          Class.Duration3-6 Self.LmsYes DeviceMobile   DeviceTab
## Low             0.08504399   0.1026393    0.9178886 0.002932551
## Moderate        0.24825986   0.2227378    0.8004640 0.039443155
## High            0.21126761   0.2816901    0.6619718 0.028169014
## 
## Coefficients of linear discriminants:
##                                        LD1         LD2
## GenderGirl                     -0.22152884 -0.04872920
## Age11-15                        0.38363422  1.08532337
## Age16-20                       -0.01084652  1.08220539
## Age21-25                        0.69291043  0.86983346
## Age26-30                        1.04543505  0.60465440
## Age6-10                         1.49120268  2.97395971
## Education.LevelSchool           0.38387891 -0.47879376
## Education.LevelUniversity      -0.37879114  0.35094550
## Institution.TypeNon Government  0.68058205 -0.26542216
## IT.StudentYes                  -0.11378649 -0.38030346
## LocationYes                     0.06539147 -0.06017140
## Load.sheddingLow                0.02020918 -0.41095102
## Financial.ConditionPoor        -0.21263758  1.08901010
## Financial.ConditionRich         2.65230241  2.72394475
## Internet.TypeWifi              -0.35274834  0.67599502
## Network.Type3G                  0.47142466 -0.36079547
## Network.Type4G                  1.31274536 -0.45498552
## Class.Duration1-3               1.62311508 -1.18259415
## Class.Duration3-6               1.62914098 -2.25688619
## Self.LmsYes                     1.09942498 -0.09322434
## DeviceMobile                   -0.51356065 -0.04599900
## DeviceTab                       0.30707452 -0.81258026
## 
## Proportion of trace:
##    LD1    LD2 
## 0.6184 0.3816
pred <- predict(model_lda, test_data)
conf_matrix <- table(Prediksi = pred$class,
                     Aktual = test_data$Adaptivity.Level)
print(conf_matrix)
##           Aktual
## Prediksi   Low Moderate High
##   Low       95       45    4
##   Moderate  42      134   14
##   High       2       15   11
accuracy <- mean(as.character(pred$class) ==                as.character(test_data$Adaptivity.Level))

print(paste("Akurasi:", round(accuracy * 100, 2), "%"))
## [1] "Akurasi: 66.3 %"
plot(model_lda)

ggplot(train_data, aes(x = Age, fill = Adaptivity.Level)) +
  geom_bar(position = "fill") +
  labs(title = "Proporsi Tingkat Adaptasi Berdasarkan Umur",
       x = "Kelompok Umur",
       y = "Proporsi",
       fill = "Adaptivity Level") +
  theme_minimal()

cat("Akurasi Model:", round(accuracy * 100, 2), "%")
## Akurasi Model: 66.3 %