Dokumen ini memuat analisis perbandingan metode Ordinary Least Squares (OLS) dan Robust MM-Estimator pada model regresi linear. Data Gallstone digunakan sebagai data sekunder untuk menentukan karakteristik variabel dan parameter acuan simulasi. Selanjutnya dilakukan simulasi Monte Carlo dengan beberapa skenario persentase pencilan.
rm(list = ls())
set.seed(123)
library(readr)
library(robustbase)
library(ggplot2)
Gallstone_dataset_uci <- read_csv2(
"C:/SEMESTER 4/Gallstone dataset-uci.csv"
)
# Melihat nama seluruh variabel
names(Gallstone_dataset_uci)
## [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"
# Melihat struktur dataset
str(Gallstone_dataset_uci)
## spc_tbl_ [319 × 39] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Gallstone Status : num [1:319] 0 0 0 0 0 0 0 0 0 0 ...
## $ Age : num [1:319] 50 47 61 41 42 96 37 41 38 38 ...
## $ Gender : num [1:319] 0 0 0 0 0 0 0 0 0 0 ...
## $ Comorbidity : num [1:319] 0 1 0 0 0 0 0 0 0 0 ...
## $ Coronary Artery Disease (CAD) : num [1:319] 0 0 0 0 0 0 0 0 0 0 ...
## $ Hypothyroidism : num [1:319] 0 0 0 0 0 0 0 0 0 0 ...
## $ Hyperlipidemia : num [1:319] 0 0 0 0 0 0 0 0 0 0 ...
## $ Diabetes Mellitus (DM) : num [1:319] 0 0 0 0 0 0 0 0 0 0 ...
## $ Height : num [1:319] 185 176 171 168 178 155 185 176 186 171 ...
## $ Weight : num [1:319] 92.8 94.5 91.1 67.7 89.6 49 67.1 114 93.7 68.6 ...
## $ Body Mass Index (BMI) : num [1:319] 27.1 30.5 31.2 24 28.3 20.4 19.6 36.8 27.1 23.5 ...
## $ Total Body Water (TBW) : num [1:319] 52.9 43.1 47.2 41.4 51.4 34 41.8 57.6 51.9 39.5 ...
## $ Extracellular Water (ECW) : num [1:319] 21.2 19.5 20.1 17 20 15.7 17.1 23.5 20.9 16.6 ...
## $ Intracellular Water (ICW) : num [1:319] 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 [1:319] 40 45 43 41 39 46 41 41 40 42 ...
## $ Total Body Fat Ratio (TBFR) (%) : num [1:319] 19.2 32.8 27.3 15.8 20 6.3 11.3 31.8 21.5 19.2 ...
## $ Lean Mass (LM) (%) : num [1:319] 80.8 67.2 72.7 84.2 80 ...
## $ Body Protein Content (Protein) (%) : num [1:319] 18.9 16.7 16.4 16.9 16.8 ...
## $ Visceral Fat Rating (VFR) : num [1:319] 9 15 15 6 8 12 3 16 8 6 ...
## $ Bone Mass (BM) : num [1:319] 3.7 3.2 3.3 2.9 3.5 2.4 3 3.8 3.6 2.8 ...
## $ Muscle Mass (MM) : num [1:319] 71.4 60.3 62.9 54.1 68.2 43.5 56.5 73.9 70 52.6 ...
## $ Obesity (%) : num [1:319] 23.4 38.8 41.7 9 28.6 7.4 10.9 67.4 23.1 6.7 ...
## $ Total Fat Content (TFC) : num [1:319] 17.8 31 24.9 10.7 17.9 3.1 7.6 36.3 20.1 13.2 ...
## $ Visceral Fat Area (VFA) : num [1:319] 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 [1:319] 39.7 32.7 34 29.2 37.4 25.9 31.4 37.5 37.8 28.8 ...
## $ Hepatic Fat Accumulation (HFA) : num [1:319] 0 0 0 1 2 0 0 3 2 0 ...
## $ Glucose : num [1:319] 102 94 103 69 109 74 94 125 93 93 ...
## $ Total Cholesterol (TC) : num [1:319] 250 172 179 173 205 133 166 246 290 239 ...
## $ Low Density Lipoprotein (LDL) : num [1:319] 175 108 124 73 154 ...
## $ High Density Lipoprotein (HDL) : num [1:319] 40 43 43 59 30 30 40 34 33 43 ...
## $ Triglyceride : num [1:319] 134 103 69 53 326 65 60 464 679 129 ...
## $ Aspartat Aminotransferaz (AST) : num [1:319] 20 14 18 20 27 13 15 26 68 19 ...
## $ Alanin Aminotransferaz (ALT) : num [1:319] 22 13 14 12 54 13 14 28 102 34 ...
## $ Alkaline Phosphatase (ALP) : num [1:319] 87 46 66 34 71 60 72 69 70 75 ...
## $ Creatinine : num [1:319] 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 [1:319] 112.5 107.1 65.5 94.1 112.5 ...
## $ C-Reactive Protein (CRP) : num [1:319] 0 0 0 0 0 0 0 0.11 1.57 0 ...
## $ Hemoglobin (HGB) : num [1:319] 16 14.4 16.2 15.4 16.8 11 13.8 16.5 16.5 16.6 ...
## $ Vitamin D : num [1:319] 33 25 30.2 35.4 40.6 45.8 20 24.5 22.7 15.6 ...
## - attr(*, "spec")=
## .. cols(
## .. `Gallstone Status` = col_double(),
## .. Age = col_double(),
## .. Gender = col_double(),
## .. Comorbidity = col_double(),
## .. `Coronary Artery Disease (CAD)` = col_double(),
## .. Hypothyroidism = col_double(),
## .. Hyperlipidemia = col_double(),
## .. `Diabetes Mellitus (DM)` = col_double(),
## .. Height = col_double(),
## .. Weight = col_double(),
## .. `Body Mass Index (BMI)` = col_double(),
## .. `Total Body Water (TBW)` = col_double(),
## .. `Extracellular Water (ECW)` = col_double(),
## .. `Intracellular Water (ICW)` = col_double(),
## .. `Extracellular Fluid/Total Body Water (ECF/TBW)` = col_double(),
## .. `Total Body Fat Ratio (TBFR) (%)` = col_double(),
## .. `Lean Mass (LM) (%)` = col_double(),
## .. `Body Protein Content (Protein) (%)` = col_double(),
## .. `Visceral Fat Rating (VFR)` = col_double(),
## .. `Bone Mass (BM)` = col_double(),
## .. `Muscle Mass (MM)` = col_double(),
## .. `Obesity (%)` = col_double(),
## .. `Total Fat Content (TFC)` = col_double(),
## .. `Visceral Fat Area (VFA)` = col_double(),
## .. `Visceral Muscle Area (VMA) (Kg)` = col_double(),
## .. `Hepatic Fat Accumulation (HFA)` = col_double(),
## .. Glucose = col_double(),
## .. `Total Cholesterol (TC)` = col_double(),
## .. `Low Density Lipoprotein (LDL)` = col_double(),
## .. `High Density Lipoprotein (HDL)` = col_double(),
## .. Triglyceride = col_double(),
## .. `Aspartat Aminotransferaz (AST)` = col_double(),
## .. `Alanin Aminotransferaz (ALT)` = col_double(),
## .. `Alkaline Phosphatase (ALP)` = col_double(),
## .. Creatinine = col_double(),
## .. `Glomerular Filtration Rate (GFR)` = col_double(),
## .. `C-Reactive Protein (CRP)` = col_double(),
## .. `Hemoglobin (HGB)` = col_double(),
## .. `Vitamin D` = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
# Melihat 10 baris pertama
head(Gallstone_dataset_uci, 10)
Variabel respons yang digunakan adalah Triglyceride, sedangkan variabel prediktornya adalah Age, Body Mass Index (BMI), Glucose, dan Total Cholesterol.
data_set <- data.frame(
Y = Gallstone_dataset_uci$Triglyceride,
X1 = Gallstone_dataset_uci$Age,
X2 = Gallstone_dataset_uci$`Body Mass Index (BMI)`,
X3 = Gallstone_dataset_uci$Glucose,
X4 = Gallstone_dataset_uci$`Total Cholesterol (TC)`
)
head(data_set, 10)
# Melihat struktur data
str(data_set)
## 'data.frame': 319 obs. of 5 variables:
## $ Y : num 134 103 69 53 326 65 60 464 679 129 ...
## $ X1: num 50 47 61 41 42 96 37 41 38 38 ...
## $ X2: num 27.1 30.5 31.2 24 28.3 20.4 19.6 36.8 27.1 23.5 ...
## $ X3: num 102 94 103 69 109 74 94 125 93 93 ...
## $ X4: num 250 172 179 173 205 133 166 246 290 239 ...
# Melihat jumlah observasi dan variabel
dim(data_set)
## [1] 319 5
# Rata-rata
colMeans(data_set)
## Y X1 X2 X3 X4
## 144.50216 48.06897 28.87712 108.68871 203.49530
# Standar deviasi
sapply(data_set, sd)
## Y X1 X2 X3 X4
## 97.904493 12.114558 5.313707 44.848714 45.758501
# Nilai minimum
sapply(data_set, min)
## Y X1 X2 X3 X4
## 1.39 20.00 17.40 69.00 60.00
# Nilai maksimum
sapply(data_set, max)
## Y X1 X2 X3 X4
## 838.0 96.0 49.7 575.0 360.0
# Median
sapply(data_set, median)
## Y X1 X2 X3 X4
## 119.0 49.0 28.3 98.0 198.0
# Korelasi antarvariabel
cor(data_set)
## Y X1 X2 X3 X4
## Y 1.00000000 0.05804325 0.173438727 0.34037768 0.326705964
## X1 0.05804325 1.00000000 0.235835013 0.14850426 0.159004883
## X2 0.17343873 0.23583501 1.000000000 0.12536774 0.004509984
## X3 0.34037768 0.14850426 0.125367744 1.00000000 0.083041839
## X4 0.32670596 0.15900488 0.004509984 0.08304184 1.000000000
Model regresi linear awal diestimasi menggunakan data penelitian. Koefisien hasil estimasi digunakan sebagai parameter acuan dalam proses simulasi.
model_awal <- lm(
Y ~ X1 + X2 + X3 + X4,
data = data_set
)
summary(model_awal)
##
## Call:
## lm(formula = Y ~ X1 + X2 + X3 + X4, data = data_set)
##
## Residuals:
## Min 1Q Median 3Q Max
## -282.51 -46.49 -22.97 29.68 528.71
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -116.0800 36.2438 -3.203 0.00150 **
## X1 -0.5870 0.4205 -1.396 0.16370
## X2 2.7776 0.9449 2.939 0.00353 **
## X3 0.6687 0.1102 6.071 3.67e-09 ***
## X4 0.6678 0.1077 6.199 1.78e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 86.56 on 314 degrees of freedom
## Multiple R-squared: 0.2282, Adjusted R-squared: 0.2183
## F-statistic: 23.21 on 4 and 314 DF, p-value: < 2.2e-16
# Parameter acuan simulasi
beta0 <- coef(model_awal)[1]
beta1 <- coef(model_awal)[2]
beta2 <- coef(model_awal)[3]
beta3 <- coef(model_awal)[4]
beta4 <- coef(model_awal)[5]
# Standar deviasi error
sigma_e <- sd(residuals(model_awal))
# Menyusun parameter
parameter <- c(
beta0 = beta0,
beta1 = beta1,
beta2 = beta2,
beta3 = beta3,
beta4 = beta4
)
parameter
## beta0.(Intercept) beta1.X1 beta2.X2 beta3.X3
## -116.0800356 -0.5870008 2.7776473 0.6687474
## beta4.X4
## 0.6678434
sigma_e
## [1] 86.0123
Variabel prediktor dibangkitkan dari distribusi normal dengan rata-rata dan standar deviasi masing-masing variabel pada data penelitian. Variabel respons dibentuk berdasarkan parameter acuan dan komponen error acak.
nindividu <- nrow(data_set)
# Membuat variabel bebas X
X1 <- rnorm(
nindividu,
mean = mean(data_set$X1),
sd = sd(data_set$X1)
)
X2 <- rnorm(
nindividu,
mean = mean(data_set$X2),
sd = sd(data_set$X2)
)
X3 <- rnorm(
nindividu,
mean = mean(data_set$X3),
sd = sd(data_set$X3)
)
X4 <- rnorm(
nindividu,
mean = mean(data_set$X4),
sd = sd(data_set$X4)
)
# Membuat error
epsilon <- rnorm(nindividu, 0, sigma_e)
# Variabel respons Y
Y <- beta0 +
beta1 * X1 +
beta2 * X2 +
beta3 * X3 +
beta4 * X4 +
epsilon
data_set_simulasi <- data.frame(
Y = Y,
X1 = X1,
X2 = X2,
X3 = X3,
X4 = X4
)
head(data_set_simulasi, 10)
# Model OLS
model_ols <- lm(
Y ~ X1 + X2 + X3 + X4,
data = data_set_simulasi
)
summary(model_ols)
##
## Call:
## lm(formula = Y ~ X1 + X2 + X3 + X4, data = data_set_simulasi)
##
## Residuals:
## Min 1Q Median 3Q Max
## -224.665 -50.856 2.709 56.172 267.348
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -43.7244 41.9774 -1.042 0.2984
## X1 -0.8715 0.3973 -2.194 0.0290 *
## X2 1.8769 0.8726 2.151 0.0323 *
## X3 0.7915 0.1008 7.853 6.49e-14 ***
## X4 0.4460 0.1006 4.433 1.29e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 81.64 on 314 degrees of freedom
## Multiple R-squared: 0.228, Adjusted R-squared: 0.2182
## F-statistic: 23.18 on 4 and 314 DF, p-value: < 2.2e-16
# Model Robust MM-Estimator
model_mm <- lmrob(
Y ~ X1 + X2 + X3 + X4,
data = data_set_simulasi,
method = "MM"
)
summary(model_mm)
##
## Call:
## lmrob(formula = Y ~ X1 + X2 + X3 + X4, data = data_set_simulasi, method = "MM")
## \--> method = "MM"
## Residuals:
## Min 1Q Median 3Q Max
## -226.454 -51.670 2.055 54.776 268.998
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -44.0576 45.8851 -0.960 0.3377
## X1 -0.8644 0.4043 -2.138 0.0333 *
## X2 1.7378 0.8795 1.976 0.0490 *
## X3 0.8074 0.1015 7.956 3.26e-14 ***
## X4 0.4604 0.1039 4.432 1.29e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Robust residual standard error: 80.55
## Multiple R-squared: 0.2331, Adjusted R-squared: 0.2233
## Convergence in 10 IRWLS iterations
##
## Robustness weights:
## 24 weights are ~= 1. The remaining 295 ones are summarized as
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.2419 0.8701 0.9541 0.9072 0.9845 0.9989
## Algorithmic parameters:
## tuning.chi bb tuning.psi refine.tol
## 1.548e+00 5.000e-01 4.685e+00 1.000e-07
## rel.tol scale.tol solve.tol zero.tol
## 1.000e-07 1.000e-10 1.000e-07 1.000e-10
## eps.outlier eps.x warn.limit.reject warn.limit.meanrw
## 3.135e-04 6.352e-10 5.000e-01 5.000e-01
## nResample max.it best.r.s k.fast.s k.max
## 500 50 2 1 200
## maxit.scale trace.lev mts compute.rd fast.s.large.n
## 200 0 1000 0 2000
## psi subsampling cov
## "bisquare" "nonsingular" ".vcov.avar1"
## compute.outlier.stats
## "SM"
## seed : int(0)
# Parameter OLS
parameter_ols <- coef(model_ols)
parameter_ols
## (Intercept) X1 X2 X3 X4
## -43.7243947 -0.8714623 1.8768792 0.7914869 0.4460443
# Parameter MM-Estimator
parameter_mm <- coef(model_mm)
parameter_mm
## (Intercept) X1 X2 X3 X4
## -44.0575996 -0.8644209 1.7377901 0.8073651 0.4604435
parameter <- c(
beta0 = beta0,
beta1 = beta1,
beta2 = beta2,
beta3 = beta3,
beta4 = beta4
)
# Parameter simulasi OLS
parameter_simulasi_ols <- coef(model_ols)
# Parameter simulasi MM-Estimator
parameter_simulasi_mm <- coef(model_mm)
# Hasil simulasi
hasil_simulasi <- data.frame(
parameter = names(parameter),
Asli = as.numeric(parameter),
OLS = as.numeric(parameter_simulasi_ols),
MM_Estimator = as.numeric(parameter_simulasi_mm)
)
hasil_simulasi
# Error estimasi
error_ols <- hasil_simulasi$Asli - hasil_simulasi$OLS
error_mm <- hasil_simulasi$Asli - hasil_simulasi$MM_Estimator
error_ols
## [1] -72.3556408 0.2844615 0.9007681 -0.1227396 0.2217990
error_mm
## [1] -72.0224359 0.2774200 1.0398572 -0.1386178 0.2073999
Grafik berikut memperlihatkan hubungan variabel respons dengan Age dan BMI pada data simulasi, beserta garis regresi linear.
ggplot(data_set_simulasi, aes(x = X1, y = Y)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
theme_minimal() +
labs(x = "Age", y = "Triglyceride")
ggplot(data_set_simulasi, aes(x = X2, y = Y)) +
geom_point(alpha = 0.5) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
theme_minimal() +
labs(x = "BMI", y = "Triglyceride")
Pencilan ditambahkan pada variabel respons (pencilan vertikal) sebesar delapan kali standar deviasi error. Bagian ini membentuk contoh data dengan pencilan sebesar 5%.
persentase <- c(0, 0.05, 0.10, 0.15)
# Membuat salinan data simulasi
data_pencilan <- data_set_simulasi
# Menentukan jumlah pencilan untuk contoh 5%
jumlah_pencilan <- round(nindividu * 0.05)
# Memilih observasi yang menjadi pencilan
indeks_pencilan <- sample(
1:nindividu,
jumlah_pencilan
)
# Menambahkan pencilan pada variabel Y
data_pencilan$Y[indeks_pencilan] <-
data_pencilan$Y[indeks_pencilan] + 8 * sigma_e
head(data_pencilan, 10)
# Model OLS
model_ols_pencilan <- lm(
Y ~ X1 + X2 + X3 + X4,
data = data_pencilan
)
summary(model_ols_pencilan)
##
## Call:
## lm(formula = Y ~ X1 + X2 + X3 + X4, data = data_pencilan)
##
## Residuals:
## Min 1Q Median 3Q Max
## -285.66 -86.16 -24.65 33.89 759.96
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -95.610496 90.411058 -1.058 0.291092
## X1 0.002541 0.855689 0.003 0.997633
## X2 0.603888 1.879469 0.321 0.748191
## X3 0.818473 0.217078 3.770 0.000195 ***
## X4 0.826043 0.216714 3.812 0.000166 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 175.8 on 314 degrees of freedom
## Multiple R-squared: 0.08826, Adjusted R-squared: 0.07665
## F-statistic: 7.599 on 4 and 314 DF, p-value: 7.439e-06
# Model Robust MM-Estimator
model_mm_pencilan <- lmrob(
Y ~ X1 + X2 + X3 + X4,
data = data_pencilan,
method = "MM"
)
summary(model_mm_pencilan)
##
## Call:
## lmrob(formula = Y ~ X1 + X2 + X3 + X4, data = data_pencilan, method = "MM")
## \--> method = "MM"
## Residuals:
## Min 1Q Median 3Q Max
## -222.464 -48.773 8.329 62.888 834.060
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -33.8992 46.1344 -0.735 0.463015
## X1 -0.8106 0.4159 -1.949 0.052193 .
## X2 1.8084 0.8806 2.054 0.040828 *
## X3 0.8031 0.1009 7.958 3.21e-14 ***
## X4 0.3827 0.1053 3.635 0.000324 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Robust residual standard error: 86.79
## Multiple R-squared: 0.2116, Adjusted R-squared: 0.2016
## Convergence in 11 IRWLS iterations
##
## Robustness weights:
## 16 observations c(34,51,61,90,113,116,124,125,139,162,166,176,182,190,293,308)
## are outliers with |weight| = 0 ( < 0.00031);
## 25 weights are ~= 1. The remaining 278 ones are summarized as
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.3438 0.8872 0.9582 0.9182 0.9872 0.9989
## Algorithmic parameters:
## tuning.chi bb tuning.psi refine.tol
## 1.548e+00 5.000e-01 4.685e+00 1.000e-07
## rel.tol scale.tol solve.tol zero.tol
## 1.000e-07 1.000e-10 1.000e-07 1.000e-10
## eps.outlier eps.x warn.limit.reject warn.limit.meanrw
## 3.135e-04 6.352e-10 5.000e-01 5.000e-01
## nResample max.it best.r.s k.fast.s k.max
## 500 50 2 1 200
## maxit.scale trace.lev mts compute.rd fast.s.large.n
## 200 0 1000 0 2000
## psi subsampling cov
## "bisquare" "nonsingular" ".vcov.avar1"
## compute.outlier.stats
## "SM"
## seed : int(0)
Simulasi dilakukan sebanyak 500 replikasi untuk setiap skenario pencilan, yaitu 0%, 5%, 10%, dan 15%. Pada setiap replikasi, model OLS dan MM-Estimator diestimasi pada data yang dibangkitkan.
# Jumlah replikasi
nreplikasi <- 500
# Skenario pencilan
skenario <- c(0, 0.05, 0.10, 0.15)
# Menyiapkan hasil simulasi
hasil_replikasi <- data.frame()
# Melakukan simulasi
for (s in skenario) {
for (i in 1:nreplikasi) {
# Membuat variabel bebas X
X1 <- rnorm(
nindividu,
mean = mean(data_set$X1),
sd = sd(data_set$X1)
)
X2 <- rnorm(
nindividu,
mean = mean(data_set$X2),
sd = sd(data_set$X2)
)
X3 <- rnorm(
nindividu,
mean = mean(data_set$X3),
sd = sd(data_set$X3)
)
X4 <- rnorm(
nindividu,
mean = mean(data_set$X4),
sd = sd(data_set$X4)
)
# Membuat error
epsilon <- rnorm(nindividu, 0, sigma_e)
# Variabel respons Y
Y <- beta0 +
beta1 * X1 +
beta2 * X2 +
beta3 * X3 +
beta4 * X4 +
epsilon
data_set_simulasi <- data.frame(
Y = Y,
X1 = X1,
X2 = X2,
X3 = X3,
X4 = X4
)
# Menentukan jumlah pencilan
jumlah_pencilan <- round(nindividu * s)
# Menambahkan pencilan
if (jumlah_pencilan > 0) {
indeks_pencilan <- sample(
1:nindividu,
jumlah_pencilan
)
data_set_simulasi$Y[indeks_pencilan] <-
data_set_simulasi$Y[indeks_pencilan] +
8 * sigma_e
}
# Model OLS
model_ols <- lm(
Y ~ X1 + X2 + X3 + X4,
data = data_set_simulasi
)
# Model Robust MM-Estimator
model_mm <- lmrob(
Y ~ X1 + X2 + X3 + X4,
data = data_set_simulasi,
method = "MM"
)
# Parameter acuan
parameter <- c(
beta0 = beta0,
beta1 = beta1,
beta2 = beta2,
beta3 = beta3,
beta4 = beta4
)
# Parameter estimasi OLS dan MM
parameter_ols <- coef(model_ols)
parameter_mm <- coef(model_mm)
# Hasil simulasi OLS
hasil_ols <- data.frame(
replikasi = i,
skenario = s * 100,
metode = "OLS",
parameter = names(parameter),
Asli = as.numeric(parameter),
Estimasi = as.numeric(parameter_ols)
)
# Hasil simulasi MM-Estimator
hasil_mm <- data.frame(
replikasi = i,
skenario = s * 100,
metode = "MM-Estimator",
parameter = names(parameter),
Asli = as.numeric(parameter),
Estimasi = as.numeric(parameter_mm)
)
# Menggabungkan hasil simulasi
hasil_replikasi <- rbind(
hasil_replikasi,
hasil_ols,
hasil_mm
)
}
}
# Melihat hasil simulasi
head(hasil_replikasi, 10)
# Melihat dimensi hasil simulasi
dim(hasil_replikasi)
## [1] 20000 6
Bias dihitung sebagai rata-rata selisih estimasi dengan parameter acuan. Mean Squared Error (MSE) merupakan rata-rata kuadrat selisih, sedangkan Root Mean Squared Error (RMSE) adalah akar kuadrat dari MSE.
# Menghitung error
hasil_replikasi$error <-
hasil_replikasi$Estimasi -
hasil_replikasi$Asli
# Menghitung kuadrat error
hasil_replikasi$error2 <-
hasil_replikasi$error^2
# Menghitung Bias
hasil_bias <- aggregate(
error ~ skenario + metode + parameter,
data = hasil_replikasi,
FUN = mean
)
names(hasil_bias)[4] <- "Bias"
hasil_bias
# Menghitung MSE
hasil_mse <- aggregate(
error2 ~ skenario + metode + parameter,
data = hasil_replikasi,
FUN = mean
)
names(hasil_mse)[4] <- "MSE"
hasil_mse
# Menghitung RMSE
hasil_mse$RMSE <- sqrt(hasil_mse$MSE)
# Menggabungkan hasil evaluasi
hasil_evaluasi <- merge(
hasil_bias,
hasil_mse,
by = c("skenario", "metode", "parameter")
)
hasil_evaluasi
Grafik berikut membandingkan Bias, MSE, dan RMSE OLS dengan MM-Estimator pada setiap skenario persentase pencilan.
ggplot(hasil_evaluasi, aes(
x = factor(skenario),
y = Bias,
fill = metode
)) +
geom_col(position = "dodge") +
facet_wrap(~parameter, scales = "free_y") +
theme_minimal() +
labs(
x = "Persentase Pencilan (%)",
y = "Bias",
title = "Perbandingan Bias OLS dan MM-Estimator"
)
ggplot(hasil_evaluasi, aes(
x = factor(skenario),
y = MSE,
fill = metode
)) +
geom_col(position = "dodge") +
facet_wrap(~parameter, scales = "free_y") +
theme_minimal() +
labs(
x = "Persentase Pencilan (%)",
y = "MSE",
title = "Perbandingan MSE OLS dan MM-Estimator"
)
ggplot(hasil_evaluasi, aes(
x = factor(skenario),
y = RMSE,
fill = metode
)) +
geom_col(position = "dodge") +
facet_wrap(~parameter, scales = "free_y") +
theme_minimal() +
labs(
x = "Persentase Pencilan (%)",
y = "RMSE",
title = "Perbandingan RMSE OLS dan MM-Estimator"
)
Pastikan lokasi berkas CSV pada bagian Import Data disesuaikan dengan lokasi penyimpanan di komputer yang digunakan. Parameter hasil regresi awal pada dokumen ini berperan sebagai parameter acuan simulasi.