1. Load Packages & Import Data

pkgs <- c("readxl", "dplyr", "tidyr", "plm", "lmtest", "car", "tseries")
inst <- pkgs[!pkgs %in% installed.packages()[, "Package"]]
if (length(inst) > 0) install.packages(inst)

library(readxl)
library(dplyr)
library(tidyr)
library(plm)
library(lmtest)
library(car)
library(tseries)

file_path <- "C:\\Users\\AXIOO\\Downloads\\Data WDI 15  Negara.xlsx"

print(excel_sheets(file_path))
## [1] "Data"              "Series - Metadata"
df_raw <- read_excel(file_path, sheet = 1)

glimpse(df_raw)
## Rows: 65
## Columns: 27
## $ Negara    <chr> "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Japan",…
## $ Indikator <chr> "Official exchange rate (LCU per US$, period average)", "Emp…
## $ `2000`    <chr> "8421.7749999999996", "16.298924186853299", "2.5030000000000…
## $ `2001`    <chr> "10260.85", "17.5538297555191", "2.4660000000000002", "402.6…
## $ `2002`    <chr> "9311.1916666666602", "17.5223213154833", "2.435999999999999…
## $ `2003`    <chr> "8577.1333333333296", "16.5392498739517", "2.41", "419.82597…
## $ `2004`    <chr> "8938.85", "16.7925260494466", "2.415", "464.71529531415172"…
## $ `2005`    <chr> "9704.7416666666595", "17.521311201506499", "2.452", "490.71…
## $ `2006`    <chr> "9159.3166666666602", "17.341138087549801", "2.4569999999999…
## $ `2007`    <chr> "9141", "17.5231849976091", "2.476", "535.35285953078244", "…
## $ `2008`    <chr> "9698.9624999999996", "18.084893515546799", "2.44", "558.932…
## $ `2009`    <chr> "10389.9375", "17.943169422561098", "2.4420000000000002", "5…
## $ `2010`    <chr> "9090.4333333333307", "18.2976528811745", "2.462000000000000…
## $ `2011`    <chr> "8770.4333333333307", "19.597561149853501", "2.5150000000000…
## $ `2012`    <chr> "9386.6291666666602", "20.6570209800248", "2.513999999999999…
## $ `2013`    <chr> "10461.24", "21.067287422428901", "2.4409999999999998", "795…
## $ `2014`    <chr> "11865.2112962963", "21.498282238903901", "2.392999999999999…
## $ `2015`    <chr> "13389.412936507901", "22.121883073933901", "2.3460000000000…
## $ `2016`    <chr> "13308.3268020541", "21.810724659761899", "2.310999999999999…
## $ `2017`    <chr> "13380.8338788891", "22.122031235954701", "2.253000000000000…
## $ `2018`    <chr> "14236.938773481699", "22.384756200108601", "2.2189999999999…
## $ `2019`    <chr> "14147.671360545401", "22.401234240839401", "2.2050000000000…
## $ `2020`    <chr> "14582.203467817701", "21.634404729147601", "2.1890000000000…
## $ `2021`    <chr> "14308.1439011896", "21.761430837269401", "2.165999999999999…
## $ `2022`    <chr> "14849.8539362104", "21.8679677163131", "2.149", "1352.96160…
## $ `2023`    <chr> "15236.884662050599", "21.951549784263001", "2.1269999999999…
## $ `2024`    <chr> "15855.448286671", "22.286172354600499", "2.1150000000000002…
print(names(df_raw))
##  [1] "Negara"    "Indikator" "2000"      "2001"      "2002"      "2003"     
##  [7] "2004"      "2005"      "2006"      "2007"      "2008"      "2009"     
## [13] "2010"      "2011"      "2012"      "2013"      "2014"      "2015"     
## [19] "2016"      "2017"      "2018"      "2019"      "2020"      "2021"     
## [25] "2022"      "2023"      "2024"

2. Reshaping Data WDI & Pemetaan Nama Variabel Aman

year_cols <- grep("^(19|20)\\d{2}(\\s*\\[YR\\d{4}\\])?$", trimws(names(df_raw)), value = TRUE)

if (length(year_cols) >= 2) {
  non_year_cols <- setdiff(names(df_raw), year_cols)
  
  if (length(non_year_cols) >= 2) {
    col_id  <- non_year_cols[1]
    col_var <- non_year_cols[2]
    
    df_clean <- df_raw %>%
      mutate(
        id_val  = trimws(as.character(.data[[col_id]])),
        var_val = trimws(as.character(.data[[col_var]]))
      ) %>%
      filter(!is.na(id_val), id_val != "", !is.na(var_val), var_val != "") %>%
      select(id_val, var_val, all_of(year_cols)) %>%
      pivot_longer(
        cols = all_of(year_cols),
        names_to = "tahun_raw",
        values_to = "nilai_raw"
      ) %>%
      mutate(
        tahun = suppressWarnings(as.numeric(substr(gsub("[^0-9]", "", tahun_raw), 1, 4))),
        nilai = suppressWarnings(as.numeric(gsub(",", ".", trimws(as.character(nilai_raw)))))
      ) %>%
      filter(!is.na(tahun)) %>%
      group_by(id_val, tahun, var_val) %>%
      summarise(nilai = mean(nilai, na.rm = TRUE), .groups = "drop") %>%
      mutate(nilai = ifelse(is.nan(nilai), NA_real_, nilai)) %>%
      pivot_wider(
        id_cols = c(id_val, tahun),
        names_from = var_val,
        values_from = nilai
      ) %>%
      rename(id = id_val)
  } else {
    col_id <- non_year_cols[1]
    df_clean <- df_raw %>%
      mutate(id = trimws(as.character(.data[[col_id]]))) %>%
      filter(!is.na(id), id != "") %>%
      select(id, all_of(year_cols)) %>%
      pivot_longer(
        cols = all_of(year_cols),
        names_to = "tahun_raw",
        values_to = "y_val"
      ) %>%
      mutate(
        tahun = suppressWarnings(as.numeric(substr(gsub("[^0-9]", "", tahun_raw), 1, 4))),
        y     = suppressWarnings(as.numeric(gsub(",", ".", trimws(as.character(y_val)))))
      ) %>%
      filter(!is.na(tahun)) %>%
      select(id, tahun, y)
  }
} else {
  col_id     <- names(df_raw)[1]
  col_tahun  <- names(df_raw)[2]
  other_cols <- setdiff(names(df_raw), c(col_id, col_tahun))
  
  df_clean <- df_raw %>%
    transmute(
      id    = trimws(as.character(.data[[col_id]])),
      tahun = suppressWarnings(as.numeric(substr(gsub("[^0-9]", "", as.character(.data[[col_tahun]])), 1, 4))),
      across(
        all_of(other_cols),
        ~ suppressWarnings(as.numeric(gsub(",", ".", trimws(as.character(.x)))))
      )
    ) %>%
    filter(!is.na(id), id != "", !is.na(tahun))
}

var_candidates <- setdiff(names(df_clean), c("id", "tahun"))
var_candidates <- var_candidates[vapply(df_clean[var_candidates], is.numeric, logical(1))]

# Jika ingin menentukan Y dan X secara manual (pakai nama asli dari var_candidates):
# orig_y <- "Electric power consumption (kWh per capita)"
# orig_x <- setdiff(var_candidates, orig_y)
orig_y <- var_candidates[1]
orig_x <- var_candidates[-1]
orig_vars <- c(orig_y, orig_x)

# Buat nama variabel aman untuk R formula & pdata.frame
safe_names <- make.names(orig_vars, unique = TRUE)
var_map <- data.frame(
  safe_name = safe_names,
  orig_name = orig_vars,
  role      = c("Y (Dependen)", rep("X (Independen)", length(orig_x))),
  stringsAsFactors = FALSE
)
print(var_map)
##                                                               safe_name
## 1                           Electric.power.consumption..kWh.per.capita.
## 2 Employment.in.industry....of.total.employment...modeled.ILO.estimate.
## 3                              Fertility.rate..total..births.per.woman.
## 4                  Official.exchange.rate..LCU.per.US...period.average.
##                                                               orig_name
## 1                           Electric power consumption (kWh per capita)
## 2 Employment in industry (% of total employment) (modeled ILO estimate)
## 3                              Fertility rate, total (births per woman)
## 4                  Official exchange rate (LCU per US$, period average)
##             role
## 1   Y (Dependen)
## 2 X (Independen)
## 3 X (Independen)
## 4 X (Independen)
df_model <- df_clean %>%
  mutate(id = as.factor(id)) %>%
  arrange(id, tahun)

for (i in seq_along(orig_vars)) {
  df_model[[safe_names[i]]] <- as.numeric(df_model[[orig_vars[i]]])
}

safe_y <- safe_names[1]
safe_x <- safe_names[-1]

pdata <- pdata.frame(df_model, index = c("id", "tahun"))
print(pdim(pdata))
## Balanced Panel: n = 15, T = 25, N = 375

3. Uji Stasioneritas Data Panel

Hipotesis nol (\(H_0\)): terdapat unit root (tidak stasioner). Hipotesis alternatif (\(H_1\)): stasioner (p-value < 0.05 menolak \(H_0\)).

run_purtest <- function(vec, test_name, exo_type = "intercept") {
  clean_vec <- as.numeric(vec)
  if (length(unique(clean_vec[!is.na(clean_vec)])) < 3) {
    cat("Lewati: variasi data tidak mencukupi.\n")
    return(invisible(NULL))
  }
  out <- tryCatch(
    purtest(vec, test = test_name, exo = exo_type, lags = "AIC"),
    error = function(e) paste("Error:", e$message)
  )
  print(out)
}

cat("--- Uji IPS (Im-Pesaran-Shin) ---\n")
## --- Uji IPS (Im-Pesaran-Shin) ---
for (i in seq_along(safe_names)) {
  cat("\nVariabel:", orig_vars[i], "(", safe_names[i], ")\n")
  run_purtest(pdata[[safe_names[i]]], test_name = "ips", exo_type = "intercept")
}
## 
## Variabel: Electric power consumption (kWh per capita) ( Electric.power.consumption..kWh.per.capita. )
## [1] "Error: argument must be coercible to non-negative integer"
## 
## Variabel: Employment in industry (% of total employment) (modeled ILO estimate) ( Employment.in.industry....of.total.employment...modeled.ILO.estimate. )
## 
##  Im-Pesaran-Shin Unit-Root Test (ex. var.: Individual Intercepts)
## 
## data:  vec
## Wtbar = NA, p-value = NA
## alternative hypothesis: stationarity
## 
## 
## Variabel: Fertility rate, total (births per woman) ( Fertility.rate..total..births.per.woman. )
## 
##  Im-Pesaran-Shin Unit-Root Test (ex. var.: Individual Intercepts)
## 
## data:  vec
## Wtbar = NA, p-value = NA
## alternative hypothesis: stationarity
## 
## 
## Variabel: Official exchange rate (LCU per US$, period average) ( Official.exchange.rate..LCU.per.US...period.average. )
## [1] "Error: NA/NaN/Inf in foreign function call (arg 6)"
cat("\n--- Uji ADF-Fisher (Maddala-Wu) ---\n")
## 
## --- Uji ADF-Fisher (Maddala-Wu) ---
for (i in seq_along(safe_names)) {
  cat("\nVariabel:", orig_vars[i], "(", safe_names[i], ")\n")
  run_purtest(pdata[[safe_names[i]]], test_name = "madwu", exo_type = "intercept")
}
## 
## Variabel: Electric power consumption (kWh per capita) ( Electric.power.consumption..kWh.per.capita. )
## [1] "Error: argument must be coercible to non-negative integer"
## 
## Variabel: Employment in industry (% of total employment) (modeled ILO estimate) ( Employment.in.industry....of.total.employment...modeled.ILO.estimate. )
## 
##  Maddala-Wu Unit-Root Test (ex. var.: Individual Intercepts)
## 
## data:  vec
## chisq = 317.5, df = 30, p-value < 2.2e-16
## alternative hypothesis: stationarity
## 
## 
## Variabel: Fertility rate, total (births per woman) ( Fertility.rate..total..births.per.woman. )
## 
##  Maddala-Wu Unit-Root Test (ex. var.: Individual Intercepts)
## 
## data:  vec
## chisq = 143.57, df = 30, p-value < 2.2e-16
## alternative hypothesis: stationarity
## 
## 
## Variabel: Official exchange rate (LCU per US$, period average) ( Official.exchange.rate..LCU.per.US...period.average. )
## [1] "Error: NA/NaN/Inf in foreign function call (arg 6)"
cat("\n--- Uji LLC (Levin-Lin-Chu) ---\n")
## 
## --- Uji LLC (Levin-Lin-Chu) ---
for (i in seq_along(safe_names)) {
  cat("\nVariabel:", orig_vars[i], "(", safe_names[i], ")\n")
  run_purtest(pdata[[safe_names[i]]], test_name = "levinlin", exo_type = "intercept")
}
## 
## Variabel: Electric power consumption (kWh per capita) ( Electric.power.consumption..kWh.per.capita. )
## [1] "Error: argument must be coercible to non-negative integer"
## 
## Variabel: Employment in industry (% of total employment) (modeled ILO estimate) ( Employment.in.industry....of.total.employment...modeled.ILO.estimate. )
## 
##  Levin-Lin-Chu Unit-Root Test (ex. var.: Individual Intercepts)
## 
## data:  vec
## z = -8.4807, p-value < 2.2e-16
## alternative hypothesis: stationarity
## 
## 
## Variabel: Fertility rate, total (births per woman) ( Fertility.rate..total..births.per.woman. )
## 
##  Levin-Lin-Chu Unit-Root Test (ex. var.: Individual Intercepts)
## 
## data:  vec
## z = 6.179, p-value = 1
## alternative hypothesis: stationarity
## 
## 
## Variabel: Official exchange rate (LCU per US$, period average) ( Official.exchange.rate..LCU.per.US...period.average. )
## [1] "Error: NA/NaN/Inf in foreign function call (arg 6)"

4. Uji Cross-Sectional Dependence (Termasuk CIPS)

cips_test <- function(x, id, time, var_name = "var") {
  d <- tibble::tibble(
    id   = as.character(id),
    time = as.numeric(as.character(time)),
    x    = as.numeric(x)
  ) %>%
    dplyr::arrange(id, time) %>%
    dplyr::group_by(id) %>%
    dplyr::mutate(
      dx      = x - dplyr::lag(x, n = 1L),
      x_lag1  = dplyr::lag(x, n = 1L),
      dx_lag1 = dplyr::lag(dx, n = 1L)
    ) %>%
    dplyr::ungroup() %>%
    dplyr::group_by(time) %>%
    dplyr::mutate(
      x_bar_lag1  = mean(x_lag1, na.rm = TRUE),
      dx_bar      = mean(dx, na.rm = TRUE),
      dx_bar_lag1 = mean(dx_lag1, na.rm = TRUE)
    ) %>%
    dplyr::ungroup()

  t_stats <- numeric(0)
  for (u in unique(d$id)) {
    sub <- dplyr::filter(d, id == u)
    if (nrow(sub) >= 4) {
      fit <- tryCatch(
        stats::lm(dx ~ x_lag1 + x_bar_lag1 + dx_bar + dx_bar_lag1, data = sub),
        error = function(e) NULL
      )
      if (!is.null(fit)) {
        sm <- summary(fit)$coefficients
        if ("x_lag1" %in% rownames(sm) && is.finite(sm["x_lag1", "t value"])) {
          t_stats <- c(t_stats, sm["x_lag1", "t value"])
        }
      }
    }
  }

  if (length(t_stats) == 0) {
    cat("Variabel:", var_name, "| Tidak dapat menghitung statistik CIPS (observasi/variasi kurang).\n")
    return(invisible(NA_real_))
  }

  cips_stat <- mean(t_stats, na.rm = TRUE)
  cat(
    "Variabel:", var_name,
    "| Statistik CIPS:", round(cips_stat, 4),
    "| N unit:", length(t_stats), "\n"
  )
  invisible(cips_stat)
}

cat("--- Uji CIPS per Variabel ---\n")
## --- Uji CIPS per Variabel ---
for (i in seq_along(safe_names)) {
  cips_test(df_model[[safe_names[i]]], df_model$id, df_model$tahun, var_name = orig_vars[i])
}
## Variabel: Electric power consumption (kWh per capita) | Statistik CIPS: -1.0185 | N unit: 14 
## Variabel: Employment in industry (% of total employment) (modeled ILO estimate) | Statistik CIPS: -1.5948 | N unit: 15 
## Variabel: Fertility rate, total (births per woman) | Statistik CIPS: -0.4944 | N unit: 15 
## Variabel: Official exchange rate (LCU per US$, period average) | Statistik CIPS: -0.8539 | N unit: 14

5. Estimasi Model Panel Statis & Uji Pemilihan Model

stopifnot(length(safe_x) >= 1)

formula_panel <- stats::as.formula(paste(safe_y, "~", paste(safe_x, collapse = " + ")))

pls <- plm(formula_panel, data = pdata, model = "pooling") # Pooled OLS / Common Effect
fem <- plm(formula_panel, data = pdata, model = "within")  # Fixed Effect
rem <- plm(formula_panel, data = pdata, model = "random")  # Random Effect

summary(pls)
## Pooling Model
## 
## Call:
## plm(formula = formula_panel, data = pdata, model = "pooling")
## 
## Unbalanced Panel: n = 14, T = 21-25, N = 345
## 
## Residuals:
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
##   -3502   -1427    -344    1233    5304 
## 
## Coefficients:
##                                                                          Estimate
## (Intercept)                                                            7.5421e+03
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  7.1219e+01
## Fertility.rate..total..births.per.woman.                              -2.9494e+03
## Official.exchange.rate..LCU.per.US...period.average.                  -6.6477e-02
##                                                                        Std. Error
## (Intercept)                                                            7.4869e+02
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  2.0228e+01
## Fertility.rate..total..births.per.woman.                               1.8642e+02
## Official.exchange.rate..LCU.per.US...period.average.                   1.2940e-02
##                                                                        t-value
## (Intercept)                                                            10.0738
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.   3.5208
## Fertility.rate..total..births.per.woman.                              -15.8211
## Official.exchange.rate..LCU.per.US...period.average.                   -5.1375
##                                                                        Pr(>|t|)
## (Intercept)                                                           < 2.2e-16
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. 0.0004887
## Fertility.rate..total..births.per.woman.                              < 2.2e-16
## Official.exchange.rate..LCU.per.US...period.average.                  4.696e-07
##                                                                          
## (Intercept)                                                           ***
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. ***
## Fertility.rate..total..births.per.woman.                              ***
## Official.exchange.rate..LCU.per.US...period.average.                  ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    2831300000
## Residual Sum of Squares: 1219600000
## R-Squared:      0.56923
## Adj. R-Squared: 0.56544
## F-statistic: 150.204 on 3 and 341 DF, p-value: < 2.22e-16
summary(fem)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = formula_panel, data = pdata, model = "within")
## 
## Unbalanced Panel: n = 14, T = 21-25, N = 345
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -3477.00  -279.40     3.47   352.60  2817.71 
## 
## Coefficients:
##                                                                          Estimate
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.   57.337514
## Fertility.rate..total..births.per.woman.                              -882.478522
## Official.exchange.rate..LCU.per.US...period.average.                     0.052701
##                                                                        Std. Error
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.   13.238459
## Fertility.rate..total..births.per.woman.                               138.909147
## Official.exchange.rate..LCU.per.US...period.average.                     0.010095
##                                                                       t-value
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  4.3311
## Fertility.rate..total..births.per.woman.                              -6.3529
## Official.exchange.rate..LCU.per.US...period.average.                   5.2206
##                                                                        Pr(>|t|)
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. 1.974e-05
## Fertility.rate..total..births.per.woman.                              7.054e-10
## Official.exchange.rate..LCU.per.US...period.average.                  3.173e-07
##                                                                          
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. ***
## Fertility.rate..total..births.per.woman.                              ***
## Official.exchange.rate..LCU.per.US...period.average.                  ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    249570000
## Residual Sum of Squares: 188020000
## R-Squared:      0.24662
## Adj. R-Squared: 0.20986
## F-statistic: 35.7895 on 3 and 328 DF, p-value: < 2.22e-16
summary(rem)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = formula_panel, data = pdata, model = "random")
## 
## Unbalanced Panel: n = 14, T = 21-25, N = 345
## 
## Effects:
##                     var   std.dev share
## idiosyncratic  573239.1     757.1 0.159
## individual    3032317.7    1741.4 0.841
## theta:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.9055  0.9134  0.9134  0.9128  0.9134  0.9134 
## 
## Residuals:
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -2949.71  -387.58   -76.88     1.13   384.23  3009.06 
## 
## Coefficients:
##                                                                          Estimate
## (Intercept)                                                           3164.907906
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.   58.336992
## Fertility.rate..total..births.per.woman.                              -949.908523
## Official.exchange.rate..LCU.per.US...period.average.                     0.049135
##                                                                        Std. Error
## (Intercept)                                                            674.532576
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.   13.375854
## Fertility.rate..total..births.per.woman.                               139.925676
## Official.exchange.rate..LCU.per.US...period.average.                     0.010163
##                                                                       z-value
## (Intercept)                                                            4.6920
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  4.3614
## Fertility.rate..total..births.per.woman.                              -6.7887
## Official.exchange.rate..LCU.per.US...period.average.                   4.8345
##                                                                        Pr(>|z|)
## (Intercept)                                                           2.705e-06
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. 1.293e-05
## Fertility.rate..total..births.per.woman.                              1.132e-11
## Official.exchange.rate..LCU.per.US...period.average.                  1.335e-06
##                                                                          
## (Intercept)                                                           ***
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. ***
## Fertility.rate..total..births.per.woman.                              ***
## Official.exchange.rate..LCU.per.US...period.average.                  ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    268930000
## Residual Sum of Squares: 202680000
## R-Squared:      0.24635
## Adj. R-Squared: 0.23972
## Chisq: 111.825 on 3 DF, p-value: < 2.22e-16
cat("--- Uji Chow (PLS vs FEM) ---\n")
## --- Uji Chow (PLS vs FEM) ---
print(tryCatch(pFtest(fem, pls), error = function(e) e$message))
## 
##  F test for individual effects
## 
## data:  formula_panel
## F = 138.43, df1 = 13, df2 = 328, p-value < 2.2e-16
## alternative hypothesis: significant effects
cat("--- Uji Hausman (FEM vs REM) ---\n")
## --- Uji Hausman (FEM vs REM) ---
print(tryCatch(phtest(fem, rem), error = function(e) e$message))
## 
##  Hausman Test
## 
## data:  formula_panel
## chisq = 40.508, df = 3, p-value = 8.315e-09
## alternative hypothesis: one model is inconsistent
cat("--- Uji Breusch-Pagan LM (PLS vs REM) ---\n")
## --- Uji Breusch-Pagan LM (PLS vs REM) ---
print(tryCatch(plmtest(pls, type = "bp"), error = function(e) e$message))
## 
##  Lagrange Multiplier Test - (Breusch-Pagan)
## 
## data:  formula_panel
## chisq = 1772.7, df = 1, p-value < 2.2e-16
## alternative hypothesis: significant effects
cat("--- Uji Pesaran CD & Scaled LM pada Residual FEM ---\n")
## --- Uji Pesaran CD & Scaled LM pada Residual FEM ---
print(tryCatch(pcdtest(fem, test = "cd"), error = function(e) e$message))
## 
##  Pesaran CD test for cross-sectional dependence in panels
## 
## data:  Electric.power.consumption..kWh.per.capita. ~ Employment.in.industry....of.total.employment...modeled.ILO.estimate. +     Fertility.rate..total..births.per.woman. + Official.exchange.rate..LCU.per.US...period.average.
## z = 0.65138, p-value = 0.5148
## alternative hypothesis: cross-sectional dependence
print(tryCatch(pcdtest(fem, test = "sclm"), error = function(e) e$message))
## 
##  Scaled LM test for cross-sectional dependence in panels
## 
## data:  Electric.power.consumption..kWh.per.capita. ~ Employment.in.industry....of.total.employment...modeled.ILO.estimate. +     Fertility.rate..total..births.per.woman. + Official.exchange.rate..LCU.per.US...period.average.
## z = 68.208, p-value < 2.2e-16
## alternative hypothesis: cross-sectional dependence

6. Uji Heterogenitas

Mencakup heteroskedastisitas (varians error antar unit) dan heterogenitas slope (perbedaan koefisien antar unit pada panel statis).

cat("--- Heteroskedastisitas: Breusch-Pagan & Bartlett ---\n")
## --- Heteroskedastisitas: Breusch-Pagan & Bartlett ---
print(tryCatch(bptest(fem, studentize = TRUE), error = function(e) e$message))
## 
##  studentized Breusch-Pagan test
## 
## data:  fem
## BP = 88.842, df = 3, p-value < 2.2e-16
print(tryCatch(bartlett.test(as.numeric(residuals(fem)) ~ index(pdata)[[1]]), error = function(e) e$message))
## [1] "variable lengths differ (found for 'index(pdata)[[1]]')"
cat("--- Heterogenitas Slope: Uji Poolability ---\n")
## --- Heterogenitas Slope: Uji Poolability ---
print(tryCatch(pooltest(formula_panel, data = pdata, model = "within"), error = function(e) e$message))
## 
##  F statistic
## 
## data:  formula_panel
## F = 58.571, df1 = 39, df2 = 289, p-value < 2.2e-16
## alternative hypothesis: unstability

7. Uji Asumsi Lainnya & Robust Standard Errors

cat("--- Autokorelasi / Serial Correlation ---\n")
## --- Autokorelasi / Serial Correlation ---
print(tryCatch(pbgtest(fem), error = function(e) e$message))
## 
##  Breusch-Godfrey/Wooldridge test for serial correlation in panel models
## 
## data:  formula_panel
## chisq = 274.1, df = 21, p-value < 2.2e-16
## alternative hypothesis: serial correlation in idiosyncratic errors
print(tryCatch(pdwtest(fem), error = function(e) e$message))
## 
##  Durbin-Watson test for serial correlation in panel models
## 
## data:  formula_panel
## DW = 0.24217, p-value < 2.2e-16
## alternative hypothesis: serial correlation in idiosyncratic errors
cat("--- Multikolinearitas (VIF) ---\n")
## --- Multikolinearitas (VIF) ---
if (length(safe_x) >= 2) {
  print(tryCatch(vif(stats::lm(formula_panel, data = df_model)), error = function(e) e$message))
} else {
  cat("VIF memerlukan minimal 2 variabel independen.\n")
}
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. 
##                                                              1.440188 
##                              Fertility.rate..total..births.per.woman. 
##                                                              1.358598 
##                  Official.exchange.rate..LCU.per.US...period.average. 
##                                                              1.076588
cat("--- Normalitas Residual ---\n")
## --- Normalitas Residual ---
res_fem <- as.numeric(residuals(fem))
res_fem <- res_fem[!is.na(res_fem)]
if (length(res_fem) >= 3 && length(res_fem) <= 5000) {
  print(shapiro.test(res_fem))
}
## 
##  Shapiro-Wilk normality test
## 
## data:  res_fem
## W = 0.90642, p-value = 8.858e-14
print(tryCatch(jarque.bera.test(res_fem), error = function(e) e$message))
## 
##  Jarque Bera Test
## 
## data:  res_fem
## X-squared = 327.66, df = 2, p-value < 2.2e-16
cat("--- Robust Standard Errors (Cluster-Robust & Driscoll-Kraay) ---\n")
## --- Robust Standard Errors (Cluster-Robust & Driscoll-Kraay) ---
print(tryCatch(coeftest(fem, vcov = vcovHC(fem, type = "HC0", cluster = "group")), error = function(e) e$message))
## 
## t test of coefficients:
## 
##                                                                          Estimate
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  5.7338e+01
## Fertility.rate..total..births.per.woman.                              -8.8248e+02
## Official.exchange.rate..LCU.per.US...period.average.                   5.2701e-02
##                                                                        Std. Error
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  4.8921e+01
## Fertility.rate..total..births.per.woman.                               4.9300e+02
## Official.exchange.rate..LCU.per.US...period.average.                   6.4616e-03
##                                                                       t value
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.   1.172
## Fertility.rate..total..births.per.woman.                               -1.790
## Official.exchange.rate..LCU.per.US...period.average.                    8.156
##                                                                        Pr(>|t|)
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.   0.24203
## Fertility.rate..total..births.per.woman.                                0.07437
## Official.exchange.rate..LCU.per.US...period.average.                  7.443e-15
##                                                                          
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.    
## Fertility.rate..total..births.per.woman.                              .  
## Official.exchange.rate..LCU.per.US...period.average.                  ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
print(tryCatch(coeftest(fem, vcov = vcovSCC(fem, type = "HC0")), error = function(e) e$message))
## 
## t test of coefficients:
## 
##                                                                          Estimate
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  5.7338e+01
## Fertility.rate..total..births.per.woman.                              -8.8248e+02
## Official.exchange.rate..LCU.per.US...period.average.                   5.2701e-02
##                                                                        Std. Error
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  8.7387e+00
## Fertility.rate..total..births.per.woman.                               1.9029e+02
## Official.exchange.rate..LCU.per.US...period.average.                   4.6578e-03
##                                                                       t value
## Employment.in.industry....of.total.employment...modeled.ILO.estimate.  6.5613
## Fertility.rate..total..births.per.woman.                              -4.6376
## Official.exchange.rate..LCU.per.US...period.average.                  11.3146
##                                                                        Pr(>|t|)
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. 2.080e-10
## Fertility.rate..total..births.per.woman.                              5.098e-06
## Official.exchange.rate..LCU.per.US...period.average.                  < 2.2e-16
##                                                                          
## Employment.in.industry....of.total.employment...modeled.ILO.estimate. ***
## Fertility.rate..total..births.per.woman.                              ***
## Official.exchange.rate..LCU.per.US...period.average.                  ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1