-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelAnalysis.Rmd
630 lines (515 loc) · 27 KB
/
ParallelAnalysis.Rmd
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
---
title: "ParallelAnalysis"
author: "Jamie Mullienaux"
date: "9/23/2022"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r Model Diagnostics, echo=FALSE}
diagnostics <- data.frame(matrix(ncol = 3, nrow = 2),
row.names = c("Adaptive Model",
"Neighbour Sensitivity"))
colnames(diagnostics) <- c("DIC", "WAIC", "LMPL")
diagnostics["Adaptive Model",1:3] <- (adaptive_models[,1]$modelfit[c(1,3,5)] +
adaptive_models[,2]$modelfit[c(1,3,5)] +
adaptive_models[,3]$modelfit[c(1,3,5)] +
adaptive_models[,4]$modelfit[c(1,3,5)]) / 4
diagnostics["Neighbour Sensitivity",1:3] <- (second_neighbour_models[,1]$modelfit[c(1,3,5)] + second_neighbour_models[,2]$modelfit[c(1,3,5)] + second_neighbour_models[,3]$modelfit[c(1,3,5)] + second_neighbour_models[,4]$modelfit[c(1,3,5)]) / 4
# Print diagnostics
diagnostics
```
```{r Get model residuals, echo=FALSE}
full_data["model.residuals"] <- (adaptive_models[,1]$residuals$response +
adaptive_models[,2]$residuals$response +
adaptive_models[,3]$residuals$response +
adaptive_models[,4]$residuals$response)/4
```
```{r Temporal diagnostics of model residuals, echo=FALSE}
# Function to perform Ljung-Box test on a set of residuals from a given model
temporal_p_values <- function(residuals){
p_vals <- rep(NA, K)
for(j in 1:K){
res <- full_data[full_data$index == j,][residuals]
p_vals[j] <- Box.test(res, lag = 7, type = c("Ljung-Box"), fitdf = 0)$p.value
}
return(cbind(uniq_la, p_vals))
}
# Calculate p-values for each LA
uniq_la <- temporal_p_values("model.residuals")
# Update the dataframe names to identify the respective models
names(uniq_la) <- c(names(uniq_la)[1:2], "Adaptive Parallel Temporal p-value")
# Identify number of LAs with statistically significant temporal auto-correlation
# Tested at 5% significance level with a Bonferroni correction
# 15/312 (4.8%) show auto-correlation at this significance level
num_significant_LAs <- sum(uniq_la["Adaptive Parallel Temporal p-value"] < 0.05/K)
# Boxplot of residuals by weekday
# Shows consistent distribution, less variation Sat/Sun due to lower case volume
weekday_resid <- ggplot(data = full_data, aes(x=Day, y=model.residuals)) +
geom_boxplot(outlier.shape = NA) +
ylim(-3,3) +
labs(y = "Model residuals")
# Line-graph of mean residuals by each day in data set
# Shows a random pattern
mean_daily_resid <- aggregate(model.residuals ~ time, full_data, FUN=mean)
daily_resid <- ggplot(mapping = aes(x = mean_daily_resid$time,
y=mean_daily_resid$model.residuals)) +
geom_line() +
labs(x = "Time", y = "Mean model residual")
# Plot analysis of temporal residuals
png("Parallel_TemporalResids.png")
grid.arrange(grobs = list(daily_resid, weekday_resid), nrow = 2)
dev.off()
```
```{r Spatial diagnostics of model residuals, echo=FALSE}
## Set-up for Moran test
time_periods <- data.frame(Day = 1:55)
# Create a function to run Moran's I for each time period
moran_temporal_test <- function(residuals, listw, num_sims, df){
p_vals <- rep(NA, N - lags - adjust)
# Loop through each time period
for(i in adjust:(N - lags - 1)){
res <- full_data[full_data$time == i, ][, residuals]
p_vals[i-4] <- moran.mc(res, listw = list_W, nsim = num_sims)$p.value
}
return(cbind(df, p_vals))
}
M = 10000
time_periods <- moran_temporal_test("model.residuals", list_W, M, time_periods)
# Update the dataframe names to identify the respective models
names(time_periods) <- c("Day", "Adaptive Parallel Spatial p-value")
# Identify number of days with statistically significant spatial auto-correlation
# Tested at 5% significance level with a Bonferroni correction
# No time period shows auto-correlation across LADs at this significance level
num_significant_days <- sum(time_periods["Adaptive Parallel Spatial p-value"] < 0.05/(N - lags - adjust))
# Plot model residuals (mean over all 4 chains)
by_LA <- group_by(full_data, code)
avg_resid <- summarise(by_LA, res = mean(model.residuals))
avg_resid_LA <- merge(x = LA, y = avg_resid, by.x = "LAD20CD", by.y = "code",
all.x = FALSE)
# Change data type so ggplot can be used
gg_LA_avg_resid = st_as_sf(avg_resid_LA)
# Save plot
png("MeanResidbyLA.png")
la_resid_plot = ggplot() + geom_sf(data = gg_LA_avg_resid, aes(fill = res)) + theme_void() + scale_fill_viridis(option = "C") + labs(fill='Residual')
grid.arrange(la_resid_plot)
dev.off()
```
## Weather Analysis
```{r Get trace plots for regression parameters, echo=FALSE}
# Convert regression posterior samples to MCMC object
parallel_beta_mcmc <- mcmc.list(adaptive_models[,1]$samples$beta,
adaptive_models[,2]$samples$beta,
adaptive_models[,3]$samples$beta,
adaptive_models[,4]$samples$beta)
parallel_beta_days <- mcmc.list(adaptive_models[,1]$samples$beta[,2:7],
adaptive_models[,2]$samples$beta[,2:7],
adaptive_models[,3]$samples$beta[,2:7],
adaptive_models[,4]$samples$beta[,2:7])
parallel_beta_other <- mcmc.list(adaptive_models[,1]$samples$beta[,-c(2:7)],
adaptive_models[,2]$samples$beta[,-c(2:7)],
adaptive_models[,3]$samples$beta[,-c(2:7)],
adaptive_models[,4]$samples$beta[,-c(2:7)])
# Assess convergence with Gelman-Rubin diagnostic and trace plots
# Mostly looks good. Some chains may not have converged for some weekdays
png("Parallel_DayTracePlots.png")
plot(parallel_beta_days, density = FALSE)
dev.off()
png("Parallel_WeatherTracePlots.png")
plot(parallel_beta_other, density = FALSE)
dev.off()
gelman.diag(parallel_beta_mcmc)
# Store all samples in a dataframe
parallel_beta_samples <- data.frame(matrix(nrow = 10000, ncol = 12))
names(parallel_beta_samples) <- c("Intercept", "Sun", "Fri", "Sat",
"Wed", "Mon", "Tue", "Temperature",
"Relative Humidity", "Wind Velocity",
"Solar Radiation", "Precipitation")
parallel_beta_samples[,1:12] <- rbind(parallel_beta_mcmc[[1]],
parallel_beta_mcmc[[2]],
parallel_beta_mcmc[[3]],
parallel_beta_mcmc[[4]])
```
```{r Analyse posteriors of regression parameters}
# Calculate 95% HPDs for regression parameters (shown on plots)
beta_hdi <- HPDinterval(as.mcmc(rbind(parallel_beta_mcmc[[1]],
parallel_beta_mcmc[[2]],
parallel_beta_mcmc[[3]],
parallel_beta_mcmc[[4]])), prob = 0.95)
# Density graphs for weekday posterior distributions
sun_posterior <- ggplot(parallel_beta_samples, aes(x = Sun)) +
geom_density() +
geom_segment(aes(x=beta_hdi[2],xend=beta_hdi[14],y=1.1,yend=1.1, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[2],xend=beta_hdi[2],y=0,yend=1.1, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[14],xend=beta_hdi[14],y=0,yend=1.1, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Sunday") +
theme(legend.position = "none") +
xlim(-0.45,0.45)
mon_posterior <- ggplot(parallel_beta_samples, aes(x = Mon)) +
geom_density() +
geom_segment(aes(x=beta_hdi[6],xend=beta_hdi[18],y=1.1,yend=1.1, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[6],xend=beta_hdi[6],y=0,yend=1.1, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[18],xend=beta_hdi[18],y=0,yend=1.1, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Monday") +
theme(legend.position = "none") +
xlim(-0.45,0.45)
wed_posterior <- ggplot(parallel_beta_samples, aes(x = Wed)) +
geom_density() +
geom_segment(aes(x=beta_hdi[5],xend=beta_hdi[17],y=1.5,yend=1.5, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[5],xend=beta_hdi[5],y=0,yend=1.5, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[17],xend=beta_hdi[17],y=0,yend=1.5, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Wednesday") +
theme(legend.position = "none") +
xlim(-0.45,0.45)
tue_posterior <- ggplot(parallel_beta_samples, aes(x = Tue)) +
geom_density() +
geom_segment(aes(x=beta_hdi[7],xend=beta_hdi[19],y=1.1,yend=1.1, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[7],xend=beta_hdi[7],y=0,yend=1.1, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[19],xend=beta_hdi[19],y=0,yend=1.1, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Tuesday") +
theme(legend.position = "none") +
xlim(-0.45,0.45)
fri_posterior <- ggplot(parallel_beta_samples, aes(x = Fri)) +
geom_density() +
geom_segment(aes(x=beta_hdi[3],xend=beta_hdi[15],y=1.2,yend=1.2, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[3],xend=beta_hdi[3],y=0,yend=1.2, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[15],xend=beta_hdi[15],y=0,yend=1.2, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Friday") +
theme(legend.position = "none") +
xlim(-0.45,0.45)
sat_posterior <- ggplot(parallel_beta_samples, aes(x = Sat)) +
geom_density() +
geom_segment(aes(x=beta_hdi[4],xend=beta_hdi[16],y=1.2,yend=1.2, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[4],xend=beta_hdi[4],y=0,yend=1.2, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[16],xend=beta_hdi[16],y=0,yend=1.2, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Saturday") +
theme(legend.position = "none") +
xlim(-0.45,0.45)
# Plot weekday posterior distributions
png(file = "Parallel_DayPosteriors.png")
grid.arrange(grobs = list(mon_posterior, tue_posterior, weds_posterior, fri_posterior, sat_posterior, sun_posterior), ncol = 2, nrow = 3)
dev.off()
# Density graphs for intercept and weather posterior distributions
intercept_posterior <- ggplot(parallel_beta_samples, aes(x = Intercept)) +
geom_density() +
geom_segment(aes(x=beta_hdi[1],xend=beta_hdi[13],y=0.35,yend=0.35, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[1],xend=beta_hdi[1],y=0,yend=0.35, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[13],xend=beta_hdi[13],y=0,yend=0.35, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Intercept") +
theme(legend.position = "none") +
xlim(-0.7, 0.7)
rh_posterior <- ggplot(parallel_beta_samples, aes(x = parallel_beta_samples[,9])) +
geom_density() +
geom_segment(aes(x=beta_hdi[9],xend=beta_hdi[21],y=30,yend=30, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[9],xend=beta_hdi[9],y=0,yend=30, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[21],xend=beta_hdi[21],y=0,yend=30, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Relative humidity") +
theme(legend.position = "none") +
xlim(-0.01,0.01)
wind_posterior <- ggplot(parallel_beta_samples, aes(x = parallel_beta_samples[,10])) +
geom_density() +
geom_segment(aes(x=beta_hdi[10],xend=beta_hdi[22],y=2.2,yend=2.2, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[10],xend=beta_hdi[10],y=0,yend=2.2, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[22],xend=beta_hdi[22],y=0,yend=2.2, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Wind velocity") +
theme(legend.position = "none") +
xlim(-0.1,0.1)
temp_posterior <- ggplot(parallel_beta_samples, aes(x = Temperature)) +
geom_density() +
geom_segment(aes(x=beta_hdi[8],xend=beta_hdi[20],y=7,yend=7, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[8],xend=beta_hdi[8],y=0,yend=7, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[20],xend=beta_hdi[20],y=0,yend=7, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Temperature") +
theme(legend.position = "none") +
xlim(-0.03,0.03)
ssrd_posterior <- ggplot(parallel_beta_samples, aes(x = parallel_beta_samples[,11])) +
geom_density() +
geom_segment(aes(x=beta_hdi[11],xend=beta_hdi[23],y=1.5,yend=1.5, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[11],xend=beta_hdi[11],y=0,yend=1.5, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[23],xend=beta_hdi[23],y=0,yend=1.5, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Solar radiation") +
theme(legend.position = "none") +
xlim(-0.1,0.1)
precipitation_posterior <- ggplot(parallel_beta_samples, aes(x = Precipitation)) +
geom_density() +
geom_segment(aes(x=beta_hdi[12],xend=beta_hdi[24],y=3,yend=3, colour = "blue", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[12],xend=beta_hdi[12],y=0,yend=3, colour = "red", linetype = "dashed")) +
geom_segment(aes(x=beta_hdi[24],xend=beta_hdi[24],y=0,yend=3, colour = "red", linetype = "dashed")) +
labs(y = "Density", x = "Parameter value", title = "Precipitation") +
theme(legend.position = "none") +
xlim(-0.05,0.05)
# Plot intercept and weather posterior distributions
png(file = "Parallel_WeatherPosteriors.png")
grid.arrange(grobs = list(intercept_posterior, temp_posterior, rh_posterior, wind_posterior, ssrd_posterior, precipitation_posterior), ncol = 2, nrow = 3)
dev.off()
```
```{r Get posteriors of hyperparameters}
# Convert posterior samples of 4 main hyperparameters to MCMC objects
# Obtain corresponding Gelman-Rubin diagnostics - they show convergence
parallel_rho <- mcmc.list(adaptive_models[,1]$samples$rho,
adaptive_models[,2]$samples$rho,
adaptive_models[,3]$samples$rho,
adaptive_models[,4]$samples$rho)
parallel_tau2 <- mcmc.list(adaptive_models[,1]$samples$tau2,
adaptive_models[,2]$samples$tau2,
adaptive_models[,3]$samples$tau2,
adaptive_models[,4]$samples$tau2)
# Assess convergence with Gelman-Rubin diagnostic and trace plots - looks good
gelman.diag(parallel_rho)
gelman.diag(parallel_tau2)
png("Parallel_RhoTracePlots.png")
plot(parallel_rho, density = FALSE)
dev.off()
png("Parallel_TauTracePlots.png")
plot(parallel_tau2, density = FALSE)
dev.off()
# Get 95% HDI (not shown on plots)
hyperparam_hdi <- HPDinterval(as.mcmc(rbind(parallel_rho[[1]],
parallel_rho[[2]],
parallel_rho[[3]],
parallel_rho[[4]])), prob = 0.95)
# Save graphical posterior densities of main hyperparameters to file
png(file="ParallelHyperparams.png")
parallel_rhoS <- ggplot(mapping = aes(x = c(parallel_rho[[1]][,1],
parallel_rho[[2]][,1],
parallel_rho[[3]][,1],
parallel_rho[[4]][,1]))) +
geom_density() +
labs(x = expression(rho["S"]), y = "Density")
parallel_rhoT <- ggplot(mapping = aes(x = c(parallel_rho[[1]][,2],
parallel_rho[[2]][,2],
parallel_rho[[3]][,2],
parallel_rho[[4]][,2]))) +
geom_density() +
labs(x = expression(rho["T"]), y = "Density")
parallel_tau <- ggplot(mapping = aes(x = c(parallel_tau2[[1]][,1],
parallel_tau2[[2]][,1],
parallel_tau2[[3]][,1],
parallel_tau2[[4]][,1]))) +
geom_density() +
labs(x = expression(tau^2), y = "Density")
parallel_tauw <- ggplot(mapping = aes(x = c(parallel_tau2[[1]][,2],
parallel_tau2[[2]][,2],
parallel_tau2[[3]][,2],
parallel_tau2[[4]][,2]))) +
geom_density() +
labs(x = expression(tau[omega]^2), y = "Density")
grid.arrange(grobs = list(parallel_rhoS, parallel_rhoT, parallel_tau,
parallel_tauw), nrow=2, ncol=2)
dev.off()
```
```{r Analysis of neighbourhood matrix posteriors, echo=FALSE}
# Set up dataframe to contain neighbourhood matrix estimates posterior samples
parallel_w <- data.frame(matrix(nrow = 10000, ncol = 808))
parallel_new_w <- data.frame(matrix(nrow = 10000, ncol = 956))
w_names <- "w 1"
for(i in 2:808){w_names <- c(w_names, paste("w",i))}
names(parallel_w) <- w_names
names(parallel_new_w) <- w_names
# Store posterior samples
parallel_w[,1:808] <- rbind(adaptive_models[,1]$samples$w,
adaptive_models[,2]$samples$w,
adaptive_models[,3]$samples$w,
adaptive_models[,4]$samples$w)
parallel_w$Iter <- 1:10000
parallel_new_w[,1:956] <- rbind(second_neighbour_models[,1]$samples$w,
second_neighbour_models[,2]$samples$w,
second_neighbour_models[,3]$samples$w,
second_neighbour_models[,4]$samples$w)
parallel_new_w$Iter <- 1:10000
# Calculate summary statistics of posterior samples for all 808 elements
mean_ws <- colMeans(parallel_w[,2:809])
median_ws <- sapply(parallel_w[,2:809], median)
lq_ws <- sapply(parallel_w[,2:809], quantile, probs = 0.25)
uq_ws <- sapply(parallel_w[,2:809], quantile, probs = 0.75)
# Plot summary statistics of posterior samples for all 808 elements
png(file="Parallel_NeighbourhoodMatrixPosteriors.png")
w_mean_hist <- ggplot(mapping = aes(x = mean_ws)) +
geom_histogram(position = "identity", bins = 25) +
labs(x = "Posterior mean", y = "Neighbouring local authority pairs")
w_median_hist <- ggplot(mapping = aes(x = median_ws)) +
geom_histogram(position = "identity", bins = 25) +
labs(x = "Posterior median", y = "Neighbouring local authority pairs")
w_lq_hist <- ggplot(mapping = aes(x = lq_ws)) +
geom_histogram(position = "identity", bins = 25) +
labs(x = "Posterior lower quartile", y = "Neighbouring local authority pairs")
w_uq_hist <- ggplot(mapping = aes(x = uq_ws)) +
geom_histogram(position = "identity", bins = 25) +
labs(x = "Posterior upper quartile", y = "Neighbouring local authority pairs")
grid.arrange(w_mean_hist, w_median_hist, w_lq_hist, w_uq_hist, ncol = 2, nrow = 2)
dev.off()
# Caclulate Gelman-Rubin diagnostic for neighbourhood matrix elements
parallel_ws <- mcmc.list(adaptive_models[,1]$samples$w,
adaptive_models[,2]$samples$w,
adaptive_models[,3]$samples$w,
adaptive_models[,4]$samples$w)
w_gelmans <- gelman.diag(adaptive_ws)[[1]][1:808]
# Histogram of neighbourhood matrix element Gelman-Rubin statistics
# Shows convergence
w_gelman_hist <- ggplot(aes(x = w_gelmans)) + geom_histogram() +
theme(legend.title = "Convergence diagnostics for neighbourhood matrix elements")
```
```{r identify neighbouring LADs of Hackney}
# Function to identify neighbours of a local authority
# Input: Neighbourhood matrix and local authority ID
# Output: IDs of neighbours
identify_neighbours <- function(nb_W, id){
neighbours <- c()
for(j in 1:K){
if(nb_W[id, j] == 1){
neighbours <- c(neighbours,j)
}
}
return(neighbours)
}
# Function to identify neighbourhood matrix elements of neighbouring authorities
# Input: Neighbourhood matrix, local authrity ID, list of neighbour authority IDs
# Output: IDs of corresponding neighbourhood matrix elements
identify_w_elements <- function(nb_W, id, nb_id){
neighbour_w <- c()
for(j in nb_id){
if(j > id){
p1 <- sum(upper.triangle(nb_W[1:(j - 1), 1:(j - 1)]))
p2 <- sum(W[1:id, j])
} else {
p1 <- sum(upper.triangle(nb_W[1:(id - 1), 1:(id - 1)]))
p2 <- sum(nb_W[1:j, id])
}
neighbour_w <- c(neighbour_w, p1 + p2)
}
return(neighbour_w)
}
## Calculate neighbours and corresponding neighbourhood matrix element IDs
# Hackney & City of London
hackney_neighbours <- identify_neighbours(W, 291)
# 286, 293, 298, 304, 307, 309, 310, 312
identify_w_elements(W, 291, hackney_neighbours)
# 727, 733, 754, 769, 780, 788, 793, 805
# Hackney & City of London - extended model
hackney__extended_neighbours <- identify_neighbours(new_W, 291)
# 281, 282, 284, 285, 286, 289, 293, 298, 299, 301, 302, 304, 305, 307, 309, 310, 311, 312
identify_w_elements(new_W, 291, hackney__extended_neighbours)
# 766, 767, 768, 769, 770, 771, 779, 810, 815, 831, 841, 853, 860, 878, 897, 910, 920, 938
```
```{r main model correlation graphs}
## Hackney graphs
png("hackney_neighbours.png")
camden <- ggplot(mapping = aes(x = parallel_w[,727])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Camden") +
theme(text=element_text(size=8))
haringey <- ggplot(mapping = aes(x = parallel_w[,733])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Haringey") +
theme(text=element_text(size=8))
islington <- ggplot(mapping = aes(x = parallel_w[,754])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Islington") +
theme(text=element_text(size=8))
newham <- ggplot(mapping = aes(x = parallel_w[,769])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Newham") +
theme(text=element_text(size=8))
southwark <- ggplot(mapping = aes(x = parallel_w[,780])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Southwark") +
theme(text=element_text(size=8))
tower_hamlets <- ggplot(mapping = aes(x = parallel_w[,788])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Tower Hamlets") +
theme(text=element_text(size=8))
waltham_forest <- ggplot(mapping = aes(x = parallel_w[,793])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Waltham Forest") +
theme(text=element_text(size=8))
westminster <- ggplot(mapping = aes(x = parallel_w[,805])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Westminster") +
theme(text=element_text(size=8))
grid.arrange(grobs = list(camden, haringey, islington, newham, southwark, tower_hamlets, waltham_forest, westminster), nrow=3, ncol=3)
dev.off()
```
```{r second order neighbour correlation graphs}
## Hackney graphs
png("hackney_extended_orig_neighbours.png")
camden <- ggplot(mapping = aes(x = parallel_new_w[,770])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Camden") +
theme(text=element_text(size=8))
haringey <- ggplot(mapping = aes(x = parallel_new_w[,779])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Haringey") +
theme(text=element_text(size=8))
islington <- ggplot(mapping = aes(x = parallel_new_w[,810])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Islington") +
theme(text=element_text(size=8))
newham <- ggplot(mapping = aes(x = parallel_new_w[,853])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Newham") +
theme(text=element_text(size=8))
southwark <- ggplot(mapping = aes(x = parallel_new_w[,878])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Southwark") +
theme(text=element_text(size=8))
tower_hamlets <- ggplot(mapping = aes(x = parallel_new_w[,897])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Tower Hamlets") +
theme(text=element_text(size=8))
waltham_forest <- ggplot(mapping = aes(x = parallel_new_w[,910])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Waltham Forest") +
theme(text=element_text(size=8))
westminster <- ggplot(mapping = aes(x = parallel_new_w[,938])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Westminster") +
theme(text=element_text(size=8))
grid.arrange(grobs = list(camden, haringey, islington, newham, southwark, tower_hamlets, waltham_forest, westminster), nrow=3, ncol=3)
dev.off()
png("hackney_extended_second_neighbours.png")
barking_and_dagenham <- ggplot(mapping = aes(x = parallel_new_w[,766])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Barking & Dagenham") +
theme(text=element_text(size=8))
barnet <- ggplot(mapping = aes(x = parallel_new_w[,767])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Barnet") +
theme(text=element_text(size=8))
brent <- ggplot(mapping = aes(x = parallel_new_w[,768])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Brent") +
theme(text=element_text(size=8))
bromley <- ggplot(mapping = aes(x = parallel_new_w[,769])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Bromley") +
theme(text=element_text(size=8))
enfield <- ggplot(mapping = aes(x = parallel_new_w[,771])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Enfield") +
theme(text=element_text(size=8))
kensignton_and_chelsea <- ggplot(mapping = aes(x = parallel_new_w[,815])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Kensington & Chelsea") +
theme(text=element_text(size=8))
lambeth <- ggplot(mapping = aes(x = parallel_new_w[,831])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Lambeth") +
theme(text=element_text(size=8))
lewisham <- ggplot(mapping = aes(x = parallel_new_w[,841])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Lewisham") +
theme(text=element_text(size=8))
redbridge <- ggplot(mapping = aes(x = parallel_new_w[,860])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Redbridge") +
theme(text=element_text(size=8))
wandsworth <- ggplot(mapping = aes(x = parallel_new_w[,920])) +
geom_density() +
labs(x = "Correlation", y = "Density", title = "Wandsworth") +
theme(text=element_text(size=8))
grid.arrange(grobs = list(barking_and_dagenham, barnet, brent, bromley, enfield,
kensignton_and_chelsea, lambeth, lewisham, redbridge, wandsworth),
nrow=4, ncol=3)
dev.off()
```