1 Persiapan

1.1 Paket

library(readxl)
library(dplyr)
library(tidyr)
library(plm)
library(lmtest)
library(car)
library(tseries)
library(sandwich)
library(knitr)
library(ggplot2)

1.2 Konfigurasi data

Ubah bagian ini sesuai data Anda. File ini otomatis mendeteksi data format wide (tahun sebagai kolom, misalnya 2001, 2002, …, 2025) dan mengubahnya ke format panel long. Jika NULL, kolom negara dan kolom indikator dideteksi otomatis, dan indikator pertama dijadikan Y. Sebaiknya isi y_var dan x_vars secara manual setelah melihat daftar indikator yang tercetak.

file_path <- "C:/Users/valem/Downloads/DATA_15_NEGARA_CLEANED.xlsx"
sheet_no  <- 1

# Nama kolom persis seperti di Excel (isi hanya jika deteksi otomatis salah)
kol_negara    <- NULL   # contoh: "Country Name"
kol_indikator <- NULL   # contoh: "Series Name" (kolom berisi nama variabel: GDP, FDI, dst.)

# Variabel regresi (isi sesuai nama indikator; spasi akan menjadi titik)
# contoh: y_var <- "GDP"; x_vars <- c("FDI", "INF", "TO")
y_var    <- NULL
x_vars   <- NULL

# Opsi transformasi: TRUE = ubah Y dan X positif menjadi log natural
pakai_log <- FALSE

alpha <- 0.05

1.3 Impor data

sheets <- excel_sheets(file_path)
cat("Sheet ditemukan:", paste(sheets, collapse = ", "), "\n")
## Sheet ditemukan: Data_Clean, Data_Long_R
pola_tahun <- "^X?(19|20)[0-9]{2}"

# Baca semua sheet sebagai teks (agar aman digabung), deteksi kolom tahun
info <- lapply(sheets, function(sh) {
  x  <- as.data.frame(read_excel(file_path, sheet = sh, col_types = "text"))
  nm <- names(x)
  kt <- nm[grepl(pola_tahun, nm)]
  ki <- setdiff(nm, kt)
  ki <- ki[!grepl("code|kode", ki, ignore.case = TRUE)]
  list(data = x, kol_th = kt, kol_id = ki, sheet = sh)
})
wide <- any(sapply(info, function(i) length(i$kol_th) > 0))

if (wide) {
  cat("Format terdeteksi: WIDE (tahun sebagai kolom) -> diubah ke format panel long.\n")

  long_list <- lapply(info, function(i) {
    if (length(i$kol_th) == 0) return(NULL)
    ids <- i$kol_id
    neg <- if (!is.null(kol_negara)) kol_negara else {
      cand <- ids[grepl("negara|country|nation|entity|wilayah", ids, ignore.case = TRUE)]
      if (length(cand)) cand[1] else ids[1] }
    sisa <- setdiff(ids, neg)
    ind <- if (!is.null(kol_indikator)) kol_indikator else {
      cand <- sisa[grepl("indikator|indicator|variab|series|seri", sisa, ignore.case = TRUE)]
      if (length(cand)) cand[1] else if (length(sisa)) sisa[1] else NA_character_ }

    out <- i$data[, c(neg, if (!is.na(ind)) ind, i$kol_th), drop = FALSE]
    names(out)[1] <- "Negara"
    if (!is.na(ind)) names(out)[2] <- "Indikator" else out$Indikator <- i$sheet

    out %>%
      pivot_longer(all_of(i$kol_th), names_to = "Tahun", values_to = "Nilai") %>%
      mutate(Negara    = trimws(Negara),
             Indikator = trimws(Indikator),
             Tahun     = as.integer(substr(gsub("[^0-9]", "", Tahun), 1, 4)),
             Nilai     = suppressWarnings(as.numeric(Nilai)))
  })

  long_all <- bind_rows(long_list)
  long_all$Indikator <- make.names(long_all$Indikator)

  df <- long_all %>%
    pivot_wider(names_from = Indikator, values_from = Nilai,
                values_fn = ~ mean(.x, na.rm = TRUE)) %>%
    mutate(across(where(is.numeric), ~ ifelse(is.nan(.x), NA, .x))) %>%
    as.data.frame()

  id_var   <- "Negara"
  time_var <- "Tahun"
} else {
  cat("Format terdeteksi: LONG (sudah berbentuk panel). Kolom 1 = negara, kolom 2 = tahun.\n")
  df <- as.data.frame(read_excel(file_path, sheet = sheet_no))
  names(df) <- make.names(names(df))
  id_var   <- names(df)[1]
  time_var <- names(df)[2]
}
## Format terdeteksi: WIDE (tahun sebagai kolom) -> diubah ke format panel long.
semua_var <- setdiff(names(df), c(id_var, time_var))
cat("\nVariabel/indikator tersedia:\n"); print(semua_var)
## 
## Variabel/indikator tersedia:
## [1] "Foreign.direct.investment..net.inflows....of.GDP."                  
## [2] "Poverty.headcount.ratio.at.national.poverty.lines....of.population."
## [3] "Trade....of.GDP."                                                   
## [4] "Population..total"
# Variabel dengan seluruh data kosong dibuang
kosong <- semua_var[sapply(df[semua_var], function(z) all(is.na(z)))]
if (length(kosong)) { cat("\nDibuang (semua NA):", paste(kosong, collapse = ", "), "\n")
  semua_var <- setdiff(semua_var, kosong) }

if (!is.null(y_var))  y_var  <- make.names(y_var)
if (!is.null(x_vars)) x_vars <- make.names(x_vars)
if (is.null(y_var))   y_var  <- semua_var[1]
if (is.null(x_vars))  x_vars <- setdiff(semua_var, y_var)

salah <- setdiff(c(y_var, x_vars), names(df))
if (length(salah)) stop("Variabel tidak ditemukan: ", paste(salah, collapse = ", "),
                        ". Cek daftar variabel di atas.")
if (length(x_vars) < 1) stop("Minimal butuh 1 variabel independen. Data hanya berisi: ",
                             paste(semua_var, collapse = ", "))

cat("\nID     :", id_var, "\nWaktu  :", time_var,
    "\nY      :", y_var, "\nX      :", paste(x_vars, collapse = ", "), "\n")
## 
## ID     : Negara 
## Waktu  : Tahun 
## Y      : Foreign.direct.investment..net.inflows....of.GDP. 
## X      : Poverty.headcount.ratio.at.national.poverty.lines....of.population., Trade....of.GDP., Population..total
df <- df[, c(id_var, time_var, y_var, x_vars)]
df[[time_var]] <- as.integer(df[[time_var]])
df[, c(y_var, x_vars)] <- lapply(df[, c(y_var, x_vars)], as.numeric)

n_awal <- nrow(df)
df <- na.omit(df)
cat("\nBaris sebelum/sesudah buang NA:", n_awal, "/", nrow(df), "\n")
## 
## Baris sebelum/sesudah buang NA: 375 / 84
if (nrow(df) == 0) stop("Tidak ada baris lengkap. Periksa pilihan y_var dan x_vars.")

if (pakai_log) {
  df[, c(y_var, x_vars)] <- lapply(df[, c(y_var, x_vars)], log)
}

pdat <- pdata.frame(df, index = c(id_var, time_var))
form <- as.formula(paste(y_var, "~", paste(x_vars, collapse = " + ")))
form
## Foreign.direct.investment..net.inflows....of.GDP. ~ Poverty.headcount.ratio.at.national.poverty.lines....of.population. + 
##     Trade....of.GDP. + Population..total

1.4 Struktur panel

N <- length(unique(df[[id_var]]))
T_ <- length(unique(df[[time_var]]))
cat("Jumlah individu (N):", N, "\nJumlah periode (T):", T_,
    "\nJumlah observasi   :", nrow(df), "\nPanel seimbang?    :", is.pbalanced(pdat), "\n")
## Jumlah individu (N): 10 
## Jumlah periode (T): 25 
## Jumlah observasi   : 84 
## Panel seimbang?    : FALSE

1.5 Statistik deskriptif

desk <- sapply(df[, c(y_var, x_vars)], function(x)
  c(N = length(x), Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x)))
kable(round(t(desk), 4), caption = "Statistik deskriptif")
Statistik deskriptif
N Mean SD Min Max
Foreign.direct.investment..net.inflows….of.GDP. 84 3.188100e+00 2.608400e+00 -1.8557 1.460530e+01
Poverty.headcount.ratio.at.national.poverty.lines….of.population. 84 1.383570e+01 8.953900e+00 0.0000 5.020000e+01
Trade….of.GDP. 84 9.319070e+01 4.491710e+01 32.9722 1.866758e+02
Population..total 84 2.907024e+08 4.559845e+08 133.0555 1.411100e+09

2 Uji Stasioneritas (Unit Root) Panel

Hipotesis untuk semua uji:

  • LLC, Fisher-ADF (Maddala-Wu), CIPS: H0 = terdapat unit root (tidak stasioner).
  • Catatan: LLC dan IPS mengasumsikan antar-individu independen. Jika terdapat cross-sectional dependence, gunakan CIPS (Pesaran 2007) yang robust terhadap dependensi antar-unit.

2.1 Uji cross-sectional dependence pada variabel (Pesaran CD)

Uji ini menentukan apakah uji generasi pertama (LLC/IPS) cukup, atau perlu CIPS.

vars_all <- c(y_var, x_vars)
cd_var <- do.call(rbind, lapply(vars_all, function(v) {
  r <- tryCatch(pcdtest(pdat[[v]] ~ 1, data = pdat, test = "cd"),
                error = function(e) NULL)
  if (is.null(r)) return(data.frame(Variabel = v, CD = NA, p.value = NA, Keputusan = "gagal dihitung"))
  data.frame(Variabel = v, CD = round(unname(r$statistic), 4),
             p.value = round(r$p.value, 4),
             Keputusan = ifelse(r$p.value < alpha, "Ada CSD -> pakai CIPS", "Tidak ada CSD (independen)"))
}))
kable(cd_var, caption = "Pesaran CD pada tiap variabel")
Pesaran CD pada tiap variabel
Variabel CD p.value Keputusan
Foreign.direct.investment..net.inflows….of.GDP. NA NA gagal dihitung
Poverty.headcount.ratio.at.national.poverty.lines….of.population. NA NA gagal dihitung
Trade….of.GDP. NA NA gagal dihitung
Population..total NA NA gagal dihitung

2.2 Fungsi bantu uji akar unit

uji_ur <- function(x, test, exo = "intercept") {
  r <- tryCatch(purtest(x, test = test, exo = exo, lags = "AIC", pmax = 2),
                error = function(e) NULL)
  if (is.null(r)) return(c(Stat = NA, p = NA))
  c(Stat = unname(r$statistic$statistic), p = unname(r$statistic$p.value))
}

uji_cips <- function(x, type = "drift") {
  r <- tryCatch(cipstest(x, lags = 1, type = type, model = "cmg", truncated = FALSE),
                error = function(e) NULL)
  if (is.null(r)) return(c(Stat = NA, p = NA))
  c(Stat = unname(r$statistic), p = unname(r$p.value))
}

tabel_ur <- function(data_pdat, vars, exo = "intercept", type = "drift") {
  out <- list()
  for (v in vars) {
    x <- data_pdat[[v]]
    llc <- uji_ur(x, "levinlin", exo)
    ips <- uji_ur(x, "ips", exo)
    mw  <- uji_ur(x, "madwu", exo)
    cps <- uji_cips(x, type)
    out[[v]] <- data.frame(
      Variabel = v,
      LLC_stat = llc["Stat"], LLC_p = llc["p"],
      IPS_stat = ips["Stat"], IPS_p = ips["p"],
      Fisher_ADF_stat = mw["Stat"], Fisher_ADF_p = mw["p"],
      CIPS_stat = cps["Stat"], CIPS_p = cps["p"],
      row.names = NULL)
  }
  res <- do.call(rbind, out)
  res[, -1] <- round(res[, -1], 4)
  res
}

2.3 Pada level

ur_level <- tabel_ur(pdat, vars_all)
kable(ur_level, caption = "Uji unit root pada LEVEL (intercept)")
Uji unit root pada LEVEL (intercept)
Variabel LLC_stat LLC_p IPS_stat IPS_p Fisher_ADF_stat Fisher_ADF_p CIPS_stat CIPS_p
Foreign.direct.investment..net.inflows….of.GDP. Foreign.direct.investment..net.inflows….of.GDP. NA NA NA NA NA NA NA NA
Poverty.headcount.ratio.at.national.poverty.lines….of.population. Poverty.headcount.ratio.at.national.poverty.lines….of.population. NA NA NA NA NA NA NA NA
Trade….of.GDP. Trade….of.GDP. NA NA NA NA NA NA NA NA
Population..total Population..total NA NA NA NA NA NA NA NA

2.4 Pada first difference

pdat_d <- pdata.frame(
  df %>% group_by(.data[[id_var]]) %>%
    mutate(across(all_of(vars_all), ~ . - dplyr::lag(.))) %>%
    ungroup() %>% na.omit(),
  index = c(id_var, time_var))
ur_diff <- tabel_ur(pdat_d, vars_all)
kable(ur_diff, caption = "Uji unit root pada FIRST DIFFERENCE (intercept)")
Uji unit root pada FIRST DIFFERENCE (intercept)
Variabel LLC_stat LLC_p IPS_stat IPS_p Fisher_ADF_stat Fisher_ADF_p CIPS_stat CIPS_p
Foreign.direct.investment..net.inflows….of.GDP. Foreign.direct.investment..net.inflows….of.GDP. NA NA NA NA NA NA NA NA
Poverty.headcount.ratio.at.national.poverty.lines….of.population. Poverty.headcount.ratio.at.national.poverty.lines….of.population. NA NA NA NA NA NA NA NA
Trade….of.GDP. Trade….of.GDP. NA NA NA NA NA NA NA NA
Population..total Population..total NA NA NA NA NA NA NA NA

2.5 Ringkasan keputusan stasioneritas

ringkas <- function(tab, label) {
  data.frame(
    Variabel = tab$Variabel,
    LLC  = ifelse(tab$LLC_p < alpha, "Stasioner", "Tidak"),
    IPS  = ifelse(tab$IPS_p < alpha, "Stasioner", "Tidak"),
    Fisher_ADF = ifelse(tab$Fisher_ADF_p < alpha, "Stasioner", "Tidak"),
    CIPS = ifelse(tab$CIPS_p < alpha, "Stasioner", "Tidak"),
    Data = label)
}
kable(rbind(ringkas(ur_level, "Level"), ringkas(ur_diff, "Diff 1")),
      caption = "Ringkasan keputusan (alpha = 5%)")
Ringkasan keputusan (alpha = 5%)
Variabel LLC IPS Fisher_ADF CIPS Data
Foreign.direct.investment..net.inflows….of.GDP. NA NA NA NA Level
Poverty.headcount.ratio.at.national.poverty.lines….of.population. NA NA NA NA Level
Trade….of.GDP. NA NA NA NA Level
Population..total NA NA NA NA Level
Foreign.direct.investment..net.inflows….of.GDP. NA NA NA NA Diff 1
Poverty.headcount.ratio.at.national.poverty.lines….of.population. NA NA NA NA Diff 1
Trade….of.GDP. NA NA NA NA Diff 1
Population..total NA NA NA NA Diff 1

Catatan interpretasi: jika variabel tidak stasioner pada level tetapi stasioner pada first difference, variabel terintegrasi orde 1 (I(1)). Pada regresi panel statis, pertimbangkan memakai data first difference atau uji kointegrasi (Pedroni/Kao) agar regresi tidak lancung (spurious). Jika CIPS_p bernilai NA, kemungkinan panel tidak seimbang (CIPS di plm butuh panel seimbang).


3 Estimasi Model Panel Statis

# Diagnosis awal: jumlah variabel vs jumlah negara, dan rank matriks X
cat("Jumlah negara (N):", N, "| Jumlah variabel independen (K):", length(x_vars), "\n")
## Jumlah negara (N): 10 | Jumlah variabel independen (K): 3
Xm <- model.matrix(form, data = df)[, -1, drop = FALSE]
cat("Rank matriks X:", qr(Xm)$rank, "dari", ncol(Xm), "kolom\n")
## Rank matriks X: 3 dari 3 kolom
if (qr(Xm)$rank < ncol(Xm)) {
  cat("PERINGATAN: ada multikolinearitas sempurna. Variabel berikut kombinasi variabel lain:\n")
  print(alias(lm(form, data = df))$Complete)
}
if (length(x_vars) >= N - 1) cat("PERINGATAN: K terlalu besar dibanding N; kurangi variabel X.\n")

pool <- plm(form, data = pdat, model = "pooling")   # Common Effect Model (CEM)
fe   <- plm(form, data = pdat, model = "within")    # Fixed Effect Model (FEM)

# REM: coba beberapa metode estimasi komponen error jika yang pertama gagal
re <- NULL; metode_re <- NA
for (m in c("swar", "walhus", "amemiya", "nerlove")) {
  re <- tryCatch(plm(form, data = pdat, model = "random", random.method = m),
                 error = function(e) NULL)
  if (!is.null(re)) { metode_re <- m; break }
}
re_ok <- !is.null(re)
if (re_ok) {
  cat("REM berhasil diestimasi dengan metode:", metode_re, "\n")
} else {
  cat("REM GAGAL diestimasi (matriks singular). Perbaiki variabel X (lihat diagnosis di atas).",
      "Analisis dilanjutkan tanpa REM (hanya CEM dan FEM).\n")
}
## REM berhasil diestimasi dengan metode: amemiya
summary(pool)
## Pooling Model
## 
## Call:
## plm(formula = form, data = pdat, model = "pooling")
## 
## Unbalanced Panel: n = 10, T = 1-25, N = 84
## 
## Residuals:
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -5.0735 -0.9672 -0.0789  0.5542  9.3309 
## 
## Coefficients:
##                                                                        Estimate
## (Intercept)                                                         -1.2176e+00
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.  8.2686e-02
## Trade....of.GDP.                                                     3.1153e-02
## Population..total                                                    1.2333e-09
##                                                                      Std. Error
## (Intercept)                                                          9.2290e-01
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.  2.8648e-02
## Trade....of.GDP.                                                     6.8866e-03
## Population..total                                                    6.9734e-10
##                                                                     t-value
## (Intercept)                                                         -1.3194
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.  2.8863
## Trade....of.GDP.                                                     4.5238
## Population..total                                                    1.7686
##                                                                      Pr(>|t|)
## (Intercept)                                                           0.19081
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.   0.00501
## Trade....of.GDP.                                                    2.084e-05
## Population..total                                                     0.08077
##                                                                        
## (Intercept)                                                            
## Poverty.headcount.ratio.at.national.poverty.lines....of.population. ** 
## Trade....of.GDP.                                                    ***
## Population..total                                                   .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    564.73
## Residual Sum of Squares: 397.36
## R-Squared:      0.29637
## Adj. R-Squared: 0.26998
## F-statistic: 11.232 on 3 and 80 DF, p-value: 3.1574e-06
summary(fe)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = form, data = pdat, model = "within")
## 
## Unbalanced Panel: n = 10, T = 1-25, N = 84
## 
## Residuals:
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
##  -4.890  -0.835   0.000   0.799   4.853 
## 
## Coefficients:
##                                                                        Estimate
## Poverty.headcount.ratio.at.national.poverty.lines....of.population. -5.1113e-02
## Trade....of.GDP.                                                     2.4845e-03
## Population..total                                                   -1.5997e-08
##                                                                      Std. Error
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.  2.9780e-02
## Trade....of.GDP.                                                     1.4077e-02
## Population..total                                                    1.2810e-08
##                                                                     t-value
## Poverty.headcount.ratio.at.national.poverty.lines....of.population. -1.7163
## Trade....of.GDP.                                                     0.1765
## Population..total                                                   -1.2488
##                                                                     Pr(>|t|)  
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.  0.09046 .
## Trade....of.GDP.                                                     0.86041  
## Population..total                                                    0.21584  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    169.24
## Residual Sum of Squares: 161.57
## R-Squared:      0.045272
## Adj. R-Squared: -0.11609
## F-statistic: 1.12225 on 3 and 71 DF, p-value: 0.34594
if (re_ok) summary(re) else cat("REM tidak tersedia.\n")
## Oneway (individual) effect Random Effect Model 
##    (Amemiya's transformation)
## 
## Call:
## plm(formula = form, data = pdat, model = "random", random.method = m)
## 
## Unbalanced Panel: n = 10, T = 1-25, N = 84
## 
## Effects:
##                  var std.dev share
## idiosyncratic  2.276   1.509 0.096
## individual    21.527   4.640 0.904
## theta:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.6908  0.9066  0.9256  0.9046  0.9351  0.9351 
## 
## Residuals:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -5.0080 -0.9403 -0.0823 -0.0563  0.5783  4.7888 
## 
## Coefficients:
##                                                                        Estimate
## (Intercept)                                                          4.6272e+00
## Poverty.headcount.ratio.at.national.poverty.lines....of.population. -2.8581e-02
## Trade....of.GDP.                                                     9.5739e-03
## Population..total                                                   -2.9315e-09
##                                                                      Std. Error
## (Intercept)                                                          2.3158e+00
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.  2.4536e-02
## Trade....of.GDP.                                                     1.3064e-02
## Population..total                                                    3.6652e-09
##                                                                     z-value
## (Intercept)                                                          1.9981
## Poverty.headcount.ratio.at.national.poverty.lines....of.population. -1.1649
## Trade....of.GDP.                                                     0.7328
## Population..total                                                   -0.7998
##                                                                     Pr(>|z|)  
## (Intercept)                                                           0.0457 *
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.   0.2441  
## Trade....of.GDP.                                                      0.4637  
## Population..total                                                     0.4238  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    193.81
## Residual Sum of Squares: 177.8
## R-Squared:      0.085642
## Adj. R-Squared: 0.051354
## Chisq: 2.54418 on 3 DF, p-value: 0.46736

4 Pemilihan Model Terbaik

4.1 Uji Chow (CEM vs FEM)

H0: CEM lebih baik. H1: FEM lebih baik.

chow <- pFtest(fe, pool)
chow
## 
##  F test for individual effects
## 
## data:  form
## F = 11.512, df1 = 9, df2 = 71, p-value = 6.529e-11
## alternative hypothesis: significant effects

4.2 Uji Hausman (FEM vs REM)

H0: REM konsisten (REM lebih baik). H1: FEM lebih baik.

if (re_ok) {
  haus <- phtest(fe, re)
} else {
  haus <- list(p.value = NA_real_)
  cat("Uji Hausman tidak dapat dihitung karena REM tidak tersedia.\n")
}
haus
## 
##  Hausman Test
## 
## data:  form
## chisq = 3.0984, df = 3, p-value = 0.3767
## alternative hypothesis: one model is inconsistent

4.3 Uji Lagrange Multiplier Breusch-Pagan (CEM vs REM)

H0: CEM lebih baik. H1: REM lebih baik.

lmbp <- plmtest(pool, type = "bp", effect = "individual")
lmbp
## 
##  Lagrange Multiplier Test - (Breusch-Pagan)
## 
## data:  form
## chisq = 10.591, df = 1, p-value = 0.001136
## alternative hypothesis: significant effects

4.4 Kesimpulan pemilihan model

pilih <- if (chow$p.value < alpha) {
  if (!re_ok || haus$p.value < alpha) "Fixed Effect Model (FEM)" else "Random Effect Model (REM)"
} else {
  if (re_ok && lmbp$p.value < alpha) "Random Effect Model (REM)" else "Common Effect Model (CEM)"
}
final <- switch(pilih,
                "Fixed Effect Model (FEM)" = fe,
                "Random Effect Model (REM)" = re,
                "Common Effect Model (CEM)" = pool)
kable(data.frame(
  Uji = c("Chow", "Hausman", "LM Breusch-Pagan"),
  p.value = round(c(chow$p.value, haus$p.value, lmbp$p.value), 4)),
  caption = "Ringkasan uji pemilihan model")
Ringkasan uji pemilihan model
Uji p.value
F Chow 0.0000
Hausman 0.3767
chisq LM Breusch-Pagan 0.0011
cat("\nModel terpilih:", pilih, "\n")
## 
## Model terpilih: Random Effect Model (REM)

5 Uji Asumsi Klasik Data Panel

5.1 Normalitas residual

H0: residual berdistribusi normal.

res_final <- as.numeric(residuals(final))
jb <- jarque.bera.test(res_final)
sw <- if (length(res_final) <= 5000) shapiro.test(res_final) else NULL
jb
## 
##  Jarque Bera Test
## 
## data:  res_final
## X-squared = 18.078, df = 2, p-value = 0.0001187
sw
## 
##  Shapiro-Wilk normality test
## 
## data:  res_final
## W = 0.95563, p-value = 0.005624
hist(res_final, breaks = 20, main = "Histogram Residual", xlab = "Residual", col = "grey80")

qqnorm(res_final); qqline(res_final)

5.2 Multikolinearitas (VIF dan korelasi)

Nilai VIF < 10 (ketat: < 5) menunjukkan tidak ada multikolinearitas serius.

if (length(x_vars) > 1) {
  vif_val <- car::vif(lm(form, data = df))
  kable(data.frame(Variabel = names(vif_val), VIF = round(as.numeric(vif_val), 3)),
        caption = "Variance Inflation Factor")
  kable(round(cor(df[, x_vars]), 3), caption = "Matriks korelasi antar variabel independen")
} else {
  cat("Hanya satu variabel independen; VIF tidak relevan.")
}
Matriks korelasi antar variabel independen
Poverty.headcount.ratio.at.national.poverty.lines….of.population. Trade….of.GDP. Population..total
Poverty.headcount.ratio.at.national.poverty.lines….of.population. 1.000 0.196 -0.300
Trade….of.GDP. 0.196 1.000 -0.612
Population..total -0.300 -0.612 1.000

5.3 Heteroskedastisitas (varians residual tidak konstan)

a. Breusch-Pagan (pooled) - H0: homoskedastis.

bp <- bptest(form, data = pdat, studentize = FALSE)
bp
## 
##  Breusch-Pagan test
## 
## data:  form
## BP = 39.014, df = 3, p-value = 1.724e-08

b. Modified Wald (groupwise heteroskedasticity, untuk FEM) - H0: homoskedastis antar unit (sigma_i^2 = sigma^2).

modified_wald <- function(model) {
  e   <- as.numeric(residuals(model))
  id  <- as.character(index(model)[[1]])
  s2i <- tapply(e, id, function(z) mean(z^2))
  Ti  <- tapply(e, id, length)
  Vi  <- sapply(names(s2i), function(g) {
    z <- e[id == g]
    sum((z^2 - s2i[[g]])^2) / (length(z) * (length(z) - 1))
  })
  s2  <- mean(s2i)
  W   <- sum((s2i - s2)^2 / Vi)
  df_ <- length(s2i)
  list(statistic = W, df = df_, p.value = pchisq(W, df_, lower.tail = FALSE))
}
mw <- modified_wald(fe)
cat("Modified Wald chi2 =", round(mw$statistic, 4), "| df =", mw$df,
    "| p-value =", round(mw$p.value, 4), "\n")
## Modified Wald chi2 = NaN | df = 10 | p-value = NaN

5.4 Autokorelasi / korelasi serial

a. Wooldridge - H0: tidak ada autokorelasi orde pertama.

pwar <- pwartest(fe)
pwar
## 
##  Wooldridge's test for serial correlation in FE panels
## 
## data:  fe
## F = 20.4, df1 = 1, df2 = 72, p-value = 2.403e-05
## alternative hypothesis: serial correlation

b. Breusch-Godfrey/Wooldridge panel

pbg <- pbgtest(fe)
pbg
## 
##  Breusch-Godfrey/Wooldridge test for serial correlation in panel models
## 
## data:  form
## chisq = 8.6472, df = 1, p-value = 0.003276
## alternative hypothesis: serial correlation in idiosyncratic errors

5.5 Cross-sectional dependence pada residual

a. Pesaran CD - H0: tidak ada dependensi antar-unit.

cd1 <- pcdtest(final, test = "cd")
cd1
## 
##  Pesaran CD test for cross-sectional dependence in panels
## 
## data:  Foreign.direct.investment..net.inflows....of.GDP. ~ Poverty.headcount.ratio.at.national.poverty.lines....of.population. +     Trade....of.GDP. + Population..total
## z = 1.4075, p-value = 0.1593
## alternative hypothesis: cross-sectional dependence

b. Breusch-Pagan LM (cocok bila T > N)

cd2 <- pcdtest(fe, test = "lm")
cd2
## 
##  Breusch-Pagan LM test for cross-sectional dependence in panels
## 
## data:  Foreign.direct.investment..net.inflows....of.GDP. ~ Poverty.headcount.ratio.at.national.poverty.lines....of.population. +     Trade....of.GDP. + Population..total
## chisq = 21.767, df = 13, p-value = 0.05907
## alternative hypothesis: cross-sectional dependence

5.6 Ringkasan uji asumsi

asumsi <- data.frame(
  Uji = c("Jarque-Bera (normalitas)", "Breusch-Pagan (heteroskedastisitas)",
          "Modified Wald (heteroskedastisitas grup)", "Wooldridge (autokorelasi)",
          "Pesaran CD (cross-sectional dependence)"),
  p.value = round(c(jb$p.value, bp$p.value, mw$p.value, pwar$p.value, cd1$p.value), 4))
asumsi$Keputusan <- ifelse(asumsi$p.value < alpha, "Asumsi dilanggar", "Asumsi terpenuhi")
kable(asumsi, caption = "Ringkasan uji asumsi (alpha = 5%)")
Ringkasan uji asumsi (alpha = 5%)
Uji p.value Keputusan
Jarque-Bera (normalitas) 0.0001 Asumsi dilanggar
Breusch-Pagan (heteroskedastisitas) 0.0000 Asumsi dilanggar
Modified Wald (heteroskedastisitas grup) NaN NA
Wooldridge (autokorelasi) 0.0000 Asumsi dilanggar
Pesaran CD (cross-sectional dependence) 0.1593 Asumsi terpenuhi

6 Uji Heterogenitas

6.1 Heterogenitas individu (efek individu / poolability)

Menguji apakah intersep (efek individu) berbeda antar negara. H0: tidak ada heterogenitas individu (pooling dapat dipakai). Ini setara dengan uji Chow di atas.

pFtest(fe, pool)
## 
##  F test for individual effects
## 
## data:  form
## F = 11.512, df1 = 9, df2 = 71, p-value = 6.529e-11
## alternative hypothesis: significant effects

6.2 Heterogenitas slope (poolability / Swamy)

H0: koefisien slope sama untuk semua negara (homogen). Jika ditolak, koefisien berbeda antar negara.

6.3 Heterogenitas varians (heteroskedastisitas antar unit)

Sudah diuji lewat Modified Wald dan Breusch-Pagan di atas. Visualisasi:

dres <- data.frame(Negara = as.character(index(final)[[1]]), Residual = as.numeric(residuals(final)))
ggplot(dres, aes(x = Negara, y = Residual)) +
  geom_boxplot(fill = "grey85") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  labs(title = "Sebaran residual per negara", x = NULL)

6.4 Efek individu hasil estimasi FEM

fx <- fixef(fe)
kable(data.frame(Negara = names(fx), Efek_Individu = round(as.numeric(fx), 4)),
      caption = "Intersep spesifik tiap negara (FEM)")
Intersep spesifik tiap negara (FEM)
Negara Efek_Individu
CAMBODIA 8.9826
CHINA 24.7698
FIJI 7.0012
INDONESIA 6.1427
MALAYSIA 4.0070
MONGOLIA 15.7067
PHILIPINA 3.8656
SOLOMON ISLAND 2.4128
THAILAND 2.7512
VIETNAM 6.5307

7 Estimasi Akhir dengan Standar Error Robust

Jika terdapat heteroskedastisitas, autokorelasi, atau cross-sectional dependence, gunakan standar error robust agar inferensi valid.

cat("Model terpilih:", pilih, "\n\n")
## Model terpilih: Random Effect Model (REM)
cat("--- Standar error konvensional ---\n")
## --- Standar error konvensional ---
print(coeftest(final))
## 
## t test of coefficients:
## 
##                                                                        Estimate
## (Intercept)                                                          4.6272e+00
## Poverty.headcount.ratio.at.national.poverty.lines....of.population. -2.8581e-02
## Trade....of.GDP.                                                     9.5739e-03
## Population..total                                                   -2.9315e-09
##                                                                      Std. Error
## (Intercept)                                                          2.3158e+00
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.  2.4536e-02
## Trade....of.GDP.                                                     1.3064e-02
## Population..total                                                    3.6652e-09
##                                                                     t value
## (Intercept)                                                          1.9981
## Poverty.headcount.ratio.at.national.poverty.lines....of.population. -1.1649
## Trade....of.GDP.                                                     0.7328
## Population..total                                                   -0.7998
##                                                                     Pr(>|t|)  
## (Intercept)                                                           0.0491 *
## Poverty.headcount.ratio.at.national.poverty.lines....of.population.   0.2475  
## Trade....of.GDP.                                                      0.4658  
## Population..total                                                     0.4262  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
cat("\n--- Robust Arellano (cluster per negara; atasi heteroskedastisitas & autokorelasi) ---\n")
## 
## --- Robust Arellano (cluster per negara; atasi heteroskedastisitas & autokorelasi) ---
cat("\n--- Driscoll-Kraay (atasi heteroskedastisitas, autokorelasi, & cross-sectional dependence) ---\n")
## 
## --- Driscoll-Kraay (atasi heteroskedastisitas, autokorelasi, & cross-sectional dependence) ---

7.1 Goodness of fit

s <- summary(final)
cat("R-squared     :", round(s$r.squared["rsq"], 4),
    "\nAdj. R-squared:", round(s$r.squared["adjrsq"], 4),
    "\nF-statistic   :", round(s$fstatistic$statistic, 4),
    "(p =", round(s$fstatistic$p.value, 4), ")\n")
## R-squared     : 0.0856 
## Adj. R-squared: 0.0514 
## F-statistic   : 2.5442 (p = 0.4674 )

8 Kesimpulan

Tulis kesimpulan Anda berdasarkan keluaran di atas:

  1. Stasioneritas: variabel yang stasioner pada level/first difference (LLC, IPS, Fisher-ADF, CIPS).
  2. Model terbaik: hasil Chow, Hausman, dan LM.
  3. Asumsi klasik: normalitas, multikolinearitas, heteroskedastisitas, autokorelasi, dan CSD.
  4. Heterogenitas: efek individu, slope, dan varians antar negara.
  5. Interpretasi koefisien dari model akhir dengan standar error robust.
sessionInfo()
## R version 4.6.1 (2026-06-24 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26200)
## 
## Matrix products: default
##   LAPACK version 3.12.1
## 
## locale:
## [1] LC_COLLATE=English_Indonesia.utf8  LC_CTYPE=English_Indonesia.utf8   
## [3] LC_MONETARY=English_Indonesia.utf8 LC_NUMERIC=C                      
## [5] LC_TIME=English_Indonesia.utf8    
## 
## time zone: Asia/Makassar
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ggplot2_4.0.4   knitr_1.52      sandwich_3.1-3  tseries_0.10-63
##  [5] car_3.1-5       carData_3.0-6   lmtest_0.9-40   zoo_1.9-1      
##  [9] plm_2.6-7       tidyr_1.3.2     dplyr_1.2.1     readxl_1.6.0   
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.10        generics_0.1.4     lattice_0.22-9     digest_0.6.39     
##  [5] magrittr_2.0.5     RColorBrewer_1.1-3 evaluate_1.0.5     grid_4.6.1        
##  [9] fastmap_1.2.0      cellranger_1.1.0   jsonlite_2.0.0     Formula_1.2-6     
## [13] purrr_1.2.2        scales_1.4.0       jquerylib_0.1.4    abind_1.4-8       
## [17] Rdpack_2.6.6       cli_3.6.6          rlang_1.3.0        rbibutils_2.4.1   
## [21] miscTools_0.6-30   withr_3.0.3        cachem_1.1.0       yaml_2.3.12       
## [25] otel_0.3.0         tools_4.6.1        parallel_4.6.1     bdsmatrix_1.3-7   
## [29] maxLik_1.6-10      curl_8.0.0         vctrs_0.7.3        R6_2.6.1          
## [33] lifecycle_1.0.5    MASS_7.3-65        pkgconfig_2.0.3    gtable_0.3.6      
## [37] pillar_1.11.1      bslib_0.12.0       quantmod_0.4.29    glue_1.8.1        
## [41] Rcpp_1.1.2         collapse_2.1.9     xfun_0.60          tibble_3.3.1      
## [45] tidyselect_1.2.1   rstudioapi_0.19.0  farver_2.1.2       htmltools_0.5.9   
## [49] nlme_3.1-169       labeling_0.4.3     xts_0.14.3         rmarkdown_2.31    
## [53] compiler_4.6.1     S7_0.2.2           quadprog_1.5-8     TTR_0.24.4