You are on page 1of 5

Nama: Joabri Sihombing

NIM : 16221002

1. Boostrap
# Buat dataframe dengan data yang diberikan
data_custom <- data.frame(
Y_custom = c(115, 72, 44, 49, 46, 11, 1, 5, 1, 0, 28, 14, 8, 4, 152, 47, 2, 2,
45, 25, 2, 100, 2, 1, 16, 61, 14, 9, 49, 70, 70, 124, 121, 115, 91, 87, 51, 35,
30, 20, 0, 4, 2, 2, 0, 83, 78, 14, 8, 2, 1, 1, 1, 0, 0, 73, 53, 1, 1, 1, 0, 1),
X1_custom = c(2, 7, 2.5, 6, 3, 4.5, 1, 3, 1.5, 3, 3, 2.5, 4, 3.5, 7.5, 8, 2, 2.5,
8, 6.5, 2.5, 5, 1, 0.5, 8, 3, 3, 5.5, 2, 5, 2, 2, 3, 7.5, 4.5, 7, 5, 4, 2.5, 7.5, 3, 4.5,
1, 3, 3, 7, 6, 4.5, 8, 5, 2, 3, 1, 6.5, 3, 5, 2, 1, 1, 2, 1, 1),
X2_custom = c(5, 11, 12, 11, 14, 14, 5, 9, 3, 1, 7, 9, 8, 9, 16, 16, 0, 3, 6, 12,
6, 12, 0, 2, 4, 8, 8, 5, 7, 0, 7, 17, 11, 20, 18, 20, 35, 35, 8, 24, 9, 0, 3, 2, 5, 13,
11, 14, 8, 5, 3, 2, 1, 12, 0, 11, 6, 3, 5, 2, 0, 0),
X3_custom = c(343, 11, 15, 9, 22, 20, 5, 19, 13, 0, 4, 8, 58, 4, 36, 21, 0, 3,
16, 32, 0, 41, 0, 1, 3, 19, 72, 46, 58, 0, 4, 117, 31, 138, 27, 245, 34, 26, 31,
151, 9, 0, 3, 3, 20, 31, 93, 22, 28, 10, 0, 4, 0, 17, 0, 13, 6, 1, 1, 0, 45, 1)
)

# Susun rumus untuk regresi


rumus_regresi_custom <- Y_custom ~ X1_custom + X2_custom +
X3_custom

# Jumlah iterasi bootstrap


R_custom <- 1000

# Persiapkan vektor untuk menyimpan hasil bootstrap


hasil_bootstrap_custom <- numeric(length = R_custom)
hasil_bootstrap_1_custom <- numeric(length = R_custom)
hasil_bootstrap_2_custom <- numeric(length = R_custom)
hasil_bootstrap_3_custom <- numeric(length = R_custom)

# Lakukan bootstrap
set.seed(123) # untuk hasil yang reprodusibel
for (i in 1:R_custom) {
# Ambil sampel dengan penggantian
indeks_sampel_custom <- sample(1:nrow(data_custom), replace = TRUE)
data_bootstrap_custom <- data_custom[indeks_sampel_custom, ]

# Fitting model pada sampel bootstrap


model_bootstrap_custom <- lm(rumus_regresi_custom, data =
data_bootstrap_custom)

# Simpan koefisien bootstrap


hasil_bootstrap_custom[i] <- coef(model_bootstrap_custom)[1]
hasil_bootstrap_1_custom[i] <- coef(model_bootstrap_custom)[2]
hasil_bootstrap_2_custom[i] <- coef(model_bootstrap_custom)[3]
hasil_bootstrap_3_custom[i] <- coef(model_bootstrap_custom)[4]
}

# Hitung rata-rata, standar deviasi (st), dan standard error (error) dari
koefisien bootstrap
rata_bootstrap_custom <- mean(hasil_bootstrap_custom)
st_bootstrap_custom <- sd(hasil_bootstrap_custom)
error_bootstrap_custom <- st_bootstrap_custom /
sqrt(length(hasil_bootstrap_custom))

rata_bootstrap_1_custom <- mean(hasil_bootstrap_1_custom)


st_bootstrap_1_custom <- sd(hasil_bootstrap_1_custom)
error_bootstrap_1_custom <- st_bootstrap_1_custom /
sqrt(length(hasil_bootstrap_1_custom))

rata_bootstrap_2_custom <- mean(hasil_bootstrap_2_custom)


st_bootstrap_2_custom <- sd(hasil_bootstrap_2_custom)
error_bootstrap_2_custom <- st_bootstrap_2_custom /
sqrt(length(hasil_bootstrap_2_custom))

rata_bootstrap_3_custom <- mean(hasil_bootstrap_3_custom)


st_bootstrap_3_custom <- sd(hasil_bootstrap_3_custom)
error_bootstrap_3_custom <- st_bootstrap_3_custom /
sqrt(length(hasil_bootstrap_3_custom))
# Tampilkan hasilnya
cat("Intercept (beta): Rata-rata =", rata_bootstrap_custom, ", St =",
st_bootstrap_custom, ", Error =", error_bootstrap_custom, "\n")
cat("X1 (beta1): Rata-rata =", rata_bootstrap_1_custom, ", St =",
st_bootstrap_1_custom, ", Error =", error_bootstrap_1_custom, "\n")
cat("X2 (beta2): Rata-rata =", rata_bootstrap_2_custom, ", St =",
st_bootstrap_2_custom, ", Error =", error_bootstrap_2_custom, "\n")
cat("X3 (beta3): Rata-rata =", rata_bootstrap_3_custom, ", St =",
st_bootstrap_3_custom, ", Error =", error

Runing

2. Jacknife
# Buat dataframe dengan data yang diberikan
data_custom <- data.frame(
Y_custom = c(115, 72, 44, 49, 46, 11, 1, 5, 1, 0, 28, 14, 8, 4, 152, 47, 2, 2,
45, 25, 2, 100, 2, 1, 16, 61, 14, 9, 49, 70, 70, 124, 121, 115, 91, 87, 51, 35,
30, 20, 0, 4, 2, 2, 0, 83, 78, 14, 8, 2, 1, 1, 1, 0, 0, 73, 53, 1, 1, 1, 0, 1),
X1_custom = c(2, 7, 2.5, 6, 3, 4.5, 1, 3, 1.5, 3, 3, 2.5, 4, 3.5, 7.5, 8, 2, 2.5,
8, 6.5, 2.5, 5, 1, 0.5, 8, 3, 3, 5.5, 2, 5, 2, 2, 3, 7.5, 4.5, 7, 5, 4, 2.5, 7.5, 3, 4.5,
1, 3, 3, 7, 6, 4.5, 8, 5, 2, 3, 1, 6.5, 3, 5, 2, 1, 1, 2, 1, 1),
X2_custom = c(5, 11, 12, 11, 14, 14, 5, 9, 3, 1, 7, 9, 8, 9, 16, 16, 0, 3, 6, 12,
6, 12, 0, 2, 4, 8, 8, 5, 7, 0, 7, 17, 11, 20, 18, 20, 35, 35, 8, 24, 9, 0, 3, 2, 5, 13,
11, 14, 8, 5, 3, 2, 1, 12, 0, 11, 6, 3, 5, 2, 0, 0),
X3_custom = c(343, 11, 15, 9, 22, 20, 5, 19, 13, 0, 4, 8, 58, 4, 36, 21, 0, 3,
16, 32, 0, 41, 0, 1, 3, 19, 72, 46, 58, 0, 4, 117, 31, 138, 27, 245, 34, 26, 31,
151, 9, 0, 3, 3, 20, 31, 93, 22, 28, 10, 0, 4, 0, 17, 0, 13, 6, 1, 1, 0, 45, 1)
)

# Susun rumus untuk regresi


rumus_regresi_custom <- Y_custom ~ X1_custom + X2_custom +
X3_custom

# Hitung jumlah observasi


jumlah_obs_custom <- nrow(data_custom)

# Persiapkan vektor untuk menyimpan hasil jackknife


hasil_jack_custom <- numeric(length = jumlah_obs_custom)
hasil_jack_1_custom <- numeric(length = jumlah_obs_custom)
hasil_jack_2_custom <- numeric(length = jumlah_obs_custom)
hasil_jack_3_custom <- numeric(length = jumlah_obs_custom)

# Lakukan jackknife
for (iterasi in 1:jumlah_obs_custom) {
# Hapus satu observasi
data_jackknife_custom <- data_custom[-iterasi, ]

# Fitting model pada data jackknife


model_jack_custom <- lm(rumus_regresi_custom, data =
data_jackknife_custom)

# Simpan koefisien jackknife


hasil_jack_custom[iterasi] <- coef(model_jack_custom)[1]
hasil_jack_1_custom[iterasi] <- coef(model_jack_custom)[2]
hasil_jack_2_custom[iterasi] <- coef(model_jack_custom)[3]
hasil_jack_3_custom[iterasi] <- coef(model_jack_custom)[4]
}

# Hitung rata-rata, standar deviasi (st), dan standar error (error) dari
koefisien jackknife
rata_jack_custom <- mean(hasil_jack_custom)
st_jack_custom <- sd(hasil_jack_custom)
error_jack_custom <- st_jack_custom / sqrt(jumlah_obs_custom)

rata_jack_1_custom <- mean(hasil_jack_1_custom)


st_jack_1_custom <- sd(hasil_jack_1_custom)
error_jack_1_custom <- st_jack_1_custom / sqrt(jumlah_obs_custom)

rata_jack_2_custom <- mean(hasil_jack_2_custom)


st_jack_2_custom <- sd(hasil_jack_2_custom)
error_jack_2_custom <- st_jack_2_custom / sqrt(jumlah_obs_custom)

rata_jack_3_custom <- mean(hasil_jack_3_custom)


st_jack_3_custom <- sd(hasil_jack_3_custom)
error_jack_3_custom <- st_jack_3_custom / sqrt(jumlah_obs_custom)

# Tampilkan hasilnya
cat("Intercept (beta): Rata-rata =", rata_jack_custom, ", St =",
st_jack_custom, ", Error =", error_jack_custom, "\n")
cat("X1 (beta1): Rata-rata =", rata_jack_1_custom, ", St =",
st_jack_1_custom, ", Error =", error_jack_1_custom, "\n")
cat("X2 (beta2): Rata-rata =", rata_jack_2_custom, ", St =",
st_jack_2_custom, ", Error =", error_jack_2_custom, "\n")
cat("X3 (beta3): Rata-rata =", rata_jack_3_custom, ", St =",
st_jack_3_custom, ", Error =", error_jack_3_custom, "\n")

Hasil

You might also like