library(readxl)
library(dplyr)
## 
## 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(plm)
## 
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
# ===== 1. Import =====
path <- "C:/Users/ADVAN/OneDrive/Dokumen/Data_Panel_WDI.xlsx"
data <- read_excel(path)

# Rapikan nama kolom (spasi/simbol -> titik)
names(data) <- make.names(names(data))

# Lihat nama kolom & isi data
print(names(data))
## [1] "Negara"                    "Kode"                     
## [3] "Tahun"                     "Y_Kemiskinan"             
## [5] "X1_Melek_Huruf"            "X2_TPT"                   
## [7] "X3_PDRB_per_kapita"        "X4_Pengeluaran_Pemerintah"
head(data)
## # A tibble: 6 × 8
##   Negara    Kode  Tahun Y_Kemiskinan X1_Melek_Huruf X2_TPT X3_PDRB_per_kapita
##   <chr>     <chr> <dbl>        <dbl>          <dbl>  <dbl>              <dbl>
## 1 Australia AUS    2000           NA             NA   6.29              2.73 
## 2 Australia AUS    2001           NA             NA   6.75              0.730
## 3 Australia AUS    2002           NA             NA   6.38              2.77 
## 4 Australia AUS    2003           NA             NA   5.93              1.91 
## 5 Australia AUS    2004           NA             NA   5.40              3.11 
## 6 Australia AUS    2005           NA             NA   5.04              1.91 
## # ℹ 1 more variable: X4_Pengeluaran_Pemerintah <dbl>
# ===== 2. Deteksi otomatis kolom negara & tahun =====
id_col   <- names(data)[grep("country|negara|economy|code", names(data), ignore.case = TRUE)][1]
time_col <- names(data)[grep("year|tahun|time", names(data), ignore.case = TRUE)][1]

cat("Kolom individu:", id_col, "\n")
## Kolom individu: Negara
cat("Kolom waktu   :", time_col, "\n")
## Kolom waktu   : Tahun
# Pastikan tahun berupa angka
data[[time_col]] <- as.numeric(as.character(data[[time_col]]))

# ===== 3. Struktur panel =====
pdata <- pdata.frame(data, index = c(id_col, time_col))
pdim(pdata)
## Balanced Panel: n = 15, T = 26, N = 390
# ===== 4. Pilih variabel =====
# Semua kolom numerik selain tahun
num_vars <- setdiff(names(data)[sapply(data, is.numeric)], time_col)
print(num_vars)
## [1] "Y_Kemiskinan"              "X1_Melek_Huruf"           
## [3] "X2_TPT"                    "X3_PDRB_per_kapita"       
## [5] "X4_Pengeluaran_Pemerintah"
# GANTI sesuai variabel Anda, contoh: Y = num_vars[1], X = sisanya
y_var  <- num_vars[1]
x_vars <- num_vars[-1]
rumus  <- as.formula(paste(y_var, "~", paste(x_vars, collapse = " + ")))
print(rumus)
## Y_Kemiskinan ~ X1_Melek_Huruf + X2_TPT + X3_PDRB_per_kapita + 
##     X4_Pengeluaran_Pemerintah
# ===== 5. Model =====
model_pooled <- plm(rumus, data = pdata, model = "pooling")
model_fe     <- plm(rumus, data = pdata, model = "within")
model_re     <- plm(rumus, data = pdata, model = "random")

summary(model_pooled)
## Pooling Model
## 
## Call:
## plm(formula = rumus, data = pdata, model = "pooling")
## 
## Unbalanced Panel: n = 7, T = 1-11, N = 29
## 
## Residuals:
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -12.320  -4.116  -1.960   0.517  29.679 
## 
## Coefficients:
##                           Estimate Std. Error t-value Pr(>|t|)   
## (Intercept)               98.21491   33.77095  2.9083 0.007709 **
## X1_Melek_Huruf            -0.95485    0.34468 -2.7702 0.010639 * 
## X2_TPT                     0.34634    0.84721  0.4088 0.686313   
## X3_PDRB_per_kapita         1.46959    0.81971  1.7928 0.085616 . 
## X4_Pengeluaran_Pemerintah -0.20441    0.16260 -1.2571 0.220816   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    4948.2
## Residual Sum of Squares: 2470.1
## R-Squared:      0.5008
## Adj. R-Squared: 0.41761
## F-statistic: 6.01934 on 4 and 24 DF, p-value: 0.0016786
summary(model_fe)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = rumus, data = pdata, model = "within")
## 
## Unbalanced Panel: n = 7, T = 1-11, N = 29
## 
## Residuals:
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -10.549  -4.598  -0.387   1.812  26.017 
## 
## Coefficients:
##                           Estimate Std. Error t-value Pr(>|t|)  
## X1_Melek_Huruf            -2.74859    1.28124 -2.1453  0.04582 *
## X2_TPT                    -1.66568    2.37957 -0.7000  0.49288  
## X3_PDRB_per_kapita         1.18560    0.95697  1.2389  0.23129  
## X4_Pengeluaran_Pemerintah -1.72908    0.69452 -2.4896  0.02279 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    2547.1
## Residual Sum of Squares: 1610.2
## R-Squared:      0.36784
## Adj. R-Squared: 0.016644
## F-statistic: 2.61848 on 4 and 18 DF, p-value: 0.069496
summary(model_re)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = rumus, data = pdata, model = "random")
## 
## Unbalanced Panel: n = 7, T = 1-11, N = 29
## 
## Effects:
##                  var std.dev share
## idiosyncratic 89.453   9.458 0.688
## individual    40.474   6.362 0.312
## theta:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.1702  0.3487  0.5347  0.4706  0.5910  0.5910 
## 
## Residuals:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -8.5820 -4.9507 -1.8171  0.1120  0.0365 28.4400 
## 
## Coefficients:
##                            Estimate Std. Error z-value Pr(>|z|)   
## (Intercept)               112.57584   43.34531  2.5972 0.009399 **
## X1_Melek_Huruf             -1.11903    0.44619 -2.5080 0.012143 * 
## X2_TPT                      0.77709    1.35101  0.5752 0.565160   
## X3_PDRB_per_kapita          1.52137    0.86664  1.7555 0.079177 . 
## X4_Pengeluaran_Pemerintah  -0.29147    0.20886 -1.3955 0.162866   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    4201.7
## Residual Sum of Squares: 2229.1
## R-Squared:      0.46968
## Adj. R-Squared: 0.38129
## Chisq: 15.7647 on 4 DF, p-value: 0.0033517
# ===== 6. Pemilihan model =====
pFtest(model_fe, model_pooled)       # Chow
## 
##  F test for individual effects
## 
## data:  rumus
## F = 1.6023, df1 = 6, df2 = 18, p-value = 0.2037
## alternative hypothesis: significant effects
phtest(model_fe, model_re)           # Hausman
## 
##  Hausman Test
## 
## data:  rumus
## chisq = 5.1868, df = 4, p-value = 0.2687
## alternative hypothesis: one model is inconsistent
plmtest(model_pooled, type = "bp")   # Breusch-Pagan LM
## 
##  Lagrange Multiplier Test - (Breusch-Pagan)
## 
## data:  rumus
## chisq = 0.60375, df = 1, p-value = 0.4372
## alternative hypothesis: significant effects
# ===== 7. Uji asumsi & robust SE =====
bptest(rumus, data = data)
## 
##  studentized Breusch-Pagan test
## 
## data:  rumus
## BP = 1.3407, df = 4, p-value = 0.8544
pbgtest(model_fe)
## 
##  Breusch-Godfrey/Wooldridge test for serial correlation in panel models
## 
## data:  rumus
## chisq = 0.82508, df = 1, p-value = 0.3637
## alternative hypothesis: serial correlation in idiosyncratic errors
coeftest(model_fe, vcov = vcovHC(model_fe, type = "HC1", cluster = "group"))
## 
## t test of coefficients:
## 
##                           Estimate Std. Error t value Pr(>|t|)  
## X1_Melek_Huruf            -2.74859    2.31199 -1.1888  0.24995  
## X2_TPT                    -1.66568    4.32172 -0.3854  0.70444  
## X3_PDRB_per_kapita         1.18560    0.89769  1.3207  0.20314  
## X4_Pengeluaran_Pemerintah -1.72908    0.90919 -1.9018  0.07333 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.