setwd("c:/Materi Kuliah/Spatial/Peta Jabar")
jabar1=readShapePoly("Jabar.shp")
## Warning: shapelib support is provided by GDAL through the sf and terra packages
## among others
plot(jabar1)
KAB = factor(c("Kab Bogor", "Kab Sukabumi", "Kab Cianjur", "Kab Bandung", "Kab Garut",
"Kab Tasikmalaya", "Kab Ciamis", "Kab Kuningan", "Kab Cirebon", "Kab Majalengka",
"Kab Sumedang", "Kan Indramayu", "Kab Subang", "Kab Purwakarta", "Kab Karawang",
"Kab Bekasi", "Kab Bandung Barat", "Kab Pangandaran", "Kota Bogor", "Kota Sukabumi",
"Kota Bandung", "Kota Cirebon", "Kab Bekasi", "Kota Depok", "Kota Cimahi",
"Kota Tasikmalaya", "Kota Banjar"))
jabar1$KAB = KAB
ID = c(1:27)
setwd("C:/Materi Kuliah/Spatial")
HIV <- read.csv("HIV.csv", sep = ",", dec =",")
head(HIV)
## ID Wilayah Indeks.Pendidikan Indeks.Kesehatan Jumlah.Nikah
## 1 1 Bogor 62.52 79.46 32039
## 2 2 Sukabumi 57.73 79.29 17731
## 3 3 Cianjur 57.36 77.82 16989
## 4 4 Bandung 65.57 83.09 28714
## 5 5 Garut 59.85 79.77 22542
## 6 6 Tasikmalaya 60.74 76.85 14977
## Jumlah.Kasus.Penyakit.HIV.AIDS
## 1 251
## 2 14
## 3 39
## 4 40
## 5 61
## 6 11
str(HIV)
## 'data.frame': 27 obs. of 6 variables:
## $ ID : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Wilayah : chr "Bogor" "Sukabumi" "Cianjur" "Bandung" ...
## $ Indeks.Pendidikan : num 62.5 57.7 57.4 65.6 59.9 ...
## $ Indeks.Kesehatan : num 79.5 79.3 77.8 83.1 79.8 ...
## $ Jumlah.Nikah : int 32039 17731 16989 28714 22542 14977 10593 8922 20539 10226 ...
## $ Jumlah.Kasus.Penyakit.HIV.AIDS: int 251 14 39 40 61 11 36 51 144 166 ...
jabar1$HIV =HIV$Jumlah.Kasus.Penyakit.HIV.AIDS#
jabar1$pendidikan =HIV$Indeks.Pendidikan#
jabar1$kesehatan = HIV$Indeks.Kesehatan
jabar1$nikah =HIV$Jumlah.Nikah
tmap_options(check.and.fix = TRUE)
par(mfrow=c(2,2))
tm_shape(jabar1) + tm_polygons("HIV",style="cont")+ tm_borders(lwd=1,col="black") + tm_text("KAB",size=0.6)
## Warning: Currect projection of shape jabar1 unknown. Long-lat (WGS84) is
## assumed.
## Warning: The shape jabar1 is invalid. See sf::st_is_valid
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
tm_shape(jabar1) + tm_polygons("pendidikan",style="cont")+ tm_borders(lwd=1,col="black") + tm_text("KAB",size=0.6)
## Warning: Currect projection of shape jabar1 unknown. Long-lat (WGS84) is
## assumed.
## Warning: The shape jabar1 is invalid. See sf::st_is_valid
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
tm_shape(jabar1) + tm_polygons("kesehatan",style="cont")+ tm_borders(lwd=1,col="black") + tm_text("KAB",size=0.6)
## Warning: Currect projection of shape jabar1 unknown. Long-lat (WGS84) is
## assumed.
## Warning: The shape jabar1 is invalid. See sf::st_is_valid
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
tm_shape(jabar1) + tm_polygons("nikah",style="cont")+ tm_borders(lwd=1,col="black") + tm_text("KAB",size=0.6)
## Warning: Currect projection of shape jabar1 unknown. Long-lat (WGS84) is
## assumed.
## Warning: The shape jabar1 is invalid. See sf::st_is_valid
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
CoordK = coordinates(jabar1)
W = poly2nb(jabar1, row.names = ID, queen = F)
W
## Neighbour list object:
## Number of regions: 27
## Number of nonzero links: 106
## Percentage nonzero weights: 14.54047
## Average number of links: 3.925926
WB = nb2mat(W, style = "B", zero.policy = T)
WB
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1
## 2 1 0 1 0 0 0 0 0 0 0 0 0 0 0
## 3 1 1 0 1 1 0 0 0 0 0 0 0 0 1
## 4 0 0 1 0 1 0 0 0 0 0 1 0 1 0
## 5 0 0 1 1 0 1 0 0 0 0 1 0 0 0
## 6 0 0 0 0 1 0 1 0 0 1 1 0 0 0
## 7 0 0 0 0 0 1 0 1 0 1 0 0 0 0
## 8 0 0 0 0 0 0 1 0 1 1 0 0 0 0
## 9 0 0 0 0 0 0 0 1 0 1 0 1 0 0
## 10 0 0 0 0 0 1 1 1 1 0 1 1 0 0
## 11 0 0 0 1 1 1 0 0 0 1 0 1 1 0
## 12 0 0 0 0 0 0 0 0 1 1 1 0 1 0
## 13 0 0 0 1 0 0 0 0 0 0 1 1 0 1
## 14 1 0 1 0 0 0 0 0 0 0 0 0 1 0
## 15 1 0 0 0 0 0 0 0 0 0 0 0 1 1
## 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0
## 17 0 0 1 1 0 0 0 0 0 0 0 0 1 1
## 18 0 0 0 0 0 1 1 0 0 0 0 0 0 0
## 19 1 0 0 0 0 0 0 0 0 0 0 0 0 0
## 20 0 1 0 0 0 0 0 0 0 0 0 0 0 0
## 21 0 0 0 1 0 0 0 0 0 0 0 0 0 0
## 22 0 0 0 0 0 0 0 0 1 0 0 0 0 0
## 23 1 0 0 0 0 0 0 0 0 0 0 0 0 0
## 24 1 0 0 0 0 0 0 0 0 0 0 0 0 0
## 25 0 0 0 1 0 0 0 0 0 0 0 0 0 0
## 26 0 0 0 0 0 1 1 0 0 0 0 0 0 0
## 27 0 0 0 0 0 0 1 0 0 0 0 0 0 0
## [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
## 1 1 1 0 0 1 0 0 0 1 1 0 0
## 2 0 0 0 0 0 1 0 0 0 0 0 0
## 3 0 0 1 0 0 0 0 0 0 0 0 0
## 4 0 0 1 0 0 0 1 0 0 0 1 0
## 5 0 0 0 0 0 0 0 0 0 0 0 0
## 6 0 0 0 1 0 0 0 0 0 0 0 1
## 7 0 0 0 1 0 0 0 0 0 0 0 1
## 8 0 0 0 0 0 0 0 0 0 0 0 0
## 9 0 0 0 0 0 0 0 1 0 0 0 0
## 10 0 0 0 0 0 0 0 0 0 0 0 0
## 11 0 0 0 0 0 0 0 0 0 0 0 0
## 12 0 0 0 0 0 0 0 0 0 0 0 0
## 13 1 0 1 0 0 0 0 0 0 0 0 0
## 14 1 0 1 0 0 0 0 0 0 0 0 0
## 15 0 1 0 0 0 0 0 0 0 0 0 0
## 16 1 0 0 0 0 0 0 0 1 0 0 0
## 17 0 0 0 0 0 0 1 0 0 0 1 0
## 18 0 0 0 0 0 0 0 0 0 0 0 0
## 19 0 0 0 0 0 0 0 0 0 0 0 0
## 20 0 0 0 0 0 0 0 0 0 0 0 0
## 21 0 0 1 0 0 0 0 0 0 0 1 0
## 22 0 0 0 0 0 0 0 0 0 0 0 0
## 23 0 1 0 0 0 0 0 0 0 1 0 0
## 24 0 0 0 0 0 0 0 0 1 0 0 0
## 25 0 0 1 0 0 0 1 0 0 0 0 0
## 26 0 0 0 0 0 0 0 0 0 0 0 0
## 27 0 0 0 0 0 0 0 0 0 0 0 0
## [,27]
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
## 7 1
## 8 0
## 9 0
## 10 0
## 11 0
## 12 0
## 13 0
## 14 0
## 15 0
## 16 0
## 17 0
## 18 0
## 19 0
## 20 0
## 21 0
## 22 0
## 23 0
## 24 0
## 25 0
## 26 0
## 27 0
## attr(,"call")
## nb2mat(neighbours = W, style = "B", zero.policy = T)
WL = nb2listw(W)
WL
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 27
## Number of nonzero links: 106
## Percentage nonzero weights: 14.54047
## Average number of links: 3.925926
##
## Weights style: W
## Weights constants summary:
## n nn S0 S1 S2
## W 27 729 27 16.73552 121.5371
plot(jabar1, axes=T, col="gray90")
text(CoordK[,1],CoordK[,2],jabar1$KAB,
col="black",cex=.5, pos=1.5)
points(CoordK[,1], CoordK[,2], pch=19, cex=0.7,col="blue")
plot(W,coordinates(jabar1), add=T, col='red')
setwd("C:/Materi Kuliah/Spatial")
HIV <- read.csv("HIV.csv")
Kasus <- HIV$Jumlah.Kasus.Penyakit.HIV.AIDS
moran.test(Kasus, WL)
##
## Moran I test under randomisation
##
## data: Kasus
## weights: WL
##
## Moran I statistic standard deviate = 0.49124, p-value = 0.3116
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.02879477 -0.03846154 0.01874434
moran.plot(Kasus, WL)
local <- localmoran(Kasus,WL)
local
## Ii E.Ii Var.Ii Z.Ii Pr(z != E(Ii))
## 1 -0.594967681 -2.742426e-01 4.836516e-01 -0.4611759 0.644672410
## 2 -0.628806942 -2.222564e-02 1.799382e-01 -1.4299715 0.152725195
## 3 0.025090069 -6.102102e-03 2.183352e-02 0.2110978 0.832810946
## 4 0.180668613 -5.666669e-03 1.651730e-02 1.4498581 0.147098090
## 5 0.047135719 -2.453999e-04 1.457318e-03 1.2411612 0.214546193
## 6 0.081636809 -2.483734e-02 8.719361e-02 0.3605802 0.718413308
## 7 0.085370466 -7.505096e-03 2.681557e-02 0.5671631 0.570603402
## 8 -0.158710114 -1.940572e-03 1.603676e-02 -1.2379508 0.215734304
## 9 1.087314962 -4.837547e-02 2.734496e-01 2.1718075 0.029870182
## 10 0.114246498 -7.974691e-02 2.641944e-01 0.3774202 0.705861353
## 11 -0.181062915 -2.308009e-02 8.117064e-02 -0.5545113 0.579228990
## 12 0.566669560 -9.835769e-02 5.267797e-01 0.9162731 0.359523698
## 13 0.085640758 -5.247353e-03 1.879135e-02 0.6630223 0.507316265
## 14 -0.199029993 -3.565451e-02 1.559625e-01 -0.4136917 0.679099920
## 15 -0.006841949 -2.166496e-06 1.286896e-05 -1.9066484 0.056566139
## 16 1.308049514 -2.049509e-01 1.349193e+00 1.3025727 0.192720677
## 17 0.420043000 -2.854523e-02 9.982945e-02 1.4197718 0.155674128
## 18 0.518401012 -2.665905e-02 3.362906e-01 0.9399116 0.347262917
## 19 -2.029868685 -2.222564e-02 5.867549e-01 -2.6209465 0.008768603
## 20 -0.159341228 -1.689878e-03 4.554960e-02 -0.7386784 0.460102299
## 21 0.167055700 -3.069892e-03 2.534067e-02 1.0687118 0.285199561
## 22 1.192829906 -4.350961e-02 1.123646e+00 1.1663341 0.243479382
## 23 -1.521113565 -3.565451e-02 2.846935e-01 -2.7840174 0.005369015
## 24 -0.204121838 -2.198728e-03 2.843286e-02 -1.1975006 0.231111505
## 25 0.269200394 -1.074622e-02 8.802250e-02 0.9435792 0.345384722
## 26 -0.049403316 -2.421170e-04 3.137076e-03 -0.8777279 0.380091381
## 27 0.361374011 -2.574014e-02 6.770948e-01 0.4704509 0.638032898
## attr(,"call")
## localmoran(x = Kasus, listw = WL)
## attr(,"class")
## [1] "localmoran" "matrix" "array"
## attr(,"quadr")
## mean median pysal
## 1 High-Low High-Low High-Low
## 2 Low-High Low-High Low-High
## 3 Low-Low Low-Low Low-Low
## 4 Low-Low Low-Low Low-Low
## 5 Low-Low High-Low Low-Low
## 6 Low-Low Low-Low Low-Low
## 7 Low-Low Low-Low Low-Low
## 8 Low-High High-High Low-High
## 9 High-High High-High High-High
## 10 High-Low High-High High-High
## 11 Low-High Low-High Low-High
## 12 High-High High-High High-High
## 13 Low-Low Low-Low Low-Low
## 14 Low-High Low-High Low-High
## 15 Low-High High-High Low-High
## 16 High-High High-High High-High
## 17 Low-Low Low-Low Low-Low
## 18 Low-Low Low-Low Low-Low
## 19 Low-High Low-High Low-High
## 20 High-Low High-Low High-Low
## 21 Low-Low High-Low Low-Low
## 22 High-High High-High High-High
## 23 Low-High Low-High Low-High
## 24 Low-High High-High Low-High
## 25 Low-Low Low-Low Low-Low
## 26 High-Low High-Low High-Low
## 27 Low-Low Low-Low Low-Low
setwd("C:/Materi Kuliah/Spatial")
HIV <- read.csv("HIV.csv", sep = ",", dec =",")
head(HIV)
## ID Wilayah Indeks.Pendidikan Indeks.Kesehatan Jumlah.Nikah
## 1 1 Bogor 62.52 79.46 32039
## 2 2 Sukabumi 57.73 79.29 17731
## 3 3 Cianjur 57.36 77.82 16989
## 4 4 Bandung 65.57 83.09 28714
## 5 5 Garut 59.85 79.77 22542
## 6 6 Tasikmalaya 60.74 76.85 14977
## Jumlah.Kasus.Penyakit.HIV.AIDS
## 1 251
## 2 14
## 3 39
## 4 40
## 5 61
## 6 11
summary(HIV)
## ID Wilayah Indeks.Pendidikan Indeks.Kesehatan
## Min. : 1.0 Length:27 Min. :56.72 Min. :76.85
## 1st Qu.: 7.5 Class :character 1st Qu.:60.06 1st Qu.:79.61
## Median :14.0 Mode :character Median :62.52 Median :80.97
## Mean :14.0 Mean :64.95 Mean :81.11
## 3rd Qu.:20.5 3rd Qu.:70.06 3rd Qu.:83.11
## Max. :27.0 Max. :77.33 Max. :85.35
## Jumlah.Nikah Jumlah.Kasus.Penyakit.HIV.AIDS
## Min. : 1737 Min. : 0.00
## 1st Qu.: 6349 1st Qu.: 13.50
## Median :12224 Median : 41.00
## Mean :12478 Mean : 66.52
## 3rd Qu.:16618 3rd Qu.: 76.50
## Max. :32039 Max. :251.00
y=HIV$Jumlah.Kasus.Penyakit.HIV.AIDS
x1=HIV$Indeks.Pendidikan
x2=HIV$Indeks.Kesehatan
x3=HIV$Jumlah.Nikah
modelols=lm(y~x1+x2+x3, data=HIV)
summary(modelols)
##
## Call:
## lm(formula = y ~ x1 + x2 + x3, data = HIV)
##
## Residuals:
## Min 1Q Median 3Q Max
## -80.42 -45.55 -24.92 37.14 151.07
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 332.096200 630.177960 0.527 0.6032
## x1 1.234264 3.222287 0.383 0.7052
## x2 -4.845815 9.575491 -0.506 0.6176
## x3 0.003789 0.001913 1.981 0.0597 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 68.62 on 23 degrees of freedom
## Multiple R-squared: 0.1597, Adjusted R-squared: 0.05004
## F-statistic: 1.457 on 3 and 23 DF, p-value: 0.2524
AIC(modelols)
## [1] 310.6363
shapiro.test(modelols$residuals)
##
## Shapiro-Wilk normality test
##
## data: modelols$residuals
## W = 0.89778, p-value = 0.01191
hist(modelols$residuals)
bptest(modelols)
##
## studentized Breusch-Pagan test
##
## data: modelols
## BP = 3.8303, df = 3, p-value = 0.2804
library(car)
## Warning: package 'car' was built under R version 4.2.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.2.3
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:maptools':
##
## pointLabel
vif(modelols)
## x1 x2 x3
## 2.603629 2.335181 1.199361
CoordK
## [,1] [,2]
## 0 106.7684 -6.561213
## 1 106.7101 -7.074610
## 2 107.1595 -7.128689
## 3 107.6104 -7.099844
## 4 107.7887 -7.359583
## 5 108.1412 -7.496875
## 6 108.4287 -7.289809
## 7 108.5594 -7.003270
## 8 108.5513 -6.745514
## 9 108.2570 -6.815748
## 10 107.9808 -6.825009
## 11 108.1686 -6.448652
## 12 107.7320 -6.484494
## 13 107.4305 -6.596106
## 14 107.3541 -6.251893
## 15 107.1207 -6.215102
## 16 107.4148 -6.896640
## 17 108.5181 -7.635446
## 18 106.7996 -6.593501
## 19 106.9244 -6.940236
## 20 107.6366 -6.919190
## 21 108.5535 -6.741885
## 22 106.9756 -6.280504
## 23 106.8170 -6.396059
## 24 107.5432 -6.886631
## 25 108.2199 -7.360152
## 26 108.5676 -7.376175
bandwidth_optimal <- gwr.sel(y ~ x1 + x2 + x3,
data = HIV,
coords = CoordK)
## Bandwidth: 0.894596 CV score: 158030.8
## Bandwidth: 1.446042 CV score: 158479.8
## Bandwidth: 0.5537839 CV score: 155214.2
## Bandwidth: 0.3431504 CV score: 149240.9
## Bandwidth: 0.2129718 CV score: 203232.3
## Bandwidth: 0.4236052 CV score: 150682.7
## Bandwidth: 0.2716146 CV score: 153332
## Bandwidth: 0.3652454 CV score: 149274.7
## Bandwidth: 0.3514671 CV score: 149213
## Bandwidth: 0.3520315 CV score: 149213
## Bandwidth: 0.3518904 CV score: 149213
## Bandwidth: 0.3519311 CV score: 149213
## Bandwidth: 0.3518497 CV score: 149213
## Bandwidth: 0.3518904 CV score: 149213
gwrmodel=gwr(y~x1+x2+x3,
data = HIV,
coords = CoordK,
adapt = bandwidth_optimal,
hatmatrix = TRUE,
se.fit = TRUE)
gwrmodel
## Call:
## gwr(formula = y ~ x1 + x2 + x3, data = HIV, coords = CoordK,
## adapt = bandwidth_optimal, hatmatrix = TRUE, se.fit = TRUE)
## Kernel function: gwr.Gauss
## Adaptive quantile: 0.3518904 (about 9 of 27 data points)
## Summary of GWR coefficient estimates at data points:
## Min. 1st Qu. Median 3rd Qu. Max.
## X.Intercept. -2.3926761 292.6839499 501.1522279 697.5436783 953.7364270
## x1 -1.2244906 -0.5868128 1.1621584 3.2251761 5.8431556
## x2 -16.5837259 -11.4513153 -6.2591380 -2.3298919 1.1771678
## x3 0.0010790 0.0015901 0.0028017 0.0051880 0.0064184
## Global
## X.Intercept. 332.0962
## x1 1.2343
## x2 -4.8458
## x3 0.0038
## Number of data points: 27
## Effective number of parameters (residual: 2traceS - traceS'S): 9.258443
## Effective degrees of freedom (residual: 2traceS - traceS'S): 17.74156
## Sigma (residual: 2traceS - traceS'S): 65.29961
## Effective number of parameters (model: traceS): 7.34044
## Effective degrees of freedom (model: traceS): 19.65956
## Sigma (model: traceS): 62.03254
## Sigma (ML): 52.93276
## AICc (GWR p. 61, eq 2.33; p. 96, eq. 4.21): 316.4536
## AIC (GWR p. 96, eq. 4.22): 298.2903
## Residual sum of squares: 75650.69
## Quasi-global R2: 0.4129815
residualgwr=gwrmodel$SDF$gwr.e
shapiro.test(residualgwr)
##
## Shapiro-Wilk normality test
##
## data: residualgwr
## W = 0.87767, p-value = 0.00428
hist(residualgwr)
bptest(gwrmodel$lm, weight = gwrmodel$gweight)
##
## studentized Breusch-Pagan test
##
## data: gwrmodel$lm
## BP = 3.8303, df = 3, p-value = 0.2804
gwr.morantest(gwrmodel, WL, zero.policy = TRUE)
##
## Leung et al. 2000 three moment approximation for Moran's I
##
## data: GWR residuals
## statistic = 18.5, df = 27.057, p-value = 0.1109
## sample estimates:
## I
## 0.04529308
Karena Pvalue > 0.05, maka h0 diterima,dapat disimpulkan tidak terdapat autokorelasi spatial pada GWR
LMZ.F3GWR.test(gwrmodel)
##
## Leung et al. (2000) F(3) test
##
## F statistic Numerator d.f. Denominator d.f. Pr(>)
## (Intercept) 0.59691 11.06087 20.504 0.81075
## x1 1.61812 10.95487 20.504 0.16692
## x2 0.93836 9.69037 20.504 0.51855
## x3 2.67185 7.61823 20.504 0.03628 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Dapat dilihat, bahwa variabel jumlah nikah merupakan variabel satu-satunya yang berpengaruh terhadap angka HIV
coefx1 <- gwrmodel$SDF$x1
coefx2 <- gwrmodel$SDF$x2
coefx3 = gwrmodel$SDF$x3
spplot(gwrmodel$SDF,"x1", main = "Koefisien GWR untuk x1", col.regions = terrain.colors(100))
spplot(gwrmodel$SDF,"x2", main = "Koefisien GWR untuk x2", col.regions = terrain.colors(100))
spplot(gwrmodel$SDF,"x3", main = "Koefisien GWR untuk x3", col.regions = terrain.colors(100))
Model dibandingkan dengan AIC. Semakin kecil AIC, maka semakin baik model
AIC(modelols)
## [1] 310.6363
gwrmodel$results$AICc
## [1] 318.6251
Model OLS lebih baik dari GWR