Regresi data panel

Regresi linear berganda

Disertasi dengan semua uji asumsi termasuk uji stasioner, prosection independen, setelah itu heterogenitas

Hanya di panel statis


1. Memuat Packages & Import Data Excel

library(readxl)
## Warning: package 'readxl' was built under R version 4.6.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.6.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.6.2
library(stringr)
## Warning: package 'stringr' was built under R version 4.6.2
library(plm)
## Warning: package 'plm' was built under R version 4.6.2
## 
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.6.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.6.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Warning: package 'car' was built under R version 4.6.2
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.6.2
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
library(tseries)
## Warning: package 'tseries' was built under R version 4.6.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
file_path <- "C:/Users/Lenovo/Downloads/Tugas data 15 Negara_Rianti palombu.xlsx"
raw_data <- read_excel(file_path, sheet = "Data")

2. Pembentukan Data Panel Statis

kode_indikator <- c(
  "NY.GDP.MKTP.KD.ZG",
  "TG.VAL.TOTL.GD.ZS",
  "NV.AGR.TOTL.ZS",
  "NV.IND.TOTL.ZS"
)

df_panel <- raw_data %>%
  filter(`Series Code` %in% kode_indikator) %>%
  pivot_longer(
    cols = matches("\\[YR[0-9]{4}\\]$"),
    names_to = "tahun_raw",
    values_to = "nilai_raw",
    values_transform = list(nilai_raw = as.character)
  ) %>%
  mutate(
    negara = as.factor(`Country Name`),
    kode_negara = as.factor(`Country Code`),
    tahun = as.integer(str_extract(tahun_raw, "[0-9]{4}")),
    nilai = suppressWarnings(as.numeric(na_if(str_trim(nilai_raw), "..")))
  ) %>%
  select(negara, kode_negara, tahun, `Series Code`, nilai) %>%
  pivot_wider(
    names_from = `Series Code`,
    values_from = nilai
  ) %>%
  transmute(
    negara = negara,
    tahun  = tahun,
    y      = NY.GDP.MKTP.KD.ZG,
    x1     = TG.VAL.TOTL.GD.ZS,
    x2     = NV.AGR.TOTL.ZS,
    x3     = NV.IND.TOTL.ZS
  ) %>%
  drop_na()

pdata <- pdata.frame(df_panel, index = c("negara", "tahun"), drop.index = FALSE)
pdim(pdata)
## Unbalanced Panel: n = 15, T = 23-25, N = 372
summary(df_panel[, c("y", "x1", "x2", "x3")])
##        y                 x1               x2                 x3       
##  Min.   :-12.016   Min.   : 16.95   Min.   : 0.02462   Min.   :10.58  
##  1st Qu.:  2.431   1st Qu.: 40.26   1st Qu.: 2.12010   1st Qu.:27.11  
##  Median :  4.704   Median : 60.47   Median : 8.87297   Median :33.61  
##  Mean   :  4.450   Mean   : 83.55   Mean   :10.65797   Mean   :34.76  
##  3rd Qu.:  6.752   3rd Qu.:111.14   3rd Qu.:14.62238   3rd Qu.:38.73  
##  Max.   : 14.520   Max.   :343.49   Max.   :57.06919   Max.   :76.43
vars_uji <- c("y", "x1", "x2", "x3")

3. Uji Cross-Sectional Dependence (Prosection Independen) & Uji Stasioneritas (Termasuk CIPS)

for (v in vars_uji) {
  cat("\n=== Pesaran CD Test:", v, "===\n")
  print(pcdtest(pdata[[v]], test = "cd"))
}
## 
## === Pesaran CD Test: y ===
## 
##  Pesaran CD test for cross-sectional dependence in panels
## 
## data:  pdata[[v]]
## z = 22.902, p-value < 2.2e-16
## alternative hypothesis: cross-sectional dependence
## 
## 
## === Pesaran CD Test: x1 ===
## 
##  Pesaran CD test for cross-sectional dependence in panels
## 
## data:  pdata[[v]]
## z = 6.4067, p-value = 1.487e-10
## alternative hypothesis: cross-sectional dependence
## 
## 
## === Pesaran CD Test: x2 ===
## 
##  Pesaran CD test for cross-sectional dependence in panels
## 
## data:  pdata[[v]]
## z = 25.384, p-value < 2.2e-16
## alternative hypothesis: cross-sectional dependence
## 
## 
## === Pesaran CD Test: x3 ===
## 
##  Pesaran CD test for cross-sectional dependence in panels
## 
## data:  pdata[[v]]
## z = 3.5778, p-value = 0.0003465
## alternative hypothesis: cross-sectional dependence
has_cips <- exists("cipstest", where = asNamespace("plm"), mode = "function")

for (v in vars_uji) {
  cat("\n=== Uji Stasioneritas:", v, "===\n")
  if (has_cips) {
    print(tryCatch(
      plm::cipstest(pdata[[v]], lags = 1, type = "intercept"),
      error = function(e) e$message
    ))
  }
  print(tryCatch(
    purtest(pdata[[v]], test = "ips", exo = "intercept", lags = "AIC"),
    error = function(e) e$message
  ))
}
## 
## === Uji Stasioneritas: y ===
## [1] "'arg' should be one of \"trend\", \"drift\", \"none\""
## [1] "NA/NaN/Inf in foreign function call (arg 6)"
## 
## === Uji Stasioneritas: x1 ===
## [1] "'arg' should be one of \"trend\", \"drift\", \"none\""
## [1] "NA/NaN/Inf in foreign function call (arg 6)"
## 
## === Uji Stasioneritas: x2 ===
## [1] "'arg' should be one of \"trend\", \"drift\", \"none\""
## [1] "NA/NaN/Inf in foreign function call (arg 6)"
## 
## === Uji Stasioneritas: x3 ===
## [1] "'arg' should be one of \"trend\", \"drift\", \"none\""
## [1] "NA/NaN/Inf in foreign function call (arg 6)"

4. Uji Heterogenitas (Hanya di Panel Statis)

pooltest(y ~ x1 + x2 + x3, data = pdata, model = "within")
## 
##  F statistic
## 
## data:  y ~ x1 + x2 + x3
## F = 1.9123, df1 = 42, df2 = 312, p-value = 0.001044
## alternative hypothesis: unstability
summary(pvcm(y ~ x1 + x2 + x3, data = pdata, model = "within"))
## Oneway (individual) effect No-pooling model
## 
## Call:
## pvcm(formula = y ~ x1 + x2 + x3, data = pdata, model = "within")
## 
## Unbalanced Panel: n = 15, T = 23-25, N = 372
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -14.0821  -0.7167   0.1904   1.0804   7.7482 
## 
## Coefficients:
##   (Intercept)            x1                  x2                   x3          
##  Min.   :-53.114   Min.   :-0.193705   Min.   :-298.15051   Min.   :-1.15084  
##  1st Qu.:-14.971   1st Qu.:-0.038124   1st Qu.:  -0.66565   1st Qu.:-0.04347  
##  Median : -5.156   Median :-0.007968   Median :  -0.01387   Median : 0.29891  
##  Mean   : -3.639   Mean   : 0.020100   Mean   : -18.85125   Mean   : 0.30885  
##  3rd Qu.:  4.228   3rd Qu.: 0.024834   3rd Qu.:   0.50856   3rd Qu.: 0.48315  
##  Max.   : 58.739   Max.   : 0.480274   Max.   :  14.16437   Max.   : 1.52898  
## 
## Total Sum of Squares: 2893300
## Residual Sum of Squares: 2047.7
## Multiple R-Squared: 0.99929

5. Estimasi & Pemilihan Model (Hanya di Panel Statis)

cem <- plm(y ~ x1 + x2 + x3, data = pdata, model = "pooling")
fem <- plm(y ~ x1 + x2 + x3, data = pdata, model = "within")
rem <- plm(y ~ x1 + x2 + x3, data = pdata, model = "random")

pFtest(fem, cem)
## 
##  F test for individual effects
## 
## data:  y ~ x1 + x2 + x3
## F = 8.7887, df1 = 14, df2 = 354, p-value < 2.2e-16
## alternative hypothesis: significant effects
phtest(fem, rem)
## 
##  Hausman Test
## 
## data:  y ~ x1 + x2 + x3
## chisq = 7.7223, df = 3, p-value = 0.05211
## alternative hypothesis: one model is inconsistent
plmtest(cem, type = "bp")
## 
##  Lagrange Multiplier Test - (Breusch-Pagan)
## 
## data:  y ~ x1 + x2 + x3
## chisq = 171.97, df = 1, p-value < 2.2e-16
## alternative hypothesis: significant effects
summary(fem)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = y ~ x1 + x2 + x3, data = pdata, model = "within")
## 
## Unbalanced Panel: n = 15, T = 23-25, N = 372
## 
## Residuals:
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -15.054  -0.991   0.350   1.464   9.277 
## 
## Coefficients:
##     Estimate Std. Error t-value  Pr(>|t|)    
## x1 0.0197045  0.0073575  2.6782  0.007748 ** 
## x2 0.3414071  0.0470384  7.2581 2.506e-12 ***
## x3 0.0296275  0.0464514  0.6378  0.524005    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    3182.4
## Residual Sum of Squares: 2574.8
## R-Squared:      0.19092
## Adj. R-Squared: 0.15207
## F-statistic: 27.8446 on 3 and 354 DF, p-value: 3.4464e-16

6. Uji Asumsi (Hanya di Panel Statis)

pbgtest(fem)
## 
##  Breusch-Godfrey/Wooldridge test for serial correlation in panel models
## 
## data:  y ~ x1 + x2 + x3
## chisq = 68.009, df = 23, p-value = 2.458e-06
## alternative hypothesis: serial correlation in idiosyncratic errors
ols_fe <- lm(y ~ x1 + x2 + x3 + factor(negara), data = df_panel)
bptest(ols_fe)
## 
##  studentized Breusch-Pagan test
## 
## data:  ols_fe
## BP = 41.596, df = 17, p-value = 0.0007669
pcdtest(fem, test = "cd")
## 
##  Pesaran CD test for cross-sectional dependence in panels
## 
## data:  y ~ x1 + x2 + x3
## z = 19.028, p-value < 2.2e-16
## alternative hypothesis: cross-sectional dependence
vif(lm(y ~ x1 + x2 + x3, data = df_panel))
##       x1       x2       x3 
## 1.028926 1.081271 1.058720
res_mod <- as.numeric(residuals(fem))
shapiro.test(res_mod)
## 
##  Shapiro-Wilk normality test
## 
## data:  res_mod
## W = 0.90362, p-value = 1.263e-14
jarque.bera.test(res_mod)
## 
##  Jarque Bera Test
## 
## data:  res_mod
## X-squared = 681.06, df = 2, p-value < 2.2e-16

7. Robust Standard Error

coeftest(fem, vcov = function(x) vcovSCC(x, type = "HC0", cluster = "group"))
## 
## t test of coefficients:
## 
##     Estimate Std. Error t value  Pr(>|t|)    
## x1 0.0197045  0.0049032  4.0187 7.152e-05 ***
## x2 0.3414071  0.0520606  6.5579 1.935e-10 ***
## x3 0.0296275  0.0515351  0.5749    0.5657    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1