Praktikum R untuk Spatial
Analisis Spatial dengan R
Library
Berikut beberapa library yang digunakan untuk melakukan analisis spatial untuk r
Plot Map
Pada praktikum ini akan digunakan peta shapefile provinsi Jawa Barat.
## OGR data source with driver: ESRI Shapefile
## Source: "D:\kuliah\Semester I\Research Method\Spatial with R\petajabar27", layer: "Peta Jabar 27"
## with 27 features
## It has 2 fields
Dataset yang akan digunakan dalam praktikum ini adalah data gizi di Jawa Barat :
data.giziburuk=read_excel("data giziburuk jabar.xlsx", sheet="Sheet1")
grid.table(head(data.giziburuk))Plot juga dapat ditambahkan label untuk masing-masing kota dan memanfaatkan variabel dari dataset untuk melakukan plot warna pada peta :
#Memberi warna peta berdasarkan data
colfunc<-colorRampPalette(c("green", "yellow","red"))
jabar$gizi=data.giziburuk$Giziburuk
spplot(jabar, "gizi", col.regions=colfunc(6), cuts = 5, main="Peta Sebaran")Spatial Weighted : Contiguity
Pada bagian ini akan digunakan data colombus:
#membaca data colombus
columbus.map <- readOGR(system.file("shapes/columbus.shp", package="spData"))## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\jwst2\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
Queen
Berikut potongan kode untuk menghitung penimbang spasial dengan metode contiguity queen pada dataset columbus
#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
grid.table(head(queen.w1))Berikut potongan kode untuk memplot hasil di atas
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
Berikut potongan kode untuk menghitung penimbang spasial dengan metode contiguity rook pada dataset columbus
Berikut potongan kode untuk memplot hasil di atas
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")Spatial Weighted : Distance
Pertama, dilakukan ekstraksi kordinat untuk menyusun matriks spasial berdasarkan jarak.
#Ekstrak koordinat
coordinates(columbus)=~X+Y #x=long; y=lat
D=as.matrix(dist(coordinates(columbus),method = "euclidean"))
grid.table(head(D))Power Distance Weights.
Inversi jarak sama dengan Power Distance Weights dengan alpha=1. dapat dilihat dari kode di bawah :
#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
Kemudian dilakukan plot untuk weight tersebut :
#inverse weight matrix - plot
plot(st_geometry(columbus.map), border="white",col='gray')
plot(invers.w, coords, add = TRUE, col = "red")Exponential Distance Weights
Berikut cara penghitungan penimbang dengan Exponential Distance Weights
#Eksponential weight matrix
alpha=2
w2=exp(-alpha*D)
#Eksponential weight matrix - row-normalized
diag(w2)<-0
rtot<-rowSums(w2, na.rm =T)
w_std<-w2/rtot
eksp.w=mat2listw(w_std, style="W") #untuk melihat matriks W
summary(eksp.w)## 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
Kemudian dilakukan plot untuk weight tersebut :
#inverse weight matrix - plot
plot(st_geometry(columbus.map), border="white",col='gray')
plot(eksp.w, coords, add = TRUE, col = "red")K-Nearest Neighbour Weights
Berikut cara penghitungan penimbang dengan k-Nearest Neighbour Weights
#Eksponential weight matrix - row-normalized
#K-nn weight matrix
w3=knearneigh(coordinates(columbus), k=2, longlat = TRUE)
knn.w=nb2listw(knn2nb(w3))
summary(knn.w)## 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
Kemudian dilakukan plot untuk weight tersebut :
#K-nn weight matrix - plot
plot(st_geometry(columbus.map), border="white",col='gray')
plot(knn.w, coords, add = TRUE, col = "red")Jodi Jhouranda Siregar, IPB University, jodijhouranda@apps.ipb.ac.id,https://rpubs.com/jodijhouranda/accentproblem↩︎