Matriks bobot spasial (\(\mathbf{W}\)) adalah representasi matematis dari kedekatan atau ketetanggaan antar unit wilayah. Matriks ini menggambarkan seberapa besar pengaruh satu wilayah terhadap wilayah lain dalam model spasial (Anselin, 1988).
Indo_Kec <- readRDS("gadm36_IDN_3_sp.rds")
Bandung_sp <- Indo_Kec[Indo_Kec$NAME_2 == "Kota Bandung", ]
Bandung_sp$id <- 1:nrow(Bandung_sp)
row.names(Bandung_sp) <- as.character(Bandung_sp$id)
CoordK <- coordinates(Bandung_sp)
W <- poly2nb(Bandung_sp, queen = TRUE)
WB <- nb2mat(W, style = 'B', zero.policy = TRUE)
WL <- nb2listw(W, style = "W")
plot(Bandung_sp, col = "gray90", main = "Peta Kecamatan Kota Bandung")
points(CoordK, pch = 19, cex = 0.7, col = "blue")
text(CoordK[, 1], CoordK[, 2], labels = Bandung_sp$id, cex = 0.7, pos = 3)
plot(W, CoordK, add = TRUE, col = "red")
Model spasial mempertimbangkan pengaruh wilayah tetangga dalam analisis regresi. Dua model utama yang digunakan adalah:
Model SAR (Spatial Autoregressive): \[ y = \rho W y + X \beta + \varepsilon \]
Model SEM (Spatial Error Model): \[ y = X\beta + u, \quad u = \lambda W u + \varepsilon \]
Model SAR digunakan jika ketergantungan spasial terdapat pada variabel dependen, sedangkan SEM digunakan jika korelasi terdapat dalam error term (LeSage & Pace, 2009).
Indeks Moran mengukur autokorelasi spasial: \[ I = \frac{n}{S_0} \cdot \frac{\sum_{i} \sum_{j} w_{ij} (x_i - \bar{x})(x_j - \bar{x})}{\sum_{i} (x_i - \bar{x})^2} \]
set.seed(123)
Bandung_sp$IPM <- round(runif(30, 65, 85), 1)
Bandung_sp$PDRB <- round(rnorm(30, 10000, 1500), 0)
Bandung_sp$Literasi <- round(runif(30, 85, 99), 1)
moran.test(Bandung_sp$IPM, WL)
##
## Moran I test under randomisation
##
## data: Bandung_sp$IPM
## weights: WL
##
## Moran I statistic standard deviate = -0.91012, p-value = 0.8186
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## -0.13964367 -0.03448276 0.01335089
moran.plot(Bandung_sp$IPM, WL)
Lagrange Multiplier Test (LM) digunakan untuk mengevaluasi apakah OLS perlu dimodifikasi menjadi model spasial (Ward & Gleditsch, 2018).
ols_model <- lm(IPM ~ PDRB + Literasi, data = Bandung_sp)
lm.LMtests(ols_model, WL, test = c("LMerr", "LMlag", "RLMerr", "RLMlag"))
##
## Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
## dependence
##
## data:
## model: lm(formula = IPM ~ PDRB + Literasi, data = Bandung_sp)
## test weights: listw
##
## RSerr = 1.0834, df = 1, p-value = 0.2979
##
##
## Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
## dependence
##
## data:
## model: lm(formula = IPM ~ PDRB + Literasi, data = Bandung_sp)
## test weights: listw
##
## RSlag = 1.2047, df = 1, p-value = 0.2724
##
##
## Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
## dependence
##
## data:
## model: lm(formula = IPM ~ PDRB + Literasi, data = Bandung_sp)
## test weights: listw
##
## adjRSerr = 0.036836, df = 1, p-value = 0.8478
##
##
## Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
## dependence
##
## data:
## model: lm(formula = IPM ~ PDRB + Literasi, data = Bandung_sp)
## test weights: listw
##
## adjRSlag = 0.15812, df = 1, p-value = 0.6909
model_sar <- lagsarlm(IPM ~ PDRB + Literasi, data = Bandung_sp, listw = WL)
summary(model_sar)
##
## Call:lagsarlm(formula = IPM ~ PDRB + Literasi, data = Bandung_sp,
## listw = WL)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.19260 -2.36696 -0.78377 5.05217 9.18910
##
## Type: lag
## Coefficients: (asymptotic standard errors)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.0574e+02 3.2949e+01 3.2094 0.00133
## PDRB -9.4803e-04 6.4479e-04 -1.4703 0.14149
## Literasi 1.4067e-01 2.4638e-01 0.5710 0.56802
##
## Rho: -0.42964, LR test value: 1.806, p-value: 0.17898
## Asymptotic standard error: 0.2792
## z-value: -1.5388, p-value: 0.12384
## Wald statistic: 2.368, p-value: 0.12384
##
## Log likelihood: -92.7214 for lag model
## ML residual variance (sigma squared): 27.272, (sigma: 5.2223)
## Number of observations: 30
## Number of parameters estimated: 5
## AIC: 195.44, (AIC for lm: 195.25)
## LM test for residual autocorrelation
## test value: 0.38014, p-value: 0.53753
model_sem <- errorsarlm(IPM ~ PDRB + Literasi, data = Bandung_sp, listw = WL)
summary(model_sem)
##
## Call:errorsarlm(formula = IPM ~ PDRB + Literasi, data = Bandung_sp,
## listw = WL)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.4546 -2.5873 -0.8035 5.0618 8.6514
##
## Type: error
## Coefficients: (asymptotic standard errors)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 75.70234590 24.65919463 3.0699 0.002141
## PDRB -0.00094758 0.00064445 -1.4704 0.141464
## Literasi 0.11078063 0.25334644 0.4373 0.661916
##
## Lambda: -0.41506, LR test value: 1.6056, p-value: 0.20511
## Asymptotic standard error: 0.28522
## z-value: -1.4552, p-value: 0.14561
## Wald statistic: 2.1177, p-value: 0.14561
##
## Log likelihood: -92.82162 for error model
## ML residual variance (sigma squared): 27.523, (sigma: 5.2463)
## Number of observations: 30
## Number of parameters estimated: 5
## AIC: 195.64, (AIC for lm: 195.25)
Telah dibahas pada bagian 1.3.
Bandung_sp$X1 <- rnorm(30, 50, 10)
Bandung_sp$X2 <- rnorm(30, 100, 20)
beta <- c(1.2, -0.6)
rho <- 0.4
Wy <- lag.listw(WL, Bandung_sp$X1 * beta[1] + Bandung_sp$X2 * beta[2])
Bandung_sp$y_sar <- rho * Wy + Bandung_sp$X1 * beta[1] + Bandung_sp$X2 * beta[2] + rnorm(30)
Bandung_sp$y_sem <- Bandung_sp$X1 * beta[1] + Bandung_sp$X2 * beta[2] + rnorm(30)
moran.test(Bandung_sp$y_sar, WL)
##
## Moran I test under randomisation
##
## data: Bandung_sp$y_sar
## weights: WL
##
## Moran I statistic standard deviate = 1.9449, p-value = 0.02589
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.18728428 -0.03448276 0.01300155
ols_model2 <- lm(y_sem ~ X1 + X2, data = Bandung_sp)
summary(ols_model2)
##
## Call:
## lm(formula = y_sem ~ X1 + X2, data = Bandung_sp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.4594 -0.5579 -0.1120 0.3661 3.1439
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.42775 1.44156 -0.297 0.769
## X1 1.20559 0.02077 58.046 <2e-16 ***
## X2 -0.59694 0.00996 -59.932 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9728 on 27 degrees of freedom
## Multiple R-squared: 0.9961, Adjusted R-squared: 0.9959
## F-statistic: 3485 on 2 and 27 DF, p-value: < 2.2e-16
model_sar2 <- lagsarlm(y_sar ~ X1 + X2, data = Bandung_sp, listw = WL)
summary(model_sar2)
##
## Call:lagsarlm(formula = y_sar ~ X1 + X2, data = Bandung_sp, listw = WL)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.99207 -0.73160 -0.21965 0.78814 2.14733
##
## Type: lag
## Coefficients: (asymptotic standard errors)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 2.870382 1.840541 1.5595 0.1189
## X1 1.124269 0.024711 45.4965 <2e-16
## X2 -0.593633 0.012759 -46.5264 <2e-16
##
## Rho: 0.36612, LR test value: 53.274, p-value: 2.901e-13
## Asymptotic standard error: 0.029079
## z-value: 12.591, p-value: < 2.22e-16
## Wald statistic: 158.53, p-value: < 2.22e-16
##
## Log likelihood: -47.13725 for lag model
## ML residual variance (sigma squared): 1.3124, (sigma: 1.1456)
## Number of observations: 30
## Number of parameters estimated: 5
## AIC: 104.27, (AIC for lm: 155.55)
## LM test for residual autocorrelation
## test value: 0.00080398, p-value: 0.97738
model_sem2 <- errorsarlm(y_sem ~ X1 + X2, data = Bandung_sp, listw = WL)
summary(model_sem2)
##
## Call:errorsarlm(formula = y_sem ~ X1 + X2, data = Bandung_sp, listw = WL)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.402836 -0.566988 0.020464 0.339313 2.916231
##
## Type: error
## Coefficients: (asymptotic standard errors)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.4092564 1.2070313 -0.3391 0.7346
## X1 1.2117993 0.0185965 65.1628 <2e-16
## X2 -0.6001212 0.0075465 -79.5233 <2e-16
##
## Lambda: -0.61565, LR test value: 3.6133, p-value: 0.057321
## Asymptotic standard error: 0.2782
## z-value: -2.213, p-value: 0.026901
## Wald statistic: 4.8972, p-value: 0.026901
##
## Log likelihood: -38.35528 for error model
## ML residual variance (sigma squared): 0.6992, (sigma: 0.83618)
## Number of observations: 30
## Number of parameters estimated: 5
## AIC: 86.711, (AIC for lm: 88.324)
model_summary <- data.frame(
Model = c("OLS", "SAR", "SEM"),
AIC = c(AIC(ols_model2), AIC(model_sar2), AIC(model_sem2)),
LogLik = c(logLik(ols_model2), logLik(model_sar2), logLik(model_sem2))
)
knitr::kable(model_summary, caption = "Perbandingan AIC dan Log-Likelihood Model")
| Model | AIC | LogLik |
|---|---|---|
| OLS | 88.32381 | -40.16190 |
| SAR | 104.27449 | -47.13725 |
| SEM | 86.71055 | -38.35528 |
Bandung_sp$res_ols <- residuals(ols_model2)
Bandung_sp$res_sar <- residuals(model_sar2)
Bandung_sp$res_sem <- residuals(model_sem2)
spplot(Bandung_sp, "res_ols", main = "Residual OLS")
spplot(Bandung_sp, "res_sar", main = "Residual SAR")
spplot(Bandung_sp, "res_sem", main = "Residual SEM")
Model spasial memberikan hasil yang lebih akurat saat terdapat korelasi spasial antar unit wilayah. Uji LM dan indeks Moran menunjukkan adanya kebutuhan model spasial. Model SAR dan SEM meningkatkan kualitas estimasi dibanding OLS.