Ringkasan analisis: 32 skenario SAE — 2 metode seleksi aux (Backward vs Top-n/10) × 4 model (EBLUP, GLMM-Logit, EB Beta-Binomial, HB Beta) × 4 partisi (All, RSE Natural Break, RSE Equal Size, Cluster-Aux). Baseline: Direct Estimator.
# Load urutan: MASS & car DULU, baru dplyr (cegah masking select/recode)
library(MASS)
library(car)
library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
library(patchwork)
library(sae) # mseFH
library(glmmTMB) # GLMM binomial
library(classInt) # Jenks Natural Breaks
library(factoextra)
library(knitr)
library(kableExtra)
# Ikat namespace kritis
select <- dplyr::select
recode <- dplyr::recodehas_jags <- tryCatch({
library(rjags); library(saeHB); TRUE
}, error = function(e) {
message("saeHB/JAGS tidak tersedia — skenario HB Beta akan fallback ke direct.")
FALSE
})
cat("Status JAGS/saeHB:", ifelse(has_jags, "Tersedia", "Tidak tersedia"), "\n")## Status JAGS/saeHB: Tersedia
Estimasi_531 <- read_excel("C:/Users/User/Downloads/Hasil_sulawesi_KabKota.xlsx")
Podes_kab2024 <- read_excel("C:/Users/User/Downloads/Podes_kab_lengkap.xlsx")
Data_Penduduk <- read_excel("C:/Users/User/Downloads/Data_Penduduk.xlsx")
Estimasi_531 <- Estimasi_531 %>% mutate(Kako = sprintf("%04d", Kako))
Podes_kab2024 <- Podes_kab2024 %>% mutate(kode_kab = sprintf("%04s", kode_kab))
df_gabungan <- Estimasi_531 %>%
left_join(Podes_kab2024, by = c("Kako" = "kode_kab")) %>%
dplyr::select(Kako, Estimasi, RSE, starts_with("X"))
df_gabungan$Kako <- as.numeric(df_gabungan$Kako)
df_final <- df_gabungan %>% left_join(Data_Penduduk, by = "Kako")
cat("Jumlah domain:", nrow(df_final), "\n")## Jumlah domain: 81
## Jumlah variabel: 112
df_final <- df_final %>%
mutate(
X6 = X6_jumlah / Jumlah_Penduduk * 1000,
X7 = X7_jumlah / Jumlah_Penduduk * 1000,
X10 = X10_jumlah / Jumlah_Penduduk * 1000,
X33 = X33_jumlah / Jumlah_Penduduk * 1000,
X11 = X11_jumlah / Jumlah_P * 1000,
X12 = X12_jumlah / Jumlah_P * 1000,
X27 = X27_jumlah / Jumlah_P * 1000,
X28 = X28_jumlah / Jumlah_P * 1000,
X29 = X29_jumlah / Jumlah_P * 1000,
X30 = X30_jumlah / Jumlah_P * 1000,
X31 = X31_jumlah / Jumlah_P * 1000,
X35 = X35_jumlah / Jumlah_P * 1000,
X36 = X36_jumlah / Jumlah_P * 1000
)eps_logit <- 1e-4
df_base <- df_final %>%
mutate(
vardir = (RSE / 100 * Estimasi)^2,
y_prop = pmax(pmin(Estimasi / 100, 1 - eps_logit), eps_logit),
y_logit = log(y_prop / (1 - y_prop))
) %>%
as.data.frame()
# ── Kandidat aux: HANYA kolom yang diawali "X" ───────────────────────────
# RSE_direct_col, grup_jenks, cluster_*, dll yang ditambahkan kemudian
# otomatis TIDAK masuk karena tidak berawalan X
kandidat_vars <- names(df_base)[startsWith(names(df_base), "X")]
# ── excl_always: otomatis semua kolom SELAIN X* ──────────────────────────
# Diupdate di setiap fungsi seleksi agar mencakup kolom baru yang
# ditambahkan setelah chunk ini (RSE_direct_col, grup_*, cluster_*)
excl_always <- setdiff(names(df_base), kandidat_vars)
cat("Variabel kandidat (", length(kandidat_vars), "):\n",
paste(kandidat_vars, collapse = ", "), "\n")## Variabel kandidat ( 119 ):
## X1, X2, X8, X9, X13, X14, X15, X16, X17, X18, X19, X21, X22, X23, X24, X25, X26, X32, X37, X38, X39, X40, X41, X43, X44, X45, X1_jumlah, X1_mean, X2_jumlah, X2_mean, X8_jumlah, X8_mean, X9_jumlah, X9_mean, X13_jumlah, X13_mean, X14_jumlah, X14_mean, X15_jumlah, X15_mean, X16_jumlah, X16_mean, X17_jumlah, X17_mean, X18_jumlah, X18_mean, X19_jumlah, X19_mean, X21_jumlah, X21_mean, X22_jumlah, X22_mean, X23_jumlah, X23_mean, X24_jumlah, X24_mean, X25_jumlah, X25_mean, X26_jumlah, X26_mean, X32_jumlah, X32_mean, X34_jumlah, X34_mean, X37_jumlah, X37_mean, X38_jumlah, X38_mean, X39_jumlah, X39_mean, X40_jumlah, X40_mean, X41_jumlah, X41_mean, X43_jumlah, X43_mean, X44_jumlah, X44_mean, X45_jumlah, X45_mean, X6_jumlah, X6_mean, X7_jumlah, X7_mean, X10_jumlah, X10_mean, X11_jumlah, X11_mean, X12_jumlah, X12_mean, X27_jumlah, X27_mean, X28_jumlah, X28_mean, X29_jumlah, X29_mean, X30_jumlah, X30_mean, X31_jumlah, X31_mean, X33_jumlah, X33_mean, X35_jumlah, X35_mean, X36_jumlah, X36_mean, X6, X7, X10, X33, X11, X12, X27, X28, X29, X30, X31, X35, X36
##
## Total dikecualikan: 9 kolom
# Helper: pilih 1 representatif terbaik per grup X (X19, X19_mean, X19_jumlah → 1)
# Pemilihan berdasarkan |cor| tertinggi ke y_col pada data segmen berjalan
pilih_per_grup <- function(df, kandidat, y_col) {
get_base <- function(x) sub("(_jumlah|_mean)$", "", x)
bases <- unique(sapply(kandidat, get_base))
terpilih <- c()
for (b in bases) {
anggota <- kandidat[get_base(kandidat) == b]
if (length(anggota) == 1) { terpilih <- c(terpilih, anggota); next }
cor_v <- sapply(anggota, function(v)
abs(cor(df[[v]], df[[y_col]], use = "complete.obs")))
cor_v <- cor_v[!is.na(cor_v)]
if (length(cor_v) == 0) { terpilih <- c(terpilih, anggota[1]); next }
terpilih <- c(terpilih, names(which.max(cor_v)))
}
unique(terpilih)
}
seleksi_backward <- function(df, y_col, excl_cols = NULL, r2_cap = 0.8) {
if (is.null(excl_cols)) excl_cols <- names(df)[!startsWith(names(df), "X")]
n_domain <- nrow(df)
max_top <- max(1L, floor(n_domain / 10L))
keep <- sapply(df, function(x) length(unique(na.omit(x))) > 1)
df <- df[, keep]
kandidat <- setdiff(names(df), excl_cols)
kandidat <- kandidat[startsWith(kandidat, "X")]
if (length(kandidat) == 0) return(character(0))
# Pilih 1 representatif per grup X (sesuai y_col & data segmen ini)
kandidat <- pilih_per_grup(df, kandidat, y_col)
if (length(kandidat) == 0) return(character(0))
cor_v <- sapply(df[kandidat], function(x)
abs(cor(x, df[[y_col]], use = "complete.obs")))
cor_v <- cor_v[!is.na(cor_v)]
if (length(cor_v) == 0) return(character(0))
vars <- names(sort(cor_v, decreasing = TRUE))[1:min(max_top, length(cor_v))]
vars <- vars[!is.na(vars)]
step_m <- try(stepAIC(
lm(as.formula(paste(y_col, "~", paste(vars, collapse = "+"))), data = df),
direction = "backward", trace = FALSE), silent = TRUE)
if (!inherits(step_m, "try-error")) {
sv <- names(coef(step_m))[-1]
if (length(sv) > 0) vars <- sv
}
repeat {
if (length(vars) <= 1) break
m <- try(lm(as.formula(paste(y_col,"~",paste(vars,collapse="+"))),data=df),silent=TRUE)
if (inherits(m,"try-error")) { vars <- vars[1]; break }
v <- try(vif(m), silent=TRUE)
if (inherits(v,"try-error")) break
v_ok <- v[!is.na(v)]
if (length(v_ok)==0 || all(v_ok<=10)) break
vars <- vars[vars != names(which.max(v_ok))]
}
if (length(vars) > 1) {
m <- lm(as.formula(paste(y_col,"~",paste(vars,collapse="+"))),data=df)
if (summary(m)$r.squared > r2_cap) vars <- vars[1]
}
return(vars)
}seleksi_topn <- function(df, y_col, excl_cols = NULL, r2_cap = 0.8) {
if (is.null(excl_cols)) excl_cols <- names(df)[!startsWith(names(df), "X")]
n_domain <- nrow(df)
max_top <- min(max(1L, floor(n_domain / 10L)), 8L)
keep <- sapply(df, function(x) length(unique(na.omit(x))) > 1)
df <- df[, keep]
kandidat <- setdiff(names(df), excl_cols)
kandidat <- kandidat[startsWith(kandidat, "X")]
if (length(kandidat) == 0) return(character(0))
# Pilih 1 representatif per grup X
kandidat <- pilih_per_grup(df, kandidat, y_col)
if (length(kandidat) == 0) return(character(0))
cor_v <- sapply(df[kandidat], function(x)
abs(cor(x, df[[y_col]], use = "complete.obs")))
cor_v <- cor_v[!is.na(cor_v)]
if (length(cor_v) == 0) return(character(0))
vars <- names(sort(cor_v, decreasing = TRUE))[1:min(max_top, length(cor_v))]
vars <- vars[!is.na(vars)]
repeat {
if (length(vars) <= 1) break
m <- try(lm(as.formula(paste(y_col,"~",paste(vars,collapse="+"))),data=df),silent=TRUE)
if (inherits(m,"try-error")) { vars <- vars[1]; break }
v <- try(vif(m), silent=TRUE)
if (inherits(v,"try-error")) break
v_ok <- v[!is.na(v)]
if (length(v_ok)==0 || all(v_ok<=10)) break
vars <- vars[vars != names(which.max(v_ok))]
}
if (length(vars) > 1) {
m <- lm(as.formula(paste(y_col,"~",paste(vars,collapse="+"))),data=df)
if (summary(m)$r.squared > r2_cap) vars <- vars[1]
}
return(vars)
}run_eblup <- function(df, vars) {
df$vardir <- pmax(df$vardir, 1e-10)
# Trim ke kolom yang dibutuhkan mseFH saja (seperti pola SAE_Lengkap_Fixed2)
# Kolom ekstra (grup_jenks, cluster_*, RSE_direct_col, dll) dikeluarkan
# agar mseFH tidak terganggu
df_m <- df[, c("Estimasi", "vardir", vars), drop = FALSE]
# Scale per kolom — lebih aman dari scale() bulk untuk kolom near-constant
for (v in vars) {
mu <- mean(df_m[[v]], na.rm = TRUE)
sd <- sd(df_m[[v]], na.rm = TRUE)
df_m[[v]] <- if (is.na(sd) || sd == 0) 0 else (df_m[[v]] - mu) / sd
df_m[[v]][is.na(df_m[[v]]) | is.nan(df_m[[v]])] <- 0
}
form <- as.formula(paste("Estimasi ~", paste(vars, collapse = "+")))
m <- try(mseFH(form, vardir = vardir, data = df_m), silent = TRUE)
if (inherits(m, "try-error") || is.null(m$est) || is.null(m$mse)) {
df$y_eblup <- df$Estimasi
df$mse <- df$vardir
} else {
ev <- as.numeric(m$est$eblup)
mv <- as.numeric(m$mse)
# Per-domain fallback: domain NaN/NA → pakai direct, bukan fallback seluruh segmen
bad <- is.na(ev) | is.nan(ev) | is.na(mv) | is.nan(mv)
if (any(bad)) {
message(" EBLUP: ", sum(bad), " domain NaN → fallback direct per domain")
ev[bad] <- df$Estimasi[bad]
mv[bad] <- df$vardir[bad]
}
df$y_eblup <- ev
df$mse <- mv
}
df$mse <- pmax(df$mse, 0)
df$RSE_direct <- sqrt(df$vardir) / pmax(df$Estimasi, 1e-6) * 100
df$RMSE_eblup <- sqrt(df$mse)
df$RSE_eblup <- df$RMSE_eblup / pmax(abs(df$y_eblup), 1e-6) * 100
df
}run_glmm <- function(df, vars) {
eps <- 1e-4
df$y_prop <- pmax(pmin(df$Estimasi / 100, 1 - eps), eps)
se_prop <- pmax(df$RSE / 100 * df$y_prop, eps)
# n_eff minimum 5 agar binomial tidak quasi-saturated
df$n_eff <- pmax(as.integer(round(df$y_prop * (1 - df$y_prop) / se_prop^2)), 5L)
df$y_count <- pmin(as.integer(round(df$y_prop * df$n_eff)), df$n_eff - 1L)
df$y_count <- pmax(df$y_count, 1L) # hindari 0 dan n_eff (separasi)
df_m <- df[, c("Kako", "y_count", "n_eff", vars)]
df_m[vars] <- as.data.frame(scale(df_m[vars]))
df_m[vars][is.nan(as.matrix(df_m[vars]))] <- 0
df_m$Kako <- as.factor(df_m$Kako)
form <- as.formula(paste(
"cbind(y_count, n_eff - y_count) ~",
paste(vars, collapse = " + "),
"+ (1 | Kako)"
))
ctrl <- glmmTMBControl(optCtrl = list(iter.max = 500, eval.max = 800))
m <- try(glmmTMB(form, data = df_m, family = binomial, control = ctrl),
silent = TRUE)
if (inherits(m, "try-error") || is.null(m)) {
message(" GLMM fit gagal → fallback direct")
df$y_glmm <- df$Estimasi
df$mse_glmm <- df$vardir
} else {
# Prediksi: fixed + random effect (sesuai slide: X_i*beta + v_i_hat)
df$y_glmm <- plogis(predict(m, type = "link", re.form = NULL)) * 100
# ── Jackknife MSE (M2i saja) — M1i diabaikan karena g1i GLMM tidak
# memiliki bentuk analytik seperti FH, jadi full MSE ≈ M2i
n_d <- nrow(df)
theta <- df$y_glmm # estimasi penuh
mse_v <- tryCatch({
jack_pred <- matrix(NA_real_, nrow = n_d, ncol = n_d)
for (i in seq_len(n_d)) {
df_j <- df_m[-i, ]
m_j <- try(glmmTMB(form, data = df_j, family = binomial,
control = glmmTMBControl(
optCtrl = list(iter.max = 300, eval.max = 500))),
silent = TRUE)
if (!inherits(m_j, "try-error")) {
pj <- try(plogis(predict(m_j, newdata = df_m, type = "link",
re.form = NA,
allow.new.levels = TRUE)) * 100,
silent = TRUE)
if (!inherits(pj, "try-error") && length(pj) == n_d)
jack_pred[i, ] <- pj
}
}
# M2i = (m-1)/m * sum(theta_i,-l - theta_i)^2
vapply(seq_len(n_d), function(j) {
ev <- jack_pred[, j][!is.na(jack_pred[, j])]
if (length(ev) >= 2) {
((length(ev) - 1) / length(ev)) * mean((ev - theta[j])^2)
} else {
(df$RSE[j] / 100 * theta[j])^2
}
}, numeric(1))
}, error = function(e) {
message(" Jackknife crash: ", e$message, " → fallback MSE empiris")
(df$RSE / 100 * df$y_glmm)^2
})
df$mse_glmm <- pmax(mse_v, 0)
}
df$RSE_direct <- df$RSE
df$RMSE_glmm <- sqrt(df$mse_glmm)
df$RSE_glmm <- df$RMSE_glmm / pmax(df$y_glmm, 1e-6) * 100
df
}#' EB Beta-Binomial dengan covariate:
#' (1) Regresi logit(p) ~ vars → mu_i (domain-specific prior mean)
#' (2) Estimasi phi (precision) via MOM dari {p_i}
#' (3) EB: p_hat_i = (a_i + y_i) / (a_i + b_i + n_eff_i) x 100
#' (4) Var posterior analytik → RSE_eb
run_eb_beta <- function(df, vars) {
eps <- 1e-4
df$y_prop <- pmax(pmin(df$Estimasi / 100, 1 - eps), eps)
se_prop <- pmax(df$RSE / 100 * df$y_prop, eps)
df$n_eff <- pmax(as.integer(round(df$y_prop * (1 - df$y_prop) / se_prop^2)), 2L)
df$y_count <- pmin(as.integer(round(df$y_prop * df$n_eff)), df$n_eff)
df$y_logit_eb <- log(df$y_prop / (1 - df$y_prop))
df[vars] <- as.data.frame(scale(df[vars]))
df[vars][is.nan(as.matrix(df[vars]))] <- 0
# Regresi untuk mu_i (prior mean per domain)
form_reg <- as.formula(paste("y_logit_eb ~", paste(vars, collapse = "+")))
reg <- try(lm(form_reg, data = df), silent = TRUE)
mu_i <- if (inherits(reg, "try-error")) {
df$y_prop
} else {
pmax(pmin(plogis(fitted(reg)), 1 - eps), eps)
}
# MOM untuk phi (precision)
p_bar <- mean(df$y_prop)
var_p <- var(df$y_prop)
phi <- max((p_bar * (1 - p_bar) / var_p) - 1, 0.01)
a_i <- mu_i * phi
b_i <- (1 - mu_i) * phi
n_i <- df$n_eff
y_i <- df$y_count
# EB estimator
a_post <- a_i + y_i
b_post <- b_i + (n_i - y_i)
denom <- a_i + b_i + n_i
p_hat_eb <- a_post / denom
df$y_eb_pct <- p_hat_eb * 100
# Var posterior analytik (Beta conjugate)
var_post <- (a_post * b_post) / (denom^2 * (denom + 1))
df$var_eb_pct <- var_post * 100^2
df$RSE_direct <- df$RSE
df$RMSE_eb <- sqrt(df$var_eb_pct)
df$RSE_eb <- df$RMSE_eb / pmax(df$y_eb_pct, 1e-6) * 100
df
}# ── Helper: identifikasi kovariat yang CI-nya menyeberang nol ────────────────
# "Tidak aman" := 2.5% < 0 DAN 97.5% > 0 (intercept selalu dikecualikan)
#
# Desain: akses POSISIONAL, bukan berbasis nama kolom/baris.
# saeHB bisa menyimpan rownames sebagai "beta[1]","beta[2]"... (notasi JAGS)
# meski di-print tampak seperti nama variabel — intersect() gagal dalam kasus itu.
#
# Struktur baku saeHB::Beta()$coefficient:
# Baris 1 = intercept (di-skip)
# Baris 2..k+1 = kovariat urutan sesuai formula = urutan var_names
# Kolom 1=Mean | 2=SD | 3=2.5% | 4=25% | 5=50% | 6=75% | 7=97.5%
#
.vars_cross_ci <- function(coef_mat, var_names) {
if (is.null(coef_mat) || nrow(coef_mat) < 2L || ncol(coef_mat) < 7L)
return(character(0))
# Baris kovariat: lewati baris-1 (intercept), petakan ke var_names
p <- min(length(var_names), nrow(coef_mat) - 1L)
sub <- coef_mat[1L + seq_len(p), , drop = FALSE]
# Kolom 3 = 2.5%, kolom 7 = 97.5% — paksa numerik agar aman
ci_lo <- as.numeric(sub[, 3L])
ci_hi <- as.numeric(sub[, 7L])
cross <- which(ci_lo < 0 & ci_hi > 0)
if (length(cross) == 0L) return(character(0))
var_names[cross] # mapping posisional kembali ke nama variabel asli
}
# ── Fungsi utama HB Beta dengan seleksi CI otomatis ─────────────────────────
#
# Algoritma:
# 1. Skala semua vars sekali di awal (per kolom, tahan near-constant).
# 2. Jalankan saeHB::Beta() — output internalnya di-suppress agar tidak duplikat.
# 3. Cetak tabel koefisien kita sendiri (bersih, per iterasi).
# 4. Cek 2.5% & 97.5%: jika ada yang menyeberang nol → hapus SD terbesar.
# 5. Ulangi (2–4) sampai semua CI aman atau tidak ada vars tersisa.
# 6. Pakai hasil run terakhir yang valid untuk output domain.
#
# Catatan penting:
# - saeHB::Beta() di-wrap capture.output() agar tidak mencetak tabel sendiri
# (yang menyebabkan output duplikat). Kita cetak sendiri versi yang lebih bersih.
# - Semua status memakai cat() bukan message() supaya tampil di stdout
# (ikut copy-paste, tidak tenggelam di stderr/merah RStudio).
#
run_hbbeta <- function(df, vars,
has_jags = get("has_jags", envir = .GlobalEnv),
iter.mcmc = 50000,
burn.in = 2000,
thin = 10,
iter.update = 5) {
# ── Fallback jika JAGS tidak tersedia ──────────────────────────────────────
if (!has_jags) {
df$y_hb_pct <- df$Estimasi
df$sd_hb <- NA_real_
df$RSE_hb <- df$RSE
return(df)
}
# ── Persiapan data ──────────────────────────────────────────────────────────
eps <- 1e-4
df$y_prop <- pmax(pmin(df$Estimasi / 100, 1 - eps), eps)
# Scale per kolom — lebih aman dari scale() bulk untuk kolom near-constant
for (v in vars) {
mu_v <- mean(df[[v]], na.rm = TRUE)
sd_v <- sd(df[[v]], na.rm = TRUE)
df[[v]] <- if (is.na(sd_v) || sd_v == 0) 0 else (df[[v]] - mu_v) / sd_v
df[[v]][is.na(df[[v]]) | is.nan(df[[v]])] <- 0
}
# ── Loop seleksi berbasis CI ────────────────────────────────────────────────
current_vars <- vars
hb_last <- NULL
max_iter <- length(vars) + 1L # batas aman: maks 1 drop per iterasi
for (iter in seq_len(max_iter)) {
# Tidak ada vars → fallback langsung
if (length(current_vars) == 0L) {
cat(" [HB] Tidak ada variabel tersisa setelah seleksi CI → fallback direct\n")
df$y_hb_pct <- df$Estimasi
df$sd_hb <- NA_real_
df$RSE_hb <- df$RSE
return(df)
}
form_hb <- as.formula(paste("y_prop ~", paste(current_vars, collapse = "+")))
# ── saeHB::Beta() di-wrap capture.output() agar tidak duplikat ─────────
# Output internal saeHB (cat/print) ditangkap & dibuang;
# kita cetak sendiri tabel koefisien di bawah.
capture.output(
hb_cur <- try(
saeHB::Beta(
formula = form_hb,
data = df,
iter.update = iter.update,
iter.mcmc = iter.mcmc,
thin = thin,
burn.in = burn.in
),
silent = TRUE
)
)
# JAGS crash / konvergensi gagal → fallback
if (inherits(hb_cur, "try-error") || is.null(hb_cur$Est)) {
cat(" [HB] Iterasi", iter, "gagal (JAGS error) → fallback direct\n")
df$y_hb_pct <- df$Estimasi
df$sd_hb <- NA_real_
df$RSE_hb <- df$RSE
return(df)
}
hb_last <- hb_cur
coef_mat <- hb_cur$coefficient
# saeHB tidak mengembalikan tabel koefisien → lewati cek CI
if (is.null(coef_mat)) {
cat(" [HB] Tabel koefisien tidak tersedia → lewati cek CI\n")
break
}
# ── Cetak tabel koefisien kita sendiri (bersih, berlabel iterasi) ───────
cat(sprintf("\n [HB Iter %d] Vars aktif (%d): %s\n",
iter, length(current_vars),
paste(current_vars, collapse = ", ")))
print(round(coef_mat, 5))
cat("\n")
# ── Cek CI ───────────────────────────────────────────────────────────────
cross_vars <- .vars_cross_ci(coef_mat, current_vars)
if (length(cross_vars) == 0L) {
# ✓ Semua aman
cat(sprintf(" [HB] ✓ Semua CI aman — selesai pada iterasi %d.\n", iter))
break
}
# ✗ Ada yang menyeberang nol → keluarkan yang SD-nya terbesar
# SD lookup juga posisional (kolom 2 = SD, baris = 1 + posisi di current_vars)
pos_cross <- match(cross_vars, current_vars) # posisi di current_vars
sd_cross <- setNames(
as.numeric(coef_mat[1L + pos_cross, 2L]),
cross_vars
)
to_drop <- names(which.max(sd_cross))
cat(sprintf(" [HB] ✗ Iter %d — CI menyeberang nol: [%s]\n",
iter, paste(cross_vars, collapse = ", ")))
cat(sprintf(" [HB] → Keluarkan '%s' (SD = %.5f, terbesar)\n",
to_drop, sd_cross[[to_drop]]))
current_vars <- setdiff(current_vars, to_drop)
# lanjut ke iterasi berikutnya dengan current_vars − 1
}
# ── Tulis hasil ke df ───────────────────────────────────────────────────────
if (is.null(hb_last) || is.null(hb_last$Est)) {
cat(" [HB] Tidak ada run valid → fallback direct\n")
df$y_hb_pct <- df$Estimasi
df$sd_hb <- NA_real_
df$RSE_hb <- df$RSE
} else {
cat(" [HB] ✓ Variabel final:", paste(current_vars, collapse = ", "), "\n")
df$y_hb_pct <- hb_last$Est$MEAN * 100
df$sd_hb <- hb_last$Est$SD
df$RSE_hb <- df$sd_hb / pmax(df$y_hb_pct / 100, 1e-6) * 100
}
df
}# ── Plot RSE line (2 metode) ─────────────────────────────────────────────
plot_rse <- function(data, col1, col2, lab1, lab2, title) {
miss <- setdiff(c(col1, col2), names(data))
if (length(miss) > 0) { message("Kolom tidak ada: ", paste(miss)); return(invisible(NULL)) }
df_plt <- data %>%
dplyr::select(Kako, all_of(c(col1, col2))) %>%
pivot_longer(-Kako, names_to = "Metode", values_to = "RSE") %>%
mutate(Metode = case_when(Metode == col1 ~ lab1, Metode == col2 ~ lab2, TRUE ~ Metode),
Kako = as.character(Kako)) %>%
filter(!is.na(RSE))
ggplot(df_plt, aes(x = Kako, y = RSE, group = Metode, color = Metode)) +
geom_line(linewidth = 0.8, alpha = 0.85) +
geom_point(size = 1.6) +
geom_hline(yintercept = 25, linetype = "dashed", color = "#c0392b", linewidth = 0.9) +
annotate("text", x = 1, y = 26.5, label = "Batas 25%", color = "#c0392b",
hjust = 0, size = 3) +
scale_color_manual(values = c("#2980b9", "#27ae60")) +
labs(title = title, x = "Kabupaten/Kota", y = "RSE (%)", color = NULL) +
theme_minimal(base_size = 11) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, size = 6),
legend.position = "top", plot.title = element_text(face = "bold", size = 12))
}
# ── Plot HB Beta Posterior (dot + CI per domain) ─────────────────────────
plot_hb_posterior <- function(df, title, group_col = NULL) {
eps <- 1e-4
df_p <- df %>%
filter(!is.na(y_hb_pct) & !is.na(sd_hb)) %>%
mutate(
ci_lo = pmax((y_hb_pct / 100 - 1.96 * sd_hb) * 100, 0),
ci_hi = pmin((y_hb_pct / 100 + 1.96 * sd_hb) * 100, 100),
Domain = reorder(as.character(Kako), y_hb_pct)
)
if (!is.null(group_col) && group_col %in% names(df_p)) {
df_p$Grup <- df_p[[group_col]]
} else {
df_p$Grup <- "Semua Domain"
}
p <- ggplot(df_p, aes(x = Domain)) +
geom_errorbar(aes(ymin = ci_lo, ymax = ci_hi, color = Grup),
width = 0.3, alpha = 0.55) +
geom_point(aes(y = y_hb_pct, color = Grup), size = 2) +
geom_point(aes(y = Estimasi), color = "#c0392b", size = 1.2, alpha = 0.6, shape = 4) +
geom_hline(yintercept = mean(df_p$y_hb_pct, na.rm = TRUE),
linetype = "dashed", color = "#2ecc71", linewidth = 0.8) +
scale_color_manual(values = c("#2980b9","#e67e22","#8e44ad","#16a085")) +
labs(title = title,
subtitle = "Titik biru = HB | Error bar = 95% CI | Silang merah = Direct | Garis hijau = Rata-rata HB",
x = "Kabupaten/Kota (urut HB)", y = "Estimasi (%)", color = "Grup") +
theme_minimal(base_size = 10) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, size = 5),
legend.position = "top",
plot.title = element_text(face = "bold", size = 12),
plot.subtitle = element_text(size = 9))
p
}
# ── Utilitas evaluasi ────────────────────────────────────────────────────
pct_ok <- function(x) round(mean(x < 25, na.rm = TRUE) * 100, 1)
pct_ok_15 <- function(x) round(mean(x < 15, na.rm = TRUE) * 100, 1)
cv_mean <- function(x) round(mean(x, na.rm = TRUE), 2)
tabel_rse <- function(data, var_rse, label) {
data.frame(
Skenario = label,
`RSE >= 25` = sum(data[[var_rse]] >= 25, na.rm = TRUE),
`RSE < 25` = sum(data[[var_rse]] < 25, na.rm = TRUE),
`% < 25%` = pct_ok(data[[var_rse]]),
`% < 15%` = pct_ok_15(data[[var_rse]]),
`CV Mean` = cv_mean(data[[var_rse]]),
check.names = FALSE
)
}
metrik <- function(est_model, est_direct, label) {
data.frame(
Skenario = label,
`RB (%)` = round(mean((est_model - est_direct) / est_direct, na.rm=TRUE)*100, 3),
`RMSE` = round(sqrt(mean((est_model - est_direct)^2, na.rm=TRUE)), 4),
check.names = FALSE
)
}
# ── Fungsi utama: jalankan model pada list segmen ────────────────────────
# aux_fn : seleksi_backward atau seleksi_topn
# model_fn: run_eblup / run_glmm / run_eb_beta / run_hbbeta
# y_col : "Estimasi" (EBLUP) atau "y_logit" (GLMM/EB/HB)
# Catatan: excl_cols tidak perlu dioper — kedua fungsi seleksi
# sudah auto-detect kolom non-X secara dinamis
run_on_segments <- function(seg_list, aux_fn, model_fn, y_col,
min_n = 4, ...) {
hasil <- list()
for (nm in names(seg_list)) {
df_s <- seg_list[[nm]]
if (nrow(df_s) < min_n) { message(" Skip ", nm, " (n=", nrow(df_s), ")"); next }
vars <- tryCatch(
aux_fn(df_s, y_col = y_col),
error = function(e) { message(" VarSel error [", nm, "]: ", e$message); character(0) }
)
if (length(vars) == 0) {
message(" Tidak ada var terpilih untuk ", nm, " — skip")
next
}
cat(" [", nm, "]", y_col, "| n =", nrow(df_s),
"| Vars:", paste(vars, collapse=", "), "\n")
res <- tryCatch(
model_fn(df_s, vars, ...),
error = function(e) {
message(" Model error [", nm, "]: ", e$message)
NULL
}
)
if (!is.null(res)) hasil[[nm]] <- res
}
if (length(hasil) == 0) return(NULL)
bind_rows(hasil)
}# RSE_direct_col: hanya untuk pembagian partisi, BUKAN kandidat aux
# Karena nama tidak berawalan "X", otomatis tereksklusi oleh kedua fungsi seleksi
df_base$RSE_direct_col <- sqrt(df_base$vardir) / pmax(df_base$Estimasi, 1e-6) * 100
brk_jenks <- classIntervals(df_base$RSE_direct_col, n = 2, style = "jenks")
df_base$grup_jenks <- cut(df_base$RSE_direct_col,
breaks = brk_jenks$brks,
labels = c("G1_RSE_Rendah", "G2_RSE_Tinggi"),
include.lowest = TRUE)
cat("Cut-off Jenks:", round(brk_jenks$brks[2], 2), "%\n")## Cut-off Jenks: 53.58 %
##
## G1_RSE_Rendah G2_RSE_Tinggi
## 62 19
med_rse <- median(df_base$RSE_direct_col, na.rm = TRUE)
df_base$grup_equal <- ifelse(df_base$RSE_direct_col <= med_rse,
"G1_RSE_Bawah", "G2_RSE_Atas")
cat("Median RSE:", round(med_rse, 2), "%\n")## Median RSE: 44.36 %
##
## G1_RSE_Bawah G2_RSE_Atas
## 41 40
# Fungsi helper cluster
make_cluster <- function(df_full, vars_cl, seed = 42) {
mat <- scale(df_full[, vars_cl, drop = FALSE])
mat[is.nan(mat)] <- 0
set.seed(seed)
km <- kmeans(mat, centers = 2, nstart = 50, iter.max = 200)
paste0("Cluster_", km$cluster)
}
# === Global var selection (ALL data) untuk menentukan vars clustering ===
# A1 (Backward) — Normal link
vars_gbk_normal <- seleksi_backward(df_base, y_col = "Estimasi")
cat("Cluster-A1-Normal vars:", paste(vars_gbk_normal, collapse=", "), "\n")## Cluster-A1-Normal vars: X45, X28
df_base$cluster_bk_normal <- make_cluster(df_base, vars_gbk_normal)
# A2 (TopN) — Normal link
vars_gtn_normal <- seleksi_topn(df_base, y_col = "Estimasi")
cat("Cluster-A2-Normal vars:", paste(vars_gtn_normal, collapse=", "), "\n")## Cluster-A2-Normal vars: X45, X8, X36_mean, X28, X29_mean, X30_mean, X31_mean
df_base$cluster_tn_normal <- make_cluster(df_base, vars_gtn_normal)
# A1 (Backward) — Logit link
vars_gbk_logit <- seleksi_backward(df_base, y_col = "y_logit")
cat("Cluster-A1-Logit vars:", paste(vars_gbk_logit, collapse=", "), "\n")## Cluster-A1-Logit vars: X8, X45, X44
df_base$cluster_bk_logit <- make_cluster(df_base, vars_gbk_logit)
# A2 (TopN) — Logit link
vars_gtn_logit <- seleksi_topn(df_base, y_col = "y_logit")
cat("Cluster-A2-Logit vars:", paste(vars_gtn_logit, collapse=", "), "\n")## Cluster-A2-Logit vars: X8, X45, X44, X39, X36_mean, X30_mean, X28, X1
df_base$cluster_tn_logit <- make_cluster(df_base, vars_gtn_logit)
# Ringkasan cluster
kable(
data.frame(
Cluster_Tipe = c("A1-Normal","A2-Normal","A1-Logit","A2-Logit"),
Variabel_Global = c(paste(vars_gbk_normal, collapse=", "),
paste(vars_gtn_normal, collapse=", "),
paste(vars_gbk_logit, collapse=", "),
paste(vars_gtn_logit, collapse=", "))
), caption = "Variabel Global untuk Pembentukan Cluster") %>%
kable_styling(bootstrap_options = c("striped","hover","condensed"))| Cluster_Tipe | Variabel_Global |
|---|---|
| A1-Normal | X45, X28 |
| A2-Normal | X45, X8, X36_mean, X28, X29_mean, X30_mean, X31_mean |
| A1-Logit | X8, X45, X44 |
| A2-Logit | X8, X45, X44, X39, X36_mean, X30_mean, X28, X1 |
p1 <- ggplot(df_base, aes(x = RSE_direct_col, fill = grup_jenks)) +
geom_histogram(bins = 25, color = "white", alpha = 0.85) +
geom_vline(xintercept = brk_jenks$brks[2], linetype="dashed", color="#c0392b") +
scale_fill_manual(values = c("#2980b9","#e67e22")) +
labs(title = "C2: RSE Natural Break (Jenks)", x = "RSE Direct (%)", y = "n", fill=NULL) +
theme_minimal(base_size = 11) + theme(legend.position = "top")
p2 <- ggplot(df_base, aes(x = RSE_direct_col, fill = grup_equal)) +
geom_histogram(bins = 25, color = "white", alpha = 0.85) +
geom_vline(xintercept = med_rse, linetype="dashed", color="#c0392b") +
scale_fill_manual(values = c("#2980b9","#e67e22")) +
labs(title = "C3: RSE Equal Size (Median)", x = "RSE Direct (%)", y = "n", fill=NULL) +
theme_minimal(base_size = 11) + theme(legend.position = "top")
p3 <- ggplot(df_base, aes(x = Estimasi, fill = cluster_bk_normal)) +
geom_boxplot(alpha = 0.8) +
scale_fill_manual(values = c("#2980b9","#27ae60")) +
labs(title = "C4 Cluster (A1-Normal)", x = "Estimasi (%)", fill=NULL) +
theme_minimal(base_size = 11) + theme(legend.position = "top")
p4 <- ggplot(df_base, aes(x = Estimasi, fill = cluster_bk_logit)) +
geom_boxplot(alpha = 0.8) +
scale_fill_manual(values = c("#2980b9","#27ae60")) +
labs(title = "C4 Cluster (A1-Logit)", x = "Estimasi (%)", fill=NULL) +
theme_minimal(base_size = 11) + theme(legend.position = "top")
(p1 | p2) / (p3 | p4)Skenario 1–8 — Model EBLUP Fay-Herriot pada skala normal. Skenario 1–4: seleksi Backward (A1). Skenario 5–8: seleksi Top-n/10 (A2). Masing-masing 4 partisi: All, RSE-NB, RSE-ES, Cluster.
## === Skenario 1: EBLUP Backward All ===
df_s1 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_backward,
model_fn = run_eblup,
y_col = "Estimasi"
)## [ All ] Estimasi | n = 81 | Vars: X45, X28
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.0836 Min. : 22.9 Min. :21.7
## 1st Qu.: 6.18 1st Qu.: 6.8055 1st Qu.: 38.3 1st Qu.:25.9
## Median : 9.32 Median : 8.8513 Median : 44.4 Median :28.2
## Mean :10.08 Mean : 8.2477 Mean : 47.6 Mean :32.6
## 3rd Qu.:13.93 3rd Qu.:10.1615 3rd Qu.: 53.5 3rd Qu.:32.3
## Max. :24.05 Max. :13.6089 Max. :101.7 Max. :97.3
S1: RSE Direct vs EBLUP Backward (All)
## === Skenario 2: EBLUP Backward RSE-NB ===
df_s2 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_backward, model_fn = run_eblup, y_col = "Estimasi"
)## [ G1_RSE_Rendah ] Estimasi | n = 62 | Vars: X30, X8, X36_mean, X19_mean, X27_mean
## [ G2_RSE_Tinggi ] Estimasi | n = 19 | Vars: X45_mean
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.087 Min. : 22.9 Min. : 10.6
## 1st Qu.: 6.18 1st Qu.: 4.060 1st Qu.: 38.3 1st Qu.: 13.7
## Median : 9.32 Median : 9.035 Median : 44.4 Median : 17.7
## Mean :10.08 Mean : 8.102 Mean : 47.6 Mean : 27.4
## 3rd Qu.:13.93 3rd Qu.:11.322 3rd Qu.: 53.5 3rd Qu.: 42.4
## Max. :24.05 Max. :15.076 Max. :101.7 Max. :108.7
plot_rse(df_s2,"RSE_direct","RSE_eblup","Direct","EBLUP-BK RSE-NB","S2: EBLUP Backward — RSE Natural Break")S2: EBLUP Backward RSE Natural Break
## === Skenario 3: EBLUP Backward RSE-ES ===
df_s3 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_backward, model_fn = run_eblup, y_col = "Estimasi"
)## [ G1_RSE_Bawah ] Estimasi | n = 41 | Vars: X19, X27_jumlah
## [ G2_RSE_Atas ] Estimasi | n = 40 | Vars: X45, X12_mean
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.0847 Min. : 22.9 Min. :15.4
## 1st Qu.: 6.18 1st Qu.: 6.0388 1st Qu.: 38.3 1st Qu.:18.0
## Median : 9.32 Median : 7.5083 Median : 44.4 Median :30.4
## Mean :10.08 Mean : 8.4013 Mean : 47.6 Mean :31.2
## 3rd Qu.:13.93 3rd Qu.:12.3065 3rd Qu.: 53.5 3rd Qu.:34.7
## Max. :24.05 Max. :16.0132 Max. :101.7 Max. :95.9
plot_rse(df_s3,"RSE_direct","RSE_eblup","Direct","EBLUP-BK RSE-ES","S3: EBLUP Backward — RSE Equal Size")S3: EBLUP Backward RSE Equal Size
## === Skenario 4: EBLUP Backward Cluster ===
df_s4 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_bk_normal),
aux_fn = seleksi_backward, model_fn = run_eblup, y_col = "Estimasi"
)## [ Cluster_1 ] Estimasi | n = 18 | Vars: X39
## [ Cluster_2 ] Estimasi | n = 63 | Vars: X29_jumlah, X25_jumlah
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.0849 Min. : 22.9 Min. :20.6
## 1st Qu.: 6.18 1st Qu.: 6.2321 1st Qu.: 38.3 1st Qu.:25.6
## Median : 9.32 Median : 8.8138 Median : 44.4 Median :28.7
## Mean :10.08 Mean : 8.3312 Mean : 47.6 Mean :33.6
## 3rd Qu.:13.93 3rd Qu.:10.6945 3rd Qu.: 53.5 3rd Qu.:36.1
## Max. :24.05 Max. :13.3972 Max. :101.7 Max. :95.8
plot_rse(df_s4,"RSE_direct","RSE_eblup","Direct","EBLUP-BK Cluster","S4: EBLUP Backward — Cluster Aux")S4: EBLUP Backward Cluster-Aux
## === Skenario 5: EBLUP TopN All ===
df_s5 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_topn, model_fn = run_eblup, y_col = "Estimasi"
)## [ All ] Estimasi | n = 81 | Vars: X45, X8, X36_mean, X28, X29_mean, X30_mean, X31_mean
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.082 Min. : 22.9 Min. :22.6
## 1st Qu.: 6.18 1st Qu.: 6.731 1st Qu.: 38.3 1st Qu.:26.4
## Median : 9.32 Median : 9.065 Median : 44.4 Median :28.7
## Mean :10.08 Mean : 8.331 Mean : 47.6 Mean :33.8
## 3rd Qu.:13.93 3rd Qu.:10.467 3rd Qu.: 53.5 3rd Qu.:33.2
## Max. :24.05 Max. :13.562 Max. :101.7 Max. :99.2
S5: EBLUP Top-n/10 All
## === Skenario 6: EBLUP TopN RSE-NB ===
df_s6 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_topn, model_fn = run_eblup, y_col = "Estimasi"
)## [ G1_RSE_Rendah ] Estimasi | n = 62 | Vars: X30, X8, X36_mean, X29_mean, X19_mean, X27_mean
## [ G2_RSE_Tinggi ] Estimasi | n = 19 | Vars: X45_mean
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.087 Min. : 22.9 Min. : 11.4
## 1st Qu.: 6.18 1st Qu.: 4.154 1st Qu.: 38.3 1st Qu.: 14.5
## Median : 9.32 Median : 8.954 Median : 44.4 Median : 18.9
## Mean :10.08 Mean : 8.143 Mean : 47.6 Mean : 28.5
## 3rd Qu.:13.93 3rd Qu.:11.252 3rd Qu.: 53.5 3rd Qu.: 42.4
## Max. :24.05 Max. :15.393 Max. :101.7 Max. :108.7
plot_rse(df_s6,"RSE_direct","RSE_eblup","Direct","EBLUP-TN RSE-NB","S6: EBLUP Top-n/10 — RSE Natural Break")S6: EBLUP Top-n/10 RSE Natural Break
## === Skenario 7: EBLUP TopN RSE-ES ===
df_s7 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_topn, model_fn = run_eblup, y_col = "Estimasi"
)## [ G1_RSE_Bawah ] Estimasi | n = 41 | Vars: X29_jumlah, X19, X10_jumlah, X27_jumlah
## [ G2_RSE_Atas ] Estimasi | n = 40 | Vars: X45, X30_mean, X12_mean, X11_mean
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.0841 Min. : 22.9 Min. :16.5
## 1st Qu.: 6.18 1st Qu.: 6.0042 1st Qu.: 38.3 1st Qu.:20.2
## Median : 9.32 Median : 7.5411 Median : 44.4 Median :31.4
## Mean :10.08 Mean : 8.5056 Mean : 47.6 Mean :33.0
## 3rd Qu.:13.93 3rd Qu.:12.3161 3rd Qu.: 53.5 3rd Qu.:36.5
## Max. :24.05 Max. :16.2031 Max. :101.7 Max. :96.7
plot_rse(df_s7,"RSE_direct","RSE_eblup","Direct","EBLUP-TN RSE-ES","S7: EBLUP Top-n/10 — RSE Equal Size")S7: EBLUP Top-n/10 RSE Equal Size
## === Skenario 8: EBLUP TopN Cluster ===
df_s8 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_tn_normal),
aux_fn = seleksi_topn, model_fn = run_eblup, y_col = "Estimasi"
)## [ Cluster_1 ] Estimasi | n = 69 | Vars: X10_jumlah, X25_jumlah, X11_jumlah, X27_jumlah, X36_jumlah, X29_jumlah
## [ Cluster_2 ] Estimasi | n = 12 | Vars: X13_jumlah
## Estimasi y_eblup RSE_direct RSE_eblup
## Min. : 0.08 Min. : 0.084 Min. : 22.9 Min. :22.2
## 1st Qu.: 6.18 1st Qu.: 6.625 1st Qu.: 38.3 1st Qu.:28.1
## Median : 9.32 Median : 8.864 Median : 44.4 Median :31.8
## Mean :10.08 Mean : 8.430 Mean : 47.6 Mean :35.8
## 3rd Qu.:13.93 3rd Qu.:10.957 3rd Qu.: 53.5 3rd Qu.:39.6
## Max. :24.05 Max. :13.700 Max. :101.7 Max. :96.9
plot_rse(df_s8,"RSE_direct","RSE_eblup","Direct","EBLUP-TN Cluster","S8: EBLUP Top-n/10 — Cluster Aux")S8: EBLUP Top-n/10 Cluster-Aux
Skenario 9–16 — GLMM binomial via
glmmTMB, random intercept per domain (1|Kako).
Proporsi dikonversi ke count efektif n_eff = p(1-p)/SE².
MSE via Jackknife leave-one-domain-out. Seleksi variabel pada skala
logit.
## === Skenario 9: GLMM Backward All ===
df_s9 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_backward, model_fn = run_glmm, y_col = "y_logit"
)## [ All ] y_logit | n = 81 | Vars: X8, X45, X44
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.525 Min. : 22.9 Min. : 1.15
## 1st Qu.: 6.18 1st Qu.: 6.539 1st Qu.: 38.3 1st Qu.: 8.13
## Median : 9.32 Median : 9.803 Median : 44.4 Median : 20.26
## Mean :10.08 Mean : 9.241 Mean : 47.6 Mean : 27.81
## 3rd Qu.:13.93 3rd Qu.:11.928 3rd Qu.: 53.5 3rd Qu.: 33.22
## Max. :24.05 Max. :19.039 Max. :101.7 Max. :301.85
S9: GLMM Backward All
## === Skenario 10: GLMM Backward RSE-NB ===
df_s10 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_backward, model_fn = run_glmm, y_col = "y_logit"
)## [ G1_RSE_Rendah ] y_logit | n = 62 | Vars: X8, X29_mean
## [ G2_RSE_Tinggi ] y_logit | n = 19 | Vars: X8
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.269 Min. : 22.9 Min. : 0.797
## 1st Qu.: 6.18 1st Qu.: 5.940 1st Qu.: 38.3 1st Qu.: 2.387
## Median : 9.32 Median :10.374 Median : 44.4 Median : 4.929
## Mean :10.08 Mean : 9.116 Mean : 47.6 Mean : 11.588
## 3rd Qu.:13.93 3rd Qu.:12.015 3rd Qu.: 53.5 3rd Qu.: 8.353
## Max. :24.05 Max. :14.088 Max. :101.7 Max. :151.880
plot_rse(df_s10,"RSE_direct","RSE_glmm","Direct","GLMM-BK RSE-NB","S10: GLMM Backward — RSE Natural Break")S10: GLMM Backward RSE Natural Break
## === Skenario 11: GLMM Backward RSE-ES ===
df_s11 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_backward, model_fn = run_glmm, y_col = "y_logit"
)## [ G1_RSE_Bawah ] y_logit | n = 41 | Vars: X19, X29_jumlah
## [ G2_RSE_Atas ] y_logit | n = 40 | Vars: X45
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.474 Min. : 22.9 Min. : 1.09
## 1st Qu.: 6.18 1st Qu.: 6.281 1st Qu.: 38.3 1st Qu.: 2.82
## Median : 9.32 Median : 8.597 Median : 44.4 Median : 5.82
## Mean :10.08 Mean : 9.164 Mean : 47.6 Mean : 19.76
## 3rd Qu.:13.93 3rd Qu.:13.092 3rd Qu.: 53.5 3rd Qu.: 16.79
## Max. :24.05 Max. :16.780 Max. :101.7 Max. :483.92
plot_rse(df_s11,"RSE_direct","RSE_glmm","Direct","GLMM-BK RSE-ES","S11: GLMM Backward — RSE Equal Size")S11: GLMM Backward RSE Equal Size
## === Skenario 12: GLMM Backward Cluster ===
df_s12 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_bk_logit),
aux_fn = seleksi_backward, model_fn = run_glmm, y_col = "y_logit"
)## [ Cluster_1 ] y_logit | n = 10 | Vars: X30
## [ Cluster_2 ] y_logit | n = 71 | Vars: X45_mean, X25_jumlah, X33_mean
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.155 Min. : 22.9 Min. : 1.04
## 1st Qu.: 6.18 1st Qu.: 7.112 1st Qu.: 38.3 1st Qu.: 5.54
## Median : 9.32 Median : 9.754 Median : 44.4 Median : 13.54
## Mean :10.08 Mean : 9.306 Mean : 47.6 Mean : 20.99
## 3rd Qu.:13.93 3rd Qu.:11.721 3rd Qu.: 53.5 3rd Qu.: 23.50
## Max. :24.05 Max. :16.703 Max. :101.7 Max. :214.88
plot_rse(df_s12,"RSE_direct","RSE_glmm","Direct","GLMM-BK Cluster","S12: GLMM Backward — Cluster Aux")S12: GLMM Backward Cluster-Aux
## === Skenario 13: GLMM TopN All ===
df_s13 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_topn, model_fn = run_glmm, y_col = "y_logit"
)## [ All ] y_logit | n = 81 | Vars: X8, X45, X44, X39, X36_mean, X30_mean, X28, X1
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.501 Min. : 22.9 Min. : 2.05
## 1st Qu.: 6.18 1st Qu.: 6.417 1st Qu.: 38.3 1st Qu.: 8.30
## Median : 9.32 Median : 9.777 Median : 44.4 Median : 21.90
## Mean :10.08 Mean : 9.253 Mean : 47.6 Mean : 27.45
## 3rd Qu.:13.93 3rd Qu.:11.880 3rd Qu.: 53.5 3rd Qu.: 31.69
## Max. :24.05 Max. :18.607 Max. :101.7 Max. :287.25
S13: GLMM Top-n/10 All
## === Skenario 14: GLMM TopN RSE-NB ===
df_s14 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_topn, model_fn = run_glmm, y_col = "y_logit"
)## [ G1_RSE_Rendah ] y_logit | n = 62 | Vars: X8, X36_mean, X29_mean, X31_mean, X26
## [ G2_RSE_Tinggi ] y_logit | n = 19 | Vars: X8
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.269 Min. : 22.9 Min. : 0.924
## 1st Qu.: 6.18 1st Qu.: 5.882 1st Qu.: 38.3 1st Qu.: 2.581
## Median : 9.32 Median :10.249 Median : 44.4 Median : 4.035
## Mean :10.08 Mean : 9.107 Mean : 47.6 Mean : 11.360
## 3rd Qu.:13.93 3rd Qu.:12.220 3rd Qu.: 53.5 3rd Qu.: 7.477
## Max. :24.05 Max. :14.690 Max. :101.7 Max. :151.880
plot_rse(df_s14,"RSE_direct","RSE_glmm","Direct","GLMM-TN RSE-NB","S14: GLMM Top-n/10 — RSE Natural Break")S14: GLMM Top-n/10 RSE Natural Break
## === Skenario 15: GLMM TopN RSE-ES ===
df_s15 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_topn, model_fn = run_glmm, y_col = "y_logit"
)## [ G1_RSE_Bawah ] y_logit | n = 41 | Vars: X19, X29_jumlah, X10_jumlah, X8
## [ G2_RSE_Atas ] y_logit | n = 40 | Vars: X45, X8, X1, X40_jumlah
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.471 Min. : 22.9 Min. : 1.14
## 1st Qu.: 6.18 1st Qu.: 6.358 1st Qu.: 38.3 1st Qu.: 1.77
## Median : 9.32 Median : 8.997 Median : 44.4 Median : 3.59
## Mean :10.08 Mean : 9.208 Mean : 47.6 Mean : 16.87
## 3rd Qu.:13.93 3rd Qu.:12.115 3rd Qu.: 53.5 3rd Qu.: 19.46
## Max. :24.05 Max. :17.228 Max. :101.7 Max. :361.45
plot_rse(df_s15,"RSE_direct","RSE_glmm","Direct","GLMM-TN RSE-ES","S15: GLMM Top-n/10 — RSE Equal Size")S15: GLMM Top-n/10 RSE Equal Size
## === Skenario 16: GLMM TopN Cluster ===
df_s16 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_tn_logit),
aux_fn = seleksi_topn, model_fn = run_glmm, y_col = "y_logit"
)## [ Cluster_1 ] y_logit | n = 67 | Vars: X45, X25_jumlah, X11_mean, X26_mean, X43_mean, X10_jumlah
## [ Cluster_2 ] y_logit | n = 14 | Vars: X30
## Estimasi y_glmm RSE_direct RSE_glmm
## Min. : 0.08 Min. : 0.168 Min. : 22.9 Min. : 1.70
## 1st Qu.: 6.18 1st Qu.: 7.509 1st Qu.: 38.3 1st Qu.: 6.44
## Median : 9.32 Median : 9.628 Median : 44.4 Median : 11.94
## Mean :10.08 Mean : 9.327 Mean : 47.6 Mean : 23.84
## 3rd Qu.:13.93 3rd Qu.:11.749 3rd Qu.: 53.5 3rd Qu.: 21.27
## Max. :24.05 Max. :16.115 Max. :101.7 Max. :333.45
plot_rse(df_s16,"RSE_direct","RSE_glmm","Direct","GLMM-TN Cluster","S16: GLMM Top-n/10 — Cluster Aux")S16: GLMM Top-n/10 Cluster-Aux
Skenario 17–24 — EB Beta-Binomial. Regresi
logit(p) ~ vars menghasilkan μᵢ (prior mean
domain-spesifik). Presisi φ diestimasi via Method of
Moments. Estimasi EB: p̂ᵢ = (aᵢ + yᵢ) / (aᵢ + bᵢ + nᵢ). MSE
dari variansi posterior Beta analytik.
## === Skenario 17: EB Backward All ===
df_s17 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_backward, model_fn = run_eb_beta, y_col = "y_logit"
)## [ All ] y_logit | n = 81 | Vars: X8, X45, X44
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.134 Min. : 22.9 Min. :21.0
## 1st Qu.: 6.18 1st Qu.: 6.668 1st Qu.: 38.3 1st Qu.:31.5
## Median : 9.32 Median : 9.905 Median : 44.4 Median :35.3
## Mean :10.08 Mean : 9.410 Mean : 47.6 Mean :37.7
## 3rd Qu.:13.93 3rd Qu.:12.489 3rd Qu.: 53.5 3rd Qu.:40.7
## Max. :24.05 Max. :19.161 Max. :101.7 Max. :83.7
S17: EB Beta Backward All
## === Skenario 18: EB Backward RSE-NB ===
df_s18 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_backward, model_fn = run_eb_beta, y_col = "y_logit"
)## [ G1_RSE_Rendah ] y_logit | n = 62 | Vars: X8, X29_mean
## [ G2_RSE_Tinggi ] y_logit | n = 19 | Vars: X8
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.106 Min. : 22.9 Min. :20.1
## 1st Qu.: 6.18 1st Qu.: 6.688 1st Qu.: 38.3 1st Qu.:28.7
## Median : 9.32 Median : 9.823 Median : 44.4 Median :31.6
## Mean :10.08 Mean : 9.486 Mean : 47.6 Mean :36.3
## 3rd Qu.:13.93 3rd Qu.:12.608 3rd Qu.: 53.5 3rd Qu.:40.7
## Max. :24.05 Max. :18.708 Max. :101.7 Max. :87.1
plot_rse(df_s18,"RSE_direct","RSE_eb","Direct","EB-BK RSE-NB","S18: EB Beta-Binomial Backward — RSE NB")S18: EB Beta Backward RSE Natural Break
## === Skenario 19: EB Backward RSE-ES ===
df_s19 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_backward, model_fn = run_eb_beta, y_col = "y_logit"
)## [ G1_RSE_Bawah ] y_logit | n = 41 | Vars: X19, X29_jumlah
## [ G2_RSE_Atas ] y_logit | n = 40 | Vars: X45
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.183 Min. : 22.9 Min. :20.1
## 1st Qu.: 6.18 1st Qu.: 6.204 1st Qu.: 38.3 1st Qu.:27.5
## Median : 9.32 Median : 8.313 Median : 44.4 Median :33.4
## Mean :10.08 Mean : 9.469 Mean : 47.6 Mean :35.4
## 3rd Qu.:13.93 3rd Qu.:13.217 3rd Qu.: 53.5 3rd Qu.:39.9
## Max. :24.05 Max. :19.118 Max. :101.7 Max. :77.3
plot_rse(df_s19,"RSE_direct","RSE_eb","Direct","EB-BK RSE-ES","S19: EB Beta-Binomial Backward — RSE ES")S19: EB Beta Backward RSE Equal Size
## === Skenario 20: EB Backward Cluster ===
df_s20 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_bk_logit),
aux_fn = seleksi_backward, model_fn = run_eb_beta, y_col = "y_logit"
)## [ Cluster_1 ] y_logit | n = 10 | Vars: X30
## [ Cluster_2 ] y_logit | n = 71 | Vars: X45_mean, X25_jumlah, X33_mean
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.0873 Min. : 22.9 Min. :21.4
## 1st Qu.: 6.18 1st Qu.: 6.5408 1st Qu.: 38.3 1st Qu.:30.4
## Median : 9.32 Median : 9.7703 Median : 44.4 Median :33.9
## Mean :10.08 Mean : 9.4931 Mean : 47.6 Mean :36.4
## 3rd Qu.:13.93 3rd Qu.:12.3782 3rd Qu.: 53.5 3rd Qu.:38.0
## Max. :24.05 Max. :17.7431 Max. :101.7 Max. :95.8
plot_rse(df_s20,"RSE_direct","RSE_eb","Direct","EB-BK Cluster","S20: EB Beta-Binomial Backward — Cluster")S20: EB Beta Backward Cluster-Aux
## === Skenario 21: EB TopN All ===
df_s21 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_topn, model_fn = run_eb_beta, y_col = "y_logit"
)## [ All ] y_logit | n = 81 | Vars: X8, X45, X44, X39, X36_mean, X30_mean, X28, X1
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.122 Min. : 22.9 Min. :21.2
## 1st Qu.: 6.18 1st Qu.: 6.516 1st Qu.: 38.3 1st Qu.:31.4
## Median : 9.32 Median : 9.938 Median : 44.4 Median :35.2
## Mean :10.08 Mean : 9.458 Mean : 47.6 Mean :37.7
## 3rd Qu.:13.93 3rd Qu.:12.728 3rd Qu.: 53.5 3rd Qu.:41.1
## Max. :24.05 Max. :18.843 Max. :101.7 Max. :82.7
S21: EB Beta Top-n/10 All
## === Skenario 22: EB TopN RSE-NB ===
df_s22 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_topn, model_fn = run_eb_beta, y_col = "y_logit"
)## [ G1_RSE_Rendah ] y_logit | n = 62 | Vars: X8, X36_mean, X29_mean, X31_mean, X26
## [ G2_RSE_Tinggi ] y_logit | n = 19 | Vars: X8
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.106 Min. : 22.9 Min. :20.2
## 1st Qu.: 6.18 1st Qu.: 6.688 1st Qu.: 38.3 1st Qu.:28.7
## Median : 9.32 Median : 9.829 Median : 44.4 Median :31.5
## Mean :10.08 Mean : 9.488 Mean : 47.6 Mean :36.3
## 3rd Qu.:13.93 3rd Qu.:12.589 3rd Qu.: 53.5 3rd Qu.:41.2
## Max. :24.05 Max. :18.584 Max. :101.7 Max. :87.1
plot_rse(df_s22,"RSE_direct","RSE_eb","Direct","EB-TN RSE-NB","S22: EB Beta-Binomial Top-n/10 — RSE NB")S22: EB Beta Top-n/10 RSE Natural Break
## === Skenario 23: EB TopN RSE-ES ===
df_s23 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_topn, model_fn = run_eb_beta, y_col = "y_logit"
)## [ G1_RSE_Bawah ] y_logit | n = 41 | Vars: X19, X29_jumlah, X10_jumlah, X8
## [ G2_RSE_Atas ] y_logit | n = 40 | Vars: X45, X8, X1, X40_jumlah
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.159 Min. : 22.9 Min. :19.9
## 1st Qu.: 6.18 1st Qu.: 6.607 1st Qu.: 38.3 1st Qu.:27.5
## Median : 9.32 Median : 8.383 Median : 44.4 Median :32.7
## Mean :10.08 Mean : 9.541 Mean : 47.6 Mean :35.3
## 3rd Qu.:13.93 3rd Qu.:13.269 3rd Qu.: 53.5 3rd Qu.:40.5
## Max. :24.05 Max. :19.361 Max. :101.7 Max. :76.9
plot_rse(df_s23,"RSE_direct","RSE_eb","Direct","EB-TN RSE-ES","S23: EB Beta-Binomial Top-n/10 — RSE ES")S23: EB Beta Top-n/10 RSE Equal Size
## === Skenario 24: EB TopN Cluster ===
df_s24 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_tn_logit),
aux_fn = seleksi_topn, model_fn = run_eb_beta, y_col = "y_logit"
)## [ Cluster_1 ] y_logit | n = 67 | Vars: X45, X25_jumlah, X11_mean, X26_mean, X43_mean, X10_jumlah
## [ Cluster_2 ] y_logit | n = 14 | Vars: X30
## Estimasi y_eb_pct RSE_direct RSE_eb
## Min. : 0.08 Min. : 0.0893 Min. : 22.9 Min. :20.8
## 1st Qu.: 6.18 1st Qu.: 5.9527 1st Qu.: 38.3 1st Qu.:30.4
## Median : 9.32 Median : 9.6409 Median : 44.4 Median :33.6
## Mean :10.08 Mean : 9.4755 Mean : 47.6 Mean :36.4
## 3rd Qu.:13.93 3rd Qu.:12.0506 3rd Qu.: 53.5 3rd Qu.:38.4
## Max. :24.05 Max. :18.3517 Max. :101.7 Max. :95.1
plot_rse(df_s24,"RSE_direct","RSE_eb","Direct","EB-TN Cluster","S24: EB Beta-Binomial Top-n/10 — Cluster")S24: EB Beta Top-n/10 Cluster-Aux
Prasyarat JAGS: Skenario 25–32 memerlukan JAGS
terinstal di OS dan paket saeHB. Download: https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/
lalu restart R. Jika JAGS tidak tersedia, otomatis fallback ke direct
estimator. Setiap skenario HB menyertakan plot
posterior dengan credible interval 95%.
## === Skenario 25: HB Backward All ===
df_s25 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_backward, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ All ] y_logit | n = 81 | Vars: X8, X45, X44
##
## [HB Iter 1] Vars aktif (3): X8, X45, X44
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.3115 0.03053 -2.37134 -2.3316 -2.3116 -2.2905 -2.2532
## X8 0.3364 0.05335 0.23181 0.3017 0.3356 0.3721 0.4406
## X45 -0.2698 0.04741 -0.36653 -0.3012 -0.2693 -0.2378 -0.1783
## X44 0.1476 0.05178 0.04371 0.1130 0.1473 0.1827 0.2472
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X8, X45, X44
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 2.02 Min. : 22.9 Min. :19.7
## 1st Qu.: 6.18 1st Qu.: 7.55 1st Qu.: 38.3 1st Qu.:22.4
## Median : 9.32 Median :10.10 Median : 44.4 Median :24.4
## Mean :10.08 Mean :10.13 Mean : 47.6 Mean :25.7
## 3rd Qu.:13.93 3rd Qu.:12.68 3rd Qu.: 53.5 3rd Qu.:27.0
## Max. :24.05 Max. :18.42 Max. :101.7 Max. :40.5
S25: HB Beta Backward RSE
S25: HB Beta Posterior — All Domain
## === Skenario 26: HB Backward RSE-NB ===
df_s26 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_backward, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ G1_RSE_Rendah ] y_logit | n = 62 | Vars: X8, X29_mean
##
## [HB Iter 1] Vars aktif (2): X8, X29_mean
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.1093 0.02553 -2.1588 -2.1271 -2.1088 -2.0924 -2.05940
## X8 0.1665 0.03116 0.1069 0.1448 0.1663 0.1877 0.22705
## X29_mean -0.1383 0.03109 -0.1998 -0.1590 -0.1377 -0.1174 -0.07766
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X8, X29_mean
## [ G2_RSE_Tinggi ] y_logit | n = 19 | Vars: X8
##
## [HB Iter 1] Vars aktif (1): X8
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -3.2088 0.08665 -3.3771 -3.2665 -3.2087 -3.1511 -3.0399
## X8 0.5422 0.09757 0.3553 0.4743 0.5413 0.6105 0.7297
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X8
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 1.09 Min. : 22.9 Min. : 5.95
## 1st Qu.: 6.18 1st Qu.: 6.21 1st Qu.: 38.3 1st Qu.: 7.48
## Median : 9.32 Median : 9.34 Median : 44.4 Median : 9.58
## Mean :10.08 Mean :10.09 Mean : 47.6 Mean :15.24
## 3rd Qu.:13.93 3rd Qu.:13.76 3rd Qu.: 53.5 3rd Qu.:13.97
## Max. :24.05 Max. :23.57 Max. :101.7 Max. :51.47
S26: HB Beta Backward RSE Natural Break
S26: HB Beta Posterior — RSE Natural Break
## === Skenario 27: HB Backward RSE-ES ===
df_s27 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_backward, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ G1_RSE_Bawah ] y_logit | n = 41 | Vars: X19, X29_jumlah
##
## [HB Iter 1] Vars aktif (2): X19, X29_jumlah
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -1.9542 0.02821 -2.0089 -1.9733 -1.9546 -1.9347 -1.9002
## X19 -0.2278 0.02991 -0.2865 -0.2478 -0.2278 -0.2076 -0.1696
## X29_jumlah -0.2052 0.02893 -0.2617 -0.2250 -0.2051 -0.1854 -0.1499
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X19, X29_jumlah
## [ G2_RSE_Atas ] y_logit | n = 40 | Vars: X45
##
## [HB Iter 1] Vars aktif (1): X45
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.723 0.04221 -2.8050 -2.751 -2.7225 -2.6951 -2.6397
## X45 -0.471 0.04692 -0.5658 -0.502 -0.4712 -0.4394 -0.3808
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X45
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 2.03 Min. : 22.9 Min. : 7.07
## 1st Qu.: 6.18 1st Qu.: 6.62 1st Qu.: 38.3 1st Qu.: 8.98
## Median : 9.32 Median : 8.58 Median : 44.4 Median :19.94
## Mean :10.08 Mean :10.16 Mean : 47.6 Mean :17.43
## 3rd Qu.:13.93 3rd Qu.:13.26 3rd Qu.: 53.5 3rd Qu.:23.40
## Max. :24.05 Max. :23.56 Max. :101.7 Max. :35.78
S27: HB Beta Backward RSE Equal Size
S27: HB Beta Posterior — RSE Equal Size
## === Skenario 28: HB Backward Cluster ===
df_s28 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_bk_logit),
aux_fn = seleksi_backward, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ Cluster_1 ] y_logit | n = 10 | Vars: X30
##
## [HB Iter 1] Vars aktif (1): X30
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -3.4229 0.1385 -3.688 -3.5178 -3.4242 -3.3282 -3.147
## X30 -0.6121 0.1499 -0.911 -0.7141 -0.6121 -0.5102 -0.322
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X30
## [ Cluster_2 ] y_logit | n = 71 | Vars: X45_mean, X25_jumlah, X33_mean
##
## [HB Iter 1] Vars aktif (3): X45_mean, X25_jumlah, X33_mean
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.1941 0.03042 -2.2543 -2.2143 -2.1944 -2.1744 -2.13238
## X45_mean -0.1492 0.02983 -0.2078 -0.1701 -0.1490 -0.1286 -0.09147
## X25_jumlah -0.1343 0.03066 -0.1932 -0.1548 -0.1342 -0.1135 -0.07427
## X33_mean -0.1517 0.03207 -0.2141 -0.1733 -0.1513 -0.1301 -0.08945
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X45_mean, X25_jumlah, X33_mean
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 0.695 Min. : 22.9 Min. :15.9
## 1st Qu.: 6.18 1st Qu.: 7.141 1st Qu.: 38.3 1st Qu.:19.1
## Median : 9.32 Median : 9.624 Median : 44.4 Median :22.1
## Mean :10.08 Mean :10.100 Mean : 47.6 Mean :24.3
## 3rd Qu.:13.93 3rd Qu.:12.760 3rd Qu.: 53.5 3rd Qu.:25.9
## Max. :24.05 Max. :21.564 Max. :101.7 Max. :61.8
S28: HB Beta Backward Cluster-Aux
S28: HB Beta Posterior — Cluster Aux (A1-Logit)
## === Skenario 29: HB TopN All ===
df_s29 <- run_on_segments(
seg_list = list("All" = df_base),
aux_fn = seleksi_topn, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ All ] y_logit | n = 81 | Vars: X8, X45, X44, X39, X36_mean, X30_mean, X28, X1
##
## [HB Iter 1] Vars aktif (8): X8, X45, X44, X39, X36_mean, X30_mean, X28, X1
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.31020 0.03037 -2.36904 -2.33015 -2.31053 -2.28994 -2.25185
## X8 0.27940 0.06539 0.15039 0.23526 0.28188 0.32437 0.40544
## X45 -0.26801 0.05041 -0.36618 -0.30260 -0.26865 -0.23305 -0.17103
## X44 0.32351 0.06986 0.19086 0.27536 0.32254 0.36975 0.46570
## X39 -0.04086 0.04654 -0.13212 -0.07195 -0.04067 -0.01032 0.05194
## X36_mean -0.20548 0.07312 -0.34828 -0.25517 -0.20643 -0.15610 -0.06113
## X30_mean 0.05908 0.05067 -0.03648 0.02376 0.05757 0.09329 0.15853
## X28 0.11093 0.03863 0.03646 0.08493 0.11064 0.13742 0.18713
## X1 0.03067 0.04442 -0.05703 0.00127 0.03067 0.06068 0.11653
##
## [HB] ✗ Iter 1 — CI menyeberang nol: [X39, X30_mean, X1]
## [HB] → Keluarkan 'X30_mean' (SD = 0.05067, terbesar)
##
## [HB Iter 2] Vars aktif (7): X8, X45, X44, X39, X36_mean, X28, X1
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.31498 0.02997 -2.37463 -2.33506 -2.3142 -2.29468 -2.25711
## X8 0.26531 0.06534 0.13623 0.22075 0.2663 0.30921 0.39429
## X45 -0.25752 0.04897 -0.35089 -0.29125 -0.2573 -0.22475 -0.16252
## X44 0.29983 0.06886 0.16705 0.25293 0.2987 0.34650 0.43657
## X39 -0.04836 0.04603 -0.13883 -0.07890 -0.0495 -0.01737 0.04467
## X36_mean -0.15815 0.06755 -0.29109 -0.20454 -0.1570 -0.11255 -0.02528
## X28 0.10670 0.03813 0.03233 0.08133 0.1070 0.13155 0.18140
## X1 0.03928 0.04501 -0.04982 0.00968 0.0394 0.06957 0.12794
##
## [HB] ✗ Iter 2 — CI menyeberang nol: [X39, X1]
## [HB] → Keluarkan 'X39' (SD = 0.04603, terbesar)
##
## [HB Iter 3] Vars aktif (6): X8, X45, X44, X36_mean, X28, X1
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.30991 0.03041 -2.37063 -2.33057 -2.3099 -2.28992 -2.25058
## X8 0.28581 0.06274 0.16260 0.24451 0.2862 0.32771 0.41236
## X45 -0.25757 0.04721 -0.34978 -0.28900 -0.2577 -0.22535 -0.16600
## X44 0.28485 0.06619 0.15857 0.24067 0.2839 0.33083 0.41566
## X36_mean -0.16002 0.06715 -0.28902 -0.20552 -0.1605 -0.11461 -0.02755
## X28 0.11069 0.03755 0.03546 0.08549 0.1108 0.13566 0.18353
## X1 0.04206 0.04393 -0.04180 0.01266 0.0409 0.07173 0.12882
##
## [HB] ✗ Iter 3 — CI menyeberang nol: [X1]
## [HB] → Keluarkan 'X1' (SD = 0.04393, terbesar)
##
## [HB Iter 4] Vars aktif (5): X8, X45, X44, X36_mean, X28
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.31031 0.03000 -2.36969 -2.3302 -2.31035 -2.28995 -2.25229
## X8 0.29216 0.06228 0.17403 0.2493 0.29180 0.33268 0.41704
## X45 -0.25362 0.04908 -0.35216 -0.2862 -0.25277 -0.22124 -0.16014
## X44 0.27493 0.06534 0.14475 0.2315 0.27535 0.31974 0.39951
## X36_mean -0.12125 0.05973 -0.23697 -0.1619 -0.12134 -0.08119 -0.00526
## X28 0.09883 0.03670 0.02813 0.0734 0.09919 0.12317 0.17107
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 4.
## [HB] ✓ Variabel final: X8, X45, X44, X36_mean, X28
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 1.84 Min. : 22.9 Min. :18.8
## 1st Qu.: 6.18 1st Qu.: 7.36 1st Qu.: 38.3 1st Qu.:21.9
## Median : 9.32 Median : 9.95 Median : 44.4 Median :24.5
## Mean :10.08 Mean :10.15 Mean : 47.6 Mean :25.7
## 3rd Qu.:13.93 3rd Qu.:12.65 3rd Qu.: 53.5 3rd Qu.:27.4
## Max. :24.05 Max. :19.97 Max. :101.7 Max. :41.7
S29: HB Beta Top-n/10 All
S29: HB Beta Posterior — All Domain
## === Skenario 30: HB TopN RSE-NB ===
df_s30 <- run_on_segments(
seg_list = split(df_base, df_base$grup_jenks),
aux_fn = seleksi_topn, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ G1_RSE_Rendah ] y_logit | n = 62 | Vars: X8, X36_mean, X29_mean, X31_mean, X26
##
## [HB Iter 1] Vars aktif (5): X8, X36_mean, X29_mean, X31_mean, X26
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.11045 0.02544 -2.15953 -2.12789 -2.11054 -2.09365 -2.05986
## X8 0.14939 0.04416 0.06153 0.12040 0.14921 0.17892 0.23497
## X36_mean -0.01930 0.05322 -0.12681 -0.05366 -0.01865 0.01600 0.08655
## X29_mean -0.09892 0.04838 -0.19229 -0.13297 -0.09874 -0.06521 -0.00387
## X31_mean -0.04409 0.04688 -0.13701 -0.07440 -0.04420 -0.01315 0.05192
## X26 0.00049 0.04535 -0.08645 -0.03089 0.00017 0.03152 0.09109
##
## [HB] ✗ Iter 1 — CI menyeberang nol: [X36_mean, X31_mean, X26]
## [HB] → Keluarkan 'X36_mean' (SD = 0.05322, terbesar)
##
## [HB Iter 2] Vars aktif (4): X8, X29_mean, X31_mean, X26
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.10918 0.02559 -2.15922 -2.12661 -2.10888 -2.09191 -2.05886
## X8 0.15205 0.03836 0.07932 0.12585 0.15086 0.17791 0.22791
## X29_mean -0.10557 0.04064 -0.18438 -0.13288 -0.10610 -0.07816 -0.02705
## X31_mean -0.04950 0.04188 -0.13268 -0.07797 -0.04963 -0.02145 0.03275
## X26 -0.00062 0.04130 -0.08143 -0.02809 -0.00124 0.02706 0.07971
##
## [HB] ✗ Iter 2 — CI menyeberang nol: [X31_mean, X26]
## [HB] → Keluarkan 'X31_mean' (SD = 0.04188, terbesar)
##
## [HB Iter 3] Vars aktif (3): X8, X29_mean, X26
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.11096 0.02590 -2.16192 -2.1283 -2.11120 -2.09378 -2.05918
## X8 0.16474 0.03570 0.09444 0.1410 0.16488 0.18895 0.23467
## X29_mean -0.12808 0.03583 -0.19878 -0.1521 -0.12812 -0.10415 -0.05967
## X26 -0.01333 0.03855 -0.08951 -0.0395 -0.01316 0.01248 0.06152
##
## [HB] ✗ Iter 3 — CI menyeberang nol: [X26]
## [HB] → Keluarkan 'X26' (SD = 0.03855, terbesar)
##
## [HB Iter 4] Vars aktif (2): X8, X29_mean
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.1093 0.02553 -2.1588 -2.1271 -2.1088 -2.0924 -2.05940
## X8 0.1665 0.03116 0.1069 0.1448 0.1663 0.1877 0.22705
## X29_mean -0.1383 0.03109 -0.1998 -0.1590 -0.1377 -0.1174 -0.07766
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 4.
## [HB] ✓ Variabel final: X8, X29_mean
## [ G2_RSE_Tinggi ] y_logit | n = 19 | Vars: X8
##
## [HB Iter 1] Vars aktif (1): X8
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -3.2088 0.08665 -3.3771 -3.2665 -3.2087 -3.1511 -3.0399
## X8 0.5422 0.09757 0.3553 0.4743 0.5413 0.6105 0.7297
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X8
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 1.09 Min. : 22.9 Min. : 5.95
## 1st Qu.: 6.18 1st Qu.: 6.21 1st Qu.: 38.3 1st Qu.: 7.48
## Median : 9.32 Median : 9.34 Median : 44.4 Median : 9.58
## Mean :10.08 Mean :10.09 Mean : 47.6 Mean :15.24
## 3rd Qu.:13.93 3rd Qu.:13.76 3rd Qu.: 53.5 3rd Qu.:13.97
## Max. :24.05 Max. :23.57 Max. :101.7 Max. :51.47
S30: HB Beta Top-n/10 RSE Natural Break
S30: HB Beta Posterior — RSE Natural Break
## === Skenario 31: HB TopN RSE-ES ===
df_s31 <- run_on_segments(
seg_list = split(df_base, df_base$grup_equal),
aux_fn = seleksi_topn, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ G1_RSE_Bawah ] y_logit | n = 41 | Vars: X19, X29_jumlah, X10_jumlah, X8
##
## [HB Iter 1] Vars aktif (4): X19, X29_jumlah, X10_jumlah, X8
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -1.95820 0.02822 -2.01480 -1.97686 -1.95710 -1.93873 -1.90431
## X19 -0.19015 0.02975 -0.24906 -0.21041 -0.19041 -0.16983 -0.13114
## X29_jumlah -0.12340 0.03904 -0.20220 -0.14932 -0.12274 -0.09777 -0.04506
## X10_jumlah -0.07314 0.04182 -0.15150 -0.10214 -0.07342 -0.04589 0.01015
## X8 0.09144 0.03059 0.03296 0.07019 0.09115 0.11224 0.15206
##
## [HB] ✗ Iter 1 — CI menyeberang nol: [X10_jumlah]
## [HB] → Keluarkan 'X10_jumlah' (SD = 0.04182, terbesar)
##
## [HB Iter 2] Vars aktif (3): X19, X29_jumlah, X8
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -1.95514 0.02885 -2.01163 -1.97494 -1.95480 -1.9362 -1.8977
## X19 -0.19373 0.03024 -0.25123 -0.21440 -0.19307 -0.1734 -0.1345
## X29_jumlah -0.19011 0.02962 -0.24665 -0.21030 -0.18992 -0.1698 -0.1332
## X8 0.08404 0.03121 0.02382 0.06345 0.08385 0.1048 0.1449
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 2.
## [HB] ✓ Variabel final: X19, X29_jumlah, X8
## [ G2_RSE_Atas ] y_logit | n = 40 | Vars: X45, X8, X1, X40_jumlah
##
## [HB Iter 1] Vars aktif (4): X45, X8, X1, X40_jumlah
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.73375 0.04316 -2.82144 -2.76241 -2.73278 -2.70442 -2.64861
## X45 -0.40329 0.06533 -0.53118 -0.44701 -0.40347 -0.36118 -0.27343
## X8 -0.07735 0.06398 -0.20356 -0.12012 -0.07830 -0.03376 0.04864
## X1 -0.14110 0.05144 -0.24170 -0.17582 -0.14071 -0.10714 -0.04034
## X40_jumlah 0.05078 0.04611 -0.03777 0.01917 0.05014 0.08285 0.13996
##
## [HB] ✗ Iter 1 — CI menyeberang nol: [X8, X40_jumlah]
## [HB] → Keluarkan 'X8' (SD = 0.06398, terbesar)
##
## [HB Iter 2] Vars aktif (3): X45, X1, X40_jumlah
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.73495 0.04235 -2.81700 -2.76325 -2.73579 -2.70629 -2.65338
## X45 -0.36094 0.05613 -0.47084 -0.39942 -0.36098 -0.32292 -0.25047
## X1 -0.12289 0.04844 -0.21737 -0.15614 -0.12290 -0.09019 -0.02497
## X40_jumlah 0.04719 0.04723 -0.04796 0.01556 0.04733 0.07846 0.13975
##
## [HB] ✗ Iter 2 — CI menyeberang nol: [X40_jumlah]
## [HB] → Keluarkan 'X40_jumlah' (SD = 0.04723, terbesar)
##
## [HB Iter 3] Vars aktif (2): X45, X1
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.7275 0.04201 -2.8093 -2.7558 -2.7281 -2.69895 -2.64561
## X45 -0.3976 0.05494 -0.5044 -0.4354 -0.3962 -0.36012 -0.29248
## X1 -0.1193 0.04645 -0.2098 -0.1510 -0.1201 -0.08783 -0.02788
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 3.
## [HB] ✓ Variabel final: X45, X1
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 2.04 Min. : 22.9 Min. : 7.23
## 1st Qu.: 6.18 1st Qu.: 6.36 1st Qu.: 38.3 1st Qu.: 9.39
## Median : 9.32 Median : 8.43 Median : 44.4 Median :19.15
## Mean :10.08 Mean :10.15 Mean : 47.6 Mean :17.71
## 3rd Qu.:13.93 3rd Qu.:13.32 3rd Qu.: 53.5 3rd Qu.:23.85
## Max. :24.05 Max. :23.49 Max. :101.7 Max. :35.74
S31: HB Beta Top-n/10 RSE Equal Size
S31: HB Beta Posterior — RSE Equal Size
## === Skenario 32: HB TopN Cluster ===
df_s32 <- run_on_segments(
seg_list = split(df_base, df_base$cluster_tn_logit),
aux_fn = seleksi_topn, model_fn = run_hbbeta, y_col = "y_logit", min_n = 5
)## [ Cluster_1 ] y_logit | n = 67 | Vars: X45, X25_jumlah, X11_mean, X26_mean, X43_mean, X10_jumlah
##
## [HB Iter 1] Vars aktif (6): X45, X25_jumlah, X11_mean, X26_mean, X43_mean, X10_jumlah
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.16498 0.03095 -2.22392 -2.18596 -2.16496 -2.14386 -2.10472
## X45 -0.10142 0.03433 -0.17036 -0.12447 -0.10119 -0.07832 -0.03409
## X25_jumlah -0.09395 0.03332 -0.15807 -0.11706 -0.09406 -0.07133 -0.02843
## X11_mean 0.03180 0.03471 -0.03604 0.00741 0.03229 0.05506 0.10103
## X26_mean -0.08824 0.03347 -0.15429 -0.11079 -0.08870 -0.06556 -0.02158
## X43_mean -0.06496 0.03135 -0.12669 -0.08624 -0.06534 -0.04403 -0.00412
## X10_jumlah -0.09838 0.03211 -0.16063 -0.12055 -0.09880 -0.07699 -0.03574
##
## [HB] ✗ Iter 1 — CI menyeberang nol: [X11_mean]
## [HB] → Keluarkan 'X11_mean' (SD = 0.03471, terbesar)
##
## [HB Iter 2] Vars aktif (5): X45, X25_jumlah, X26_mean, X43_mean, X10_jumlah
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -2.19288 0.02980 -2.2516 -2.21308 -2.19313 -2.17340 -2.13437
## X45 -0.10888 0.03252 -0.1717 -0.13101 -0.10898 -0.08720 -0.04367
## X25_jumlah -0.08238 0.03191 -0.1446 -0.10435 -0.08175 -0.06092 -0.01950
## X26_mean -0.11212 0.03364 -0.1772 -0.13535 -0.11236 -0.08878 -0.04576
## X43_mean -0.06827 0.03302 -0.1325 -0.09081 -0.06810 -0.04544 -0.00411
## X10_jumlah -0.07377 0.03141 -0.1353 -0.09446 -0.07363 -0.05221 -0.01357
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 2.
## [HB] ✓ Variabel final: X45, X25_jumlah, X26_mean, X43_mean, X10_jumlah
## [ Cluster_2 ] y_logit | n = 14 | Vars: X30
##
## [HB Iter 1] Vars aktif (1): X30
## Mean SD 2.5% 25% 50% 75% 97.5%
## intercept -3.1744 0.1205 -3.4098 -3.2584 -3.1721 -3.0907 -2.938
## X30 -0.4275 0.1241 -0.6738 -0.5118 -0.4289 -0.3422 -0.183
##
## [HB] ✓ Semua CI aman — selesai pada iterasi 1.
## [HB] ✓ Variabel final: X30
## Estimasi y_hb_pct RSE RSE_hb
## Min. : 0.08 Min. : 1.00 Min. : 22.9 Min. : 7.49
## 1st Qu.: 6.18 1st Qu.: 6.01 1st Qu.: 38.3 1st Qu.: 9.12
## Median : 9.32 Median : 9.42 Median : 44.4 Median :11.38
## Mean :10.08 Mean :10.10 Mean : 47.6 Mean :17.57
## 3rd Qu.:13.93 3rd Qu.:13.38 3rd Qu.: 53.5 3rd Qu.:15.59
## Max. :24.05 Max. :23.58 Max. :101.7 Max. :65.62
S32: HB Beta Top-n/10 Cluster-Aux
S32: HB Beta Posterior — Cluster Aux (A2-Logit)
# Standarisasi: tiap df hasil ditambah y_model & RSE_model
standardize_result <- function(df, y_col, rse_col, skenario_id) {
if (is.null(df)) return(NULL)
df %>%
mutate(
y_model = .data[[y_col]],
RSE_model = .data[[rse_col]],
Skenario = skenario_id
) %>%
dplyr::select(Kako, Estimasi, RSE, y_model, RSE_model, Skenario)
}
# Baseline direct
df_direct_eval <- df_base %>%
mutate(RSE_direct_eval = RSE_direct_col) %>%
dplyr::select(Kako, Estimasi, RSE, RSE_direct_eval)
# ── Tabel distribusi RSE ─────────────────────────────────────────────────
make_tabel_row <- function(df, rse_col, label) {
if (is.null(df) || !rse_col %in% names(df)) {
return(data.frame(Skenario = label, `RSE>=25` = NA, `RSE<25` = NA,
`%<25` = NA, `%<15` = NA, `CV Mean` = NA,
check.names = FALSE))
}
tabel_rse(df, rse_col, label)
}
eval_rows <- list(
make_tabel_row(df_base, "RSE_direct_col", "0. Direct"),
# EBLUP
make_tabel_row(df_s1, "RSE_eblup", "S01 EBLUP-BK All"),
make_tabel_row(df_s2, "RSE_eblup", "S02 EBLUP-BK RSE-NB"),
make_tabel_row(df_s3, "RSE_eblup", "S03 EBLUP-BK RSE-ES"),
make_tabel_row(df_s4, "RSE_eblup", "S04 EBLUP-BK Cluster"),
make_tabel_row(df_s5, "RSE_eblup", "S05 EBLUP-TN All"),
make_tabel_row(df_s6, "RSE_eblup", "S06 EBLUP-TN RSE-NB"),
make_tabel_row(df_s7, "RSE_eblup", "S07 EBLUP-TN RSE-ES"),
make_tabel_row(df_s8, "RSE_eblup", "S08 EBLUP-TN Cluster"),
# GLMM
make_tabel_row(df_s9, "RSE_glmm", "S09 GLMM-BK All"),
make_tabel_row(df_s10, "RSE_glmm", "S10 GLMM-BK RSE-NB"),
make_tabel_row(df_s11, "RSE_glmm", "S11 GLMM-BK RSE-ES"),
make_tabel_row(df_s12, "RSE_glmm", "S12 GLMM-BK Cluster"),
make_tabel_row(df_s13, "RSE_glmm", "S13 GLMM-TN All"),
make_tabel_row(df_s14, "RSE_glmm", "S14 GLMM-TN RSE-NB"),
make_tabel_row(df_s15, "RSE_glmm", "S15 GLMM-TN RSE-ES"),
make_tabel_row(df_s16, "RSE_glmm", "S16 GLMM-TN Cluster"),
# EB Beta
make_tabel_row(df_s17, "RSE_eb", "S17 EB-BK All"),
make_tabel_row(df_s18, "RSE_eb", "S18 EB-BK RSE-NB"),
make_tabel_row(df_s19, "RSE_eb", "S19 EB-BK RSE-ES"),
make_tabel_row(df_s20, "RSE_eb", "S20 EB-BK Cluster"),
make_tabel_row(df_s21, "RSE_eb", "S21 EB-TN All"),
make_tabel_row(df_s22, "RSE_eb", "S22 EB-TN RSE-NB"),
make_tabel_row(df_s23, "RSE_eb", "S23 EB-TN RSE-ES"),
make_tabel_row(df_s24, "RSE_eb", "S24 EB-TN Cluster"),
# HB Beta
make_tabel_row(df_s25, "RSE_hb", "S25 HB-BK All"),
make_tabel_row(df_s26, "RSE_hb", "S26 HB-BK RSE-NB"),
make_tabel_row(df_s27, "RSE_hb", "S27 HB-BK RSE-ES"),
make_tabel_row(df_s28, "RSE_hb", "S28 HB-BK Cluster"),
make_tabel_row(df_s29, "RSE_hb", "S29 HB-TN All"),
make_tabel_row(df_s30, "RSE_hb", "S30 HB-TN RSE-NB"),
make_tabel_row(df_s31, "RSE_hb", "S31 HB-TN RSE-ES"),
make_tabel_row(df_s32, "RSE_hb", "S32 HB-TN Cluster")
)
tabel_eval <- bind_rows(eval_rows)kable(tabel_eval,
caption = "Distribusi RSE — 32 Skenario + Direct",
col.names = c("Skenario", "RSE≥25", "RSE<25", "% <25%", "% <15%", "CV Mean")) %>%
kable_styling(bootstrap_options = c("striped","hover","condensed","bordered"),
full_width = FALSE, font_size = 12) %>%
row_spec(1, background = "#fff3cd") %>%
row_spec(
which(tabel_eval$`%<25` == max(tabel_eval$`%<25`, na.rm = TRUE))[-1],
bold = TRUE, background = "#d4edda"
) %>%
pack_rows("EBLUP", 2, 9) %>%
pack_rows("GLMM", 10, 17) %>%
pack_rows("EB Beta-Binomial", 18, 25) %>%
pack_rows("HB Beta-Binomial", 26, 33)| Skenario | RSE≥25 | RSE<25 | % <25% | % <15% | CV Mean |
|---|---|---|---|---|---|
|
80 | 1 | 1.2 | 0.0 | 47.56 |
| EBLUP | |||||
| S01 EBLUP-BK All | 66 | 15 | 18.5 | 0.0 | 32.57 |
| S02 EBLUP-BK RSE-NB | 26 | 55 | 67.9 | 35.8 | 27.37 |
| S03 EBLUP-BK RSE-ES | 48 | 33 | 40.7 | 0.0 | 31.20 |
| S04 EBLUP-BK Cluster | 66 | 15 | 18.5 | 0.0 | 33.65 |
| S05 EBLUP-TN All | 72 | 9 | 11.1 | 0.0 | 33.79 |
| S06 EBLUP-TN RSE-NB | 27 | 54 | 66.7 | 27.2 | 28.54 |
| S07 EBLUP-TN RSE-ES | 53 | 28 | 34.6 | 0.0 | 33.04 |
| S08 EBLUP-TN Cluster | 75 | 6 | 7.4 | 0.0 | 35.84 |
| GLMM | |||||
| S09 GLMM-BK All | 34 | 47 | 58.0 | 42.0 | 27.81 |
| S10 GLMM-BK RSE-NB | 12 | 69 | 85.2 | 84.0 | 11.59 |
| S11 GLMM-BK RSE-ES | 13 | 68 | 84.0 | 70.4 | 19.76 |
| S12 GLMM-BK Cluster | 19 | 62 | 76.5 | 53.1 | 20.99 |
| S13 GLMM-TN All | 35 | 46 | 56.8 | 39.5 | 27.45 |
| S14 GLMM-TN RSE-NB | 12 | 69 | 85.2 | 82.7 | 11.36 |
| S15 GLMM-TN RSE-ES | 13 | 68 | 84.0 | 69.1 | 16.87 |
| S16 GLMM-TN Cluster | 16 | 65 | 80.2 | 56.8 | 23.84 |
| EB Beta-Binomial | |||||
| S17 EB-BK All | 79 | 2 | 2.5 | 0.0 | 37.68 |
| S18 EB-BK RSE-NB | 75 | 6 | 7.4 | 0.0 | 36.29 |
| S19 EB-BK RSE-ES | 71 | 10 | 12.3 | 0.0 | 35.39 |
| S20 EB-BK Cluster | 79 | 2 | 2.5 | 0.0 | 36.36 |
| S21 EB-TN All | 79 | 2 | 2.5 | 0.0 | 37.66 |
| S22 EB-TN RSE-NB | 75 | 6 | 7.4 | 0.0 | 36.29 |
| S23 EB-TN RSE-ES | 71 | 10 | 12.3 | 0.0 | 35.33 |
| S24 EB-TN Cluster | 79 | 2 | 2.5 | 0.0 | 36.38 |
| HB Beta-Binomial | |||||
| S25 HB-BK All | 36 | 45 | 55.6 | 0.0 | 25.75 |
| S26 HB-BK RSE-NB | 19 | 62 | 76.5 | 76.5 | 15.24 |
| S27 HB-BK RSE-ES | 16 | 65 | 80.2 | 46.9 | 17.43 |
| S28 HB-BK Cluster | 22 | 59 | 72.8 | 0.0 | 24.35 |
| S29 HB-TN All | 34 | 47 | 58.0 | 0.0 | 25.66 |
| S30 HB-TN RSE-NB | 19 | 62 | 76.5 | 76.5 | 15.24 |
| S31 HB-TN RSE-ES | 17 | 64 | 79.0 | 46.9 | 17.71 |
| S32 HB-TN Cluster | 16 | 65 | 80.2 | 71.6 | 17.57 |
get_est <- function(df, col) if (!is.null(df) && col %in% names(df)) df[[col]] else NULL
metrik_rows <- list(
metrik(get_est(df_s1, "y_eblup"), df_s1$Estimasi, "S01 EBLUP-BK All"),
metrik(get_est(df_s2, "y_eblup"), df_s2$Estimasi, "S02 EBLUP-BK RSE-NB"),
metrik(get_est(df_s3, "y_eblup"), df_s3$Estimasi, "S03 EBLUP-BK RSE-ES"),
metrik(get_est(df_s4, "y_eblup"), df_s4$Estimasi, "S04 EBLUP-BK Cluster"),
metrik(get_est(df_s5, "y_eblup"), df_s5$Estimasi, "S05 EBLUP-TN All"),
metrik(get_est(df_s6, "y_eblup"), df_s6$Estimasi, "S06 EBLUP-TN RSE-NB"),
metrik(get_est(df_s7, "y_eblup"), df_s7$Estimasi, "S07 EBLUP-TN RSE-ES"),
metrik(get_est(df_s8, "y_eblup"), df_s8$Estimasi, "S08 EBLUP-TN Cluster"),
metrik(get_est(df_s9, "y_glmm"), df_s9$Estimasi, "S09 GLMM-BK All"),
metrik(get_est(df_s10, "y_glmm"), df_s10$Estimasi, "S10 GLMM-BK RSE-NB"),
metrik(get_est(df_s11, "y_glmm"), df_s11$Estimasi, "S11 GLMM-BK RSE-ES"),
metrik(get_est(df_s12, "y_glmm"), df_s12$Estimasi, "S12 GLMM-BK Cluster"),
metrik(get_est(df_s13, "y_glmm"), df_s13$Estimasi, "S13 GLMM-TN All"),
metrik(get_est(df_s14, "y_glmm"), df_s14$Estimasi, "S14 GLMM-TN RSE-NB"),
metrik(get_est(df_s15, "y_glmm"), df_s15$Estimasi, "S15 GLMM-TN RSE-ES"),
metrik(get_est(df_s16, "y_glmm"), df_s16$Estimasi, "S16 GLMM-TN Cluster"),
metrik(get_est(df_s17, "y_eb_pct"), df_s17$Estimasi, "S17 EB-BK All"),
metrik(get_est(df_s18, "y_eb_pct"), df_s18$Estimasi, "S18 EB-BK RSE-NB"),
metrik(get_est(df_s19, "y_eb_pct"), df_s19$Estimasi, "S19 EB-BK RSE-ES"),
metrik(get_est(df_s20, "y_eb_pct"), df_s20$Estimasi, "S20 EB-BK Cluster"),
metrik(get_est(df_s21, "y_eb_pct"), df_s21$Estimasi, "S21 EB-TN All"),
metrik(get_est(df_s22, "y_eb_pct"), df_s22$Estimasi, "S22 EB-TN RSE-NB"),
metrik(get_est(df_s23, "y_eb_pct"), df_s23$Estimasi, "S23 EB-TN RSE-ES"),
metrik(get_est(df_s24, "y_eb_pct"), df_s24$Estimasi, "S24 EB-TN Cluster"),
metrik(get_est(df_s25, "y_hb_pct"), df_s25$Estimasi, "S25 HB-BK All"),
metrik(get_est(df_s26, "y_hb_pct"), df_s26$Estimasi, "S26 HB-BK RSE-NB"),
metrik(get_est(df_s27, "y_hb_pct"), df_s27$Estimasi, "S27 HB-BK RSE-ES"),
metrik(get_est(df_s28, "y_hb_pct"), df_s28$Estimasi, "S28 HB-BK Cluster"),
metrik(get_est(df_s29, "y_hb_pct"), df_s29$Estimasi, "S29 HB-TN All"),
metrik(get_est(df_s30, "y_hb_pct"), df_s30$Estimasi, "S30 HB-TN RSE-NB"),
metrik(get_est(df_s31, "y_hb_pct"), df_s31$Estimasi, "S31 HB-TN RSE-ES"),
metrik(get_est(df_s32, "y_hb_pct"), df_s32$Estimasi, "S32 HB-TN Cluster")
)
tabel_metrik <- bind_rows(Filter(Negate(is.null), metrik_rows))
kable(tabel_metrik,
caption = "Relative Bias (%) & RMSE terhadap Direct Estimator") %>%
kable_styling(bootstrap_options = c("striped","hover","condensed","bordered"),
full_width = FALSE, font_size = 12) %>%
row_spec(which(abs(tabel_metrik$`RB (%)`) == min(abs(tabel_metrik$`RB (%)`), na.rm=TRUE)),
bold = TRUE, background = "#d4edda")| Skenario | RB (%) | RMSE |
|---|---|---|
| S01 EBLUP-BK All | -6.336 | 3.8559 |
| S02 EBLUP-BK RSE-NB | -11.100 | 4.1048 |
| S03 EBLUP-BK RSE-ES | -9.116 | 3.2683 |
| S04 EBLUP-BK Cluster | -6.150 | 3.6493 |
| S05 EBLUP-TN All | -5.870 | 3.7863 |
| S06 EBLUP-TN RSE-NB | -10.974 | 4.0329 |
| S07 EBLUP-TN RSE-ES | -8.461 | 3.1051 |
| S08 EBLUP-TN Cluster | -6.284 | 3.4471 |
| S09 GLMM-BK All | 11.118 | 2.4846 |
| S10 GLMM-BK RSE-NB | 4.191 | 3.6762 |
| S11 GLMM-BK RSE-ES | 6.220 | 2.8721 |
| S12 GLMM-BK Cluster | 9.401 | 2.9225 |
| S13 GLMM-TN All | 11.320 | 2.5729 |
| S14 GLMM-TN RSE-NB | 4.400 | 3.7664 |
| S15 GLMM-TN RSE-ES | 7.038 | 3.0583 |
| S16 GLMM-TN Cluster | 10.842 | 3.2166 |
| S17 EB-BK All | 2.070 | 1.9740 |
| S18 EB-BK RSE-NB | 0.581 | 2.0928 |
| S19 EB-BK RSE-ES | 0.754 | 1.7348 |
| S20 EB-BK Cluster | 2.910 | 2.0358 |
| S21 EB-TN All | 2.162 | 1.9689 |
| S22 EB-TN RSE-NB | 0.586 | 2.0865 |
| S23 EB-TN RSE-ES | 0.903 | 1.7279 |
| S24 EB-TN Cluster | 3.135 | 2.1364 |
| S25 HB-BK All | 62.291 | 2.1717 |
| S26 HB-BK RSE-NB | 23.031 | 0.6044 |
| S27 HB-BK RSE-ES | 64.998 | 1.2007 |
| S28 HB-BK Cluster | 25.118 | 1.3695 |
| S29 HB-TN All | 70.966 | 2.1041 |
| S30 HB-TN RSE-NB | 23.031 | 0.6044 |
| S31 HB-TN RSE-ES | 61.875 | 1.1923 |
| S32 HB-TN Cluster | 24.808 | 0.6399 |
direct_pct <- pct_ok(df_base$RSE_direct_col)
plot_df <- tabel_eval %>%
filter(Skenario != "0. Direct") %>%
mutate(
pct_25 = `% < 25%`, # << nama persis dari debug
Model = case_when(
grepl("EBLUP", Skenario) ~ "EBLUP",
grepl("GLMM", Skenario) ~ "GLMM",
grepl("EB-BK|EB-TN", Skenario) ~ "EB Beta",
grepl("HB-BK|HB-TN", Skenario) ~ "HB Beta",
TRUE ~ "Lain"
),
AuxMetode = ifelse(grepl("-BK", Skenario), "Backward", "Top-n/10"),
Skenario = factor(Skenario, levels = rev(unique(Skenario)))
)
ggplot(plot_df, aes(x = Skenario, y = pct_25, fill = Model, alpha = AuxMetode)) +
geom_col(width = 0.72) +
geom_text(aes(label = paste0(pct_25, "%")),
hjust = -0.12, size = 3, fontface = "bold") +
geom_vline(xintercept = direct_pct,
linetype = "dashed", color = "#c0392b", linewidth = 0.9) +
scale_fill_manual(values = c(
"EBLUP" = "#2980b9", "GLMM" = "#8e44ad",
"EB Beta" = "#e67e22", "HB Beta" = "#27ae60"
)) +
scale_alpha_manual(values = c("Backward" = 1, "Top-n/10" = 0.65)) +
scale_y_continuous(limits = c(0, 115), expand = c(0, 0)) +
coord_flip() +
labs(title = "% Domain RSE < 25% — 32 Skenario SAE",
subtitle = "Garis merah = baseline Direct | Gelap = Backward, Terang = Top-n/10",
x = NULL, y = "% Domain RSE < 25%", fill = "Model", alpha = "Seleksi Aux") +
theme_minimal(base_size = 11) +
theme(legend.position = "top", plot.title = element_text(face = "bold", size = 13),
panel.grid.major.y = element_blank())make_scatter_df <- function(df, y_col, rse_col, label) {
if (is.null(df) || !all(c(y_col, rse_col, "RSE_direct") %in% names(df))) return(NULL)
df %>%
mutate(RSE_model = .data[[rse_col]], Skenario = label) %>%
dplyr::select(Kako, RSE_direct, RSE_model, Skenario) # << RSE_direct bukan RSE_direct_col
}
scatter_list <- list(
make_scatter_df(df_s1, "y_eblup", "RSE_eblup", "S01 EBLUP-BK All"),
make_scatter_df(df_s2, "y_eblup", "RSE_eblup", "S02 EBLUP-BK RSE-NB"),
make_scatter_df(df_s3, "y_eblup", "RSE_eblup", "S03 EBLUP-BK RSE-ES"),
make_scatter_df(df_s4, "y_eblup", "RSE_eblup", "S04 EBLUP-BK Clust"),
make_scatter_df(df_s5, "y_eblup", "RSE_eblup", "S05 EBLUP-TN All"),
make_scatter_df(df_s6, "y_eblup", "RSE_eblup", "S06 EBLUP-TN RSE-NB"),
make_scatter_df(df_s7, "y_eblup", "RSE_eblup", "S07 EBLUP-TN RSE-ES"),
make_scatter_df(df_s8, "y_eblup", "RSE_eblup", "S08 EBLUP-TN Clust"),
make_scatter_df(df_s9, "y_glmm", "RSE_glmm", "S09 GLMM-BK All"),
make_scatter_df(df_s10, "y_glmm", "RSE_glmm", "S10 GLMM-BK RSE-NB"),
make_scatter_df(df_s11, "y_glmm", "RSE_glmm", "S11 GLMM-BK RSE-ES"),
make_scatter_df(df_s12, "y_glmm", "RSE_glmm", "S12 GLMM-BK Clust"),
make_scatter_df(df_s13, "y_glmm", "RSE_glmm", "S13 GLMM-TN All"),
make_scatter_df(df_s14, "y_glmm", "RSE_glmm", "S14 GLMM-TN RSE-NB"),
make_scatter_df(df_s15, "y_glmm", "RSE_glmm", "S15 GLMM-TN RSE-ES"),
make_scatter_df(df_s16, "y_glmm", "RSE_glmm", "S16 GLMM-TN Clust"),
make_scatter_df(df_s17, "y_eb_pct", "RSE_eb", "S17 EB-BK All"),
make_scatter_df(df_s18, "y_eb_pct", "RSE_eb", "S18 EB-BK RSE-NB"),
make_scatter_df(df_s19, "y_eb_pct", "RSE_eb", "S19 EB-BK RSE-ES"),
make_scatter_df(df_s20, "y_eb_pct", "RSE_eb", "S20 EB-BK Clust"),
make_scatter_df(df_s21, "y_eb_pct", "RSE_eb", "S21 EB-TN All"),
make_scatter_df(df_s22, "y_eb_pct", "RSE_eb", "S22 EB-TN RSE-NB"),
make_scatter_df(df_s23, "y_eb_pct", "RSE_eb", "S23 EB-TN RSE-ES"),
make_scatter_df(df_s24, "y_eb_pct", "RSE_eb", "S24 EB-TN Clust"),
make_scatter_df(df_s25, "y_hb_pct", "RSE_hb", "S25 HB-BK All"),
make_scatter_df(df_s26, "y_hb_pct", "RSE_hb", "S26 HB-BK RSE-NB"),
make_scatter_df(df_s27, "y_hb_pct", "RSE_hb", "S27 HB-BK RSE-ES"),
make_scatter_df(df_s28, "y_hb_pct", "RSE_hb", "S28 HB-BK Clust"),
make_scatter_df(df_s29, "y_hb_pct", "RSE_hb", "S29 HB-TN All"),
make_scatter_df(df_s30, "y_hb_pct", "RSE_hb", "S30 HB-TN RSE-NB"),
make_scatter_df(df_s31, "y_hb_pct", "RSE_hb", "S31 HB-TN RSE-ES"),
make_scatter_df(df_s32, "y_hb_pct", "RSE_hb", "S32 HB-TN Clust")
)
df_scatter <- bind_rows(Filter(Negate(is.null), scatter_list)) %>%
filter(!is.na(RSE_direct) & !is.na(RSE_model))
lim_max <- quantile(c(df_scatter$RSE_direct, df_scatter$RSE_model), 0.99, na.rm = TRUE)
ggplot(df_scatter, aes(x = RSE_direct, y = RSE_model)) +
geom_point(alpha = 0.5, size = 1.3, color = "#2980b9") +
geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "#c0392b") +
geom_hline(yintercept = 25, linetype = "dotted", color = "#e67e22") +
geom_vline(xintercept = 25, linetype = "dotted", color = "#e67e22") +
facet_wrap(~ Skenario, ncol = 4, scales = "free") +
coord_cartesian(xlim = c(0, lim_max), ylim = c(0, lim_max)) +
labs(title = "RSE Direct vs RSE Model — 32 Skenario SAE",
subtitle = "Di bawah diagonal merah = lebih presisi dari direct",
x = "RSE Direct (%)", y = "RSE Model (%)") +
theme_minimal(base_size = 9) +
theme(strip.text = element_text(face = "bold", size = 7),
plot.title = element_text(face = "bold"))heat_df <- tabel_eval %>%
filter(Skenario != "0. Direct") %>%
mutate(
pct_25 = `% < 25%`, # << nama persis
Model = case_when(
grepl("EBLUP", Skenario) ~ "EBLUP",
grepl("GLMM", Skenario) ~ "GLMM",
grepl("EB-BK|EB-TN", Skenario) ~ "EB-Beta",
TRUE ~ "HB-Beta"
),
AuxMetode = ifelse(grepl("-BK", Skenario), "Backward", "Top-n/10"),
Partisi = case_when(
grepl("All", Skenario) ~ "All",
grepl("RSE-NB", Skenario) ~ "RSE-NB",
grepl("RSE-ES", Skenario) ~ "RSE-ES",
TRUE ~ "Cluster"
),
Partisi = factor(Partisi, levels = c("All","RSE-NB","RSE-ES","Cluster")),
Model = factor(Model, levels = c("EBLUP","GLMM","EB-Beta","HB-Beta")),
AuxMetode = factor(AuxMetode, levels = c("Backward","Top-n/10"))
)
ggplot(heat_df, aes(x = Partisi, y = Model, fill = pct_25)) +
geom_tile(color = "white", linewidth = 0.8) +
geom_text(aes(label = paste0(pct_25, "%")), size = 4, fontface = "bold") +
facet_wrap(~ AuxMetode, ncol = 2) +
scale_fill_gradient2(low = "#e74c3c", mid = "#f39c12", high = "#27ae60",
midpoint = 60, name = "% Domain\nRSE < 25%") +
labs(title = "Heatmap Performa — % Domain RSE < 25%",
subtitle = "Kiri = Backward | Kanan = Top-n/10",
x = "Partisi", y = "Model") +
theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold"),
strip.text = element_text(face = "bold", size = 12),
axis.text = element_text(size = 11))best_rse <- tabel_eval %>%
filter(Skenario != "0. Direct") %>%
mutate(pct_25 = `% < 25%`) %>% # << tarik dulu
slice_max(pct_25, n = 3, with_ties = FALSE)
best_rb <- tabel_metrik %>%
slice_min(abs(`RB (%)`), n = 3, with_ties = FALSE)
cat("=== Top 3 Model (% domain RSE < 25%) ===\n")## === Top 3 Model (% domain RSE < 25%) ===
## Skenario pct_25 % < 15% CV Mean
## 1 S10 GLMM-BK RSE-NB 85.2 84.0 11.59
## 2 S14 GLMM-TN RSE-NB 85.2 82.7 11.36
## 3 S11 GLMM-BK RSE-ES 84.0 70.4 19.76
##
## === Top 3 Model (Relative Bias terkecil) ===
## Skenario RB (%) RMSE
## 1 S18 EB-BK RSE-NB 0.581 2.093
## 2 S22 EB-TN RSE-NB 0.586 2.087
## 3 S19 EB-BK RSE-ES 0.754 1.735
Panduan interpretasi 32 skenario:
Seleksi Aux (Backward vs Top-n/10): Backward
lebih selektif (stepAIC membuang variabel non-signifikan); Top-n/10
mempertahankan variabel dengan korelasi tertinggi. Keduanya menggunakan
floor(n_partisi/10) variabel awal dari pre-filter
korelasi.
EBLUP (Normal): Baseline kuat untuk domain tidak terlalu ekstrim proporsinya. Rentan estimasi negatif jika proporsi mendekati 0.
GLMM (Logit, glmmTMB): Menangani bounded response [0,1] lebih proper. Random intercept per domain memberi shrinkage empiris. MSE dari Jackknife leave-one-domain-out.
EB Beta-Binomial: Analytical — cepat dan tidak perlu MCMC. Prior mean domain-spesifik dari regresi logit. Posterior variance analytik dari distribusi Beta konjugat.
HB Beta (saeHB/JAGS): Paling proper secara Bayesian. Memanfaatkan full posterior distribution. Plot posterior dengan 95% CI memperlihatkan ketidakpastian per domain.
Partisi RSE-NB vs RSE-ES: Natural Break (Jenks) memisahkan berdasarkan gap alami distribusi RSE; Equal Size memastikan kedua grup seimbang. RSE-NB lebih heterogen antar grup, RSE-ES lebih stabil dalam estimasi.
Cluster-Aux: Variabel clustering berbeda per kombinasi (Aux method, Link function), membuat cluster lebih relevan secara statistik untuk masing-masing model.
Dianalisis dengan R. Seluruh 32 skenario merupakan kombinasi dari 2 metode seleksi aux × 4 model × 4 partisi.