-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-Association.r
237 lines (175 loc) · 6.26 KB
/
09-Association.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
234
235
236
237
# 09) Spatial Association
# Libraries
library(sp)
library(spdep)
library(rgdal)
library(rgeos)
library(tmap)
library(tmaptools)
library(raster)
library(elsa)
# My custom function
myMap = function(inPol, invar="A",legendTitle="Υπόμνημα", inTitle="Τίτλος"){
newVarName = paste0("Rounded_",invar)
inPol[[newVarName]] = round(inPol[[invar]],2)
result=tm_shape(inPol) +
tm_fill(newVarName, palette="RdBu", style="pretty", midpoint=NA,
legend.format=list(text.separator=""), title=legendTitle) +
tm_text(newVarName, size=1.7, print.tiny=T)+
tm_layout(main.title=inTitle, title.size =1.8, legend.title.size=1.5,
legend.text.size=1.2, legend.position=c(1,0.6),
outer.margins=c(0,0,0,0.15),main.title.position="center") +
tm_borders(alpha=0.9, col="black",lwd=2, lty=1)
print(result)
}
# Create some data
n=5
A = matrix(0, nrow=n, ncol =n, byrow=FALSE)
A[c(1,7:8, 21:22)]=1
A[c(13:14,10)]=2
A[c(5,11)]=1
A[c(16,19, 25)]=3
#A[c(16,19,25)]=3
rA = raster(A)
B = matrix(0, nrow=n, ncol =n, byrow=FALSE)
B[c(1:10)]=2
B[c(13:15, 18:20)]=1
B[c(24:25)]=4
B[c(24:25)]=3
rB = raster(B)
r= raster::brick(rA,rB)
names(r)=c("A","B")
proj4string(r) = CRS("+init=epsg:4326")
pA=rasterToPolygons(r, dissolve=FALSE)
library(stringr)
pA$id = paste0("P", str_pad(1:length(pA), 2, pad = "0") )
writeOGR(pA, "pA.geojson", layer="pA", driver="GeoJSON", overwrite=T)
#readOGR("pa.geojson")
#writeOGR(pA, "pA","pA",driver="ESRI Shapefile")
# Visualization of initial data
myMap(pA, "A", inTitle = "Values of variable 'A'")
myMap(pA, "B", inTitle = "Values of variable 'B'")
centers = coordinates(pA)
# Coordinates
library(ggplot2)
ggplot(pA, aes(x = long, y = lat, group = id)) +
geom_polygon(colour='cyan4', fill='white') + coord_equal()
# Define the neighbour
neighbours = poly2nb(pA, queen = T)
neighbours
par( mfrow=c(1,1), mar =c(0,0,1,0) )
plot(pA, col='white', border='black', main="Γειτονία με βάση τα κοινά όρια", lwd=2)
plot(neighbours, centers, col='red', lwd=0.7, lty=5, add=TRUE, pch=21)
par( mfrow=c(1,1), mar =c(4,4,4,4) )
# Convert the neighbour structure to
# weights structure
listw = nb2listw(neighbours)
listw
# Global test of Morans' Ι
I1 = moran.test(pA$A,listw)
I2 = moran.test(pA$B,listw)
I1
I2
# Value of the test of Moran’s I
I1[["estimate"]][["Moran I statistic"]]
# ‘p’ value for the evaluation
# of statistical importance of the model.
I1[["p.value"]]
# As ‘p’ value has been calculated analyticaly and not by a Monte Carlo Simulation
# For better examination of the statistical iportance of the model, we conduct a Monte Carlo Simulation.
sim1 = moran.mc(pA$A, listw, nsim=599)
sim1
# Visualize the distribution of values
# of the Monte Carlo Simulation of Moran's I
plot(sim1, main="Κατανομή τιμών της Monte Carlo προσομοίωσης του Moran's I",
xlab="Values of variable 'Α'",
ylab="Πυκνότητα",
lty=2, col="red", lwd=2)
# Global test of Geary’s C
C1 = geary.test(pA$A,listw)
C2 = geary.test(pA$B,listw)
C1
C2
sim2 = geary.mc(pA$A, listw, nsim=599)
sim2
# Visualize the distribution of values of the Monte Carlo simulations of Geary's C
plot(sim2, main="Κατανομή τιμών της Monte Carlo προσομοίωσης του Geary's C",
xlab="Values of variable 'Α'",
ylab="Πυκνότητα", lty=2, col="blue", lwd=2)
# Moran's scaterplot
moran.plot(pA$A, listw = nb2listw(neighbours, style = "W"),
main="Moran scaterplot",
xlab="Values of variable 'Α'", pch=19,
ylab="Values of variable 'Α' at spatial lag",
col="red")
# Local Moran
# Positive values of Ii indicate that a polygon
# has other polygons with similar values in its neighbour.
# * Ii: local moran statistic
# * E.Ii: Αναμενόμενη τιμή του: local moran statistic
# * Var.Ii: μεταβλητότητα της τιμής του: local moran statistic
# * Z.Ii: Τυπικές αποκλίσεις των τιμών του Ι (z values)
# * Pr(): p-value
# Οπτικοποίηση τοπικού δείκτη Moran
local = localmoran(x = pA$A, listw = nb2listw(neighbours, style = "W"))
head(local)
# Attach results back to polygons
moran.map = cbind(pA, local)
head(moran.map)
myMap(moran.map, "Ii",inTitle = "Local Moran (values of Ι)")
myMap(moran.map, "Pr.z....E.Ii..",inTitle = "Local Moran (p values)")
# Map of local moran (categories)
# Create an empty vector
quadrant = rep(0,nrow(local))
# Center of the variable, around its Mean
m.VariableA = pA$A - mean(pA$A)
# Center of local Moran, around the Mean
m.local = local[,1] - mean(local[,1])
# Level of Significance
signif = 0.5
# Καταμερισμός πολυγώνων σε κατηγορίες
quadrant[m.VariableA >0 & m.local>0] = 4 # HH
quadrant[m.VariableA <0 & m.local<0] = 1 # LL
quadrant[m.VariableA <0 & m.local>0] = 2 # LH
quadrant[m.VariableA >0 & m.local<0] = 3 # HL
quadrant[local[,5]>signif] = 0
# Visualization of categories map
brks = c(0:4)
colors = c("white","blue",rgb(0,0,1,alpha=0.4),rgb(1,0,0,alpha=0.4),"red")
par(mar=c(0,0,1,7), xpd=TRUE )
plot(pA ,col=colors[findInterval(quadrant,brks,all.inside=FALSE)],
main="Χάρτης κατηγοριών Moran")
legend("right", legend = c("Ασύμαντο","LL","LH","HL","HH"),
inset=c(-0.18), cex=1.5, fill=colors,bty="n")
text(centers, label=paste0(pA$id,"\n",pA$A), cex=1.5 )
# Loacal
# Loacal Moran’s I:
library(elsa)
lisa.i = lisa(pA, d1=0,d2=0.2,statistic='I', zcol="A")
summary(lisa.i$Ii)
myMap(lisa.i,"Ii", inTitle = "Τοπικός Δεικτης Moran")
# Loacal Geary’s C
lisa.c = lisa(pA, d1=0, d2=0.2, statistic='c', longlat=F, zcol="A")
summary(lisa.c$LocalGeray)
myMap(lisa.c,"LocalGeray", inTitle = "Τοπικός Δεικτης Geary")
# Global
# Global Moran's I
elsa::moran(r$A)
elsa::moran(r$B)
# Global Geary’s C
elsa::geary(r$A)
elsa::geary(r$B)
# Getis-Ord
nb = dnearneigh(centers, 0, 0.3)
nb
# Binary neighbouring matrix
nb_lw = nb2listw(nb, style = 'B')
nb_lw
plot(pA)
plot(nb, centers, add=T, col="red")
local_g = localG(pA$A, nb_lw)
local_g = cbind(pA, as.matrix(local_g)) # pA or pA@data
names(local_g)[4] = "gstat"
head(local_g)
# Χάρτης Getis-Ord "G"
myMap(local_g,"gstat", inTitle = "Map of Getis Org G")