Dataset yang digunakan dalam analisis ini adalah Smoking and Drinking Dataset yang berisi berbagai indikator kesehatan individu serta informasi mengenai konsumsi alkohol dan status merokok.
library(readr)
data <- read.csv("C:/Users/USER/OneDrive - Universitas Diponegoro/smoking_driking_dataset_Ver01.csv")
head(data)## sex age height weight waistline sight_left sight_right hear_left hear_right
## 1 Male 35 170 75 90 1.0 1.0 1 1
## 2 Male 30 180 80 89 0.9 1.2 1 1
## 3 Male 40 165 75 91 1.2 1.5 1 1
## 4 Male 50 175 80 91 1.5 1.2 1 1
## 5 Male 50 165 60 80 1.0 1.2 1 1
## 6 Male 50 165 55 75 1.2 1.5 1 1
## SBP DBP BLDS tot_chole HDL_chole LDL_chole triglyceride hemoglobin
## 1 120 80 99 193 48 126 92 17.1
## 2 130 82 106 228 55 148 121 15.8
## 3 120 70 98 136 41 74 104 15.8
## 4 145 87 95 201 76 104 106 17.6
## 5 138 82 101 199 61 117 104 13.8
## 6 142 92 99 218 77 95 232 13.8
## urine_protein serum_creatinine SGOT_AST SGOT_ALT gamma_GTP SMK_stat_type_cd
## 1 1 1.0 21 35 40 1
## 2 1 0.9 20 36 27 3
## 3 1 0.9 47 32 68 1
## 4 1 1.1 29 34 18 1
## 5 1 0.8 19 12 25 1
## 6 3 0.8 29 40 37 3
## DRK_YN
## 1 Y
## 2 N
## 3 N
## 4 N
## 5 N
## 6 Y
## sex age height weight
## Length:991346 Min. :20.00 Min. :130.0 Min. : 25.00
## Class :character 1st Qu.:35.00 1st Qu.:155.0 1st Qu.: 55.00
## Mode :character Median :45.00 Median :160.0 Median : 60.00
## Mean :47.61 Mean :162.2 Mean : 63.28
## 3rd Qu.:60.00 3rd Qu.:170.0 3rd Qu.: 70.00
## Max. :85.00 Max. :190.0 Max. :140.00
## waistline sight_left sight_right hear_left
## Min. : 8.00 Min. :0.1000 Min. :0.1000 Min. :1.000
## 1st Qu.: 74.10 1st Qu.:0.7000 1st Qu.:0.7000 1st Qu.:1.000
## Median : 81.00 Median :1.0000 Median :1.0000 Median :1.000
## Mean : 81.23 Mean :0.9808 Mean :0.9784 Mean :1.031
## 3rd Qu.: 87.80 3rd Qu.:1.2000 3rd Qu.:1.2000 3rd Qu.:1.000
## Max. :999.00 Max. :9.9000 Max. :9.9000 Max. :2.000
## hear_right SBP DBP BLDS
## Min. :1.00 Min. : 67.0 Min. : 32.00 Min. : 25.0
## 1st Qu.:1.00 1st Qu.:112.0 1st Qu.: 70.00 1st Qu.: 88.0
## Median :1.00 Median :120.0 Median : 76.00 Median : 96.0
## Mean :1.03 Mean :122.4 Mean : 76.05 Mean :100.4
## 3rd Qu.:1.00 3rd Qu.:131.0 3rd Qu.: 82.00 3rd Qu.:105.0
## Max. :2.00 Max. :273.0 Max. :185.00 Max. :852.0
## tot_chole HDL_chole LDL_chole triglyceride
## Min. : 30.0 Min. : 1.00 Min. : 1 Min. : 1.0
## 1st Qu.: 169.0 1st Qu.: 46.00 1st Qu.: 89 1st Qu.: 73.0
## Median : 193.0 Median : 55.00 Median : 111 Median : 106.0
## Mean : 195.6 Mean : 56.94 Mean : 113 Mean : 132.1
## 3rd Qu.: 219.0 3rd Qu.: 66.00 3rd Qu.: 135 3rd Qu.: 159.0
## Max. :2344.0 Max. :8110.00 Max. :5119 Max. :9490.0
## hemoglobin urine_protein serum_creatinine SGOT_AST
## Min. : 1.00 Min. :1.000 Min. : 0.1000 Min. : 1.00
## 1st Qu.:13.20 1st Qu.:1.000 1st Qu.: 0.7000 1st Qu.: 19.00
## Median :14.30 Median :1.000 Median : 0.8000 Median : 23.00
## Mean :14.23 Mean :1.094 Mean : 0.8605 Mean : 25.99
## 3rd Qu.:15.40 3rd Qu.:1.000 3rd Qu.: 1.0000 3rd Qu.: 28.00
## Max. :25.00 Max. :6.000 Max. :98.0000 Max. :9999.00
## SGOT_ALT gamma_GTP SMK_stat_type_cd DRK_YN
## Min. : 1.00 Min. : 1.00 Min. :1.000 Length:991346
## 1st Qu.: 15.00 1st Qu.: 16.00 1st Qu.:1.000 Class :character
## Median : 20.00 Median : 23.00 Median :1.000 Mode :character
## Mean : 25.75 Mean : 37.14 Mean :1.608
## 3rd Qu.: 29.00 3rd Qu.: 39.00 3rd Qu.:2.000
## Max. :7210.00 Max. :999.00 Max. :3.000
## 'data.frame': 991346 obs. of 24 variables:
## $ sex : chr "Male" "Male" "Male" "Male" ...
## $ age : int 35 30 40 50 50 50 45 35 55 40 ...
## $ height : int 170 180 165 175 165 165 150 175 170 175 ...
## $ weight : int 75 80 75 80 60 55 55 65 75 75 ...
## $ waistline : num 90 89 91 91 80 75 69 84.2 84 82 ...
## $ sight_left : num 1 0.9 1.2 1.5 1 1.2 0.5 1.2 1.2 1.5 ...
## $ sight_right : num 1 1.2 1.5 1.2 1.2 1.5 0.4 1 0.9 1.5 ...
## $ hear_left : num 1 1 1 1 1 1 1 1 1 1 ...
## $ hear_right : num 1 1 1 1 1 1 1 1 1 1 ...
## $ SBP : num 120 130 120 145 138 142 101 132 145 132 ...
## $ DBP : num 80 82 70 87 82 92 58 80 85 105 ...
## $ BLDS : num 99 106 98 95 101 99 89 94 104 100 ...
## $ tot_chole : num 193 228 136 201 199 218 196 185 217 195 ...
## $ HDL_chole : num 48 55 41 76 61 77 66 58 56 60 ...
## $ LDL_chole : num 126 148 74 104 117 95 115 107 141 118 ...
## $ triglyceride : num 92 121 104 106 104 232 75 101 100 83 ...
## $ hemoglobin : num 17.1 15.8 15.8 17.6 13.8 13.8 12.3 14.4 15.1 13.9 ...
## $ urine_protein : num 1 1 1 1 1 3 1 1 1 1 ...
## $ serum_creatinine: num 1 0.9 0.9 1.1 0.8 0.8 0.8 0.8 0.8 0.9 ...
## $ SGOT_AST : num 21 20 47 29 19 29 19 18 32 21 ...
## $ SGOT_ALT : num 35 36 32 34 12 40 12 18 23 38 ...
## $ gamma_GTP : num 40 27 68 18 25 37 12 35 26 16 ...
## $ SMK_stat_type_cd: num 1 3 1 1 1 3 1 3 1 2 ...
## $ DRK_YN : chr "Y" "N" "N" "N" ...
Rata-rata usia responden dalam dataset adalah 47.6144908 tahun.
Variabel utama dalam dataset antara lain:
| Kategori | Variabel | Deskripsi |
|---|---|---|
| Demografi | sex, age |
Jenis Kelamin dan Usia |
| Fisik | height, weight,
waistline |
Antropometri dasar |
| Tekanan Darah | SBP, DBP |
Tekanan darah Sistolik & Diastolik |
| Metabolik | BLDS, tot_chole |
Gula darah dan Kolesterol Total |
| Target | SMK_stat_type_cd |
Status Merokok (Variabel Dependen) |
Metode yang digunakan dalam penelitian ini adalah Random Forest, yaitu metode ensemble learning yang membangun banyak pohon keputusan (decision tree) dan menggabungkan hasil prediksi dari setiap pohon untuk menghasilkan prediksi akhir.
Secara matematis model Random Forest untuk klasifikasi dapat dituliskan sebagai:
\[ \hat{y} = \text{mode}\{T_1(x), T_2(x), ..., T_B(x)\} \]
dengan:
Setiap pohon keputusan dibangun menggunakan teknik bootstrap sampling, yaitu pengambilan sampel secara acak dengan pengembalian.
Secara matematis proses bootstrap dapat dituliskan sebagai:
\[ D_b \sim \text{Bootstrap}(D), \quad b = 1,2,...,B \]
dengan:
Selain itu Random Forest juga menggunakan random feature selection pada setiap node decision tree.
Tahap preprocessing dilakukan untuk memastikan bahwa tipe data sesuai dengan kebutuhan model klasifikasi.
data$sex <- as.factor(data$sex)
data$DRK_YN <- as.factor(data$DRK_YN)
data$SMK_stat_type_cd <- as.factor(data$SMK_stat_type_cd)Untuk mempercepat proses komputasi, sebagian data diambil sebagai sampel.
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Variabel dependen yang digunakan dalam analisis ini adalah status merokok (SMK_stat_type_cd).
Variabel independen terdiri dari berbagai indikator kesehatan dan karakteristik individu seperti usia, jenis kelamin, tekanan darah, kadar kolesterol, serta konsumsi alkohol.
Visualisasi distribusi usia responden.
library(ggplot2)
ggplot(data, aes(x = age)) +
geom_histogram(bins = 30, fill = "#34495e", color = "white") +
theme_classic() +
labs(title = "Distribusi Usia Responden",
x = "Usia",
y = "Frekuensi")Visualisasi hubungan konsumsi alkohol dengan status merokok.
ggplot(data, aes(x = DRK_YN, fill = SMK_stat_type_cd)) +
geom_bar(position = "dodge") +
scale_fill_manual(values = c("#bdc3c7", "#7f8c8d", "#2c3e50")) +
theme_classic() +
labs(title = "Hubungan Konsumsi Alkohol dan Status Merokok")Dataset dibagi menjadi data training sebesar 80% dan data testing sebesar 20%.
## Loading required package: lattice
trainIndex <- createDataPartition(data$SMK_stat_type_cd,
p = 0.8,
list = FALSE)
train <- data[trainIndex, ]
test <- data[-trainIndex, ]Model Random Forest dibangun menggunakan seluruh variabel prediktor.
## Warning: package 'randomForest' was built under R version 4.0.5
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
##
## margin
## The following object is masked from 'package:dplyr':
##
## combine
model_rf <- randomForest(
SMK_stat_type_cd ~ sex + age + height + weight + waistline +
sight_left + sight_right + hear_left + hear_right +
SBP + DBP + BLDS + tot_chole + HDL_chole + LDL_chole +
triglyceride + hemoglobin + urine_protein +
serum_creatinine + SGOT_AST + SGOT_ALT + gamma_GTP +
DRK_YN,
data = train,
ntree = 200,
importance = TRUE
)
model_rf##
## Call:
## randomForest(formula = SMK_stat_type_cd ~ sex + age + height + weight + waistline + sight_left + sight_right + hear_left + hear_right + SBP + DBP + BLDS + tot_chole + HDL_chole + LDL_chole + triglyceride + hemoglobin + urine_protein + serum_creatinine + SGOT_AST + SGOT_ALT + gamma_GTP + DRK_YN, data = train, ntree = 200, importance = TRUE)
## Type of random forest: classification
## Number of trees: 200
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 31.4%
## Confusion matrix:
## 1 2 3 class.error
## 1 20449 1540 2195 0.1544410
## 2 2344 2219 2537 0.6874648
## 3 2420 1525 4771 0.4526159
Model diuji menggunakan data testing untuk melihat performa klasifikasi.
pred <- predict(model_rf, test)
library(caret)
library(ggplot2)
cm <- confusionMatrix(pred, test$SMK_stat_type_cd)
cm## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3
## 1 5131 571 624
## 2 376 563 338
## 3 539 641 1217
##
## Overall Statistics
##
## Accuracy : 0.6911
## 95% CI : (0.6819, 0.7001)
## No Information Rate : 0.6046
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.4307
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3
## Sensitivity 0.8487 0.3172 0.5585
## Specificity 0.6978 0.9132 0.8491
## Pos Pred Value 0.8111 0.4409 0.5077
## Neg Pred Value 0.7510 0.8611 0.8735
## Prevalence 0.6046 0.1775 0.2179
## Detection Rate 0.5131 0.0563 0.1217
## Detection Prevalence 0.6326 0.1277 0.2397
## Balanced Accuracy 0.7732 0.6152 0.7038
cm_table <- as.data.frame(cm$table)
ggplot(cm_table, aes(x = Prediction, y = Reference, fill = Freq)) +
geom_tile() +
geom_text(aes(label = Freq), color = "black", size = 5) +
scale_fill_gradient(low = "white", high = "steelblue") +
labs(title = "Confusion Matrix Random Forest",
x = "Predicted Class",
y = "Actual Class") +
theme_minimal()Evaluasi model menghasilkan beberapa metrik seperti:
Random Forest dapat menunjukkan variabel yang paling berpengaruh dalam proses klasifikasi.
## 1 2 3 MeanDecreaseAccuracy
## sex 93.2439907 59.6769631 37.5255952 119.6313468
## age 29.0277623 13.8270439 30.1068891 42.3830270
## height 16.5914566 6.2788997 15.7829891 21.2417348
## weight 23.7056426 12.3912994 -6.9515681 23.0506724
## waistline 21.8762709 14.0570192 -4.1799824 21.3824260
## sight_left 10.2510460 1.8159827 -2.1174692 7.1692290
## sight_right 11.6696043 2.5095112 -1.5138729 8.5863784
## hear_left 2.7646427 -0.5819839 4.3807679 3.5612970
## hear_right -0.1729895 -0.6504948 2.8385141 0.7112073
## SBP 16.9102308 -0.2105271 0.5473108 12.7027789
## DBP 13.2430090 1.9627943 2.0029533 12.9151146
## BLDS 13.4986313 3.0395511 -4.3123606 9.3051499
## tot_chole 21.1016675 -0.8283395 3.3242130 18.7678532
## HDL_chole 13.9601235 -4.2598904 16.9093299 18.7794264
## LDL_chole 17.4734563 1.4482822 3.7511098 17.7041140
## triglyceride 24.0856327 -6.0302811 20.8447275 24.3033753
## hemoglobin 29.2405642 -14.0654161 35.1849843 38.1857234
## urine_protein 0.7536554 0.5096980 -0.2289757 0.6752036
## serum_creatinine 18.9539654 9.9881208 -9.8952054 16.3721717
## SGOT_AST 18.8383343 6.5622660 0.3146932 18.9233752
## SGOT_ALT 22.8655193 6.9534284 -5.5555528 20.0913882
## gamma_GTP 30.9921885 -5.0753581 46.9194241 46.8725881
## DRK_YN 31.1131181 16.6528417 44.3815865 49.8059175
## MeanDecreaseGini
## sex 2633.05666
## age 898.50742
## height 1265.11248
## weight 924.69415
## waistline 1084.51841
## sight_left 629.00700
## sight_right 626.03508
## hear_left 42.51966
## hear_right 39.39504
## SBP 958.15262
## DBP 894.76493
## BLDS 1031.65748
## tot_chole 1030.00918
## HDL_chole 1031.65984
## LDL_chole 1050.44071
## triglyceride 1202.36370
## hemoglobin 1870.53761
## urine_protein 103.44307
## serum_creatinine 857.86110
## SGOT_AST 929.91080
## SGOT_ALT 992.58162
## gamma_GTP 1486.29463
## DRK_YN 620.38018
Variabel dengan nilai importance tertinggi menunjukkan faktor yang paling berpengaruh terhadap status merokok.
Model terbaik dipilih berdasarkan nilai akurasi tertinggi dari hasil evaluasi model Random Forest.
Model ini mampu mengklasifikasikan status merokok dengan mempertimbangkan berbagai indikator kesehatan serta faktor gaya hidup individu.
Berdasarkan analisis menggunakan metode Random Forest, model mampu memprediksi status merokok dengan mempertimbangkan berbagai indikator kesehatan dan faktor gaya hidup. Variabel seperti usia, trigliserida, hemoglobin, serta konsumsi alkohol memiliki kontribusi yang signifikan dalam menentukan kebiasaan merokok individu.