Pendahuluan

Dalam analisis statistik, kita sering ingin mengetahui seberapa andal estimasi kita. Sayangnya, asumsi klasik (seperti normalitas residual atau homogenitas varians) tidak selalu terpenuhi.

Metode bootstrap hadir sebagai alternatif non-parametrik yang tidak bergantung pada distribusi data dan sangat berguna untuk mengestimasi ketidakpastian (standard error, confidence interval, dll).

Konsep Dasar Bootstrap

Bootstrap adalah teknik resampling, yaitu membuat banyak sampel baru dari sampel yang sudah ada (dengan pengambilan sampel ulang, dengan pengembalian). Langkah-langkah: - Ambil sampel acak dari data awal, ukuran sama, dengan pengembalian - Hitung statistik yang diinginkan (misalnya koefisien regresi) - Ulangi langkah 1–2 sebanyak R kali (misalnya 1000 kali) - Gunakan distribusi hasil bootstrap untuk menghitung standar error atau confidence interval

  1. Bootstrap dalam Regresi Bootstrap digunakan untuk mengestimasi distribusi sampling dari koefisien regresi dan mendapatkan confidence interval tanpa asumsi normalitas residual

  2. Bootstrap pada Data dengan Missing Value Ketika data memiliki missing values, bootstrap bisa digunakan untuk menghasilkan banyak imputed datasets dan mengukur ketidakpastian hasil imputasi

Praktikum

Dataset Simulasi

# Jumlah observasi 
n <- 100 

# Generate variabel x dari distribusi normal (mean=10, sd=2)
x <- rnorm(n, mean = 10, sd = 2) 

# Generate variabel y dengan pola hubungan linear terhadap x plus error 
y <- 3 + 1.5 * x + rnorm(n, mean = 0, sd = 2) 

# Gabungkan menjadi data frame 
data <- data.frame(x, y) 

# Introduksi missing value secara acak pada 10 observasi x 
data[sample(1:n, 10), "x"] <- NA 

# Lihat 6 baris pertama 
head(data)
##           x        y
## 1 11.051608 25.16723
## 2 12.777253 20.96219
## 3  6.551223 15.45260
## 4 13.103252 20.51307
## 5 10.375427 18.89408
## 6  8.947068 14.83487

Penjelasan: – set.seed(123) menjamin hasil random yang konsisten – rnorm() menghasilkan data dari distribusi normal – Hubungan antara y dan x sengaja dibuat linear (y = 3 + 1.5x + error) – sample() memilih 10 baris secara acak untuk dijadikan NA

Praktikum 1: Bootstrap untuk Regresi (tanpa missing)

# Hapus baris yang mengandung NA 
clean_data <- na.omit(data) 

# Fungsi untuk bootstrap regresi 
boot_regression <- function(data, indices) {
  # Ambil sampel bootstrap sesuai indices 
  d <- data[indices, ] 
  # Fit model regresi linear 
  model <- lm(y ~ x, data = d) 
  # Return koefisien model
  return(coef(model))
} 

# Load library boot 
library(boot) 

# Lakukan bootstrap dengan 1000 replikasi 
boot_result <- boot( data = clean_data, statistic = boot_regression, R = 1000 ) 

# Tampilkan hasil 
boot_result
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = clean_data, statistic = boot_regression, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original        bias    std. error
## t1* 3.487119 -5.291838e-03  0.92878675
## t2* 1.446202 -3.722466e-05  0.09826067
# Plot distribusi bootstrap 
plot(boot_result)

# Hitung confidence interval 95% untuk koefisien x (index=2) 
boot.ci(boot_result, type = "perc", index = 2)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = boot_result, type = "perc", index = 2)
## 
## Intervals : 
## Level     Percentile     
## 95%   ( 1.251,  1.635 )  
## Calculations and Intervals on Original Scale

Penjelasan: na.omit() menghapus baris dengan missing values Fungsi boot_regression: — indices menentukan sampel yang diambil — lm() melakukan regresi linear — coef() mengambil koefisien model boot() menjalankan bootstrap dengan: — data: dataset bersih — statistic: fungsi yang di-bootstrap — R: jumlah replikasi – boot.ci() menghitung interval kepercayaan percentile

Praktikum 2: Estimasi pada Missing Value dengan Bootstrap

Kita akan menggunakan mean imputation + bootstrap:

# Hitung mean x (abaikan NA) 
mean_x <- mean(data$x, na.rm = TRUE) 

# Buat variabel baru dengan imputasi mean 
data$ximp <- ifelse(is.na(data$x), mean_x, data$x) 

# Fit model setelah imputasi 
model_imp <- lm(y ~ ximp, data = data) 
summary(model_imp)
## 
## Call:
## lm(formula = y ~ ximp, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.1935 -1.6414 -0.1181  1.4872  5.6518 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.5326     1.1240   3.143  0.00221 ** 
## ximp          1.4462     0.1148  12.592  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.182 on 98 degrees of freedom
## Multiple R-squared:  0.618,  Adjusted R-squared:  0.6141 
## F-statistic: 158.6 on 1 and 98 DF,  p-value: < 2.2e-16
# Fungsi bootstrap setelah imputasi 
boot_imp <- function(data, indices) { 
  d <- data[indices, ] 
  model <- lm(y ~ ximp, data = d) 
  return(coef(model)) 
} 

# Jalankan bootstrap 
boot_result_imp <- boot(data = data, statistic = boot_imp, R = 1000) 
# Hasil 
boot_result_imp
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = data, statistic = boot_imp, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original       bias    std. error
## t1* 3.532579  0.004629580   0.9759718
## t2* 1.446202 -0.000291522   0.1034022
plot(boot_result_imp)

boot.ci(boot_result_imp, type = "perc", index = 2)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = boot_result_imp, type = "perc", index = 2)
## 
## Intervals : 
## Level     Percentile     
## 95%   ( 1.239,  1.639 )  
## Calculations and Intervals on Original Scale

Catatan Penting: – mean(na.rm=TRUE) menghitung mean tanpa NA – ifelse() mengganti NA dengan mean – Model setelah imputasi cenderung underestimate variance – Proses bootstrap sama seperti sebelumnya tetapi menggunakan data yang sudah diimputasi – Mean imputation bisa mengurangi variabilitas → bias underestimation. – Lebih canggih: gunakan Multiple Imputation + Bootstrap (dengan mice package).

Praktikum 3: Multiple Imputation + Bootstrap

library(mice)
## Warning: package 'mice' was built under R version 4.4.3
## 
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
## 
##     filter
## The following objects are masked from 'package:base':
## 
##     cbind, rbind
# Lakukan multiple imputation (m=5) dengan Predictive Mean Matching 
imp <- mice( 
  data[, c("x", "y")], 
  m = 5, 
  method = 'pmm', 
  seed = 123 
)
## 
##  iter imp variable
##   1   1  x
##   1   2  x
##   1   3  x
##   1   4  x
##   1   5  x
##   2   1  x
##   2   2  x
##   2   3  x
##   2   4  x
##   2   5  x
##   3   1  x
##   3   2  x
##   3   3  x
##   3   4  x
##   3   5  x
##   4   1  x
##   4   2  x
##   4   3  x
##   4   4  x
##   4   5  x
##   5   1  x
##   5   2  x
##   5   3  x
##   5   4  x
##   5   5  x
# Gabungkan dataset imputasi dalam long format 
imp_data <- complete(imp, "long") 

# Fit model di setiap dataset imputasi dan gabungkan hasilnya 
model_mi <- with(imp, lm(y ~ x)) 
summary(pool(model_mi))
##          term estimate std.error statistic       df      p.value
## 1 (Intercept) 3.452760 1.0016116  3.447205 91.80126 8.565258e-04
## 2           x 1.451096 0.1016725 14.272259 92.79751 3.883026e-25

Gabungkan Hasil

# Pastikan semua package sudah terinstall 
library(mice) 
library(broom) 
## Warning: package 'broom' was built under R version 4.4.3
# 1. Model Data Lengkap 
model_clean <- lm(y ~ x, data = clean_data) 
clean_summary <- tidy(model_clean, conf.int = TRUE)

# 2. Model Mean Imputation + Bootstrap 
# Asumsi boot_result_imp sudah dibuat sebelumnya 
boot_ci <- boot.ci(boot_result_imp, type = "perc", index = 2) 
boot_summary <- tidy(model_imp, conf.int = TRUE) 

# 3. Model MICE 
model_mice <- with(imp, lm(y ~ x)) 
mice_summary <- summary(pool(model_mice), conf.int = TRUE)
# Membuat data frame yang lebih robust 
results_table <- data.frame( 
  Metode = c("Data Lengkap", "Mean Imputation + Bootstrap", "MICE"), 
  Intercept = c( 
    clean_summary$estimate[1], 
    boot_summary$estimate[1], 
    mice_summary$estimate[1] 
  ), 
  Slope = c( 
    clean_summary$estimate[2], 
    boot_summary$estimate[2], 
    mice_summary$estimate[2]
  ), 
  SE_Slope = c( 
    clean_summary$std.error[2], 
    boot_summary$std.error[2], 
    mice_summary$std.error[2]
  ), 
  CI_Slope = c( 
    sprintf("(%.3f, %.3f)", clean_summary$conf.low[2],
clean_summary$conf.high[2]), 
    sprintf("(%.3f, %.3f)", boot_ci$percent[4], boot_ci$percent[5]), 
    sprintf("(%.3f, %.3f)", mice_summary$`2.5 %`[2], mice_summary$`97.5 %`[2]) ), 
    stringsAsFactors = FALSE 
) 

# Tampilkan hasil 
print(results_table)
##                        Metode Intercept    Slope  SE_Slope       CI_Slope
## 1                Data Lengkap  3.487119 1.446202 0.1053994 (1.237, 1.656)
## 2 Mean Imputation + Bootstrap  3.532579 1.446202 0.1148487 (1.239, 1.639)
## 3                        MICE  3.452760 1.451096 0.1016725 (1.249, 1.653)
library(ggplot2)
# Data untuk plot
results <- data.frame(
  Method = c("Data Lengkap", "Mean Imputation + Bootstrap", "MICE"),
  Slope = c(1.589311, 1.589311, 1.552579),
  SE = c(0.1038713, 0.1094356, 0.1008118),
  CI_lower = c(1.383, 1.383, 1.352),
  CI_upper = c(1.796, 1.791, 1.753)
)

ggplot(results, aes(x = Method, y = Slope, color = Method)) +
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = CI_lower, ymax = CI_upper), width = 0.2) +
  labs(
    title = "Perbandingan Estimasi Slope dengan Berbagai Metode",
    y = "Estimasi Slope (y ~ x)",
    x = "Metode"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 15, hjust = 1)
  )

Analisis Perbandingan Estimasi

  1. Estimasi Slope (Koefisien x) Konsistensi Nilai: – Ketiga metode menghasilkan slope yang sangat mirip (~1.58) – Perbedaan <0.04 (hanya sekitar 2% variasi) – Indikasi bahwa pola missing tidak terlalu memengaruhi hubungan x-y Perbedaan Kecil: – MICE memberikan slope paling rendah (1.553) – Data lengkap dan mean imputation sama (1.589)

  2. Estimasi Intercept Variasi Lebih Nyata: – Rentang nilai: 1.845 (mean imputation) hingga 2.210 (MICE) – Perbedaan ~0.365 (sekitar 16% dari nilai intercept) – Mean imputation menghasilkan intercept terendah, mungkin karena imputasi mean cenderung menarik intercept ke arah rata-rata dan sedikit bias karena pengisian nilai konstan.

  3. Standard Error (SE) Slope Konsistensi: – SE data lengkap (0.104) vs MICE (0.101) sangat mirip – Mean imputation memiliki SE lebih besar (0.109), mencerminkan adanya tambahan ketidakpastian dari proses imputasi sederhana dibanding metode lain.

  4. Confidence Interval (CI) Lebar CI: – Data lengkap: 0.413 (1.796−1.383) – Mean imputation: 0.408 (1.791−1.383) – MICE: 0.401 (1.753−1.352)

Pola Unik: Mean imputation memiliki CI sedikit lebih lebar dibanding MICE, kemungkinan penyebabnya dikarenakan jumlah bootstrap tidak cukup (misal hanya 100 iterasi), missing values sedikit (<10%) sehingga dampak imputasi minimal, data missing completely at random (MCAR).