-
Notifications
You must be signed in to change notification settings - Fork 4
/
Geographically weighted regressions for prioritizing educational planning, policies, and interventions - Supporting code.r
230 lines (185 loc) · 11.1 KB
/
Geographically weighted regressions for prioritizing educational planning, policies, and interventions - Supporting code.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
options(warn = -1)
#install.packages("GWmodel")
library("GWmodel")
#install.packages("gwrr")
library("gwrr")
#install.packages("sp")
library("sp")
#install.packages("gstat")
library("gstat")
#install.packages("ModelMap")
library("ModelMap")
#install.packages("RColorBrewer")
library("RColorBrewer")
#install.packages("ape")
library("ape")
#install.packages("usdm")
library("usdm")
#install.packages("rgdal")
library("rgdal")
#install.packages("tidyr")
library("tidyr")
#install.packages("MASS")
library("MASS")
#install.packages("Compositional")
library("Compositional")
#install.packages("dplyr")
library("dplyr")
#install.packages("spatialEco")
library("spatialEco")
#install.packages("broom")
library("broom")
install.packages("pastecs")
library("pastecs")
setwd("C:\\Users\\g.vargas\\BOX\\IIEP_MyProjects\\MP_01000298_RND_SDA\\WorkFiles_Experts\\298-Issue-Papers\\298-Issue-Paper-GWR\\Replication files")
PolygonShape <- readOGR(dsn = "Data\\Colombia", layer = "GWR - Colombia", drop_unsupported_fields=FALSE, disambiguateFIDs=TRUE)
cat(" ")
cat("The final names of the variables that were imported are the following:")
colnames(PolygonShape@data)
ListVariables <- list("E_s11_to_1", "MurderRate", "ProxyGDP", "Poverty", "SISBEN1PC", "GC_indrura", "Vulnerabil", "Threat", "Lack_Respo", "TransEducP", "TransAlimE", "HS_Cober_7", "HS_Cober_1", "G_IGA_tota", "GC_discapi")
PolygonShapeTrimmed <- PolygonShape[c("Lat", "Long", "admin2Pcod", "E_s11_to_1", "SISBEN1PC", "MurderRate", "ProxyGDP", "Poverty", "GC_indrura", "Vulnerabil", "Threat", "Lack_Respo", "TransEducP", "TransAlimE", "HS_Cober_7", "HS_Cober_1", "G_IGA_tota", "GC_discapi")]
PolygonShapeTrimmed <- sp.na.omit(PolygonShapeTrimmed)
VariablesToSummarize <- PolygonShapeTrimmed[c("E_s11_to_1", "SISBEN1PC", "MurderRate", "ProxyGDP", "Poverty", "GC_indrura", "Vulnerabil", "Threat", "Lack_Respo", "TransEducP", "TransAlimE", "HS_Cober_7", "HS_Cober_1", "G_IGA_tota", "GC_discapi")]@data
stat.desc(VariablesToSummarize)
DeVar <- ListVariables[[1]]
InDeVars <- unlist(ListVariables[2:length(ListVariables)], recursive = TRUE, use.names = FALSE)
model.sel <- model.selection.gwr(DeVar ,InDeVars, data = PolygonShapeTrimmed,
kernel = "bisquare", adaptive = TRUE, bw = 80)
sorted.models <- model.sort.gwr(model.sel, numVars = length(InDeVars),
ruler.vector = model.sel[[2]][,2])
model.list <- sorted.models[[1]]
model.view.gwr(DeVar, InDeVars, model.list = model.list)
plot(sorted.models[[2]][,2], col = "black", pch = 20, lty = 5,
main = "Alternative view of GWR model selection procedure",
ylab = "AICc", xlab = "Model number", type = "b")
attach(PolygonShapeTrimmed@data)
modelOLS <- lm(as.formula(sorted.models[[1]][[length(sorted.models[[1]])]][[1]]))
summary(modelOLS)
tidy_modelOLS <- tidy(modelOLS)
write.csv(tidy_modelOLS, "Tables\\ModelOLS.csv")
DependentVariable <- c(ListVariables[[1]])
# MoransIListVariables <- c(ListVariables[[1]])
# MoransIListVariables <- append(MoransIListVariables, "Lat")
# MoransIListVariables <- append(MoransIListVariables, "Long")
PolygonShapeData <- PolygonShapeTrimmed@data
PolygonShapeData.dists <- as.matrix(dist(cbind(PolygonShapeData$Long, PolygonShapeData$Lat)))
PolygonShapeData.dists.inv <- 1/PolygonShapeData.dists
diag(PolygonShapeData.dists.inv) <- 0
Moran.I(PolygonShapeData$E_s11_to_1, PolygonShapeData.dists.inv, na.rm=FALSE)
# gw.ss.bx <- gwss(PolygonShapeTrimmed, PolygonShapeTrimmed, vars = c("E_s11_to_1", "MurderRate", "ProxyGDP", "Poverty"),
# kernel = "boxcar", adaptive = TRUE, bw = 48, quantile = TRUE)
gw.ss.bx <- gwss(PolygonShapeTrimmed, PolygonShapeTrimmed, vars = c(ListVariables),
kernel = "boxcar", adaptive = TRUE, bw = 48, quantile = TRUE)
# gw.ss.bs <- gwss(PolygonShapeTrimmed, PolygonShapeTrimmed, vars = c("E_s11_to_1", "MurderRate", "ProxyGDP", "Poverty"),
# kernel = "bisquare", adaptive = TRUE, bw = 48)
gw.ss.bs <- gwss(PolygonShapeTrimmed, PolygonShapeTrimmed, vars = c(ListVariables),
kernel = "bisquare", adaptive = TRUE, bw = 48)
map.na = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(329000,261500), scale = 4000, col=1)
map.scale.1 = list("SpatialPolygonsRescale", layout.scale.bar(),
offset = c(326500,217000), scale = 5000, col=1, fill=c("transparent","blue"))
map.scale.2 = list("sp.text", c(326500,217900), "0", cex=0.9, col=1)
map.scale.3 = list("sp.text", c(331500,217900), "5km", cex=0.9, col=1)
map.layout <- list(map.na,map.scale.1,map.scale.2,map.scale.3)
mypalette.1 <- brewer.pal(9, "Reds")
mypalette.2 <- brewer.pal(9, "Blues")
mypalette.3 <- brewer.pal(9, "Greens")
LSD_DepVar <- paste(ListVariables[[1]],"LSD", sep="_")
IQR_DepVar <- paste(ListVariables[[1]],"IQR", sep="_")
CorrDepVarIndVar1 <- paste("Corr",paste(ListVariables[[1]], ListVariables[[2]], sep="."), sep="_")
CorrDepVarIndVar2 <- paste("Corr",paste(ListVariables[[1]], ListVariables[[3]], sep="."), sep="_")
SpeRhoDepVarIndVar1 <- paste("Spearman_rho", paste(ListVariables[[1]], ListVariables[[2]], sep="."), sep="_")
SpeRhoDepVarIndVar3 <- paste("Spearman_rho", paste(ListVariables[[1]], ListVariables[[4]], sep="."), sep="_")
# spplot(gw.ss.bx$SDF, "E_s11_to_1_LSD", key.space = "right",
# col.regions = mypalette.1, cuts = 7,
# main = "GW standard deviations for SABER11 (basic)", sp.layout = map.layout)
spplot(gw.ss.bx$SDF, LSD_DepVar, key.space = "right",
col.regions = mypalette.1, pretty=TRUE, cuts=8,
main = "GW standard deviations for dependent variable (basic)", sp.layout = map.layout)
# spplot(gw.ss.bx$SDF, "E_s11_to_1_IQR", key.space = "right",
# col.regions = mypalette.1, cuts = 7,
# main = "GW inter-quartile ranges for SABER11 (robust)",
# sp.layout = map.layout)
spplot(gw.ss.bx$SDF, IQR_DepVar, key.space = "right",
col.regions = mypalette.1, pretty=TRUE, cuts=8,
main = "GW inter-quartile ranges for dependent variable (robust)",
sp.layout = map.layout)
# spplot(gw.ss.bx$SDF, "Corr_E_s11_to_1.MurderRate", key.space = "right",
# col.regions = mypalette.2, at = c(-0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1),
# main = "GW correlations: SABER11 and MurderRate (box-car kernel)",
# sp.layout = map.layout)
spplot(gw.ss.bx$SDF, CorrDepVarIndVar1, key.space = "right",
col.regions = mypalette.2, pretty=TRUE, cuts=8,
main = "GW correlations: Dependent and first independent variable (box-car kernel)",
sp.layout = map.layout)
# spplot(gw.ss.bx$SDF, "Corr_E_s11_to_1.ProxyGDP", key.space = "right",
# col.regions = mypalette.2, at = c(-0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1),
# main = "GW correlations: SABER11 and GDP (box-car kernel)",
# sp.layout = map.layout)
spplot(gw.ss.bx$SDF, CorrDepVarIndVar2, key.space = "right",
col.regions = mypalette.2, pretty=TRUE, cuts=8,
main = "GW correlations: Dependent and second independent variable (box-car kernel)",
sp.layout = map.layout)
# spplot(gw.ss.bs$SDF, "Spearman_rho_E_s11_to_1.MurderRate",key.space = "right",
# col.regions = mypalette.3, at = c(-0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1),
# main = "GW correlations: SABER11 and Murder rate (robust)",
# sp.layout = map.layout)
spplot(gw.ss.bs$SDF, SpeRhoDepVarIndVar1,key.space = "right",
col.regions = mypalette.3, pretty=TRUE, cuts=8,
main = "GW correlations: Dependent and first independent variable (robust)",
sp.layout = map.layout)
# spplot(gw.ss.bs$SDF, "Spearman_rho_E_s11_to_1.Poverty",key.space = "right",
# col.regions = mypalette.3, at = c(-0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1),
# main = "GW correlations: SABER11 and Poverty (robust)",
# sp.layout = map.layout)
spplot(gw.ss.bs$SDF, SpeRhoDepVarIndVar3,key.space = "right",
col.regions = mypalette.3, pretty=TRUE, cuts=8,
main = "GW correlations: Dependent and third independent variable (robust)",
sp.layout = map.layout)
#bw.gwr.1 <- bw.gwr(whatever_theoretical_model_is_selected, data = PolygonShapeTrimmed, approach = "AICc",
# kernel = "bisquare", adaptive = TRUE)
bw.gwr.1 <- bw.gwr(as.formula(sorted.models[[1]][[length(sorted.models[[1]])]][[1]]), data = PolygonShapeTrimmed, approach = "AICc",
kernel = "bisquare", adaptive = TRUE)
cat("")
cat("The optimal adaptative bandwidth is equal to ", bw.gwr.1)
#gwr.res <- gwr.basic(whatever_theoretical_model_is_selected, data = PolygonShapeTrimmed, bw = bw.gwr.1,
# kernel = "bisquare", adaptive = TRUE, F123.test = TRUE)
gwr.res <- gwr.basic(as.formula(sorted.models[[1]][[length(sorted.models[[1]])]][[1]]), data = PolygonShapeTrimmed, bw = bw.gwr.1,
kernel = "bisquare", adaptive = TRUE, F123.test = TRUE)
gwr.res
writeOGR(gwr.res$SDF, "Data\\Colombia", driver="ESRI Shapefile", layer='Results', overwrite_layer = TRUE)
names(gwr.res$SDF)
#spplot(gwr.res$SDF, MurderRate, key.space = "right",
# col.regions = mypalette.2, pretty=TRUE, cuts=8,
# main = "Basic GW regression coefficient estimates for murder rates",
# sp.layout = map.layout)
spplot(gwr.res$SDF, ListVariables[[2]], key.space = "right",
col.regions = mypalette.2, pretty=TRUE, cuts=8,
main = "Basic GW regression coefficient estimates for the first independent variable",
sp.layout = map.layout)
spplot(gwr.res$SDF, ListVariables[[3]], key.space = "right",
col.regions = mypalette.2, pretty=TRUE, cuts=8,
main = "Basic GW regression coefficient estimates for the second independent variable",
sp.layout = map.layout)
spplot(gwr.res$SDF, ListVariables[[4]], key.space = "right",
col.regions = mypalette.2, pretty=TRUE, cuts=8,
main = "Basic GW regression coefficient estimates for the third independent variable",
sp.layout = map.layout)
#rgwr.res <- gwr.robust(as.formula(sorted.models[[1]][[length(sorted.models[[1]])]][[1]]), data = PolygonShapeTrimmed, bw = bw.gwr.1,
# kernel = "bisquare", adaptive = TRUE, F123.test = TRUE)
rgwr.res <- gwr.robust(as.formula(sorted.models[[1]][[length(sorted.models[[1]])]][[1]]), data = PolygonShapeTrimmed, bw = bw.gwr.1,
kernel = "bisquare", adaptive = TRUE, F123.test = TRUE)
rgwr.res
spplot(rgwr.res$SDF, ListVariables[[2]], key.space = "right",
col.regions = mypalette.1, pretty=TRUE, cuts=8,
main = "Robust GW regression coefficient estimates for the first independent variable",
sp.layout = map.layout)
spplot(rgwr.res$SDF, ListVariables[[3]], key.space = "right",
col.regions = mypalette.2, pretty=TRUE, cuts=8,
main = "Robust GW regression coefficient estimates for the second independent variable",
sp.layout = map.layout)
spplot(rgwr.res$SDF, ListVariables[[4]], key.space = "right",
col.regions = mypalette.3, pretty=TRUE, cuts=8,
main = "Robust GW regression coefficient estimates for the third independent variable",
sp.layout = map.layout)
writeOGR(rgwr.res$SDF, "Data\\Colombia", driver="ESRI Shapefile", layer='Results - Robust', overwrite_layer = TRUE)