-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspatialreg-sumut.R
233 lines (198 loc) · 6.29 KB
/
spatialreg-sumut.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
### Analisis Regresi Spasial ###
library(sf)
library(sp)
library(spdep)
library(readxl)
library(spatialreg)
library(ggplot2)
#Input data
apts_sumut <- read_excel("D:/Data/Data Sumut.xlsx")
View(apts_sumut)
#Peta
petasumut <- st_read("D:/Data/sumaterautara.shp")
View(petasumut)
st_geometry_type(petasumut)
st_crs(petasumut)
sumutshp<- st_make_valid(petasumut)
#Merge data
data_joined <- merge(sumutshp, apts_sumut, by = "WADMKK")
sumut <- as(data_joined, "Spatial")
#Definisi variabel
Y<- log(sumut$JPS)
X1<- sumut$RMG
X2<- sumut$RMS
X3<- sumut$RLS
X4<- sumut$TPT
X5<- sumut$APM
X6<- sumut$KEPADATAN_PENDUDUK
#Eksplorasi Data
summary(apts_sumut)
library(RColorBrewer)
#JPS: Y
coljps <- colorRampPalette(c("limegreen", "yellow", "red"))(3)
breaks<- seq(min(sumut$JPS), max(sumut$JPS), length.out = 4)
spplot(sumut, "JPS",
col.regions = coljps,
at = breaks,
main = "Peta Sebaran Angka Putus Sekolah Sumatera Utara",
sp.layout = list(
list("sp.text", coordinates(sumut),
sumut$WADMKK, cex = 0.4, col = "black")
))
#RMG: X1
colrmg <- brewer.pal(3, "Greens")
spplot(sumut, "RMG",
col.regions = colrmg,
cuts = 2,
main = "Peta Sebaran Rasio Murid Guru Sumatera Utara",
sp.layout = list(
list("sp.text", coordinates(sumut),
sumut$WADMKK, cex = 0.4, col = "black")
))
#RMS: X2
colrms <- brewer.pal(3, "Blues")
spplot(sumut, "RMS",
col.regions = colrms,
cuts = 2,
main = "Peta Sebaran Rasio Murid Sekolah Sumatera Utara",
sp.layout = list(
list("sp.text", coordinates(sumut),
sumut$WADMKK, cex = 0.4, col = "black")
))
#RLS: X3
colrls <- brewer.pal(3, "Reds")
spplot(sumut, "RLS",
col.regions = colrls,
cuts = 2,
main = "Peta Sebaran Rata-Rata Lama Sekolah Sumatera Utara",
sp.layout = list(
list("sp.text", coordinates(sumut),
sumut$WADMKK, cex = 0.4, col = "black")
))
#TPT: X4
coltpt <- brewer.pal(3, "Oranges")
spplot(sumut, "TPT",
col.regions = coltpt,
cuts = 2,
main = "Peta Sebaran Tingkat Pengangguran Terbuka Sumatera Utara",
sp.layout = list(
list("sp.text", coordinates(sumut),
sumut$WADMKK, cex = 0.4, col = "black")
))
#APM: X5
colapm <- brewer.pal(3, "Purples")
spplot(sumut, "APM",
col.regions = colapm,
cuts = 2,
main = "Peta Sebaran Angka Partisipasi Murni Sumatera Utara",
sp.layout = list(
list("sp.text", coordinates(sumut),
sumut$WADMKK, cex = 0.4, col = "black")
))
#KP: X6
colkp <- brewer.pal(3, "YlOrBr")
spplot(sumut, "KEPADATAN_PENDUDUK",
col.regions = colkp,
cuts = 2,
main = "Peta Sebaran Kepadatan Penduduk Sumatera Utara",
sp.layout = list(
list("sp.text", coordinates(sumut),
sumut$WADMKK, cex = 0.4, col = "black")
))
# Model regresi berganda
model_lm <- lm(Y ~ X1 + X2 + X3 + X4 + X5 + X6, data = sumut)
summary(model_lm)
# Cek asumsi regresi berganda
## normalitas
# Q-Q Plot
qqnorm(residuals(model_lm))
qqline(residuals(model_lm), col = "red")
#jarque bera
library(tseries)
normalitas <- jarque.bera.test(residuals(model_lm))
normalitas
# multikolinearitas
library(olsrr)
multikolinearitas <- ols_vif_tol(model_lm)
multikolinearitas
# heterokedastisitas
library(lmtest)
heterokedastisitas <- bptest(model_lm)
heterokedastisitas
# plot residual vs nilai prediksi untuk melihat pola heteroskedastisitas
plot(fitted(model_lm), residuals(model_lm),
main = "Residual vs Fitted",
xlab = "Fitted Values", ylab = "Residuals")
abline(h = 0, col = "red")
# autokorelasi
autokorelasi <- dwtest(model_lm)
autokorelasi
### Matriks pembobot spasial
# Queen
queen <- read_excel("D:/Data/matriks_queen.xlsx")
colnames <- queen[[1]]
rownames <- colnames
nb_queen <- as.matrix(queen[,-1])
listw_queen<- mat2listw(nb_queen, zero.policy = TRUE)
listw_queen
# Rook
rook <- read_excel("D:/Data/matriks_rook.xlsx")
colnames <- rook[[1]]
rownames <- colnames
nb_rook <- as.matrix(rook[,-1])
listw_rook<- mat2listw(nb_rook, zero.policy = TRUE)
listw_rook
#Bishop
bishop <- read_excel("D:/Data/matriks_bishop.xlsx")
colnames <- bishop[[1]]
rownames <- colnames
nb_bishop <- as.matrix(bishop[,-1])
listw_bishop<- mat2listw(nb_bishop, zero.policy = TRUE)
listw_bishop
# Indeks moran
moran_queen <- moran.test(Y, listw = listw_queen)
moran_rook <- moran.test(Y, listw = listw_rook)
moran_bishop <- moran.test(Y, listw = listw_bishop)
moranI_queen <- round(moran_queen$estimate[1], 4)
moranI_rook <- round(moran_rook$estimate[1], 4)
# Visualisasi autokorelasi spasial (Moran Plot)
moran.plot(Y, listw = listw_queen, labels = sumut$WADMKK,
pch = 1, ylab = "Spatial lag of Y")
text(x = min(Y), y = max(lag.listw(listw_queen, Y)),
labels = paste("Moran's I Queen:", moranI_queen),
pos = 4, col = "black", font = 1, cex = 0.5)
moran.plot(Y, listw = listw_rook, labels = sumut$WADMKK,
pch = 1, ylab = "Spatial lag of Y")
text(x = min(Y), y = max(lag.listw(listw_rook, Y)),
labels = paste("Moran's I Rook:", moranI_rook),
pos = 4, col = "black", font = 1, cex = 0.5)
# Lagrange Multiplier
# Uji Lagrange Multiplier (LM)
lm.RStests(model_lm, listw_queen, test = "all")
lm.RStests(model_lm, listw_rook, test = "all")
lm.RStests(model_lm, listw_bishop, test = "all")
# Model Regresi Spasial: SEM
model_sem_queen <- errorsarlm(Y ~ X1 + X2 + X3 + X4 + X5 + X6, data = sumut,
listw = listw_queen)
summary(model_sem_queen)
#Uji kebaikan model
#R²
#Model regresi berganda
ypred<- model_lm$fitted.values
sst <- sum((Y - mean(Y))^2)
ssr <- sum((Y - ypred)^2)
rsquare_lm <- 1 - (ssr/sst)
#Model regresi spasial
#SEM
#queen
ypred_queen<- model_sem_queen$fitted.values
sst <- sum((Y - mean(Y))^2)
ssr <- sum((Y - ypred_queen)^2)
rsquare_sem_queen <- 1 - (ssr/sst)
rsquare_lm
rsquare_sem_queen
#AIC
aic_lm <- AIC (model_lm)
aic_sem_queen <- AIC(model_sem_queen)
aic_lm
aic_sem_queen