-
Notifications
You must be signed in to change notification settings - Fork 0
/
pilot_cities.R
329 lines (272 loc) · 14.2 KB
/
pilot_cities.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
###############################################################################
###################### CITY DIFFERENCE #########################
# Which map shows the most areas coloured red?
set.seed(2015)
var.g.dummy <- gstat(formula = z ~ 1,
locations = ~ longitude + latitude,
dummy = T, beta = 1, model = vgm(psill = 1, model = "Gau", range = 0.3),
nmax = 12)
# Create underlying spatially dependent data for 12 null plots
# use an underlying spatial covariance model
var.sim1 <- predict(var.g.dummy, newdata = sa3_centroids, nsim = 12) %>%
left_join(sa3_centroids, ., by=c("longitude", "latitude"))
var.sim2 <- predict(var.g.dummy, newdata = sa3_centroids, nsim = 12) %>%
left_join(sa3_centroids, ., by=c("longitude", "latitude"))
var.sim3 <- predict(var.g.dummy, newdata = sa3_centroids, nsim = 12) %>%
left_join(sa3_centroids, ., by=c("longitude", "latitude"))
var.sim4 <- predict(var.g.dummy, newdata = sa3_centroids, nsim = 12) %>%
left_join(sa3_centroids, ., by=c("longitude", "latitude"))
###############################################################################
###################### SMOOTH #########################
sa3_sims1_1 <- as_tibble(var.sim1) %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims1_2 <- sa3_sims1_1 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims1_3 <- sa3_sims1_2 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims1_4 <- sa3_sims1_3 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims1_5 <- sa3_sims1_4 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
smoothing1 <- bind_rows(
"smooth11" = sa3_sims1_1,
"smooth12" = sa3_sims1_2,
"smooth13" = sa3_sims1_3,
"smooth14" = sa3_sims1_4,
"smooth15" = sa3_sims1_5, .id = "groups")
sa3_long1 <- sa3_sims1_5 %>%
select(-longitude, -latitude, -logsize) %>%
gather(key = "simulation", value = "value", -sa3_name_2016) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation)))
###############################################################################
sa3_sims2_1 <- as_tibble(var.sim2) %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims2_2 <- sa3_sims2_1 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims2_3 <- sa3_sims2_2 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims2_4 <- sa3_sims2_3 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims2_5 <- sa3_sims2_4 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
smoothing2 <- bind_rows(
"smooth21" = sa3_sims2_1,
"smooth22" = sa3_sims2_2,
"smooth23" = sa3_sims2_3,
"smooth24" = sa3_sims2_4,
"smooth25" = sa3_sims2_5, .id = "groups")
sa3_long2 <- sa3_sims2_5 %>%
select(-longitude, -latitude, -logsize) %>%
gather(key = "simulation", value = "value", -sa3_name_2016) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation)))
###############################################################################
sa3_sims3_1 <- as_tibble(var.sim3) %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims3_2 <- sa3_sims3_1 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims3_3 <- sa3_sims3_2 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims3_4 <- sa3_sims3_3 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims3_5 <- sa3_sims3_4 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
smoothing3 <- bind_rows(
"smooth31" = sa3_sims3_1,
"smooth32" = sa3_sims3_2,
"smooth33" = sa3_sims3_3,
"smooth34" = sa3_sims3_4,
"smooth35" = sa3_sims3_5, .id = "groups")
sa3_long3 <- sa3_sims3_5 %>%
select(-longitude, -latitude, -logsize) %>%
gather(key = "simulation", value = "value", -sa3_name_2016) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation)))
###############################################################################
sa3_sims4_1 <- as_tibble(var.sim4) %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims4_2 <- sa3_sims4_1 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims4_3 <- sa3_sims4_2 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims4_4 <- sa3_sims4_3 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims4_5 <- sa3_sims4_4 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
smoothing4 <- bind_rows(
"smooth41" = sa3_sims4_1,
"smooth42" = sa3_sims4_2,
"smooth43" = sa3_sims4_3,
"smooth44" = sa3_sims4_4,
"smooth45" = sa3_sims4_5, .id = "groups")
sa3_long4 <- sa3_sims4_5 %>%
select(-longitude, -latitude, -logsize) %>%
gather(key = "simulation", value = "value", -sa3_name_2016) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation)))
#####################################################################
###############################################################################
###################### ADD TREND #########################
###############################################################################
# only use the most smoothed
sa3_min1 <- sa3_long1 %>% pull(value) %>% min()
sa3_max1 <- sa3_long1 %>% pull(value) %>% max()
sa3_mean1 <- sa3_long1 %>% pull(value) %>% mean()
sa3_min2 <- sa3_long2 %>% pull(value) %>% min()
sa3_max2 <- sa3_long2 %>% pull(value) %>% max()
sa3_mean2 <- sa3_long2 %>% pull(value) %>% mean()
sa3_min3 <- sa3_long3 %>% pull(value) %>% min()
sa3_max3 <- sa3_long3 %>% pull(value) %>% max()
sa3_mean3 <- sa3_long3 %>% pull(value) %>% mean()
sa3_min4 <- sa3_long4 %>% pull(value) %>% min()
sa3_max4 <- sa3_long4 %>% pull(value) %>% max()
sa3_mean4 <- sa3_long4 %>% pull(value) %>% mean()
#####################################################################
# increase the values of Brisbane, depending on distance from Brisbane city
# allocated: the data set containing the allocated hexagon centroid for each sa3
max_dist <- 1478314 # furthest area from any focal point
sa3_cities <- allocated %>%
select(sa3_name_2016, longitude, latitude, points, focal_dist) %>%
mutate(city_distance = (max_dist - focal_dist)^8,
dist = scales::rescale(city_distance,
to = c(0,1)),
cities = ifelse(dist < 0.85, NA,
scales::rescale(city_distance,
to = c(min(sa3_min1,sa3_min2,sa3_min3,sa3_min4),
max(sa3_max1,sa3_max2,sa3_max3,sa3_max4)))))
### Start with shapes - geographies
aus_geo_cities1 <- sa3 %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long1) %>%
left_join(., sa3_cities)
aus_geo_cities2 <- sa3 %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long2) %>%
left_join(., sa3_cities)
aus_geo_cities3 <- sa3 %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long3) %>%
left_join(., sa3_cities)
aus_geo_cities4 <- sa3 %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long4) %>%
left_join(., sa3_cities)
### Start with shapes - hexagons
aus_hex_cities1 <- hexagons_sf %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long1) %>%
left_join(., sa3_cities)
aus_hex_cities2 <- hexagons_sf %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long2) %>%
left_join(., sa3_cities)
aus_hex_cities3 <- hexagons_sf %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long3) %>%
left_join(., sa3_cities)
aus_hex_cities4 <- hexagons_sf %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long4) %>%
left_join(., sa3_cities)
###############################################################################
# Add the distribution will be added to one of the null plots
# Choose a location for the true data in the plot
geo_plot_cities <- function(data, position, min, max) {
aus_geo_cities_plot <- data %>%
mutate(true = cities) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation))) %>%
# add the spatial trend model to the null data plot
# scale the null data around the mean of the data
group_by(simulation) %>%
mutate(value = ifelse(simulation == position,
# for the true data plot, keep random values if not close enough to cities
ifelse(is.na(cities), value, true),
# for all others rescale to the same range
value)) %>%
mutate(value = scales::rescale((value), c(min, max))) %>%
# Plot this data as a geographic map
ggplot() +
geom_sf(aes(fill = value), colour = NA) +
scale_fill_distiller(type = "div", palette = "RdYlBu") +
facet_wrap(~ simulation) + theme_minimal() + guides(fill = FALSE) +
theme(plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black", colour = NA),
strip.background = element_rect(fill = "black", colour = NA),
strip.text.x = element_text(colour = "white", size = 40),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
ggsave(filename = paste0("figures/final/aus_cities_", position, "_geo.png"),
plot = aus_geo_cities_plot,
device = "png", dpi = 300,
height = 14, width = 18)
return(aus_geo_cities_plot)
}
hex_plot_cities <- function(data, position, min, max) {
aus_hex_cities_plot <- data %>%
mutate(true = cities) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation))) %>%
# add the spatial trend model to the null data plot
# scale the null data around the mean of the data
group_by(simulation) %>%
mutate(value = ifelse(simulation == position,
# for the true data plot, keep random values if not close enough to cities
ifelse(is.na(cities), value, true),
# for all others rescale to the same range
value)) %>%
mutate(value = scales::rescale((value), c(min, max))) %>%
# Plot this data as a geographic map
ggplot() +
geom_sf(data = aus_underlay, colour = "grey", fill = NA, size = 0.01) +
geom_sf(aes(fill = value), colour = NA) +
scale_fill_distiller(type = "div", palette = "RdYlBu") +
facet_wrap(~ simulation) + theme_minimal() + guides(fill = FALSE) +
theme(plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black", colour = NA),
strip.background = element_rect(fill = "black", colour = NA),
strip.text.x = element_text(colour = "white", size = 40),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
ggsave(filename = paste0("figures/final/aus_cities_", position, "_hex.png"),
plot = aus_hex_cities_plot,
device = "png", dpi = 300,
height = 14, width = 18)
return(aus_hex_cities_plot)
}
cities_positions <- c(12,3,9,4)
min_list <- list(sa3_min1,sa3_min2,sa3_min3,sa3_min4)
max_list <- list(sa3_max1,sa3_max2,sa3_max3,sa3_max4)
geo_list <- list(aus_geo_cities1, aus_geo_cities2, aus_geo_cities3, aus_geo_cities4)
hex_list <- list(aus_hex_cities1, aus_hex_cities2, aus_hex_cities3, aus_hex_cities4)
geo_plot_cities(aus_geo_cities1, position = cities_positions[[1]], min_list[[1]], max_list[[1]])
geo_plot_cities(aus_geo_cities2, position = cities_positions[[2]], min_list[[2]], max_list[[2]])
geo_plot_cities(aus_geo_cities3, position = cities_positions[[3]], min_list[[3]], max_list[[3]])
geo_plot_cities(aus_geo_cities4, position = cities_positions[[4]], min_list[[4]], max_list[[4]])
hex_plot_cities(aus_hex_cities1, position = cities_positions[[1]], min_list[[1]], max_list[[1]])
hex_plot_cities(aus_hex_cities2, position = cities_positions[[2]], min_list[[2]], max_list[[2]])
hex_plot_cities(aus_hex_cities3, position = cities_positions[[3]], min_list[[3]], max_list[[3]])
hex_plot_cities(aus_hex_cities4, position = cities_positions[[4]], min_list[[4]], max_list[[4]])