-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClustering_Functions_95.R
527 lines (390 loc) · 26.8 KB
/
Clustering_Functions_95.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
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
# Clustering Functions for Motif files
library(ggplot2)
library(maps)
library(mapdata)
library(maptools)
library(gridExtra)
library(here)
library(tidyverse)
library(spdep)
require(dplyr)
library(RANN)
library(rgdal)
library(RColorBrewer)
library(knitr)
library(magrittr)
require(readr)
require(dplyr)
setwd("/Users/ninamasters/measles-spatial-model")
#### #make simplified environment ####
#define function to make nxn grid of spatial points data frame and spatial polygon data frame
make_me_a_grid <- function(n){
x <- seq(1,n, by = 1)
y <- seq(1,n, by = 1)
num_cells = length(x)*length(y)
xy <<- expand.grid(x=x, y=y)
grid.pts<<-SpatialPointsDataFrame(coords= xy, data=xy)
#make points a gridded object
gridded(grid.pts) <- TRUE
#plot(grid.pts)
grid <- as(grid.pts, "SpatialPolygons") #encode as spatial polygons
#str(grid)
gridspdf <<- SpatialPolygonsDataFrame(grid, data=data.frame(ID=row.names(grid), row.names=row.names(grid)))
return(grid.pts)
}
#make grid of 16 x 16 to get 256-square cell grid
make_me_a_grid(16)
gridspdf$ID <- sapply(slot(gridspdf, "polygons"), function(x) slot(x, "ID"))
# set up adjacencies / boundaries using both queen and rook style boundaries
queen_boundaries_grid <- poly2nb(gridspdf, queen = T)
rook_boundaries_grid <- poly2nb(gridspdf, queen = F)
############################### Pull in unique codes on aggregation scales ###############################
############################### here we have 1,2,3,4 for quadrants, etc. #################################
level_unique <- read.csv("/Users/ninamasters/Desktop/Dissertation/Aim 1 - Spatial Model/Clustering Motifs/level_unique.csv")
motifsu <- merge(gridspdf,level_unique, by="ID")
##############################################################################################
######################## Now create grids under different motifs #############################
##############################################################################################
#define function make_me_a_motif
motif_df <- function(x,y,z,q){
#create distribution of the non-vaccinators in each quadrant
quadrant <- c(1,2,3,4)
probability_quadrant <- as.data.frame(cbind(quadrant,x))
#create distribution of the non-vaccinators in each quadrant at the neighborhood level
neighborhoods <- c(1:16)
neighb_quad <- c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4)
y_neighb <- rep(y, 2)
probability_neighb <- as.data.frame(cbind(neighborhoods, neighb_quad))
probability_neighb <- as.data.frame(cbind(probability_neighb, y_neighb))
names(probability_neighb) <- c("neighb", "quadrant", "y")
prob_2 <- dplyr::left_join(probability_quadrant, probability_neighb, by="quadrant")
#create distribution of non-vaccinators in each quadrant at the block level (n = 64)
blocks <- c(1:64)
block_neighb <- c(1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,13,13,14,14,15,15,16,16)
y_block <- rep(z, 4)
probability_block <- as.data.frame(cbind(blocks, block_neighb))
probability_block <- as.data.frame(cbind(probability_block, y_block))
names(probability_block) <- c("block", "neighb", "z")
prob_3 <- dplyr::left_join(prob_2, probability_block, by="neighb")
#create distribution of non-vaccinators in each quadrant at the individual cell level (n = 256)
cells <- c(1:256)
cell_block <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64)
y_cell <- rep(q, 8)
probability_cell <- as.data.frame(cbind(cells, cell_block))
probability_cell <- as.data.frame(cbind(probability_cell, y_cell))
names(probability_cell) <- c("cell", "block", "q")
prob_4 <- dplyr::left_join(prob_3, probability_cell, by="block")
#now calculate cumulative probability
prob_4 <- dplyr::mutate(prob_4,cumulative_prob = (x*y*z*q))
#sort data by cell
prob_4 <- prob_4[order(prob_4$cell),]
#### multinomial draw according to distributions in prob_4
#add the number of nonvaccinators per cell to prob_4 data frame
prob_4 <- dplyr::mutate(prob_4, nonvax_percell =rmultinom(1, 12800, prob_4$cumulative_prob))
#create percent of non-vaccinators (/1000, multiplied by 100 to get percent)
prob_4 <- dplyr::mutate(prob_4, percent_nonvax =nonvax_percell/1000*100, vax_percell = 1000-nonvax_percell, infected_percell = 0) #final prob_4 dataset
vax_motif <- dplyr::select(prob_4, cell, percent_nonvax, nonvax_percell, vax_percell, infected_percell)
vax_motif <- dplyr::mutate(vax_motif, ID = paste("g", cell, sep=""))
names(vax_motif) <- c("cell", "percent_nonvax", "So", "Ro", "Io", "ID")
# now merge with gridspdf, the spatial polygon data frame containing our grid information
vax_motif_grid <<- merge(gridspdf,vax_motif, by="ID")
# calculate the Moran's I
w <- poly2nb(vax_motif_grid, queen = T, row.names = vax_motif_grid$ID)
#use style = "W" for row-standardized values of Moran's I
wm <- nb2mat(w, style='W', zero.policy = TRUE)
ww <- nb2listw(w, style = 'W', zero.policy =TRUE)
m <- moran(vax_motif_grid$percent_nonvax, ww, length(ww$neighbours), S0=Szero(ww))
return(vax_motif_grid)
}
#define function make_me_a_motif
make_me_a_motif <- function(x,y,z,q){
#create distribution of the non-vaccinators in each quadrant
quadrant <- c(1,2,3,4)
probability_quadrant <- as.data.frame(cbind(quadrant,x))
#create distribution of the non-vaccinators in each quadrant at the neighborhood level
neighborhoods <- c(1:16)
neighb_quad <- c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4)
y_neighb <- rep(y, 2)
probability_neighb <- as.data.frame(cbind(neighborhoods, neighb_quad))
probability_neighb <- as.data.frame(cbind(probability_neighb, y_neighb))
names(probability_neighb) <- c("neighb", "quadrant", "y")
prob_2 <- dplyr::left_join(probability_quadrant, probability_neighb, by="quadrant")
#create distribution of non-vaccinators in each quadrant at the block level (n = 64)
blocks <- c(1:64)
block_neighb <- c(1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,13,13,14,14,15,15,16,16)
y_block <- rep(z, 4)
probability_block <- as.data.frame(cbind(blocks, block_neighb))
probability_block <- as.data.frame(cbind(probability_block, y_block))
names(probability_block) <- c("block", "neighb", "z")
prob_3 <- dplyr::left_join(prob_2, probability_block, by="neighb")
#create distribution of non-vaccinators in each quadrant at the individual cell level (n = 256)
cells <- c(1:256)
cell_block <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64)
y_cell <- rep(q, 8)
probability_cell <- as.data.frame(cbind(cells, cell_block))
probability_cell <- as.data.frame(cbind(probability_cell, y_cell))
names(probability_cell) <- c("cell", "block", "q")
prob_4 <- dplyr::left_join(prob_3, probability_cell, by="block")
#now calculate cumulative probability
prob_4 <- dplyr::mutate(prob_4,cumulative_prob = (x*y*z*q))
#sort data by cell
prob_4 <- prob_4[order(prob_4$cell),]
#### multinomial draw according to distributions in prob_4
#add the number of nonvaccinators per cell to prob_4 data frame
prob_4 <- dplyr::mutate(prob_4, nonvax_percell =rmultinom(1, 12800, prob_4$cumulative_prob))
#create percent of non-vaccinators (/1000, multiplied by 100 to get percent)
prob_4 <- dplyr::mutate(prob_4, percent_nonvax =nonvax_percell/1000*100, vax_percell = 1000-nonvax_percell, infected_percell = 0) #final prob_4 dataset
vax_motif <- dplyr::select(prob_4, cell, percent_nonvax, nonvax_percell, vax_percell, infected_percell)
vax_motif <- dplyr::mutate(vax_motif, ID = paste("g", cell, sep=""))
names(vax_motif) <- c("cell", "percent_nonvax", "So", "Ro", "Io", "ID")
# now merge with gridspdf, the spatial polygon data frame containing our grid information
vax_motif_grid <<- merge(gridspdf,vax_motif, by="ID")
#now print out a visual grid with that motif
plotvar <- vax_motif_grid$percent_nonvax
#define color scheme for plot
plotclr <- brewer.pal(9,"PuRd")
colcode <- ifelse((plotvar <= 0), plotclr[1],
ifelse((plotvar > 0 & plotvar <= 1), plotclr[2],
ifelse((plotvar > 1 & plotvar <= 2), plotclr[3],
ifelse((plotvar > 2 & plotvar <= 5), plotclr[4],
ifelse((plotvar > 5 & plotvar <= 10), plotclr[5],
ifelse((plotvar > 10 & plotvar <= 15), plotclr[6],
ifelse((plotvar > 15 & plotvar <= 20), plotclr[7],
ifelse((plotvar > 20 & plotvar <= 25), plotclr[8],
plotclr[9]))))))))
# calculate the Moran's I
w <- poly2nb(vax_motif_grid, queen = T, row.names = vax_motif_grid$ID)
class(w)
#use style = "W" for row-standardized values of Moran's I
wm <- nb2mat(w, style='W', zero.policy = TRUE)
ww <- nb2listw(w, style = 'W', zero.policy =TRUE)
m <- moran(vax_motif_grid$percent_nonvax, ww, length(ww$neighbours), S0=Szero(ww))
m1 <- moran.test(vax_motif_grid$percent_nonvax, ww, randomisation=FALSE, alternative = "two.sided")
#define plot margins
par(mar=c(1, 10, 5, 1))
#for x, y4, z4, q4
p <- plot(vax_motif_grid, col = colcode)
title(paste(x[1], y[1], z[1], q[1], "Moran's I = ", round(m$I,4)), line = 0.6)
# save moran list as well
moran_list <<- list(level_1 = x[1], level_2 = y[1], level_3 = z[1], level_4 = q[1], Moran = m$I, Moran_Var = m1[[3]][[3]])
return(p)
}
#define function make_me_a_motif_plot
make_me_a_motif_plot <- function(x,y,z,q){
#create distribution of the non-vaccinators in each quadrant
quadrant <- c(1,2,3,4)
probability_quadrant <- as.data.frame(cbind(quadrant,x))
#create distribution of the non-vaccinators in each quadrant at the neighborhood level
neighborhoods <- c(1:16)
neighb_quad <- c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4)
y_neighb <- rep(y, 2)
probability_neighb <- as.data.frame(cbind(neighborhoods, neighb_quad))
probability_neighb <- as.data.frame(cbind(probability_neighb, y_neighb))
names(probability_neighb) <- c("neighb", "quadrant", "y")
prob_2 <- dplyr::left_join(probability_quadrant, probability_neighb, by="quadrant")
#create distribution of non-vaccinators in each quadrant at the block level (n = 64)
blocks <- c(1:64)
block_neighb <- c(1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,13,13,14,14,15,15,16,16)
y_block <- rep(z, 4)
probability_block <- as.data.frame(cbind(blocks, block_neighb))
probability_block <- as.data.frame(cbind(probability_block, y_block))
names(probability_block) <- c("block", "neighb", "z")
prob_3 <- dplyr::left_join(prob_2, probability_block, by="neighb")
#create distribution of non-vaccinators in each quadrant at the individual cell level (n = 256)
cells <- c(1:256)
cell_block <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64)
y_cell <- rep(q, 8)
probability_cell <- as.data.frame(cbind(cells, cell_block))
probability_cell <- as.data.frame(cbind(probability_cell, y_cell))
names(probability_cell) <- c("cell", "block", "q")
prob_4 <- dplyr::left_join(prob_3, probability_cell, by="block")
#now calculate cumulative probability
prob_4 <- dplyr::mutate(prob_4,cumulative_prob = (x*y*z*q))
#sort data by cell
prob_4 <- prob_4[order(prob_4$cell),]
#### multinomial draw according to distributions in prob_4
#add the number of nonvaccinators per cell to prob_4 data frame
prob_4 <- dplyr::mutate(prob_4, nonvax_percell =rmultinom(1, 12800, prob_4$cumulative_prob))
#create percent of non-vaccinators (/1000, multiplied by 100 to get percent)
prob_4 <- dplyr::mutate(prob_4, percent_nonvax =nonvax_percell/1000*100, vax_percell = 1000-nonvax_percell, infected_percell = 0) #final prob_4 dataset
vax_motif <- dplyr::select(prob_4, cell, percent_nonvax, nonvax_percell, vax_percell, infected_percell)
vax_motif <- dplyr::mutate(vax_motif, ID = paste("g", cell, sep=""))
names(vax_motif) <- c("cell", "percent_nonvax", "So", "Ro", "Io", "ID")
# now merge with gridspdf, the spatial polygon data frame containing our grid information
vax_motif_grid <<- merge(gridspdf,vax_motif, by="ID")
#now print out a visual grid with that motif
plotvar <- vax_motif_grid$percent_nonvax
#define color scheme for plot
plotclr <- brewer.pal(9,"Reds")
colcode <- ifelse((plotvar <= 0), plotclr[1],
ifelse((plotvar > 0 & plotvar <= 1), plotclr[2],
ifelse((plotvar > 1 & plotvar <= 2), plotclr[3],
ifelse((plotvar > 2 & plotvar <= 5), plotclr[4],
ifelse((plotvar > 5 & plotvar <= 10), plotclr[5],
ifelse((plotvar > 10 & plotvar <= 15), plotclr[6],
ifelse((plotvar > 15 & plotvar <= 20), plotclr[7],
ifelse((plotvar > 20 & plotvar <= 25), plotclr[8],
plotclr[9]))))))))
# calculate the Moran's I
w <- poly2nb(vax_motif_grid, queen = T, row.names = vax_motif_grid$ID)
class(w)
#use style = "W" for row-standardized values of Moran's I
wm <- nb2mat(w, style='W', zero.policy = TRUE)
ww <- nb2listw(w, style = 'W', zero.policy =TRUE)
m <- moran(vax_motif_grid$percent_nonvax, ww, length(ww$neighbours), S0=Szero(ww))
m1 <- moran.test(vax_motif_grid$percent_nonvax, ww, randomisation=FALSE, alternative = "two.sided")
#define plot margins
par(mar=c(2, 10, 5, 1))
jpeg(paste0("/Users/ninamasters/Desktop/Dissertation/Aim 1 - Spatial Model/Clustering Motifs/Motifs 0.95/Motif",i,".jpg"), width = 550, height = 350)
plot(vax_motif_grid, col = colcode)
title(paste("95% Vaccination: Clustering L1-L4: ", x[1], y[1], z[1], q[1]), line = 0.6)
#add legend
legend("left", inset = c(0,0), title = "Unvaccinated per cell", legend = c("0", "0-1%", "1-2%", "2-5%", "5-10%", "10-15%", "15-20%", "20-25%", ">25%"), col = brewer.pal(9,"Reds")[1:9], lty = 1, lwd = 8, cex = 1)
dev.off()
#return(p)
}
make_me_a_moran <- function(x,y,z,q){
#create distribution of the non-vaccinators in each quadrant
quadrant <- c(1,2,3,4)
probability_quadrant <- as.data.frame(cbind(quadrant,x))
#create distribution of the non-vaccinators in each quadrant at the neighborhood level
neighborhoods <- c(1:16)
neighb_quad <- c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4)
y_neighb <- rep(y, 2)
probability_neighb <- as.data.frame(cbind(neighborhoods, neighb_quad))
probability_neighb <- as.data.frame(cbind(probability_neighb, y_neighb))
names(probability_neighb) <- c("neighb", "quadrant", "y")
prob_2 <- dplyr::left_join(probability_quadrant, probability_neighb, by="quadrant")
#create distribution of non-vaccinators in each quadrant at the block level (n = 64)
blocks <- c(1:64)
block_neighb <- c(1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,13,13,14,14,15,15,16,16)
y_block <- rep(z, 4)
probability_block <- as.data.frame(cbind(blocks, block_neighb))
probability_block <- as.data.frame(cbind(probability_block, y_block))
names(probability_block) <- c("block", "neighb", "z")
prob_3 <- dplyr::left_join(prob_2, probability_block, by="neighb")
#create distribution of non-vaccinators in each quadrant at the individual cell level (n = 256)
cells <- c(1:256)
cell_block <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64)
y_cell <- rep(q, 8)
probability_cell <- as.data.frame(cbind(cells, cell_block))
probability_cell <- as.data.frame(cbind(probability_cell, y_cell))
names(probability_cell) <- c("cell", "block", "q")
prob_4 <- dplyr::left_join(prob_3, probability_cell, by="block")
#now calculate cumulative probability
prob_4 <- dplyr::mutate(prob_4,cumulative_prob = (x*y*z*q))
#sort data by cell
prob_4 <- prob_4[order(prob_4$cell),]
#### multinomial draw according to distributions in prob_4
#add the number of nonvaccinators per cell to prob_4 data frame
prob_4 <- dplyr::mutate(prob_4, nonvax_percell =rmultinom(1, 12800, prob_4$cumulative_prob))
#create percent of non-vaccinators (/1000, multiplied by 100 to get percent)
prob_4 <- dplyr::mutate(prob_4, percent_nonvax =nonvax_percell/1000*100, vax_percell = 1000-nonvax_percell, infected_percell = 0) #final prob_4 dataset
vax_motif <- dplyr::select(prob_4, cell, percent_nonvax, nonvax_percell, vax_percell, infected_percell)
vax_motif <- dplyr::mutate(vax_motif, ID = paste("g", cell, sep=""))
names(vax_motif) <- c("cell", "percent_nonvax", "So", "Ro", "Io", "ID")
# now merge with gridspdf, the spatial polygon data frame containing our grid information
vax_motif_grid <<- merge(gridspdf,vax_motif, by="ID")
# calculate the Moran's I
w <- poly2nb(vax_motif_grid, queen = T, row.names = vax_motif_grid$ID)
class(w)
#use style = "W" for row-standardized values of Moran's I
wm <- nb2mat(w, style='W', zero.policy = TRUE)
ww <- nb2listw(w, style = 'W', zero.policy =TRUE)
m <- moran(vax_motif_grid$percent_nonvax, ww, length(ww$neighbours), S0=Szero(ww))
# save moran list as well
moran_list <<- list(level_1 = x[1], level_2 = y[1], level_3 = z[1], level_4 = q[1], Moran = m$I)
return(moran_list)
}
#list of the variable combinations
#can create different "x" motifs here for outermost layer of clustering
x <- c(0.85, 0.05, 0.05, 0.05)
x1 <- c(0.7, 0.1, 0.1, 0.1)
x2 <- c(0.58, 0.14, 0.14, 0.14)
x3 <- c(0.4, 0.2, 0.2, 0.2)
x4 <- c(0.25, 0.25, 0.25, 0.25)
#can create different "y" motifs for neighborhood level
y <- c(0.85, 0.05, 0.85, 0.05, 0.05, 0.05, 0.05, 0.05)
y1 <- c(0.7, 0.1, 0.7, 0.1, 0.1, 0.1, 0.1, 0.1)
y2 <- c(0.58, 0.14, 0.58, 0.14, 0.14, 0.14, 0.14, 0.14)
y3 <- c(0.4, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.2)
y4 <- c(0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25) #homogeneous quadrants at neighborhood level
#can create different "y" motifs for block level
z <- c(0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05)
z1 <- c(0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
z2 <- c(0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14)
z3 <- c(0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2)
z4 <- c(0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25) #homogeneous quadrants at neighborhood level
#can create different "y" motifs for individual level
q <- c(0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.85, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05)
q1 <- c(0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.7, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
q2 <- c(0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.58, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14)
q3 <- c(0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2)
q4 <- c(0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25) #homogeneous quadrants at neighborhood level
#combination of grids:
x_grid <- list(x, x1, x2, x3, x4)
names(x_grid) <- c("x", "x1", "x2", "x3", "x4")
y_grid <- list(y, y1, y2, y3, y4)
names(y_grid) <- c("y", "y1", "y2", "y3", "y4")
z_grid <- list(z, z1, z2, z3, z4)
names(z_grid) <- c("z", "z1", "z2", "z3", "z4")
q_grid <- list(q, q1, q2, q3, q4)
names(q_grid) <- c("q", "q1", "q2", "q3", "q4")
combo_list <- as.list(expand.grid(x_grid, y_grid, z_grid, q_grid))
names(combo_list) <- c("v1", "v2", "v3", "v4")
#combo_list[["v1"]]
#################################################################################
######################## Isolation Calculations #################################
# calculate the isolation index
isolation <- function(vax_motif_grid){
a <- vax_motif_grid$So[(vax_motif_grid$So+vax_motif_grid$Ro) > 0]
n <- (vax_motif_grid$So+vax_motif_grid$Ro)[(vax_motif_grid$So+vax_motif_grid$Ro) > 0]
total_a <- sum(a)
d <<- sum((a/total_a)*(a/n))
return(d)
}
make_me_an_isolation <- function(x,y,z,q){
#create distribution of the non-vaccinators in each quadrant
quadrant <- c(1,2,3,4)
probability_quadrant <- as.data.frame(cbind(quadrant,x))
#create distribution of the non-vaccinators in each quadrant at the neighborhood level
neighborhoods <- c(1:16)
neighb_quad <- c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4)
y_neighb <- rep(y, 2)
probability_neighb <- as.data.frame(cbind(neighborhoods, neighb_quad))
probability_neighb <- as.data.frame(cbind(probability_neighb, y_neighb))
names(probability_neighb) <- c("neighb", "quadrant", "y")
prob_2 <- dplyr::left_join(probability_quadrant, probability_neighb, by="quadrant")
#create distribution of non-vaccinators in each quadrant at the block level (n = 64)
blocks <- c(1:64)
block_neighb <- c(1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,13,13,14,14,15,15,16,16)
y_block <- rep(z, 4)
probability_block <- as.data.frame(cbind(blocks, block_neighb))
probability_block <- as.data.frame(cbind(probability_block, y_block))
names(probability_block) <- c("block", "neighb", "z")
prob_3 <- dplyr::left_join(prob_2, probability_block, by="neighb")
#create distribution of non-vaccinators in each quadrant at the individual cell level (n = 256)
cells <- c(1:256)
cell_block <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64,57,57,58,58,59,59,60,60,61,61,62,62,63,63,64,64)
y_cell <- rep(q, 8)
probability_cell <- as.data.frame(cbind(cells, cell_block))
probability_cell <- as.data.frame(cbind(probability_cell, y_cell))
names(probability_cell) <- c("cell", "block", "q")
prob_4 <- dplyr::left_join(prob_3, probability_cell, by="block")
#now calculate cumulative probability
prob_4 <- dplyr::mutate(prob_4,cumulative_prob = (x*y*z*q))
#sort data by cell
prob_4 <- prob_4[order(prob_4$cell),]
#### multinomial draw according to distributions in prob_4
#add the number of nonvaccinators per cell to prob_4 data frame
prob_4 <- dplyr::mutate(prob_4, nonvax_percell =rmultinom(1, 12800, prob_4$cumulative_prob))
#create percent of non-vaccinators (/1000, multiplied by 100 to get percent)
prob_4 <- dplyr::mutate(prob_4, percent_nonvax =nonvax_percell/1000*100, vax_percell = 1000-nonvax_percell, infected_percell = 0) #final prob_4 dataset
vax_motif <- dplyr::select(prob_4, cell, percent_nonvax, nonvax_percell, vax_percell, infected_percell)
vax_motif <- dplyr::mutate(vax_motif, ID = paste("g", cell, sep=""))
names(vax_motif) <- c("cell", "percent_nonvax", "So", "Ro", "Io", "ID")
# now merge with gridspdf, the spatial polygon data frame containing our grid information
vax_motif_grid <<- merge(gridspdf,vax_motif, by="ID")
isolation_list <<- list(level_1 = x[1], level_2 = y[1], level_3 = z[1], level_4 = q[1], isolation_index = isolation(vax_motif_grid))
return(isolation_list)
}