-
Notifications
You must be signed in to change notification settings - Fork 0
/
pilot_ns.R
253 lines (191 loc) · 8.86 KB
/
pilot_ns.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
###############################################################################
###################### NORTH TO SOUTH #########################
# Example of three maps
set.seed(2017)
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
var.sim <- predict(var.g.dummy, newdata = sa3_centroids, nsim = 3) %>%
left_join(sa3_centroids, ., by=c("longitude", "latitude"))
sims <- c("sim1", "sim2", "sim3")
# use an underlying spatial covariance model
sa3_sims1 <- as_tibble(var.sim) %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
# add a north to south model
sa3_ns <- sa3_centroids %>%
mutate(ns = log(abs(latitude))) %>%
mutate(ns = scales::rescale(ns, to = c(1,2)))
ggplot(sa3_ns) + geom_histogram(aes(ns))
sa3_long <- sa3_sims1 %>%
select(-longitude, -latitude, -logsize) %>%
gather(key = "simulation", value = "value", -sa3_name_2016) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation)))
### Start with shapes - geographies
aus_geo_ns_data <- sa3 %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long) %>%
left_join(., sa3_ns)
### Start with shapes - hexagons
aus_hex_ns_data <- hexagons_sf %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long) %>%
left_join(., sa3_ns)
###############################################################################
# Add the distribution will be added to one of the null plots
# Choose a location for the true data in the plot
pos <- 1
aus_geo_sa3_ns <- aus_geo_ns_data %>%
mutate(true = ns) %>%
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
mutate(value = ifelse(simulation == pos,
scales::rescale((true), c(-1,3)),
scales::rescale((value), c(-1,1))))
pos <- 3
aus_hex_sa3_ns <- aus_hex_ns_data %>%
mutate(true = ns) %>%
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
mutate(value = ifelse(simulation == pos,
scales::rescale((true), c(-1,3)),
scales::rescale((value), c(-1,1))))
aus_geo_ns <- aus_geo_sa3_ns %>%
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 = "figures/train/aus3_geo_ns.png", plot = aus_geo_ns, device = "png", dpi = 300,
height = 9, width = 18)
aus_hex_ns <- aus_hex_sa3_ns %>%
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 = "figures/train/aus3_hex_ns.png", plot = aus_hex_ns, device = "png", dpi = 300,
height = 9, width = 18)
# Are areas located in the north more blue than areas in the south?
set.seed(2017)
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
var.sim <- predict(var.g.dummy, newdata = sa3_centroids, nsim = 12) %>%
left_join(sa3_centroids, ., by=c("longitude", "latitude"))
# use an underlying spatial covariance model
sa3_sims1 <- as_tibble(var.sim) %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims2 <- sa3_sims1 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
sa3_sims3 <- sa3_sims2 %>%
mutate_at(sims, ~map_dbl(1:nrow(sa3), spatial_smoother,
values_vector = ., area_weight = 0.5, neighbours_list = sa3_neighbours))
smoothing <- bind_rows(
"smooth1" = sa3_sims1,
"smooth2" = sa3_sims2,
"smooth3" = sa3_sims3, .id = "groups")
sa3_long <- smoothing %>%
select(-longitude, -latitude, -logsize) %>%
gather(key = "simulation", value = "value", -sa3_name_2016, -groups) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation)))
# only use the most smoothed
sa3_min <- sa3_long %>%
filter(groups == "smooth3") %>%
pull(value) %>% min()
sa3_max <- sa3_long %>%
filter(groups == "smooth3") %>%
pull(value) %>% max()
sa3_mean <- sa3_long %>%
filter(groups == "smooth3") %>%
pull(value) %>% mean()
#####################################################################
# add a north to south model
sa3_ns <- sa3_centroids %>%
mutate(ns = log(abs(latitude))) %>%
mutate(ns = scales::rescale(ns, to = c(sa3_mean,sa3_max)))
ggplot(sa3_ns) + geom_histogram(aes(ns))
### Start with shapes - geographies
aus_geo_ns <- sa3 %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long) %>%
left_join(., sa3_ns)
### Start with shapes - hexagons
aus_hex_ns <- hexagons_sf %>%
select(sa3_name_2016) %>%
# Add the 16 simulated values for each area
left_join(., sa3_long) %>%
left_join(., sa3_ns)
###############################################################################
# Add the distribution will be added to one of the null plots
# Choose a location for the true data in the plot
pos <- 8
aus_geo_sa3_ns <- aus_geo_ns %>%
mutate(true = ns) %>%
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
mutate(value = ifelse(simulation == pos,
scales::rescale((value+true), c(sa3_min, sa3_max)),
scales::rescale((value), c(sa3_min, sa3_max))))
pos <- 8
aus_hex_sa3_ns <- aus_hex_ns %>%
mutate(true = ns) %>%
mutate(simulation = as.numeric(gsub("sim", "", simulation))) %>%
# add the spatial trend model to the null data plot
# scale the new data around the distribution of null data
mutate(value = ifelse(simulation == pos,
scales::rescale((value+true), c(sa3_min, sa3_max)),
scales::rescale((value), c(sa3_min, sa3_max))))
###############################################################################
############################ Population ns ############################
###############################################################################
aus_geo_ns <- aus_geo_sa3_ns %>%
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 = "figures/ns/aus_geo_ns.pdf", plot = aus_geo_ns, device = "pdf", dpi = 300,
height = 14, width = 18)
aus_hex_ns <- aus_hex_sa3_ns %>%
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 = "figures/ns/aus_hex_ns.pdf", plot = aus_hex_ns, device = "pdf", dpi = 300,
height = 14, width = 18)