Spasial Dengan R
Loading Data Set
Data Colombus
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/Febri/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/Febri/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/Febri/Documents/R/win-library/4.0/rgdal/proj
## character(0)
## 'data.frame': 49 obs. of 22 variables:
## $ AREA : num 0.3094 0.2593 0.1925 0.0838 0.4889 ...
## $ PERIMETER : num 2.44 2.24 2.19 1.43 3 ...
## $ COLUMBUS. : int 2 3 4 5 6 7 8 9 10 11 ...
## $ COLUMBUS.I: int 5 1 6 2 7 8 4 3 18 10 ...
## $ POLYID : int 1 2 3 4 5 6 7 8 9 10 ...
## $ NEIG : int 5 1 6 2 7 8 4 3 18 10 ...
## $ HOVAL : num 80.5 44.6 26.4 33.2 23.2 ...
## $ INC : num 19.53 21.23 15.96 4.48 11.25 ...
## $ CRIME : num 15.7 18.8 30.6 32.4 50.7 ...
## $ OPEN : num 2.851 5.297 4.535 0.394 0.406 ...
## $ PLUMB : num 0.217 0.321 0.374 1.187 0.625 ...
## $ DISCBD : num 5.03 4.27 3.89 3.7 2.83 3.78 2.74 2.89 3.17 4.33 ...
## $ X : num 38.8 35.6 39.8 36.5 40 ...
## $ Y : num 44.1 42.4 41.2 40.5 38 ...
## $ AREA : num 10.39 8.62 6.98 2.91 16.83 ...
## $ NSA : num 1 1 1 1 1 1 1 1 1 1 ...
## $ NSB : num 1 1 1 1 1 1 1 1 1 1 ...
## $ EW : num 1 0 1 0 1 1 0 0 1 1 ...
## $ CP : num 0 0 0 0 0 0 0 0 0 0 ...
## $ THOUS : num 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 ...
## $ NEIGNO : num 1005 1001 1006 1002 1007 ...
## $ PERIM : num 2.44 2.24 2.19 1.43 3 ...
## starting httpd help server ...
## done
## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\Febri\Documents\R\win-library\4.0\spData\shapes\columbus.shp", layer: "columbus"
## with 49 features
## It has 20 fields
## Integer64 fields read as strings: COLUMBUS_ COLUMBUS_I POLYID
Data Gizi Buruk
library(readxl)
setwd("F:\\Pascasarjana\\02 Kuliah\\Semester 1\\STA 500 MPK\\SPASIAL DENGAN R\\Spatial with R")
data.giziburuk<-read_excel("data giziburuk jabar.xlsx", sheet="Sheet1")
dim(data.giziburuk)## [1] 27 5
## tibble [27 x 5] (S3: tbl_df/tbl/data.frame)
## $ kode kabko: num [1:27] 3201 3202 3203 3204 3205 ...
## $ KABKOT : chr [1:27] "BOGOR" "SUKABUMI" "CIANJUR" "BANDUNG" ...
## $ Giziburuk : num [1:27] 211 117 133 186 132 110 104 111 159 129 ...
## $ Long : num [1:27] 107 107 107 108 108 ...
## $ Lat : num [1:27] -6.54 -7.07 -7.05 -7.07 -7.34 ...
Plot Data
## OGR data source with driver: ESRI Shapefile
## Source: "F:\Pascasarjana\02 Kuliah\Semester 1\STA 500 MPK\SPASIAL DENGAN R\Spatial with R\petajabar27", layer: "Peta Jabar 27"
## with 27 features
## It has 2 fields
palette(rainbow(6))
#Memberi Warna pada Peta
colfunc<-colorRampPalette(c("green", "yellow","red"))
jabar$gizi<-data.giziburuk$Giziburuk
spplot(jabar, "gizi", col.regions=colfunc(6), cuts = 5, main="Peta Sebaran")Spatial Weights
Queen Weight
## Loading required package: sf
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
#queen weight - matriks
columbus.map <- st_read(system.file("shapes/columbus.shp", package="spData"), quiet=TRUE)
queen.w <- poly2nb(as(columbus.map, "Spatial"),queen = T)
summary(queen.w)## Neighbour list object:
## Number of regions: 49
## Number of nonzero links: 236
## Percentage nonzero weights: 9.829238
## Average number of links: 4.816327
## Link number distribution:
##
## 2 3 4 5 6 7 8 9 10
## 5 9 12 5 9 3 4 1 1
## 5 least connected regions:
## 1 6 42 46 47 with 2 links
## 1 most connected region:
## 20 with 10 links
queen.w1<-nb2mat(queen.w,style = "B") #untuk melihat bentuk matriks bobot
View(queen.w1)
#queen weights - plot
plot(st_geometry(columbus.map), border="white",col='gray')
coords <- st_coordinates(st_centroid(st_geometry(columbus.map)))
plot(queen.w, coords, add = TRUE, col = "red")Rook Weight
#rook weights - matriks dan plot
plot(st_geometry(columbus.map), border="white",col='gray')
rook.w <- poly2nb(as(columbus.map, "Spatial"), queen=FALSE)
coords <- st_coordinates(st_centroid(st_geometry(columbus.map)))
plot(rook.w, coords, add = TRUE, col = "red")#rook and queen - plot
plot(st_geometry(columbus.map), border="white",col='grey')
plot(queen.w, coords, add = TRUE, col = "red")
plot(rook.w, coords, add = TRUE, col = "blue")#Ekstrak koordinat
coordinates(columbus)=~X+Y #x=long; y=lat
D=as.matrix(dist(coordinates(columbus),method = "euclidean"))
head(D)## 1005 1001 1006 1002 1007 1008 1004 1003
## 1005 0.000000 3.601180 3.064719 4.229952 6.189426 6.888151 7.850425 5.753061
## 1001 3.601180 0.000000 4.368067 2.057670 6.201331 8.700973 4.568205 3.828448
## 1006 3.064719 4.368067 0.000000 3.384967 3.185671 4.365192 7.028833 3.971525
## 1002 4.229952 2.057670 3.384967 0.000000 4.320936 7.355277 3.783080 1.822142
## 1007 6.189426 6.201331 3.185671 4.320936 0.000000 3.952975 6.662624 3.375514
## 1008 6.888151 8.700973 4.365192 7.355277 3.952975 0.000000 10.426360 7.063039
## 1018 1010 1038 1037 1039 1040 1009 1036
## 1005 9.378280 11.667848 7.827862 8.123510 8.182381 9.012620 9.195744 10.061812
## 1001 10.143177 13.389615 6.476057 6.439544 5.989098 6.780740 8.468827 8.532900
## 1006 6.385297 9.129169 5.262634 5.730594 6.150225 6.924335 6.273516 7.438977
## 1002 8.326080 11.842387 4.430677 4.444875 4.156547 4.981447 6.414375 6.533266
## 1007 4.011398 7.762503 2.748746 3.439300 4.363894 4.915698 3.115782 4.601574
## 1008 3.374271 4.804083 6.609873 7.335529 8.314708 8.833079 6.019675 8.053725
## 1011 1042 1041 1017 1043 1019 1012
## 1005 13.711330 10.490235 9.888868 14.517249 14.443022 11.552360 15.724901
## 1001 15.188420 8.476589 7.721270 15.266075 12.058740 11.816469 17.040922
## 1006 11.040654 8.134895 7.680756 11.562050 12.195293 8.497112 12.984798
## 1002 13.514809 6.609276 5.906539 13.371051 10.385875 9.850914 15.290814
## 1007 9.272355 5.616581 5.405377 9.068987 9.623994 5.648116 10.990111
## 1008 6.823586 9.299977 9.229546 7.949672 13.115199 5.832392 8.848529
## 1035 1032 1020 1021 1031 1033 1034
## 1005 12.180329 11.204003 11.175591 13.482362 13.407051 13.461476 13.729476
## 1001 10.336562 10.209682 10.758148 13.615500 12.812749 12.435339 12.322736
## 1006 9.643470 8.312736 8.146025 10.423137 10.387420 10.540475 10.937541
## 1002 8.430593 8.157769 8.712137 11.614241 10.755487 10.391479 10.326326
## 1007 6.823210 5.161093 4.987386 7.507044 7.218034 7.365712 7.840975
## 1008 10.139951 7.760932 6.675627 7.672605 8.728000 9.533651 10.466923
## 1045 1013 1022 1044 1023 1046 1030 1024
## 1005 17.89268 18.12241 14.661476 17.94064 15.67553 19.60817 15.257552 15.453209
## 1001 14.69637 19.11704 14.677561 15.08192 15.47664 16.44723 13.826194 14.640233
## 1006 16.41655 15.26096 11.598568 16.03672 12.61168 18.04781 12.450687 12.459871
## 1002 13.67927 17.25701 12.656001 13.72953 13.43263 15.38600 11.840709 12.587263
## 1007 14.56390 12.94056 8.630649 13.73913 9.56078 16.06710 9.334264 9.277353
## 1008 18.40872 11.32460 8.886894 17.37254 10.10012 19.85284 11.785435 10.844079
## 1047 1016 1014 1049 1029 1025 1028 1048
## 1005 22.66643 18.79971 20.48088 20.03867 16.73740 17.33559 18.22742 22.68873
## 1001 19.37661 19.31722 21.36729 17.12308 15.86295 16.88245 16.93904 19.51695
## 1006 21.26934 15.80718 17.59093 18.16040 13.74878 14.28303 15.33815 21.10079
## 1002 18.47516 17.35142 19.46962 15.82005 13.81481 14.82555 14.93858 18.46611
## 1007 19.39829 13.13278 15.17079 15.84918 10.56534 11.16443 12.16958 19.03827
## 1008 23.21116 12.28082 13.70732 19.44898 12.08634 11.98129 14.14177 22.76628
## 1015 1027 1026
## 1005 22.37715 18.32913 19.49792
## 1001 22.95545 17.40226 18.79252
## 1006 19.40692 15.34309 16.46848
## 1002 20.98929 15.35996 16.73865
## 1007 16.76878 12.15936 13.30854
## 1008 15.75123 13.61896 14.36067
Inverse Weight Matrix
w=1/D
# inverse weight matrix - row-normalized
diag(w)<-0
rtot<-rowSums(w, na.rm =T)
w_std<-w/rtot
rowSums(w_std, na.rm=T)## 1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1026
## 1
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 49
## Number of nonzero links: 2352
## Percentage nonzero weights: 97.95918
## Average number of links: 48
## Link number distribution:
##
## 48
## 49
## 49 least connected regions:
## 1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027 1026 with 48 links
## 49 most connected regions:
## 1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027 1026 with 48 links
##
## Weights style: W
## Weights constants summary:
## n nn S0 S1 S2
## W 49 2401 49 3.256803 197.9015
#inverse weight matrix - plot
plot(st_geometry(columbus.map), border="white",col='gray')
plot(invers.w, coords, add = TRUE, col = "red")Eksponential Weight Matrix
alpha=2
w2=exp(-alpha*D)
#Eksponential weight matrix - row-normalized
diag(w2)<-0
rtot<-rowSums(w2, na.rm =T)
rtot## 1005 1001 1006 1002 1007 1008
## 0.003150253 0.017823292 0.005763766 0.045100893 0.011300088 0.001792430
## 1004 1003 1018 1010 1038 1037
## 0.004261037 0.049090821 0.010424809 0.013234614 0.300765245 0.417464829
## 1039 1040 1009 1036 1011 1042
## 0.350515673 0.444919714 0.059724386 0.153617820 0.030111304 0.316560932
## 1041 1017 1043 1019 1012 1035
## 0.420307425 0.012134291 0.003374524 0.043723238 0.022265122 0.055995129
## 1032 1020 1021 1031 1033 1034
## 0.084148018 0.066912227 0.126129961 0.085267602 0.131871874 0.118723352
## 1045 1013 1022 1044 1023 1046
## 0.033336811 0.018316285 0.175084301 0.019984866 0.116094481 0.042800517
## 1030 1024 1047 1016 1014 1049
## 0.067440873 0.121961269 0.045022278 0.010368792 0.017752766 0.024420349
## 1029 1025 1028 1048 1015 1027
## 0.147947913 0.057594408 0.035246053 0.046590528 0.006467291 0.106080820
## 1026
## 0.042992874
## 1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027
## 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## 1026
## 1
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 49
## Number of nonzero links: 2352
## Percentage nonzero weights: 97.95918
## Average number of links: 48
## Link number distribution:
##
## 48
## 49
## 49 least connected regions:
## 1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027 1026 with 48 links
## 49 most connected regions:
## 1005 1001 1006 1002 1007 1008 1004 1003 1018 1010 1038 1037 1039 1040 1009 1036 1011 1042 1041 1017 1043 1019 1012 1035 1032 1020 1021 1031 1033 1034 1045 1013 1022 1044 1023 1046 1030 1024 1047 1016 1014 1049 1029 1025 1028 1048 1015 1027 1026 with 48 links
##
## Weights style: W
## Weights constants summary:
## n nn S0 S1 S2
## W 49 2401 49 38.80069 206.8219
#Eksponential weight matrix - plot
plot(st_geometry(columbus.map), border="white",col='gray')
plot(eksp.w, coords, add = TRUE, col = "red")K-nn Weight Matrix
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 49
## Number of nonzero links: 98
## Percentage nonzero weights: 4.081633
## Average number of links: 2
## Non-symmetric neighbours list
## Link number distribution:
##
## 2
## 49
## 49 least connected regions:
## 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 with 2 links
## 49 most connected regions:
## 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 with 2 links
##
## Weights style: W
## Weights constants summary:
## n nn S0 S1 S2
## W 49 2401 49 41.5 212
#K-nn weight matrix - plot
plot(st_geometry(columbus.map), border="white",col='gray')
plot(knn.w, coords, add = TRUE, col = "red")