Regspas Praktikum 11
A. Model Prediksi RGP Data China
1. Persiapan Data
# Load Package
suppressPackageStartupMessages({
library(sf)
library(readxl)
library(tidyverse)
library(ggplot2)
library(spdep)
library(spatialreg)
library(MuMIn)
library(patchwork)
})## Warning: package 'sf' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'readr' was built under R version 4.5.3
## Warning: package 'dplyr' was built under R version 4.5.3
## Warning: package 'spdep' was built under R version 4.5.3
## Warning: package 'spData' was built under R version 4.5.3
## Warning: package 'spatialreg' was built under R version 4.5.3
## Warning: package 'MuMIn' was built under R version 4.5.3
## Warning: package 'patchwork' was built under R version 4.5.3
# Membaca data spasial dalam format shapefile (.shp)
shp_china <- st_read("/Users/user/OneDrive/Desktop/Documents/College/SEMESTER_5/REGSPAS/Regspas_11/data china/China29/China29.shp")## Reading layer `China29' from data source
## `C:\Users\user\OneDrive\Desktop\Documents\College\SEMESTER_5\REGSPAS\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("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...
2. Eksplorasi Data
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## 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" ...
## 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
3. Regresi OLS
##
## 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
4. Matriks Pembobot Spasial
# Baca jarak kereta dari sheet Railway Distance
rail <- read_excel("Data_China.xlsx", sheet = "Railway Distance", skip = 1)## New names:
## • `` -> `...1`
5. Uji Morans’I
Uji Diagnostik Autokorelasi Spasial Global
##
## 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
6. Uji Lagrange Multiplier
## Please update scripts to use lm.RStests in place of lm.LMtests
## 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
7. SLX Model
Pemodelan Efek Limpahan Lokal: Spatially-Lagged X Model (SLX)
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
8. LR test : SLX vs OLS Regression
## 'log Lik.' -295.1173 (df=4)
## '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
## [1] 597.2431
## [1] 599.9012
B. Model Dependensi Spasial (Jawa Barat)
1. Data Preprocessing
shp_jabar <- st_read("/Users/user/OneDrive/Desktop/Documents/College/SEMESTER_5/REGSPAS/Regspas_11/JABAR/Jabar2/Jabar2.shp")## Reading layer `Jabar2' from data source
## `C:\Users\user\OneDrive\Desktop\Documents\College\SEMESTER_5\REGSPAS\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("/Users/user/OneDrive/Desktop/Documents/College/SEMESTER_5/REGSPAS/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()## [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"
2. Exploratory Analysis
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
## 3. Matriks Pembobot Spasial
4. 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
5. OLS Regression
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
6. Uji Lagrange Multiplier
## Please update scripts to use lm.RStests in place of lm.LMtests
## 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
7. Spatial Error Model (SEM)
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)
8. Model 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
9.Model SARMA
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)
10. LR Test : SEM vs 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