Laporan ini memodelkan status gallstone (0 = tidak ada, 1 = ada)
menggunakan metode Artificial Neural Network (ANN) dengan
package nnet. Tahapan analisis meliputi exploratory
data (cleaning, visualisasi, dan hubungan X dengan Y), statistika
deskriptif, pembagian data training dan testing (50%, 75%, 80%, 90%),
pemodelan ANN, evaluasi, serta penentuan model terbaik.
library(nnet)
library(ggplot2)
library(readxl)
gallstone <- read_excel("C:/Users/ACER/OneDrive/Documents/KULIAH DATA/MECHINE LEARNING/GALLSTONE UCL.xlsx")
gallstone <- as.data.frame(gallstone)
names(gallstone) <- make.names(names(gallstone), unique = TRUE)
names(gallstone) <- gsub("\\.+", ".", names(gallstone))
names(gallstone) <- gsub("\\.$", "", names(gallstone))
names(gallstone)
[1] "Gallstone.Status"
[2] "Age"
[3] "Gender"
[4] "Comorbidity"
[5] "Coronary.Artery.Disease.CAD"
[6] "Hypothyroidism"
[7] "Hyperlipidemia"
[8] "Diabetes.Mellitus.DM"
[9] "Height"
[10] "Weight"
[11] "Body.Mass.Index.BMI"
[12] "Total.Body.Water.TBW"
[13] "Extracellular.Water.ECW"
[14] "Intracellular.Water.ICW"
[15] "Extracellular.Fluid.Total.Body.Water.ECF.TBW"
[16] "Total.Body.Fat.Ratio.TBFR"
[17] "Lean.Mass.LM"
[18] "Body.Protein.Content.Protein"
[19] "Visceral.Fat.Rating.VFR"
[20] "Bone.Mass.BM"
[21] "Muscle.Mass.MM"
[22] "Obesity"
[23] "Total.Fat.Content.TFC"
[24] "Visceral.Fat.Area.VFA"
[25] "Visceral.Muscle.Area.VMA.Kg"
[26] "Hepatic.Fat.Accumulation.HFA"
[27] "Glucose"
[28] "Total.Cholesterol.TC"
[29] "Low.Density.Lipoprotein.LDL"
[30] "High.Density.Lipoprotein.HDL"
[31] "Triglyceride"
[32] "Aspartat.Aminotransferaz.AST"
[33] "Alanin.Aminotransferaz.ALT"
[34] "Alkaline.Phosphatase.ALP"
[35] "Creatinine"
[36] "Glomerular.Filtration.Rate.GFR"
[37] "C.Reactive.Protein.CRP"
[38] "Hemoglobin.HGB"
[39] "Vitamin.D"
dim(gallstone)
[1] 319 39
str(gallstone)
'data.frame': 319 obs. of 39 variables:
$ Gallstone.Status : num 0 0 0 0 0 0 0 0 0 0 ...
$ Age : num 50 47 61 41 42 96 37 41 38 38 ...
$ Gender : num 0 0 0 0 0 0 0 0 0 0 ...
$ Comorbidity : num 0 1 0 0 0 0 0 0 0 0 ...
$ Coronary.Artery.Disease.CAD : num 0 0 0 0 0 0 0 0 0 0 ...
$ Hypothyroidism : num 0 0 0 0 0 0 0 0 0 0 ...
$ Hyperlipidemia : num 0 0 0 0 0 0 0 0 0 0 ...
$ Diabetes.Mellitus.DM : num 0 0 0 0 0 0 0 0 0 0 ...
$ Height : num 185 176 171 168 178 155 185 176 186 171 ...
$ Weight : num 92.8 94.5 91.1 67.7 89.6 49 67.1 114 93.7 68.6 ...
$ Body.Mass.Index.BMI : num 27.1 30.5 31.2 24 28.3 20.4 19.6 36.8 27.1 23.5 ...
$ Total.Body.Water.TBW : num 52.9 43.1 47.2 41.4 51.4 34 41.8 57.6 51.9 39.5 ...
$ Extracellular.Water.ECW : num 21.2 19.5 20.1 17 20 15.7 17.1 23.5 20.9 16.6 ...
$ Intracellular.Water.ICW : num 31.7 23.6 27.1 24.4 31.4 18.3 24.7 34.1 31 22.9 ...
$ Extracellular.Fluid.Total.Body.Water.ECF.TBW: num 40 45 43 41 39 46 41 41 40 42 ...
$ Total.Body.Fat.Ratio.TBFR : num 19.2 32.8 27.3 15.8 20 6.3 11.3 31.8 21.5 19.2 ...
$ Lean.Mass.LM : num 80.8 67.2 72.7 84.2 80 ...
$ Body.Protein.Content.Protein : num 18.9 16.7 16.4 16.9 16.8 ...
$ Visceral.Fat.Rating.VFR : num 9 15 15 6 8 12 3 16 8 6 ...
$ Bone.Mass.BM : num 3.7 3.2 3.3 2.9 3.5 2.4 3 3.8 3.6 2.8 ...
$ Muscle.Mass.MM : num 71.4 60.3 62.9 54.1 68.2 43.5 56.5 73.9 70 52.6 ...
$ Obesity : num 23.4 38.8 41.7 9 28.6 7.4 10.9 67.4 23.1 6.7 ...
$ Total.Fat.Content.TFC : num 17.8 31 24.9 10.7 17.9 3.1 7.6 36.3 20.1 13.2 ...
$ Visceral.Fat.Area.VFA : num 10.6 18.4 16.2 6.5 10.4 0.9 4.6 23.7 12.7 8.2 ...
$ Visceral.Muscle.Area.VMA.Kg : num 39.7 32.7 34 29.2 37.4 25.9 31.4 37.5 37.8 28.8 ...
$ Hepatic.Fat.Accumulation.HFA : num 0 0 0 1 2 0 0 3 2 0 ...
$ Glucose : num 102 94 103 69 109 74 94 125 93 93 ...
$ Total.Cholesterol.TC : num 250 172 179 173 205 133 166 246 290 239 ...
$ Low.Density.Lipoprotein.LDL : num 175 108 124 73 154 ...
$ High.Density.Lipoprotein.HDL : num 40 43 43 59 30 30 40 34 33 43 ...
$ Triglyceride : num 134 103 69 53 326 65 60 464 679 129 ...
$ Aspartat.Aminotransferaz.AST : num 20 14 18 20 27 13 15 26 68 19 ...
$ Alanin.Aminotransferaz.ALT : num 22 13 14 12 54 13 14 28 102 34 ...
$ Alkaline.Phosphatase.ALP : num 87 46 66 34 71 60 72 69 70 75 ...
$ Creatinine : num 0.82 0.87 1.25 1.02 0.82 1.46 0.77 1.3 0.91 0.91 ...
$ Glomerular.Filtration.Rate.GFR : num 112.5 107.1 65.5 94.1 112.5 ...
$ C.Reactive.Protein.CRP : num 0 0 0 0 0 0 0 0.11 1.57 0 ...
$ Hemoglobin.HGB : num 16 14.4 16.2 15.4 16.8 11 13.8 16.5 16.5 16.6 ...
$ Vitamin.D : num 33 25 30.2 35.4 40.6 45.8 20 24.5 22.7 15.6 ...
colSums(is.na(gallstone))
Gallstone.Status
0
Age
0
Gender
0
Comorbidity
0
Coronary.Artery.Disease.CAD
0
Hypothyroidism
0
Hyperlipidemia
0
Diabetes.Mellitus.DM
0
Height
0
Weight
0
Body.Mass.Index.BMI
0
Total.Body.Water.TBW
0
Extracellular.Water.ECW
0
Intracellular.Water.ICW
0
Extracellular.Fluid.Total.Body.Water.ECF.TBW
0
Total.Body.Fat.Ratio.TBFR
0
Lean.Mass.LM
0
Body.Protein.Content.Protein
0
Visceral.Fat.Rating.VFR
0
Bone.Mass.BM
0
Muscle.Mass.MM
0
Obesity
0
Total.Fat.Content.TFC
0
Visceral.Fat.Area.VFA
0
Visceral.Muscle.Area.VMA.Kg
0
Hepatic.Fat.Accumulation.HFA
0
Glucose
0
Total.Cholesterol.TC
0
Low.Density.Lipoprotein.LDL
0
High.Density.Lipoprotein.HDL
0
Triglyceride
0
Aspartat.Aminotransferaz.AST
0
Alanin.Aminotransferaz.ALT
0
Alkaline.Phosphatase.ALP
0
Creatinine
0
Glomerular.Filtration.Rate.GFR
0
C.Reactive.Protein.CRP
0
Hemoglobin.HGB
0
Vitamin.D
0
sum(is.na(gallstone))
[1] 0
sum(duplicated(gallstone))
[1] 0
gallstone <- gallstone[!duplicated(gallstone), ]
gallstone <- na.omit(gallstone)
konstan <- sapply(gallstone, function(v) length(unique(v)) == 1)
names(gallstone)[konstan]
character(0)
nrow(gallstone)
[1] 319
target <- "Gallstone.Status"
kat_vars <- names(gallstone)[c(3:8, 26)]
num_vars <- setdiff(names(gallstone), c(target, kat_vars))
cek_outlier <- function(v){
q <- quantile(v, c(0.25, 0.75))
iqr <- q[2] - q[1]
sum(v < (q[1] - 1.5 * iqr) | v > (q[2] + 1.5 * iqr))
}
outlier_tbl <- data.frame(
Variabel = num_vars,
Jumlah_Outlier = sapply(gallstone[, num_vars], cek_outlier),
Persen = round(sapply(gallstone[, num_vars], cek_outlier) / nrow(gallstone) * 100, 2),
row.names = NULL
)
knitr::kable(outlier_tbl[order(-outlier_tbl$Jumlah_Outlier), ],
row.names = FALSE,
caption = "Jumlah Outlier per Variabel Numerik (Aturan IQR)")
| Variabel | Jumlah_Outlier | Persen |
|---|---|---|
| C.Reactive.Protein.CRP | 34 | 10.66 |
| Alanin.Aminotransferaz.ALT | 27 | 8.46 |
| Glucose | 25 | 7.84 |
| Aspartat.Aminotransferaz.AST | 23 | 7.21 |
| Triglyceride | 20 | 6.27 |
| Glomerular.Filtration.Rate.GFR | 13 | 4.08 |
| Alkaline.Phosphatase.ALP | 12 | 3.76 |
| Body.Protein.Content.Protein | 8 | 2.51 |
| Extracellular.Fluid.Total.Body.Water.ECF.TBW | 7 | 2.19 |
| Total.Fat.Content.TFC | 7 | 2.19 |
| Obesity | 6 | 1.88 |
| Body.Mass.Index.BMI | 5 | 1.57 |
| Visceral.Fat.Area.VFA | 5 | 1.57 |
| Total.Cholesterol.TC | 5 | 1.57 |
| High.Density.Lipoprotein.HDL | 5 | 1.57 |
| Low.Density.Lipoprotein.LDL | 4 | 1.25 |
| Visceral.Fat.Rating.VFR | 3 | 0.94 |
| Hemoglobin.HGB | 3 | 0.94 |
| Age | 2 | 0.63 |
| Weight | 2 | 0.63 |
| Total.Body.Water.TBW | 2 | 0.63 |
| Creatinine | 2 | 0.63 |
| Vitamin.D | 2 | 0.63 |
| Extracellular.Water.ECW | 1 | 0.31 |
| Intracellular.Water.ICW | 1 | 0.31 |
| Muscle.Mass.MM | 1 | 0.31 |
| Height | 0 | 0.00 |
| Total.Body.Fat.Ratio.TBFR | 0 | 0.00 |
| Lean.Mass.LM | 0 | 0.00 |
| Bone.Mass.BM | 0 | 0.00 |
| Visceral.Muscle.Area.VMA.Kg | 0 | 0.00 |
gallstone[[target]] <- factor(
gallstone[[target]],
levels = c(0, 1),
labels = c("0", "1")
)
y <- gallstone[[target]]
x1 <- gallstone$Age
x2 <- gallstone$Body.Mass.Index.BMI
x3 <- gallstone$Glucose
x4 <- gallstone$Total.Cholesterol.TC
table(y)
y
0 1
161 158
prop.table(table(y))
y
0 1
0.5047022 0.4952978
ggplot(data.frame(y = y), aes(x = y, fill = y)) +
geom_bar() +
geom_text(stat = "count", aes(label = after_stat(count)), vjust = -0.5) +
labs(x = "Gallstone Status", y = "Frekuensi", fill = "Gallstone Status") +
theme_minimal()
Distribusi Gallstone Status
n_obs <- nrow(gallstone)
num_long <- data.frame(
Variabel = rep(num_vars, each = n_obs),
Nilai = as.vector(as.matrix(gallstone[, num_vars])),
Status = rep(y, times = length(num_vars))
)
kat_long <- data.frame(
Variabel = rep(kat_vars, each = n_obs),
Nilai = factor(as.vector(as.matrix(gallstone[, kat_vars]))),
Status = rep(y, times = length(kat_vars))
)
ggplot(num_long, aes(x = Nilai)) +
geom_histogram(bins = 20, fill = "#17D5D3", color = "white") +
facet_wrap(~ Variabel, scales = "free", ncol = 5) +
labs(x = "Nilai", y = "Frekuensi") +
theme_minimal() +
theme(strip.text = element_text(size = 5),
axis.text = element_text(size = 5))
Histogram Variabel Numerik
ggplot(num_long, aes(y = Nilai)) +
geom_boxplot(fill = "#17D5D3") +
facet_wrap(~ Variabel, scales = "free", ncol = 5) +
labs(y = "Nilai") +
theme_minimal() +
theme(strip.text = element_text(size = 5),
axis.text = element_text(size = 5),
axis.text.x = element_blank())
Boxplot Variabel Numerik
ggplot(kat_long, aes(x = Nilai)) +
geom_bar(fill = "#17D5D3") +
facet_wrap(~ Variabel, scales = "free", ncol = 4) +
labs(x = "Kategori", y = "Frekuensi") +
theme_minimal() +
theme(strip.text = element_text(size = 6))
Bar Plot Variabel Kategorik
getplotx <- function(data, color = "black"){
d <- data.frame(
x = seq_along(data),
y = data
)
p <- ggplot(d, aes(x = x, y = y)) +
geom_point(color = color) +
labs(x = "index", y = "data") +
theme_minimal()
return(p)
}
getplotx(x1, color = "#17D5D3")
getplotx(x2, color = "#17D5D3")
getplotx(x3, color = "#17D5D3")
getplotx(x4, color = "#17D5D3")
getplotxy <- function(datax, datay){
d <- data.frame(
x = datax,
y = datay
)
p <- ggplot(d, aes(x = x, y = y, color = y)) +
geom_point() +
labs(
x = "data x",
y = "Gallstone Status",
color = "Gallstone Status"
) +
theme_minimal()
return(p)
}
getplotxy(x1, y)
getplotxy(x2, y)
getplotxy(x3, y)
getplotxy(x4, y)
ggplot(num_long, aes(x = Status, y = Nilai, fill = Status)) +
geom_boxplot() +
facet_wrap(~ Variabel, scales = "free_y", ncol = 5) +
labs(x = "Gallstone Status", y = "Nilai", fill = "Gallstone Status") +
theme_minimal() +
theme(strip.text = element_text(size = 5),
axis.text = element_text(size = 5))
Boxplot Variabel Numerik menurut Gallstone Status
ggplot(kat_long, aes(x = Nilai, fill = Status)) +
geom_bar(position = "fill") +
facet_wrap(~ Variabel, scales = "free_x", ncol = 4) +
labs(x = "Kategori", y = "Proporsi", fill = "Gallstone Status") +
theme_minimal() +
theme(strip.text = element_text(size = 6))
Proporsi Gallstone Status menurut Variabel Kategorik
cor_mat <- cor(gallstone[, num_vars])
cor_long <- as.data.frame(as.table(cor_mat))
names(cor_long) <- c("Var1", "Var2", "Korelasi")
ggplot(cor_long, aes(x = Var1, y = Var2, fill = Korelasi)) +
geom_tile() +
scale_fill_gradient2(low = "#D55E00", mid = "white", high = "#17D5D3", limits = c(-1, 1)) +
labs(x = NULL, y = NULL) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 5),
axis.text.y = element_text(size = 5))
Heatmap Korelasi Antar Variabel Numerik
y_num <- as.numeric(as.character(y))
kor_y <- data.frame(
Variabel = num_vars,
Korelasi_dengan_Y = round(as.vector(cor(gallstone[, num_vars], y_num)), 4)
)
knitr::kable(kor_y[order(-abs(kor_y$Korelasi_dengan_Y)), ],
row.names = FALSE,
caption = "Korelasi Variabel Numerik dengan Gallstone Status")
| Variabel | Korelasi_dengan_Y |
|---|---|
| Vitamin.D | -0.3549 |
| C.Reactive.Protein.CRP | 0.2820 |
| Lean.Mass.LM | -0.2257 |
| Total.Body.Fat.Ratio.TBFR | 0.2255 |
| Bone.Mass.BM | -0.2166 |
| Hemoglobin.HGB | -0.1969 |
| Extracellular.Water.ECW | -0.1784 |
| Total.Fat.Content.TFC | 0.1702 |
| Extracellular.Fluid.Total.Body.Water.ECF.TBW | -0.1698 |
| High.Density.Lipoprotein.HDL | 0.1586 |
| Visceral.Fat.Area.VFA | 0.1404 |
| Aspartat.Aminotransferaz.AST | -0.1349 |
| Creatinine | -0.1323 |
| Body.Mass.Index.BMI | 0.1215 |
| Total.Body.Water.TBW | -0.1112 |
| Alkaline.Phosphatase.ALP | 0.1099 |
| Height | -0.1079 |
| Muscle.Mass.MM | -0.0933 |
| Body.Protein.Content.Protein | -0.0864 |
| Visceral.Muscle.Area.VMA.Kg | -0.0730 |
| Low.Density.Lipoprotein.LDL | -0.0565 |
| Triglyceride | -0.0508 |
| Weight | 0.0487 |
| Alanin.Aminotransferaz.ALT | -0.0419 |
| Age | 0.0363 |
| Intracellular.Water.ICW | -0.0353 |
| Glomerular.Filtration.Rate.GFR | -0.0283 |
| Visceral.Fat.Rating.VFR | 0.0183 |
| Total.Cholesterol.TC | 0.0141 |
| Glucose | -0.0115 |
| Obesity | -0.0099 |
skewness <- function(v){
m <- mean(v)
s <- sd(v)
mean((v - m)^3) / s^3
}
sd_num <- data.frame(
Variabel = num_vars,
N = sapply(gallstone[, num_vars], length),
Mean = sapply(gallstone[, num_vars], mean),
SD = sapply(gallstone[, num_vars], sd),
Min = sapply(gallstone[, num_vars], min),
Q1 = sapply(gallstone[, num_vars], quantile, 0.25),
Median = sapply(gallstone[, num_vars], median),
Q3 = sapply(gallstone[, num_vars], quantile, 0.75),
Max = sapply(gallstone[, num_vars], max),
Skewness = sapply(gallstone[, num_vars], skewness),
row.names = NULL
)
sd_num[, -1] <- round(sd_num[, -1], 3)
knitr::kable(sd_num, row.names = FALSE,
caption = "Statistika Deskriptif Variabel Numerik")
| Variabel | N | Mean | SD | Min | Q1 | Median | Q3 | Max | Skewness |
|---|---|---|---|---|---|---|---|---|---|
| Age | 319 | 48.069 | 12.115 | 20.00 | 38.500 | 49.000 | 56.000 | 96.00 | 0.133 |
| Height | 319 | 167.157 | 10.053 | 145.00 | 159.500 | 168.000 | 175.000 | 191.00 | -0.080 |
| Weight | 319 | 80.565 | 15.709 | 42.90 | 69.600 | 78.800 | 91.250 | 143.50 | 0.428 |
| Body.Mass.Index.BMI | 319 | 28.877 | 5.314 | 17.40 | 25.250 | 28.300 | 31.850 | 49.70 | 0.663 |
| Total.Body.Water.TBW | 319 | 40.588 | 7.930 | 13.00 | 34.200 | 39.800 | 47.000 | 66.20 | 0.206 |
| Extracellular.Water.ECW | 319 | 17.071 | 3.162 | 9.00 | 14.800 | 17.100 | 19.400 | 27.80 | 0.020 |
| Intracellular.Water.ICW | 319 | 23.634 | 5.349 | 13.80 | 19.300 | 23.000 | 27.550 | 57.10 | 0.943 |
| Extracellular.Fluid.Total.Body.Water.ECF.TBW | 319 | 42.212 | 3.244 | 29.23 | 40.075 | 42.000 | 44.000 | 52.00 | -0.509 |
| Total.Body.Fat.Ratio.TBFR | 319 | 28.275 | 8.444 | 6.30 | 22.025 | 27.820 | 34.810 | 50.92 | 0.133 |
| Lean.Mass.LM | 319 | 71.638 | 8.438 | 48.99 | 65.165 | 72.110 | 77.850 | 93.67 | -0.124 |
| Body.Protein.Content.Protein | 319 | 15.939 | 2.335 | 5.56 | 14.465 | 15.870 | 17.430 | 24.81 | -0.047 |
| Visceral.Fat.Rating.VFR | 319 | 9.078 | 4.333 | 1.00 | 6.000 | 9.000 | 12.000 | 31.00 | 0.790 |
| Bone.Mass.BM | 319 | 2.803 | 0.509 | 1.40 | 2.400 | 2.800 | 3.200 | 4.00 | 0.205 |
| Muscle.Mass.MM | 319 | 54.273 | 10.604 | 4.70 | 45.800 | 53.900 | 62.600 | 78.80 | -0.102 |
| Obesity | 319 | 29.786 | 21.209 | 0.40 | 13.900 | 25.400 | 41.700 | 125.60 | 1.144 |
| Total.Fat.Content.TFC | 319 | 23.488 | 9.608 | 3.10 | 17.000 | 22.600 | 28.550 | 62.50 | 0.802 |
| Visceral.Fat.Area.VFA | 319 | 12.172 | 5.262 | 0.90 | 8.570 | 11.590 | 15.100 | 41.00 | 1.048 |
| Visceral.Muscle.Area.VMA.Kg | 319 | 30.403 | 4.461 | 18.90 | 27.250 | 30.408 | 33.800 | 41.10 | -0.056 |
| Glucose | 319 | 108.689 | 44.849 | 69.00 | 92.000 | 98.000 | 109.000 | 575.00 | 5.882 |
| Total.Cholesterol.TC | 319 | 203.495 | 45.759 | 60.00 | 172.000 | 198.000 | 233.000 | 360.00 | 0.430 |
| Low.Density.Lipoprotein.LDL | 319 | 126.652 | 38.541 | 11.00 | 100.500 | 122.000 | 151.000 | 293.00 | 0.535 |
| High.Density.Lipoprotein.HDL | 319 | 49.476 | 17.719 | 25.00 | 40.000 | 46.500 | 56.000 | 273.00 | 6.468 |
| Triglyceride | 319 | 144.502 | 97.904 | 1.39 | 83.000 | 119.000 | 172.000 | 838.00 | 2.760 |
| Aspartat.Aminotransferaz.AST | 319 | 21.685 | 16.698 | 8.00 | 15.000 | 18.000 | 23.000 | 195.00 | 6.929 |
| Alanin.Aminotransferaz.ALT | 319 | 26.856 | 27.884 | 3.00 | 14.250 | 19.000 | 30.000 | 372.00 | 7.209 |
| Alkaline.Phosphatase.ALP | 319 | 73.113 | 24.181 | 7.00 | 58.000 | 71.000 | 86.000 | 197.00 | 0.788 |
| Creatinine | 319 | 0.801 | 0.176 | 0.46 | 0.650 | 0.790 | 0.920 | 1.46 | 0.615 |
| Glomerular.Filtration.Rate.GFR | 319 | 100.819 | 16.971 | 10.60 | 94.170 | 104.000 | 110.745 | 132.00 | -1.796 |
| C.Reactive.Protein.CRP | 319 | 1.854 | 4.990 | 0.00 | 0.000 | 0.215 | 1.615 | 43.40 | 5.360 |
| Hemoglobin.HGB | 319 | 14.418 | 1.776 | 8.50 | 13.300 | 14.400 | 15.700 | 18.80 | -0.381 |
| Vitamin.D | 319 | 21.401 | 9.982 | 3.50 | 13.250 | 22.000 | 28.060 | 53.10 | 0.276 |
summary(gallstone)
Gallstone.Status Age Gender Comorbidity
0:161 Min. :20.00 Min. :0.0000 Min. :0.0000
1:158 1st Qu.:38.50 1st Qu.:0.0000 1st Qu.:0.0000
Median :49.00 Median :0.0000 Median :0.0000
Mean :48.07 Mean :0.4922 Mean :0.3354
3rd Qu.:56.00 3rd Qu.:1.0000 3rd Qu.:1.0000
Max. :96.00 Max. :1.0000 Max. :3.0000
Coronary.Artery.Disease.CAD Hypothyroidism Hyperlipidemia
Min. :0.00000 Min. :0.00000 Min. :0.00000
1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.00000
Median :0.00000 Median :0.00000 Median :0.00000
Mean :0.03762 Mean :0.02821 Mean :0.02508
3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000
Max. :1.00000 Max. :1.00000 Max. :1.00000
Diabetes.Mellitus.DM Height Weight Body.Mass.Index.BMI
Min. :0.0000 Min. :145.0 Min. : 42.90 Min. :17.40
1st Qu.:0.0000 1st Qu.:159.5 1st Qu.: 69.60 1st Qu.:25.25
Median :0.0000 Median :168.0 Median : 78.80 Median :28.30
Mean :0.1348 Mean :167.2 Mean : 80.56 Mean :28.88
3rd Qu.:0.0000 3rd Qu.:175.0 3rd Qu.: 91.25 3rd Qu.:31.85
Max. :1.0000 Max. :191.0 Max. :143.50 Max. :49.70
Total.Body.Water.TBW Extracellular.Water.ECW Intracellular.Water.ICW
Min. :13.00 Min. : 9.00 Min. :13.80
1st Qu.:34.20 1st Qu.:14.80 1st Qu.:19.30
Median :39.80 Median :17.10 Median :23.00
Mean :40.59 Mean :17.07 Mean :23.63
3rd Qu.:47.00 3rd Qu.:19.40 3rd Qu.:27.55
Max. :66.20 Max. :27.80 Max. :57.10
Extracellular.Fluid.Total.Body.Water.ECF.TBW Total.Body.Fat.Ratio.TBFR
Min. :29.23 Min. : 6.30
1st Qu.:40.08 1st Qu.:22.02
Median :42.00 Median :27.82
Mean :42.21 Mean :28.27
3rd Qu.:44.00 3rd Qu.:34.81
Max. :52.00 Max. :50.92
Lean.Mass.LM Body.Protein.Content.Protein Visceral.Fat.Rating.VFR
Min. :48.99 Min. : 5.56 Min. : 1.000
1st Qu.:65.17 1st Qu.:14.46 1st Qu.: 6.000
Median :72.11 Median :15.87 Median : 9.000
Mean :71.64 Mean :15.94 Mean : 9.078
3rd Qu.:77.85 3rd Qu.:17.43 3rd Qu.:12.000
Max. :93.67 Max. :24.81 Max. :31.000
Bone.Mass.BM Muscle.Mass.MM Obesity Total.Fat.Content.TFC
Min. :1.400 Min. : 4.70 Min. : 0.40 Min. : 3.10
1st Qu.:2.400 1st Qu.:45.80 1st Qu.: 13.90 1st Qu.:17.00
Median :2.800 Median :53.90 Median : 25.40 Median :22.60
Mean :2.803 Mean :54.27 Mean : 29.79 Mean :23.49
3rd Qu.:3.200 3rd Qu.:62.60 3rd Qu.: 41.70 3rd Qu.:28.55
Max. :4.000 Max. :78.80 Max. :125.60 Max. :62.50
Visceral.Fat.Area.VFA Visceral.Muscle.Area.VMA.Kg Hepatic.Fat.Accumulation.HFA
Min. : 0.90 Min. :18.90 Min. :0.00
1st Qu.: 8.57 1st Qu.:27.25 1st Qu.:0.00
Median :11.59 Median :30.41 Median :1.00
Mean :12.17 Mean :30.40 Mean :1.15
3rd Qu.:15.10 3rd Qu.:33.80 3rd Qu.:2.00
Max. :41.00 Max. :41.10 Max. :4.00
Glucose Total.Cholesterol.TC Low.Density.Lipoprotein.LDL
Min. : 69.0 Min. : 60.0 Min. : 11.0
1st Qu.: 92.0 1st Qu.:172.0 1st Qu.:100.5
Median : 98.0 Median :198.0 Median :122.0
Mean :108.7 Mean :203.5 Mean :126.7
3rd Qu.:109.0 3rd Qu.:233.0 3rd Qu.:151.0
Max. :575.0 Max. :360.0 Max. :293.0
High.Density.Lipoprotein.HDL Triglyceride Aspartat.Aminotransferaz.AST
Min. : 25.00 Min. : 1.39 Min. : 8.00
1st Qu.: 40.00 1st Qu.: 83.00 1st Qu.: 15.00
Median : 46.50 Median :119.00 Median : 18.00
Mean : 49.48 Mean :144.50 Mean : 21.68
3rd Qu.: 56.00 3rd Qu.:172.00 3rd Qu.: 23.00
Max. :273.00 Max. :838.00 Max. :195.00
Alanin.Aminotransferaz.ALT Alkaline.Phosphatase.ALP Creatinine
Min. : 3.00 Min. : 7.00 Min. :0.4600
1st Qu.: 14.25 1st Qu.: 58.00 1st Qu.:0.6500
Median : 19.00 Median : 71.00 Median :0.7900
Mean : 26.86 Mean : 73.11 Mean :0.8006
3rd Qu.: 30.00 3rd Qu.: 86.00 3rd Qu.:0.9200
Max. :372.00 Max. :197.00 Max. :1.4600
Glomerular.Filtration.Rate.GFR C.Reactive.Protein.CRP Hemoglobin.HGB
Min. : 10.60 Min. : 0.000 Min. : 8.50
1st Qu.: 94.17 1st Qu.: 0.000 1st Qu.:13.30
Median :104.00 Median : 0.215 Median :14.40
Mean :100.82 Mean : 1.854 Mean :14.42
3rd Qu.:110.75 3rd Qu.: 1.615 3rd Qu.:15.70
Max. :132.00 Max. :43.400 Max. :18.80
Vitamin.D
Min. : 3.50
1st Qu.:13.25
Median :22.00
Mean :21.40
3rd Qu.:28.06
Max. :53.10
for (v in kat_vars) {
cat("\nVariabel:", v, "\n")
print(table(gallstone[[v]]))
print(round(prop.table(table(gallstone[[v]])) * 100, 2))
}
Variabel: Gender
0 1
162 157
0 1
50.78 49.22
Variabel: Comorbidity
0 1 2 3
217 99 1 2
0 1 2 3
68.03 31.03 0.31 0.63
Variabel: Coronary.Artery.Disease.CAD
0 1
307 12
0 1
96.24 3.76
Variabel: Hypothyroidism
0 1
310 9
0 1
97.18 2.82
Variabel: Hyperlipidemia
0 1
311 8
0 1
97.49 2.51
Variabel: Diabetes.Mellitus.DM
0 1
276 43
0 1
86.52 13.48
Variabel: Hepatic.Fat.Accumulation.HFA
0 1 2 3 4
129 41 122 26 1
0 1 2 3 4
40.44 12.85 38.24 8.15 0.31
minmax_scale <- function(dtrain, dtest, target = "Gallstone.Status"){
vars <- setdiff(names(dtrain), target)
for (v in vars) {
mn <- min(dtrain[[v]])
mx <- max(dtrain[[v]])
rg <- ifelse(mx - mn == 0, 1, mx - mn)
dtrain[[v]] <- (dtrain[[v]] - mn) / rg
dtest[[v]] <- (dtest[[v]] - mn) / rg
}
list(train = dtrain, test = dtest)
}
evaluasi <- function(pred, aktual, positif = "1"){
pred <- factor(pred, levels = levels(aktual))
mc <- table(Prediksi = pred, Aktual = aktual)
TP <- sum(pred == positif & aktual == positif)
TN <- sum(pred != positif & aktual != positif)
FP <- sum(pred == positif & aktual != positif)
FN <- sum(pred != positif & aktual == positif)
akurasi <- (TP + TN) / (TP + TN + FP + FN)
sensitivity <- ifelse(TP + FN == 0, NA, TP / (TP + FN))
specificity <- ifelse(TN + FP == 0, NA, TN / (TN + FP))
precision <- ifelse(TP + FP == 0, NA, TP / (TP + FP))
f1 <- ifelse(is.na(precision) | is.na(sensitivity) |
(precision + sensitivity) == 0,
NA, 2 * precision * sensitivity / (precision + sensitivity))
list(
mc = mc,
metrik = c(Akurasi = akurasi, Sensitivity = sensitivity,
Specificity = specificity, Precision = precision, F1 = f1)
)
}
set.split <- c(0.50, 0.75, 0.80, 0.90)
set.size <- c(2, 3, 5, 7, 10)
set.maxit <- c(100, 300, 500)
n_skenario <- length(set.split) * length(set.size) * length(set.maxit)
hasil <- data.frame(
split = numeric(n_skenario),
size = numeric(n_skenario),
maxit = numeric(n_skenario),
n_train = numeric(n_skenario),
n_test = numeric(n_skenario),
akurasi = numeric(n_skenario),
sensitivity = numeric(n_skenario),
specificity = numeric(n_skenario),
precision = numeric(n_skenario),
f1 = numeric(n_skenario)
)
n <- nrow(gallstone)
tmp <- 0
for (s in 1:length(set.split)) {
set.seed(123)
index <- sample(n, size = round(n * set.split[s]), replace = FALSE)
dtrain <- gallstone[index, ]
dtest <- gallstone[-index, ]
sc <- minmax_scale(dtrain, dtest)
dtrain_s <- sc$train
dtest_s <- sc$test
for (i in 1:length(set.size)) {
for (j in 1:length(set.maxit)) {
tmp <- tmp + 1
set.seed(123)
model_ANN <- nnet(
Gallstone.Status ~ .,
data = dtrain_s,
size = set.size[i],
maxit = set.maxit[j],
MaxNWts = 5000,
trace = F
)
pred_y <- predict(model_ANN, newdata = dtest_s, type = "class")
ev <- evaluasi(pred_y, dtest_s$Gallstone.Status)
hasil[tmp, ] <- c(set.split[s], set.size[i], set.maxit[j],
nrow(dtrain), nrow(dtest), ev$metrik)
}
}
}
knitr::kable(hasil, digits = 4, row.names = FALSE,
caption = "Hasil Evaluasi Seluruh Skenario ANN")
| split | size | maxit | n_train | n_test | akurasi | sensitivity | specificity | precision | f1 |
|---|---|---|---|---|---|---|---|---|---|
| 0.50 | 2 | 100 | 160 | 159 | 0.6855 | 0.6875 | 0.6835 | 0.6875 | 0.6875 |
| 0.50 | 2 | 300 | 160 | 159 | 0.6855 | 0.6875 | 0.6835 | 0.6875 | 0.6875 |
| 0.50 | 2 | 500 | 160 | 159 | 0.6855 | 0.6875 | 0.6835 | 0.6875 | 0.6875 |
| 0.50 | 3 | 100 | 160 | 159 | 0.7044 | 0.5125 | 0.8987 | 0.8367 | 0.6357 |
| 0.50 | 3 | 300 | 160 | 159 | 0.7044 | 0.5125 | 0.8987 | 0.8367 | 0.6357 |
| 0.50 | 3 | 500 | 160 | 159 | 0.7044 | 0.5125 | 0.8987 | 0.8367 | 0.6357 |
| 0.50 | 5 | 100 | 160 | 159 | 0.6792 | 0.6875 | 0.6709 | 0.6790 | 0.6832 |
| 0.50 | 5 | 300 | 160 | 159 | 0.7170 | 0.7000 | 0.7342 | 0.7273 | 0.7134 |
| 0.50 | 5 | 500 | 160 | 159 | 0.7484 | 0.7125 | 0.7848 | 0.7703 | 0.7403 |
| 0.50 | 7 | 100 | 160 | 159 | 0.7044 | 0.6000 | 0.8101 | 0.7619 | 0.6713 |
| 0.50 | 7 | 300 | 160 | 159 | 0.7044 | 0.6000 | 0.8101 | 0.7619 | 0.6713 |
| 0.50 | 7 | 500 | 160 | 159 | 0.7044 | 0.6000 | 0.8101 | 0.7619 | 0.6713 |
| 0.50 | 10 | 100 | 160 | 159 | 0.6604 | 0.5875 | 0.7342 | 0.6912 | 0.6351 |
| 0.50 | 10 | 300 | 160 | 159 | 0.6604 | 0.5875 | 0.7342 | 0.6912 | 0.6351 |
| 0.50 | 10 | 500 | 160 | 159 | 0.6604 | 0.5875 | 0.7342 | 0.6912 | 0.6351 |
| 0.75 | 2 | 100 | 239 | 80 | 0.8000 | 0.7027 | 0.8837 | 0.8387 | 0.7647 |
| 0.75 | 2 | 300 | 239 | 80 | 0.8000 | 0.7297 | 0.8605 | 0.8182 | 0.7714 |
| 0.75 | 2 | 500 | 239 | 80 | 0.8000 | 0.7297 | 0.8605 | 0.8182 | 0.7714 |
| 0.75 | 3 | 100 | 239 | 80 | 0.7250 | 0.7568 | 0.6977 | 0.6829 | 0.7179 |
| 0.75 | 3 | 300 | 239 | 80 | 0.7250 | 0.7568 | 0.6977 | 0.6829 | 0.7179 |
| 0.75 | 3 | 500 | 239 | 80 | 0.7250 | 0.7568 | 0.6977 | 0.6829 | 0.7179 |
| 0.75 | 5 | 100 | 239 | 80 | 0.8375 | 0.7027 | 0.9535 | 0.9286 | 0.8000 |
| 0.75 | 5 | 300 | 239 | 80 | 0.8375 | 0.7027 | 0.9535 | 0.9286 | 0.8000 |
| 0.75 | 5 | 500 | 239 | 80 | 0.8375 | 0.7027 | 0.9535 | 0.9286 | 0.8000 |
| 0.75 | 7 | 100 | 239 | 80 | 0.8125 | 0.7838 | 0.8372 | 0.8056 | 0.7945 |
| 0.75 | 7 | 300 | 239 | 80 | 0.8125 | 0.7838 | 0.8372 | 0.8056 | 0.7945 |
| 0.75 | 7 | 500 | 239 | 80 | 0.8125 | 0.7838 | 0.8372 | 0.8056 | 0.7945 |
| 0.75 | 10 | 100 | 239 | 80 | 0.7375 | 0.7838 | 0.6977 | 0.6905 | 0.7342 |
| 0.75 | 10 | 300 | 239 | 80 | 0.7375 | 0.7838 | 0.6977 | 0.6905 | 0.7342 |
| 0.75 | 10 | 500 | 239 | 80 | 0.7375 | 0.7838 | 0.6977 | 0.6905 | 0.7342 |
| 0.80 | 2 | 100 | 255 | 64 | 0.8906 | 0.9310 | 0.8571 | 0.8438 | 0.8852 |
| 0.80 | 2 | 300 | 255 | 64 | 0.8906 | 0.9310 | 0.8571 | 0.8438 | 0.8852 |
| 0.80 | 2 | 500 | 255 | 64 | 0.8906 | 0.9310 | 0.8571 | 0.8438 | 0.8852 |
| 0.80 | 3 | 100 | 255 | 64 | 0.8281 | 0.8621 | 0.8000 | 0.7812 | 0.8197 |
| 0.80 | 3 | 300 | 255 | 64 | 0.7969 | 0.7931 | 0.8000 | 0.7667 | 0.7797 |
| 0.80 | 3 | 500 | 255 | 64 | 0.8125 | 0.7586 | 0.8571 | 0.8148 | 0.7857 |
| 0.80 | 5 | 100 | 255 | 64 | 0.8125 | 0.8621 | 0.7714 | 0.7576 | 0.8065 |
| 0.80 | 5 | 300 | 255 | 64 | 0.7812 | 0.8621 | 0.7143 | 0.7143 | 0.7813 |
| 0.80 | 5 | 500 | 255 | 64 | 0.7812 | 0.8621 | 0.7143 | 0.7143 | 0.7813 |
| 0.80 | 7 | 100 | 255 | 64 | 0.7500 | 0.7586 | 0.7429 | 0.7097 | 0.7333 |
| 0.80 | 7 | 300 | 255 | 64 | 0.7500 | 0.7586 | 0.7429 | 0.7097 | 0.7333 |
| 0.80 | 7 | 500 | 255 | 64 | 0.7500 | 0.7586 | 0.7429 | 0.7097 | 0.7333 |
| 0.80 | 10 | 100 | 255 | 64 | 0.7500 | 0.6897 | 0.8000 | 0.7407 | 0.7143 |
| 0.80 | 10 | 300 | 255 | 64 | 0.7500 | 0.6897 | 0.8000 | 0.7407 | 0.7143 |
| 0.80 | 10 | 500 | 255 | 64 | 0.7500 | 0.6897 | 0.8000 | 0.7407 | 0.7143 |
| 0.90 | 2 | 100 | 287 | 32 | 0.7812 | 0.6364 | 0.8571 | 0.7000 | 0.6667 |
| 0.90 | 2 | 300 | 287 | 32 | 0.7812 | 0.6364 | 0.8571 | 0.7000 | 0.6667 |
| 0.90 | 2 | 500 | 287 | 32 | 0.7812 | 0.6364 | 0.8571 | 0.7000 | 0.6667 |
| 0.90 | 3 | 100 | 287 | 32 | 0.7500 | 0.8182 | 0.7143 | 0.6000 | 0.6923 |
| 0.90 | 3 | 300 | 287 | 32 | 0.7500 | 0.8182 | 0.7143 | 0.6000 | 0.6923 |
| 0.90 | 3 | 500 | 287 | 32 | 0.7500 | 0.8182 | 0.7143 | 0.6000 | 0.6923 |
| 0.90 | 5 | 100 | 287 | 32 | 0.8438 | 0.8182 | 0.8571 | 0.7500 | 0.7826 |
| 0.90 | 5 | 300 | 287 | 32 | 0.8438 | 0.8182 | 0.8571 | 0.7500 | 0.7826 |
| 0.90 | 5 | 500 | 287 | 32 | 0.8438 | 0.8182 | 0.8571 | 0.7500 | 0.7826 |
| 0.90 | 7 | 100 | 287 | 32 | 0.7500 | 0.7273 | 0.7619 | 0.6154 | 0.6667 |
| 0.90 | 7 | 300 | 287 | 32 | 0.7188 | 0.6364 | 0.7619 | 0.5833 | 0.6087 |
| 0.90 | 7 | 500 | 287 | 32 | 0.7188 | 0.6364 | 0.7619 | 0.5833 | 0.6087 |
| 0.90 | 10 | 100 | 287 | 32 | 0.7812 | 0.7273 | 0.8095 | 0.6667 | 0.6957 |
| 0.90 | 10 | 300 | 287 | 32 | 0.7812 | 0.7273 | 0.8095 | 0.6667 | 0.6957 |
| 0.90 | 10 | 500 | 287 | 32 | 0.7812 | 0.7273 | 0.8095 | 0.6667 | 0.6957 |
hasil.urut <- hasil[order(-hasil$akurasi, -hasil$f1), ]
knitr::kable(head(hasil.urut, 10), digits = 4, row.names = FALSE,
caption = "10 Skenario dengan Akurasi Tertinggi")
| split | size | maxit | n_train | n_test | akurasi | sensitivity | specificity | precision | f1 |
|---|---|---|---|---|---|---|---|---|---|
| 0.80 | 2 | 100 | 255 | 64 | 0.8906 | 0.9310 | 0.8571 | 0.8438 | 0.8852 |
| 0.80 | 2 | 300 | 255 | 64 | 0.8906 | 0.9310 | 0.8571 | 0.8438 | 0.8852 |
| 0.80 | 2 | 500 | 255 | 64 | 0.8906 | 0.9310 | 0.8571 | 0.8438 | 0.8852 |
| 0.90 | 5 | 100 | 287 | 32 | 0.8438 | 0.8182 | 0.8571 | 0.7500 | 0.7826 |
| 0.90 | 5 | 300 | 287 | 32 | 0.8438 | 0.8182 | 0.8571 | 0.7500 | 0.7826 |
| 0.90 | 5 | 500 | 287 | 32 | 0.8438 | 0.8182 | 0.8571 | 0.7500 | 0.7826 |
| 0.75 | 5 | 100 | 239 | 80 | 0.8375 | 0.7027 | 0.9535 | 0.9286 | 0.8000 |
| 0.75 | 5 | 300 | 239 | 80 | 0.8375 | 0.7027 | 0.9535 | 0.9286 | 0.8000 |
| 0.75 | 5 | 500 | 239 | 80 | 0.8375 | 0.7027 | 0.9535 | 0.9286 | 0.8000 |
| 0.80 | 3 | 100 | 255 | 64 | 0.8281 | 0.8621 | 0.8000 | 0.7812 | 0.8197 |
knitr::kable(
aggregate(cbind(akurasi, sensitivity, specificity, f1) ~ split,
data = hasil, FUN = mean),
digits = 4, row.names = FALSE,
caption = "Rata-rata Metrik Evaluasi per Proporsi Data Training"
)
| split | akurasi | sensitivity | specificity | f1 |
|---|---|---|---|---|
| 0.50 | 0.6939 | 0.6175 | 0.7713 | 0.6684 |
| 0.75 | 0.7825 | 0.7495 | 0.8109 | 0.7632 |
| 0.80 | 0.7990 | 0.8092 | 0.7905 | 0.7835 |
| 0.90 | 0.7771 | 0.7333 | 0.8000 | 0.6931 |
ggplot(hasil, aes(x = factor(size), y = akurasi,
color = factor(maxit), group = factor(maxit))) +
geom_line() +
geom_point() +
facet_wrap(~ paste0("Training ", split * 100, "%")) +
labs(x = "Size (neuron hidden layer)", y = "Akurasi", color = "maxit") +
theme_minimal()
Akurasi ANN pada Setiap Skenario
hasil.terbaik <- hasil.urut[1, ]
knitr::kable(hasil.terbaik, digits = 4, row.names = FALSE,
caption = "Skenario Model Terbaik")
| split | size | maxit | n_train | n_test | akurasi | sensitivity | specificity | precision | f1 |
|---|---|---|---|---|---|---|---|---|---|
| 0.8 | 2 | 100 | 255 | 64 | 0.8906 | 0.931 | 0.8571 | 0.8438 | 0.8852 |
set.seed(123)
index <- sample(n, size = round(n * hasil.terbaik$split), replace = FALSE)
dtrain <- gallstone[index, ]
dtest <- gallstone[-index, ]
sc <- minmax_scale(dtrain, dtest)
dtrain_s <- sc$train
dtest_s <- sc$test
set.seed(123)
model_final <- nnet(
Gallstone.Status ~ .,
data = dtrain_s,
size = hasil.terbaik$size,
maxit = hasil.terbaik$maxit,
MaxNWts = 5000,
trace = F
)
pred_final <- predict(model_final, newdata = dtest_s, type = "class")
ev_final <- evaluasi(pred_final, dtest_s$Gallstone.Status)
knitr::kable(as.data.frame.matrix(ev_final$mc),
caption = "Confusion Matrix Model Terbaik (Data Testing)")
| 0 | 1 | |
|---|---|---|
| 0 | 30 | 2 |
| 1 | 5 | 27 |
knitr::kable(data.frame(Metrik = names(ev_final$metrik),
Nilai = round(as.vector(ev_final$metrik), 4)),
caption = "Metrik Evaluasi Model Terbaik (Data Testing)")
| Metrik | Nilai |
|---|---|
| Akurasi | 0.8906 |
| Sensitivity | 0.9310 |
| Specificity | 0.8571 |
| Precision | 0.8438 |
| F1 | 0.8852 |
pred_train <- predict(model_final, newdata = dtrain_s, type = "class")
evaluasi(pred_train, dtrain_s$Gallstone.Status)$metrik["Akurasi"]
Akurasi
0.9607843