-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGWR stunting jawa timur 2022.Rmd
339 lines (303 loc) · 8.49 KB
/
GWR stunting jawa timur 2022.Rmd
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
---
title: "stunting"
author: "Yohanita"
date: "2023-08-14"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r library}
library(car)
library(lmtest)
library(spgwr)
library(fBasics)
library(AICcmodavg)
library(foreign)
library(lattice)
library(zoo)
library(ape)
library(Matrix)
library(mvtnorm)
library(emulator)
library(MLmetrics)
library(GWmodel)
library(sp)
```
```{r data}
library(readxl)
DATAKU <- read_excel("data statin.xlsx",
col_types = c("text", "numeric", "numeric",
"numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric","numeric","numeric","numeric"))
View(DATAKU)
attach(DATAKU)
DATAKU
```
#analisis deskriptif
```{r sum}
summary(DATAKU)
```
#Regresi OLS
```{r}
regols<-lm(formula=Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU)
summary(regols)
```
#deteksi multikolinearitas
```{r}
vif(regols)
```
#pengujian signifikansi parameter OLS
#pengujian serentak (Uji F) melihat signifikansi seluruh model
```{r}
anova(regols)
```
#Confident interval
#uji parsial (uji T)
```{r}
confint.lm(regols, level=0.95)
prediksi<-predict(regols)
prediksi
```
#pengujian asumsi klasik regresi ols dan efek spasial
#uji normalitas residual
```{r}
resid<-abs(regols$residuals)
res=regols$residual
ks.test(res,"pnorm",mean(res),sd(res),alternative=c("two.sided"))
```
#uji heterogenitas spasial (heterokedastisitas) untuk melihat keragaman data spasial
```{r}
bptest(lm(regols$residuals~X1+X2+X3+X4+X5+X6+X7+X8, data=DATAKU))
```
#pengujian indepennden autokorelasi
```{r}
dwtest(lm(regols$residuals~X1+X2+X3+X4+X5+X6+X7+X8, data = DATAKU))
```
GWR
#Mencari bandwidth optimal (adaptive bandwidth)
```{r}
library(spgwr)
b <- gwr.sel(Y~X1+X2+X3+X4+X5+X6+X7+X8,coords=cbind(DATAKU$Latitude,DATAKU$Longitude),data=DATAKU, adapt=TRUE,gweight=gwr.Gauss)
b
```
##estimasi parameter adaptive gauss bandwith GWR
```{r}
gwr.adaptgauss<-gwr(Y~X1+X2+X3+X4+X5+X6+X7+X8,data = DATAKU,coords = cbind(DATAKU$Longitude, DATAKU$Latitude),adapt = b,hatmatrix = TRUE,gweight = gwr.Gauss)
gwr.adaptgauss$bandwidth
```
#mencari jarak euclidean GWR
```{r}
v=DATAKU[12]
u=DATAKU[11]
v<-as.matrix(v)
u<-as.matrix(u)
j<-nrow(v)
i<-nrow(u)
jarak<-matrix(0,38,38)
for (i in 1:38) {
for (j in 1:38) {
jarak[i,j]<-sqrt((u[i,]-u[j,])**2+(v[i,]-v[j,])**2)
}
}
jarak
```
#mencari pembobot GWr setiap lokasi
```{r}
h<-as.matrix(gwr.adaptgauss$bandwidth)
i<-nrow(h)
W<-matrix(0,38,38)
for (i in 1:38) {
for (j in 1:38) {
W[i,j]<-exp(-(1/2)*(jarak[i,j]/h[i,])**2)
W[i,j]<-ifelse(jarak[i,j]<h[i,],W[i,j],0)
}
}
W
```
#fungsi pembobot kernel
#fungsi kernel fixed (satu)
##fixed kernel gaussian
```{r}
fixgauss=gwr.sel(Y~X1+X2+X3+X4+X5+X6+X7+X8,data = DATAKU,adapt=FALSE,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),gweight=gwr.Gauss)
gwr.fixgauss=gwr(Y~X1+X2+X3+X4+X5+X6+X7+X8,data = DATAKU,bandwidth = fixgauss,coords = cbind(DATAKU$Longitude, DATAKU$Latitude),hatmatrix = TRUE,gweight = gwr.Gauss)
gwr.fixgauss
names(gwr.fixgauss)
BFC02.gwr.test(gwr.fixgauss)
LMZ.F1GWR.test(gwr.fixgauss)
LMZ.F2GWR.test(gwr.fixgauss)
LMZ.F3GWR.test(gwr.fixgauss)
anova(gwr.fixgauss)
```
##Fixed kernel Bisquare
```{r}
#bandwidth
fixbisquare=gwr.sel(Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU,adapt
=FALSE,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),gweight=gwr.bisquare)
#estimasi parameter
gwr.fixbisquare=gwr(Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU,bandwidth=fixbisquare,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),hatmatrix=TRUE,gweight=gwr.bisquare)
gwr.fixbisquare
names(gwr.fixbisquare)
BFC02.gwr.test(gwr.fixbisquare)
LMZ.F1GWR.test(gwr.fixbisquare)
LMZ.F2GWR.test(gwr.fixbisquare)
anova(gwr.fixbisquare)
```
##fixed kernel Tricube
#estimasi parameter
```{r}
fixtricube=gwr.sel(Y~X1+X2+X3+X4+X5+X6+X7+X8,data = DATAKU,adapt = FALSE,coords = cbind(DATAKU$Longitude, DATAKU$Latitude),gweight = gwr.tricube)
gwr.fixtricube=gwr(Y~X1+X2+X3+X4+X5+X6+X7+X8,data = DATAKU,bandwidth = fixtricube,coords = cbind(DATAKU$Longitude, DATAKU$Latitude),hatmatrix = TRUE,gweight = gwr.tricube)
gwr.fixtricube
names(gwr.fixtricube)
BFC02.gwr.test(gwr.fixtricube)
LMZ.F1GWR.test(gwr.fixtricube)
LMZ.F2GWR.test(gwr.fixtricube)
anova(gwr.fixtricube)
```
#FUNGSI KERNEL ADAPTIVE (dua)
##fungsi kernel adaptive gaussian
```{r}
#bandwidth
adaptgauss=gwr.sel(Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU,adapt
=TRUE,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),gweight=gwr.Gauss)
#estimasi parameter
gwr.adaptgauss=gwr(Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU,adapt=adaptgauss, coords=cbind(DATAKU$Longitude, DATAKU$Latitude),hatmatrix=TRUE,gweight=gwr.Gauss)
gwr.adaptgauss
names(gwr.adaptgauss)
BFC02.gwr.test(gwr.adaptgauss)
LMZ.F1GWR.test(gwr.adaptgauss)
LMZ.F2GWR.test(gwr.adaptgauss)
anova(gwr.adaptgauss)
gwr.adaptgauss[5]
```
##FUNGSI KERNEL ADAPTIVE BISQUARE
```{r}
#bandwidth
adaptbisquare=gwr.sel(Y~X1+X2+X3+X4+X5+X6+X7+X8, data=DATAKU,adapt=TRUE,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),gweight=gwr.bisquare)
#estimasi parameter
gwr.adaptbisquare=gwr(Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU,adapt=adaptbisquare,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),hatmatrix=TRUE,gweight=gwr.bisquare)
gwr.adaptbisquare
names(gwr.adaptbisquare)
BFC02.gwr.test(gwr.adaptbisquare)
LMZ.F1GWR.test(gwr.adaptbisquare)
LMZ.F2GWR.test(gwr.adaptbisquare)
anova(gwr.adaptbisquare)
```
##FUNGSI KERNEL ADAPTIVE TRICUBE
```{r}
#bandwidth
adapttricube=gwr.sel(Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU,adapt=TRUE,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),gweight=gwr.tricube)
#estimasi parameter
gwr.adapttricube=gwr(Y~X1+X2+X3+X4+X5+X6+X7+X8,data=DATAKU,adapt=adapttricube,coords=cbind(DATAKU$Longitude, DATAKU$Latitude),hatmatrix=TRUE,gweight=gwr.tricube)
gwr.adapttricube
names(gwr.adapttricube)
BFC02.gwr.test(gwr.adapttricube)
LMZ.F1GWR.test(gwr.adapttricube)
LMZ.F2GWR.test(gwr.adapttricube)
anova(gwr.adapttricube)
```
#menampilkan t hitung
```{r}
gwr.fixgauss[1]
t_Intercept0=gwr.fixgauss$SDF$`(Intercept)`/gwr.fixgauss$SDF$`(Intercept)_se`
t_X1=gwr.fixgauss$SDF$X1/gwr.fixgauss$SDF$X1_se
t_X1
t_X2=gwr.fixgauss$SDF$X2/gwr.fixgauss$SDF$X2_se
t_X2
t_X3=gwr.fixgauss$SDF$X3/gwr.fixgauss$SDF$X3_se
t_X3
t_X4=gwr.fixgauss$SDF$X4/gwr.fixgauss$SDF$X4_se
t_X4
t_X5=gwr.fixgauss$SDF$X5/gwr.fixgauss$SDF$X5_se
t_X5
t_X6=gwr.fixgauss$SDF$X6/gwr.fixgauss$SDF$X6_se
t_X6
t_X7=gwr.fixgauss$SDF$X7/gwr.fixgauss$SDF$X7_se
t_X7
t_X8=gwr.fixgauss$SDF$X8/gwr.fixgauss$SDF$X8_se
t_X8
```
#membaca Output
```{r}
gwr.fixgauss$SDF$"(Intercept)"
gwr.fixgauss$SDF$X1
gwr.fixgauss$SDF$X2
gwr.fixgauss$SDF$X3
gwr.fixgauss$SDF$X4
gwr.fixgauss$SDF$X5
gwr.fixgauss$SDF$X6
gwr.fixgauss$SDF$X7
gwr.fixgauss$SDF$X8
```
#uji kecocokan model GWR
```{r}
BFC02.gwr.test(gwr.fixgauss)
LMZ.F1GWR.test(gwr.fixgauss)
LMZ.F2GWR.test(gwr.fixgauss)
```
#menampilkan r-square lokal
```{r}
gwr.fixgauss.R2=gwr.fixgauss$SDF$localR2
gwr.fixgauss.R2
```
#Evaluasi hasil prediksi dan data observasi menggunakan grafik
```{r}
require (ggplot2)
plot(DATAKU$Y, type="l", col="black")
lines(gwr.fixgauss$SDF$pred, type="l", col="red")
lines(prediksi, type="l", col="blue")
legend("topright",c("Observasi","Prediksi OLS","Prediksi GWR"),
col=c("black","blue","red"), lwd=3)
```
##Perbandingan Korelasi Antar Prediksi dengan Observasi
```{r}
obs<-DATAKU$Y
gwr_pred<-gwr.fixgauss$SDF$pred
gwr_pred
cor(prediksi,obs)^2
cor(gwr_pred,obs)^2
AIC(regols)
library(tidyverse)
data.frame("MODEL" = c("GWR","Regresi Klasik"),
"AIC" = c(gwr.fixgauss[["results"]][["AICh"]],AIC(regols)))%>% arrange(AIC)
```
```{r dataframe}
results <-as.data.frame(gwr.fixgauss$SDF)
names(results)
```
```{r maps}
library(sf)
library(dplyr)
library(raster)
shp <- shapefile("Jawa_Timur_ADMIN_BPS.shp")
plot(shp)
colfunc <- colorRampPalette(c('green','yellow','red'))
color <- colfunc(16)
shp$Y <-DATAKU$Y
spplot(shp,"Y", col.regions=color,lmain="Peta Sebaran Persentase Stunting Jawa Timur Tahun 2022")
```
```{r eksplorasi}
library(sf)
library(ggplot2)
library(readxl)
stunting <- read_excel("visualisasi stunting.xlsx")
stunting
# Membaca data shapefile
spatial_data <- st_read("Jawa_Timur_ADMIN_BPS.shp")
# Plot peta dengan ggplot2
p <- ggplot() +
geom_sf(data = spatial_data, aes(fill = stunting$stunting)) +
scale_fill_gradient(low = "green", high = "red", name = "Tingkat Stunting (%)") +
labs(title = "Peta Sebaran Persentase Stunting Jawa Timur Tahun 2022",
x = "Longitude",
y = "Latitude") +
theme_minimal()
library(ggrepel)
p + geom_text_repel(
data = spatial_data,
aes(label = Kabupaten, x = st_coordinates(st_centroid(geometry))[, 1], y = st_coordinates(st_centroid(geometry))[, 2]),
size = 3
)
```