Praktikum 11

GRP

# Load Package
suppressPackageStartupMessages({
  library(sf)
  library(readxl)
  library(tidyverse)
  library(ggplot2)
  library(spdep)
  library(spatialreg)
  library(MuMIn)
  library(patchwork)
  library(corrplot)   # Visualisasi korelasi
  library(nortest)    # Uji normalitas sisaan (Anderson-Darling)
  library(car)        # Uji multikolinearitas (VIF)
  library(lmtest)     # Uji kehomogenan ragam (Breusch-Pagan)
})

# Membaca data spasial dalam format shapefile (.shp)
shp_china <- st_read("C:\\Users\\Hype\\Documents\\Semester 5\\Resgpas\\Regspas(R)\\Regspas_11\\data china\\China29\\China29.shp")
## Reading layer `China29' from data source 
##   `C:\Users\Hype\Documents\Semester 5\Resgpas\Regspas(R)\Regspas_11\data china\China29\China29.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 29 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 73.5577 ymin: 20.22264 xmax: 134.7739 ymax: 53.56086
## Geodetic CRS:  WGS 84
data_china <- read_excel("C:\\Users\\Hype\\Documents\\Semester 5\\Resgpas\\Regspas(R)\\Regspas_11\\data china\\Data_China.xlsx")

china_join <- shp_china %>%
  left_join(data_china, by = c("NAME_1" = "Region"))

head(china_join)
## Simple feature collection with 6 features and 16 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 92.76416 ymin: 20.22264 xmax: 120.8376 ymax: 42.77393
## Geodetic CRS:  WGS 84
##   ID_0 ISO NAME_0 ID_1    NAME_1 HASC_1 CCN_1 CCA_1    TYPE_1    ENGTYPE_1
## 1   49 CHN  China    1     Anhui  CN.AH     0  <NA>     Sh?ng     Province
## 2   49 CHN  China    2   Beijing  CN.BJ     0  <NA> Zhíxiáshì Municipality
## 3   49 CHN  China    3 Chongqing  CN.CQ     0  <NA> Zhíxiáshì Municipality
## 4   49 CHN  China    4    Fujian  CN.FJ     0  <NA>     Sh?ng     Province
## 5   49 CHN  China    5     Gansu  CN.GS     0  <NA>     Sh?ng     Province
## 6   49 CHN  China    6 Guangdong  CN.GD     0  <NA>     Sh?ng     Province
##   NL_NAME_1 VARNAME_1     Long      Lat   GRP Level_of_Urbanization
## 1     ??|??     ?nhu? 117.2262 31.82579 28792                 46.50
## 2     ??|??   B?ij?ng 116.4107 40.18491 87475                 86.20
## 3     ??|?? Chóngqìng 107.8748 30.05865 38914                 56.98
## 4        ??    Fújiàn 117.9815 26.08641 52763                 59.60
## 5     ??|??     G?nsù 100.9339 37.82079 21978                 38.75
## 6     ??|?? Gu?ngd?ng 113.4164 23.34225 54095                 67.40
##                         geometry
## 1 MULTIPOLYGON (((116.4263 34...
## 2 MULTIPOLYGON (((116.6669 40...
## 3 MULTIPOLYGON (((108.5419 32...
## 4 MULTIPOLYGON (((117.689 23....
## 5 MULTIPOLYGON (((97.18472 42...
## 6 MULTIPOLYGON (((110.1182 20...
GRP <- ggplot(data = china_join) +
  geom_sf(
    aes(fill = GRP),
    color = "white",
    linewidth = 0.2
  ) +
  scale_fill_viridis_c(
    option = "magma",
    name = "GRP"
  ) +
  labs(
    title = "Gross Regional Product",
    subtitle = "Data kontinu per provinsi",
    x = "Longitude",
    y = "Latitude"
  ) +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    plot.title = element_text(face = "bold", size = 14),
    plot.subtitle = element_text(size = 11)
  )

Urbanization <- ggplot(data = china_join) +
  geom_sf(
    aes(fill = `Level_of_Urbanization`),
    color = "white",
    linewidth = 0.2
  ) +
  scale_fill_viridis_c(
    option = "magma",
    name = "Level of Urbanization"
  ) +
  labs(
    title = "Level of Urbanization",
    subtitle = "Data kontinu per provinsi",
    x = "Longitude",
    y = "Latitude"
  ) +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    plot.title = element_text(face = "bold", size = 14),
    plot.subtitle = element_text(size = 11)
  )

GRP | Urbanization

# Pengecekan struktur data
str(china_join)
## Classes 'sf' and 'data.frame':   29 obs. of  17 variables:
##  $ ID_0                 : chr  "49" "49" "49" "49" ...
##  $ ISO                  : chr  "CHN" "CHN" "CHN" "CHN" ...
##  $ NAME_0               : chr  "China" "China" "China" "China" ...
##  $ ID_1                 : chr  "1" "2" "3" "4" ...
##  $ NAME_1               : chr  "Anhui" "Beijing" "Chongqing" "Fujian" ...
##  $ HASC_1               : chr  "CN.AH" "CN.BJ" "CN.CQ" "CN.FJ" ...
##  $ CCN_1                : chr  "0" "0" "0" "0" ...
##  $ CCA_1                : chr  NA NA NA NA ...
##  $ TYPE_1               : chr  "Sh?ng" "Zhíxiáshì" "Zhíxiáshì" "Sh?ng" ...
##  $ ENGTYPE_1            : chr  "Province" "Municipality" "Municipality" "Province" ...
##  $ NL_NAME_1            : chr  "??|??" "??|??" "??|??" "??" ...
##  $ VARNAME_1            : chr  "?nhu?" "B?ij?ng" "Chóngqìng" "Fújiàn" ...
##  $ Long                 : num  117 116 108 118 101 ...
##  $ Lat                  : num  31.8 40.2 30.1 26.1 37.8 ...
##  $ GRP                  : num  28792 87475 38914 52763 21978 ...
##  $ Level_of_Urbanization: num  46.5 86.2 57 59.6 38.8 ...
##  $ geometry             :sfc_MULTIPOLYGON of length 29; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:7291, 1:2] 116 116 116 116 116 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:16] "ID_0" "ISO" "NAME_0" "ID_1" ...
# Pengecekan Missing Value
colSums(is.na(china_join))
##                  ID_0                   ISO                NAME_0 
##                     0                     0                     0 
##                  ID_1                NAME_1                HASC_1 
##                     0                     0                     0 
##                 CCN_1                 CCA_1                TYPE_1 
##                     0                    29                     0 
##             ENGTYPE_1             NL_NAME_1             VARNAME_1 
##                     0                     0                     0 
##                  Long                   Lat                   GRP 
##                     0                     0                     0 
## Level_of_Urbanization              geometry 
##                     0                     0
# Boxplot - Identifikasi Pencilan (China)
par(mfrow = c(1, 2))
boxplot(china_join$GRP, main = "Sebaran GRP", col = "dodgerblue")
boxplot(china_join$Level_of_Urbanization, main = "Sebaran Urbanization", col = "dodgerblue")

par(mfrow = c(1, 1)) # Reset layout plot

# Scatter plot & uji korelasi Pearson
plot(china_join$Level_of_Urbanization, china_join$GRP,
     pch = 19, xlab = "Level of Urbanization", ylab = "GRP")

cor.test(china_join$GRP, china_join$Level_of_Urbanization)
## 
##  Pearson's product-moment correlation
## 
## data:  china_join$GRP and china_join$Level_of_Urbanization
## t = 15.118, df = 27, p-value = 1.064e-14
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.8864400 0.9744558
## sample estimates:
##       cor 
## 0.9457012

OLS

china.lm <- lm(GRP ~ Level_of_Urbanization, data = china_join)
summary(china.lm)
## 
## Call:
## lm(formula = GRP ~ Level_of_Urbanization, data = china_join)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12050.9  -4410.4   -374.8   2961.6  14948.1 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -31895.5     5197.6  -6.137 1.48e-06 ***
## Level_of_Urbanization   1400.0       92.6  15.118 1.06e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6595 on 27 degrees of freedom
## Multiple R-squared:  0.8944, Adjusted R-squared:  0.8904 
## F-statistic: 228.6 on 1 and 27 DF,  p-value: 1.064e-14
# 1. Uji Normalitas Sisaan (Anderson-Darling & Q-Q Plot)
err.china <- residuals(china.lm)
ad.test(err.china)
## 
##  Anderson-Darling normality test
## 
## data:  err.china
## A = 0.48099, p-value = 0.215
hist(err.china)

qqnorm(err.china, datax = TRUE)
qqline(rnorm(length(err.china), mean(err.china), sd(err.china)), datax = TRUE, col = "red")

# 2. Uji Kehomogenan Ragam / Heterogenitas Spasial (Breusch-Pagan)
lmtest::bptest(china.lm)
## 
##  studentized Breusch-Pagan test
## 
## data:  china.lm
## BP = 3.9868, df = 1, p-value = 0.04586

Matriks Pembobot Spasial

# Baca jarak kereta dari sheet Railway Distance
rail <- read_excel("C:\\Users\\Hype\\Documents\\Semester 5\\Resgpas\\Regspas(R)\\Regspas_11\\data china\\Data_China.xlsx", sheet = "Railway Distance", skip = 1)
## New names:
## • `` -> `...1`
rail_matrix <- as.matrix(rail[, 3:31])
rownames(rail_matrix) <- colnames(rail_matrix)

# Inverse distance (w_ij = 1/d_ij, diagonal = 0)
W_rail <- 1 / rail_matrix
diag(W_rail) <- 0

# Row-standardize
W_rail_std <- W_rail / rowSums(W_rail)

# Buat listw object
W <- mat2listw(W_rail_std, style = "W")
# Simulasi Monte Carlo untuk Moran's I dengan pembobot W (Railway Distance)
moran.mc(china_join$GRP, listw = W, nsim = 99)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  china_join$GRP 
## weights: W  
## number of simulations + 1: 100 
## 
## statistic = -0.084643, observed rank = 8, p-value = 0.92
## alternative hypothesis: greater
# Alternatif Pembobot Jarak Centroid: K-Nearest Neighbor (k = 5)
centroids_china <- st_centroid(china_join)
## Warning: st_centroid assumes attributes are constant over geometries
longlat_china <- st_coordinates(centroids_china)
W_china_knn <- nb2listw(knn2nb(knearneigh(longlat_china, k = 5, longlat = TRUE)), style = "W")
moran.test(china_join$GRP, W_china_knn, randomisation = TRUE, alternative = "greater")
## 
##  Moran I test under randomisation
## 
## data:  china_join$GRP  
## weights: W_china_knn    
## 
## Moran I statistic standard deviate = 3.6303, p-value = 0.0001416
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       0.310384053      -0.035714286       0.009089188

Morans I

col.moran <- lm.morantest(china.lm, W)
col.moran
## 
##  Global Moran I for regression residuals
## 
## data:  
## model: lm(formula = GRP ~ Level_of_Urbanization, data = china_join)
## weights: W
## 
## Moran I statistic standard deviate = 1.1234, p-value = 0.1306
## alternative hypothesis: greater
## sample estimates:
## Observed Moran I      Expectation         Variance 
##      0.010629595     -0.033552435      0.001546711
moran.test(china_join$GRP, W, randomisation = TRUE, alternative = "two.sided")
## 
##  Moran I test under randomisation
## 
## data:  china_join$GRP  
## weights: W    
## 
## Moran I statistic standard deviate = -1.2565, p-value = 0.2089
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      -0.084643090      -0.035714286       0.001516249
moran.test(china_join$Level_of_Urbanization, W, randomisation = TRUE, alternative = "two.sided")
## 
##  Moran I test under randomisation
## 
## data:  china_join$Level_of_Urbanization  
## weights: W    
## 
## Moran I statistic standard deviate = -1.5162, p-value = 0.1295
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      -0.094084264      -0.035714286       0.001481994
moran.plot(china_join$GRP, W, labels = china_join$NAME_1)

Lagrange M

china.lagrange1 <- lm.LMtests(china.lm, W, test = c("LMerr", "RLMerr", "LMlag", "RLMlag", "SARMA"))
## Please update scripts to use lm.RStests in place of lm.LMtests
summary(china.lagrange1)
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## data:  
## model: lm(formula = GRP ~ Level_of_Urbanization, data = china_join)
## test weights: listw
##  
##          statistic parameter p.value
## RSerr    0.0277967         1  0.8676
## adjRSerr 0.0726855         1  0.7875
## RSlag    0.0035418         1  0.9525
## adjRSlag 0.0484307         1  0.8258
## SARMA    0.0762273         2  0.9626

SLX, SAR, dan SDM Model

china.SLX <- lmSLX(GRP ~ Level_of_Urbanization, data = china_join, listw = W, Durbin = TRUE)
summary(china.SLX)
## 
## Call:
## lm(formula = formula(paste("y ~ ", paste(colnames(x)[-1], collapse = "+"))), 
##     data = as.data.frame(x), weights = weights)
## 
## Coefficients:
##                            Estimate    Std. Error  t value     Pr(>|t|)  
## (Intercept)                -2.335e+04   4.130e+04  -5.654e-01   5.766e-01
## Level_of_Urbanization       1.387e+03   1.139e+02   1.217e+01   3.074e-12
## lag.Level_of_Urbanization  -1.418e+02   6.801e+02  -2.086e-01   8.364e-01
# Model SAR
china.sar <- lagsarlm(GRP ~ Level_of_Urbanization, data = china_join, listw = W)
## Warning in lagsarlm(GRP ~ Level_of_Urbanization, data = china_join, listw = W): inversion of asymptotic covariance matrix failed for tol.solve = 2.22044604925031e-16 
##   reciprocal condition number = 5.98595e-18 - using numerical Hessian.
summary(china.sar, Nagelkerke = TRUE)
## 
## Call:lagsarlm(formula = GRP ~ Level_of_Urbanization, data = china_join, 
##     listw = W)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -12088.51  -4296.93   -416.98   2876.41  14986.37 
## 
## Type: lag 
## Coefficients: (numerical Hessian approximate standard errors) 
##                         Estimate Std. Error z value Pr(>|z|)
## (Intercept)           -30834.100  14146.689 -2.1796  0.02929
## Level_of_Urbanization   1397.282     95.289 14.6636  < 2e-16
## 
## Rho: -0.020318, LR test value: 0.0034465, p-value: 0.95319
## Approximate (numerical Hessian) standard error: 0.25344
##     z-value: -0.080168, p-value: 0.9361
## Wald statistic: 0.0064269, p-value: 0.9361
## 
## Log likelihood: -295.1398 for lag model
## ML residual variance (sigma squared): 40490000, (sigma: 6363.2)
## Nagelkerke pseudo-R-squared: 0.89436 
## Number of observations: 29 
## Number of parameters estimated: 4 
## AIC: 598.28, (AIC for lm: 596.28)
# Model SDM
china.sdm <- lagsarlm(GRP ~ Level_of_Urbanization, data = china_join, listw = W, type = "mixed")
## Warning in lagsarlm(GRP ~ Level_of_Urbanization, data = china_join, listw = W, : inversion of asymptotic covariance matrix failed for tol.solve = 2.22044604925031e-16 
##   reciprocal condition number = 5.98794e-18 - using numerical Hessian.
summary(china.sdm)
## 
## Call:lagsarlm(formula = GRP ~ Level_of_Urbanization, data = china_join, 
##     listw = W, type = "mixed")
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -12395.33  -3468.27   -511.69   2616.38  15209.70 
## 
## Type: mixed 
## Coefficients: (numerical Hessian approximate standard errors) 
##                            Estimate Std. Error z value Pr(>|z|)
## (Intercept)               -17459.53   43755.44 -0.3990   0.6899
## Level_of_Urbanization       1384.38     106.97 12.9416   <2e-16
## lag.Level_of_Urbanization   -360.85    1047.62 -0.3444   0.7305
## 
## Rho: 0.1397, LR test value: 0.052341, p-value: 0.81904
## Approximate (numerical Hessian) standard error: 0.5566
##     z-value: 0.25099, p-value: 0.80182
## Wald statistic: 0.062994, p-value: 0.80182
## 
## Log likelihood: -295.0911 for mixed model
## ML residual variance (sigma squared): 40307000, (sigma: 6348.8)
## Number of observations: 29 
## Number of parameters estimated: 5 
## AIC: 600.18, (AIC for lm: 598.23)
err.china.slx <- residuals(china.SLX)
ad.test(err.china.slx)
## 
##  Anderson-Darling normality test
## 
## data:  err.china.slx
## A = 0.48975, p-value = 0.2044
moran.test(err.china.slx, W, alternative = "two.sided")
## 
##  Moran I test under randomisation
## 
## data:  err.china.slx  
## weights: W    
## 
## Moran I statistic standard deviate = 1.2671, p-value = 0.2051
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       0.013846962      -0.035714286       0.001529833
# Khusus objek Sarlm (SAR / SDM), bisa cek Breusch-Pagan spasial:
bptest.Sarlm(china.sar)
## 
##  studentized Breusch-Pagan test
## 
## data:  
## BP = 3.9341, df = 1, p-value = 0.04732
# log likelihood masing-masing model
logLik(china.SLX)
## 'log Lik.' -295.1173 (df=4)
logLik(china.lm)
## 'log Lik.' -295.1415 (df=3)
# nilai LR
LR <- 2 * (as.numeric(logLik(china.SLX)) - as.numeric(logLik(china.lm)))

# derajat bebas
df <- attr(logLik(china.SLX), "df") - attr(logLik(china.lm), "df")

# p-value
pchisq(LR, df = df, lower.tail = FALSE)
## [1] 0.8257453
AICc(china.lm)
## [1] 597.2431
AICc(china.SLX)
## [1] 599.9012

Spillover

Im_china <- impacts(china.SLX, listw = W)
Im_china
## Impact measures (SlX, glht):
##                              Direct  Indirect    Total
## Level_of_Urbanization dy/dx 1386.61 -141.8371 1244.773

Kesimpulan

Berdasarkan hasil analisis spasial pada data provinsi di China, model regresi OLS terpilih sebagai model terbaik untuk memodelkan pengaruh tingkat urbanisasi terhadap Gross Regional Product (GRP). Uji Global Moran’s I pada sisaan model OLS serta uji Lagrange Multiplier menunjukkan hasil yang tidak signifikan, yang mengindikasikan bahwa tidak terdapat autokorelasi spasial global maupun efek limpahan spasial antarprovinsi. Selain itu, pengujian lanjutan menggunakan model SLX dan evaluasi nilai Likelihood Ratio serta AICc membuktikan bahwa penambahan variabel lag spasial tetangga tidak memberikan peningkatan performa yang bermakna, sehingga model regresi klasik OLS sudah memadai.

Dependensi

shp_jabar <- st_read("C:\\Users\\Hype\\Documents\\Semester 5\\Resgpas\\Regspas(R)\\Regspas_11\\JABAR\\Jabar2\\Jabar2.shp")
## Reading layer `Jabar2' from data source 
##   `C:\Users\Hype\Documents\Semester 5\Resgpas\Regspas(R)\Regspas_11\JABAR\Jabar2\Jabar2.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 26 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 106.3705 ymin: -7.823398 xmax: 108.8338 ymax: -5.91377
## CRS:           NA
data_jabar <- read_excel("C:\\Users\\Hype\\Documents\\Semester 5\\Resgpas\\Regspas(R)\\Regspas_11\\JABAR\\data_jabar.xlsx")
data_jabar$KABKOTNO <- as.character(data_jabar$KABKOTNO)

jabar <- shp_jabar %>%
  left_join(data_jabar, by = "KABKOTNO") %>%
  na.omit()

colnames(jabar)
##  [1] "PROVNO.x"   "KABKOTNO"   "KODE2010.x" "PROVINSI.x" "KABKOT.x"  
##  [6] "SUMBER"     "IDSP2010.x" "PROVNO.y"   "KODE2010.y" "PROVINSI.y"
## [11] "KABKOT.y"   "IDSP2010.y" "Long"       "Lat"        "p.miskin15"
## [16] "p.miskin16" "j.miskin15" "j.miskin16" "AHH2015"    "AHH2016"   
## [21] "EYS2015"    "EYS2016"    "MYS2015"    "MYS2016"    "EXP2015"   
## [26] "EXP2016"    "APM.SD15"   "APM.SMP15"  "APM.SMA15"  "APM.PT15"  
## [31] "APK.SD15"   "APK.SMP15"  "APK.SMA15"  "APK.PT15"   "APS.USIA15"
## [36] "APS.USIA2"  "APS.USIA3"  "APS.USIA4"  "geometry"
plot(jabar[, c(
  "p.miskin15", "AHH2015", "EYS2015", "MYS2015", "EXP2015",
  "APM.SD15", "APM.SMP15", "APM.SMA15", "APM.PT15",
  "APK.SD15", "APK.SMP15", "APK.SMA15", "APK.PT15",
  "APS.USIA15", "APS.USIA2", "APS.USIA3", "APS.USIA4"
)])
## Warning: plotting the first 9 out of 17 attributes; use max.plot = 17 to plot
## all

# Drop geometry agar bisa dibuat matriks korelasi & pairs
vars_jabar <- jabar %>%
  st_drop_geometry() %>%
  select(p.miskin15, EYS2015, APM.SD15, APK.SD15, APK.PT15, APS.USIA15)

# Boxplot - Identifikasi Pencilan
par(mfrow = c(2, 3))
boxplot(vars_jabar$p.miskin15, main = "p.miskin15", col = "dodgerblue")
boxplot(vars_jabar$EYS2015, main = "EYS2015", col = "dodgerblue")
boxplot(vars_jabar$APM.SD15, main = "APM.SD15", col = "dodgerblue")
boxplot(vars_jabar$APK.SD15, main = "APK.SD15", col = "dodgerblue")
boxplot(vars_jabar$APK.PT15, main = "APK.PT15", col = "dodgerblue")
boxplot(vars_jabar$APS.USIA15, main = "APS.USIA15", col = "dodgerblue")

par(mfrow = c(1, 1))

# Scatter plot & Matriks Korelasi
pairs(vars_jabar, pch = 19, lower.panel = NULL)

corrplot(cor(vars_jabar), method = "number")

Matriks Pembobot Spasial

# =====================================================
# SPATIAL WEIGHT
# =====================================================
nb_jabar <- suppressWarnings(poly2nb(jabar))

lw_jabar <- nb2listw(
  nb_jabar,
  style = "W",
  zero.policy = TRUE
)
# Simulasi Monte Carlo untuk pembobot Queen (lw_jabar)
moran.mc(jabar$p.miskin15, listw = lw_jabar, zero.policy = TRUE, nsim = 99)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  jabar$p.miskin15 
## weights: lw_jabar  
## number of simulations + 1: 100 
## 
## statistic = 0.41614, observed rank = 98, p-value = 0.02
## alternative hypothesis: greater
# Alternatif 1: Rook Contiguity
rook_jabar <- suppressWarnings(poly2nb(jabar, queen = FALSE))
W_jabar_rook <- nb2listw(rook_jabar, style = "W", zero.policy = TRUE)
moran.test(jabar$p.miskin15, W_jabar_rook, randomisation = TRUE, alternative = "greater", zero.policy = TRUE)
## 
##  Moran I test under randomisation
## 
## data:  jabar$p.miskin15  
## weights: W_jabar_rook  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 2.1155, p-value = 0.0172
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.41613663       -0.09090909        0.05744913
# Alternatif 2: K-Nearest Neighbor (k = 5)
centroids_jabar <- st_centroid(jabar)
## Warning: st_centroid assumes attributes are constant over geometries
longlat_jabar <- st_coordinates(centroids_jabar)
W_jabar_knn <- nb2listw(knn2nb(knearneigh(longlat_jabar, k = 5, longlat = TRUE)), style = "W")
moran.test(jabar$p.miskin15, W_jabar_knn, randomisation = TRUE, alternative = "greater")
## 
##  Moran I test under randomisation
## 
## data:  jabar$p.miskin15  
## weights: W_jabar_knn    
## 
## Moran I statistic standard deviate = 2.2592, p-value = 0.01194
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.20792453       -0.06250000        0.01432783

Penentuan Variabel - Indeks Moran

# =========================================================
# ANALISIS MORAN'S I RESIDUAL
# MENCARI KOMBINASI X TERBAIK
# DENGAN P-VALUE TERKECIL
# =========================================================

# =========================================================
# 5. DAFTAR VARIABEL
# =========================================================
variabel_list <- c(
  "p.miskin15", "AHH2015", "EYS2015", "MYS2015", "EXP2015",
  "APM.SD15", "APM.SMP15", "APM.SMA15", "APM.PT15",
  "APK.SD15", "APK.SMP15", "APK.SMA15", "APK.PT15",
  "APS.USIA15", "APS.USIA2", "APS.USIA3", "APS.USIA4"
)

# =========================================================
# 6. TENTUKAN VARIABEL Y
# =========================================================
variabel_Y <- "p.miskin15"

# variabel kandidat X
variabel_X <- setdiff(
  variabel_list,
  variabel_Y
)

# =========================================================
# 7. DATAFRAME HASIL
# =========================================================
hasil_model <- data.frame(
  Formula = character(),
  Jumlah_X = numeric(),
  Moran_I = numeric(),
  P_Value = numeric(),
  stringsAsFactors = FALSE
)

# =========================================================
# 8. LOOP KOMBINASI VARIABEL
# =========================================================
maksimum_X <- 6

for (k in 1:maksimum_X) {
  # membuat kombinasi
  kombinasi_x <- combn(
    variabel_X,
    k,
    simplify = FALSE
  )
  
  # loop tiap kombinasi
  for (x_set in kombinasi_x) {
    tryCatch({
      # =====================================================
      # MEMBUAT FORMULA
      # =====================================================
      formula_text <- paste(
        variabel_Y,
        "~",
        paste(x_set, collapse = " + ")
      )
      
      rumus <- as.formula(formula_text)
      
      # =====================================================
      # MODEL OLS
      # =====================================================
      model_ols <- lm(
        rumus,
        data = jabar
      )
      
      # =====================================================
      # MORAN TEST RESIDUAL
      # =====================================================
      uji_moran <- lm.morantest(
        model_ols,
        lw_jabar,
        zero.policy = TRUE
      )
      
      # =====================================================
      # AMBIL NILAI
      # =====================================================
      moran_i <- as.numeric(
        uji_moran$estimate["Observed Moran I"]
      )
      
      p_val <- uji_moran$p.value
      
      # =====================================================
      # SIMPAN JIKA SIGNIFIKAN
      # =====================================================
      if (p_val < 0.05) {
        hasil_model <- rbind(
          hasil_model,
          data.frame(
            Formula = formula_text,
            Jumlah_X = k,
            Moran_I = moran_i,
            P_Value = p_val
          )
        )
      }
    }, error = function(e) {
      # abaikan error
      NULL
    })
  }
}

# =========================================================
# 9. URUTKAN HASIL
# =========================================================
hasil_model <- hasil_model %>%
  arrange(P_Value)

# =========================================================
# 11. MODEL TERBAIK
# P-VALUE PALING KECIL
# =========================================================
if (nrow(hasil_model) == 0) {
  cat("Tidak ada model signifikan.\n")
} else {
  # ambil model terbaik
  model_terbaik <- hasil_model %>%
    slice(1)
  
  cat(
    "Formula              : ",
    as.character(model_terbaik$Formula),
    "\n",
    sep = ""
  )
  
  cat(
    "Jumlah Variabel X    : ",
    model_terbaik$Jumlah_X,
    "\n",
    sep = ""
  )
  
  cat(
    "Moran's I            : ",
    round(model_terbaik$Moran_I, 5),
    "\n",
    sep = ""
  )
  
  cat(
    "P-Value              : ",
    format(
      model_terbaik$P_Value,
      scientific = FALSE
    ),
    "\n",
    sep = ""
  )
}
## Formula              : p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + APS.USIA15
## Jumlah Variabel X    : 5
## Moran's I            : 0.5012
## P-Value              : 0.005018003

OLS

jabar.lm <- lm(p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + APS.USIA15, data = jabar)
summary(jabar.lm)
## 
## Call:
## lm(formula = p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + 
##     APS.USIA15, data = jabar)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.2541 -1.5129  0.7468  1.7253  2.9766 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -263.5186   236.1804  -1.116   0.2883  
## EYS2015        2.7220     2.1113   1.289   0.2238  
## APM.SD15      -0.5289     0.6939  -0.762   0.4619  
## APK.SD15       0.4526     0.2946   1.536   0.1527  
## APK.PT15      -0.3228     0.1731  -1.865   0.0891 .
## APS.USIA15     2.4849     2.5164   0.987   0.3446  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.114 on 11 degrees of freedom
## Multiple R-squared:  0.5749, Adjusted R-squared:  0.3816 
## F-statistic: 2.975 on 5 and 11 DF,  p-value: 0.06127
# 1. Uji Normalitas Sisaan (Anderson-Darling & Q-Q Plot)
err.jabar <- residuals(jabar.lm)
ad.test(err.jabar)
## 
##  Anderson-Darling normality test
## 
## data:  err.jabar
## A = 0.49311, p-value = 0.1877
hist(err.jabar)

qqnorm(err.jabar, datax = TRUE)
qqline(rnorm(length(err.jabar), mean(err.jabar), sd(err.jabar)), datax = TRUE, col = "red")

# 2. Uji Kehomogenan Ragam / Heterogenitas Spasial (Breusch-Pagan)
lmtest::bptest(jabar.lm)
## 
##  studentized Breusch-Pagan test
## 
## data:  jabar.lm
## BP = 0.88003, df = 5, p-value = 0.9717
# 3. Uji Multikolinearitas (VIF)
car::vif(jabar.lm)
##    EYS2015   APM.SD15   APK.SD15   APK.PT15 APS.USIA15 
##   5.314614   1.904803   1.909792   5.864388   1.432641

Lagrange M

jabar.lagrange <- lm.LMtests(jabar.lm, lw_jabar, test = c("LMerr", "RLMerr", "LMlag", "RLMlag", "SARMA"))
## Please update scripts to use lm.RStests in place of lm.LMtests
summary(jabar.lagrange)
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## data:  
## model: lm(formula = p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 +
## APK.PT15 + APS.USIA15, data = jabar)
## test weights: listw
##  
##          statistic parameter  p.value   
## RSerr      7.23955         1 0.007132 **
## adjRSerr   5.48939         1 0.019132 * 
## RSlag      2.01129         1 0.156133   
## adjRSlag   0.26113         1 0.609346   
## SARMA      7.50067         2 0.023510 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
moran.test(jabar$p.miskin15, lw_jabar, randomisation = TRUE, alternative = "two.sided", zero.policy = TRUE)
## 
##  Moran I test under randomisation
## 
## data:  jabar$p.miskin15  
## weights: lw_jabar  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 2.1155, p-value = 0.03439
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.41613663       -0.09090909        0.05744913
moran.test(jabar$EYS2015, lw_jabar, randomisation = TRUE, alternative = "two.sided", zero.policy = TRUE)
## 
##  Moran I test under randomisation
## 
## data:  jabar$EYS2015  
## weights: lw_jabar  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 1.6572, p-value = 0.09748
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.32060386       -0.09090909        0.06166281
moran.test(jabar$APM.SD15, lw_jabar, randomisation = TRUE, alternative = "two.sided", zero.policy = TRUE)
## 
##  Moran I test under randomisation
## 
## data:  jabar$APM.SD15  
## weights: lw_jabar  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 0.4281, p-value = 0.6686
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.01082607       -0.09090909        0.05647319
moran.plot(jabar$p.miskin15, lw_jabar, labels = jabar$KABKOT.x, zero.policy = TRUE)

SEM, SAR, dan SDM

jabar.err <- errorsarlm(p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + APS.USIA15, lw_jabar, data = jabar, zero.policy = TRUE)
summary(jabar.err)
## 
## Call:errorsarlm(formula = p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + 
##     APK.PT15 + APS.USIA15, data = jabar, listw = lw_jabar, zero.policy = TRUE)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.96720 -0.97706  0.17825  0.76731  1.45103 
## 
## Type: error 
## Regions with no neighbours included:
##  18 19 21 25 26 
## Coefficients: (asymptotic standard errors) 
##                Estimate  Std. Error z value  Pr(>|z|)
## (Intercept) -425.458866   84.564213 -5.0312 4.874e-07
## EYS2015        4.311729    1.035451  4.1641 3.126e-05
## APM.SD15      -1.521207    0.226462 -6.7173 1.852e-11
## APK.SD15       0.640932    0.101930  6.2880 3.216e-10
## APK.PT15      -0.358322    0.069408 -5.1626 2.436e-07
## APS.USIA15     4.680477    0.828208  5.6513 1.592e-08
## 
## Lambda: 0.9059, LR test value: 23.088, p-value: 1.5476e-06
## Asymptotic standard error: 0.060606
##     z-value: 14.947, p-value: < 2.22e-16
## Wald statistic: 223.43, p-value: < 2.22e-16
## 
## Log likelihood: -28.18863 for error model
## ML residual variance (sigma squared): 1.0473, (sigma: 1.0234)
## Number of observations: 17 
## Number of parameters estimated: 8 
## AIC: 72.377, (AIC for lm: 93.465)
# Model SAR
jabar.sar <- lagsarlm(p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + APS.USIA15,
                      data = jabar, listw = lw_jabar, zero.policy = TRUE)
summary(jabar.sar, Nagelkerke = TRUE)
## 
## Call:lagsarlm(formula = p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + 
##     APK.PT15 + APS.USIA15, data = jabar, listw = lw_jabar, zero.policy = TRUE)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -5.35929 -0.59379  0.56548  1.22936  3.30456 
## 
## Type: lag 
## Regions with no neighbours included:
##  18 19 21 25 26 
## Coefficients: (asymptotic standard errors) 
##               Estimate Std. Error z value Pr(>|z|)
## (Intercept) -125.57741  210.78513 -0.5958 0.551335
## EYS2015        5.98300    2.26537  2.6411 0.008264
## APM.SD15      -1.25194    0.55826 -2.2426 0.024924
## APK.SD15       0.47950    0.21824  2.1972 0.028008
## APK.PT15      -0.56418    0.18450 -3.0578 0.002229
## APS.USIA15     1.39601    2.18125  0.6400 0.522169
## 
## Rho: 0.32622, LR test value: 2.3723, p-value: 0.1235
## Asymptotic standard error: 0.1807
##     z-value: 1.8053, p-value: 0.071034
## Wald statistic: 3.259, p-value: 0.071034
## 
## Log likelihood: -38.54641 for lag model
## ML residual variance (sigma squared): 5.2836, (sigma: 2.2986)
## Nagelkerke pseudo-R-squared: 0.63023 
## Number of observations: 17 
## Number of parameters estimated: 8 
## AIC: 93.093, (AIC for lm: 93.465)
## LM test for residual autocorrelation
## test value: 7.2833, p-value: 0.0069599
# Model SDM
jabar.sdm <- lagsarlm(p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + APS.USIA15,
                      data = jabar, listw = lw_jabar, zero.policy = TRUE, type = "mixed")
summary(jabar.sdm)
## 
## Call:lagsarlm(formula = p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + 
##     APK.PT15 + APS.USIA15, data = jabar, listw = lw_jabar, type = "mixed", 
##     zero.policy = TRUE)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -1.761337 -0.537131 -0.014668  0.470410  1.646040 
## 
## Type: mixed 
## Regions with no neighbours included:
##  18 19 21 25 26 
## Coefficients: (asymptotic standard errors) 
##                   Estimate  Std. Error z value  Pr(>|z|)
## (Intercept)    -223.304626  126.817210 -1.7608 0.0782657
## EYS2015           6.334925    1.930583  3.2814 0.0010331
## APM.SD15         -1.411171    0.442053 -3.1923 0.0014114
## APK.SD15          0.595334    0.098329  6.0545 1.408e-09
## APK.PT15         -0.512337    0.160297 -3.1962 0.0013926
## APS.USIA15        2.359615    1.324744  1.7812 0.0748821
## lag.EYS2015      -0.658247    3.837250 -0.1715 0.8637981
## lag.APM.SD15      2.391188    0.500735  4.7754 1.794e-06
## lag.APK.SD15     -1.106008    0.318673 -3.4707 0.0005192
## lag.APK.PT15     -0.035526    0.338363 -0.1050 0.9163814
## lag.APS.USIA15   -1.090206    0.444275 -2.4539 0.0141317
## 
## Rho: 0.7606, LR test value: 8.4198, p-value: 0.0037115
## Asymptotic standard error: 0.12636
##     z-value: 6.0194, p-value: 1.7508e-09
## Wald statistic: 36.233, p-value: 1.7508e-09
## 
## Log likelihood: -25.75121 for mixed model
## ML residual variance (sigma squared): 0.95434, (sigma: 0.9769)
## Number of observations: 17 
## Number of parameters estimated: 13 
## AIC: 77.502, (AIC for lm: 83.922)
## LM test for residual autocorrelation
## test value: 0.36579, p-value: 0.54531

SLX

jabar.SLX <- lmSLX(p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + APS.USIA15, lw_jabar, data = jabar, zero.policy = TRUE, Durbin = TRUE)
summary(jabar.SLX)
## 
## Call:
## lm(formula = formula(paste("y ~ ", paste(colnames(x)[-1], collapse = "+"))), 
##     data = as.data.frame(x), weights = weights)
## 
## Coefficients:
##                 Estimate    Std. Error  t value     Pr(>|t|)  
## (Intercept)     -145.59982   307.35728    -0.47372     0.65245
## EYS2015            2.63817     4.59362     0.57431     0.58663
## APM.SD15           0.26488     0.97666     0.27122     0.79532
## APK.SD15           0.59059     0.23882     2.47293     0.04827
## APK.PT15          -0.20577     0.38277    -0.53759     0.61021
## APS.USIA15         0.36388     3.17195     0.11472     0.91241
## lag.EYS2015       -4.02200     9.31944    -0.43157     0.68113
## lag.APM.SD15       3.42980     1.21392     2.82538     0.03014
## lag.APK.SD15      -0.41090     0.69159    -0.59414     0.57412
## lag.APK.PT15       0.38003     0.82137     0.46268     0.65990
## lag.APS.USIA15    -2.48727     1.03245    -2.40910     0.05264

SARIMA

jabar.sarma <- sacsarlm(p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + APK.PT15 + APS.USIA15, lw_jabar, data = jabar, zero.policy = TRUE)
summary(jabar.sarma)
## 
## Call:sacsarlm(formula = p.miskin15 ~ EYS2015 + APM.SD15 + APK.SD15 + 
##     APK.PT15 + APS.USIA15, data = jabar, listw = lw_jabar, zero.policy = TRUE)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -1.837342 -0.900247 -0.050073  0.685523  1.606525 
## 
## Type: sac 
## Coefficients: (asymptotic standard errors) 
##                Estimate  Std. Error z value  Pr(>|z|)
## (Intercept) -442.074286   83.148799 -5.3167 1.057e-07
## EYS2015        4.793132    1.140231  4.2037 2.626e-05
## APM.SD15      -1.707710    0.293038 -5.8276 5.623e-09
## APK.SD15       0.672837    0.103147  6.5231 6.886e-11
## APK.PT15      -0.392055    0.076727 -5.1097 3.226e-07
## APS.USIA15     4.939024    0.838588  5.8897 3.869e-09
## 
## Rho: 0.20377
## Asymptotic standard error: 0.19972
##     z-value: 1.0202, p-value: 0.30761
## Lambda: 0.91256
## Asymptotic standard error: 0.065071
##     z-value: 14.024, p-value: < 2.22e-16
## 
## LR test value: 23.918, p-value: 6.4015e-06
## 
## Log likelihood: -27.77358 for sac model
## ML residual variance (sigma squared): 0.97176, (sigma: 0.98578)
## Number of observations: 17 
## Number of parameters estimated: 9 
## AIC: 73.547, (AIC for lm: 93.465)

SEM vs SARIMA

anova(jabar.err, jabar.sarma)
##             Model df    AIC  logLik Test L.Ratio p-value
## jabar.err       1  8 72.377 -28.189    1                
## jabar.sarma     2  9 73.547 -27.774    2 0.83008 0.36225
# Contoh untuk model terbaik Jabar (jabar.err)
err.jabar.sem <- residuals(jabar.err)

# 1. Kenormalan sisaan
ad.test(err.jabar.sem)
## 
##  Anderson-Darling normality test
## 
## data:  err.jabar.sem
## A = 0.48895, p-value = 0.1924
# 2. Kehomogenan ragam spasial
bptest.Sarlm(jabar.err)
## 
##  studentized Breusch-Pagan test
## 
## data:  
## BP = 9.828, df = 5, p-value = 0.08026
# 3. Kebebasan sisaan (memastikan autokorelasi spasial sudah hilang)
moran.test(err.jabar.sem, lw_jabar, alternative = "two.sided", zero.policy = TRUE)
## 
##  Moran I test under randomisation
## 
## data:  err.jabar.sem  
## weights: lw_jabar  
## n reduced by no-neighbour observations  
## 
## Moran I statistic standard deviate = 0.98133, p-value = 0.3264
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##        0.14850588       -0.09090909        0.05952171

AIC

AICc(jabar.lm)
## [1] 105.9096
AICc(jabar.err)
## [1] 90.37725
AICc(jabar.sarma)
## [1] 99.26145

Spillover

# Menghitung Direct, Indirect (Spillover), dan Total Impact (contoh pada jabar.sarma)
Im_jabar <- impacts(jabar.sarma, listw = lw_jabar)
Im_jabar
## Impact measures (sac, exact):
##                      Direct    Indirect      Total
## EYS2015 dy/dx     4.8529413  1.16680904  6.0197503
## APM.SD15 dy/dx   -1.7290184 -0.41571374 -2.1447322
## APK.SD15 dy/dx    0.6812325  0.16379102  0.8450235
## APK.PT15 dy/dx   -0.3969468 -0.09543925 -0.4923860
## APS.USIA15 dy/dx  5.0006532  1.20232392  6.2029771
# Menghitung Efek Umpan Balik (Feedback Effect)
sum_jabar <- summary(jabar.sarma)
koef_jabar <- sum_jabar$coefficients[-1]
diref_jabar <- Im_jabar$direct
umbal_jabar <- diref_jabar - koef_jabar

cbind.data.frame(
  Koefisien = koef_jabar,
  EfekLangsung = diref_jabar,
  UmpanBalik = umbal_jabar
)
##             Koefisien EfekLangsung   UmpanBalik
## EYS2015     4.7931324    4.8529413  0.059808838
## APM.SD15   -1.7077096   -1.7290184 -0.021308847
## APK.SD15    0.6728368    0.6812325  0.008395676
## APK.PT15   -0.3920547   -0.3969468 -0.004892069
## APS.USIA15  4.9390239    5.0006532  0.061629276

Kesimpulan

Berdasarkan analisis regresi spasial pada data kemiskinan di Jawa Barat, model Spatial Error Model (SEM) terbukti sebagai model terbaik yang mampu mengatasi adanya dependensi spasial pada data. Berdasarkan uji Lagrange Multiplier, efek ketergantungan spasial pada wilayah Jawa Barat bersumber dari komponen error (sisaan) dan bukan dari lag spasial variabel dependen. Hal ini dikonfirmasi melalui perbandingan nilai AIC, di mana model SEM menghasilkan nilai AIC yang jauh lebih kecil dibandingkan model regresi OLS biasa maupun model spasial lainnya, sehingga menunjukkan bahwa faktor-faktor tak teramati antarwilayah yang berdekatan memegang peranan penting dalam pemodelan kemiskinan di Jawa Barat.