-
Notifications
You must be signed in to change notification settings - Fork 0
/
COVID19 distancing.R
251 lines (210 loc) · 13.2 KB
/
COVID19 distancing.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
## spatial and temporal distancing COVID-19
## CJA Bradshaw, Flinders University
## corey.bradshaw@flinders.edu.au
## March 2020
## remove everything
rm(list = ls())
# set up spatial matrix
mat.dim <- 20
spat.layer <- matrix(data=0, nrow=mat.dim, ncol=mat.dim)
popn <- 1000 # population size
infect.pr <- 0.02
max.move <- round(mat.dim/2, 0) # maximum movement in one coordinate per time interval
min.move <- 0
move.prob <- 0.5 # probability of moving in a given time interval
move.pr.red <- 0.2 # equates to an 80% reduction in movement probability
move.pr <- move.prob*move.pr.red
exp.pr <- 0.5 # probability of exposure (decreases with increasing temporal distancing)
# infection spread
t.steps <- 50
iter <- 1000
itdiv <- iter/10
tot.inf.out <- matrix(data=NA, nrow=iter, ncol=t.steps+1)
for (s in 1:iter) {
# place population randomly into grid
row.rn <- sample(1:mat.dim, popn, replace=T)
col.rn <- sample(1:mat.dim, popn, replace=T)
spat.init <- spat.layer
for (p in 1:popn) {
spat.init[row.rn[p], col.rn[p]] <- spat.init[row.rn[p], col.rn[p]] + 1
}
spat.init
# start infection processes
init.infect <- 1 # initial number of infected in the matrix
# randomly allocate infected number of people to start matrix
st.pop.coords <- which(spat.init > 0, arr.ind=T) # populated cell coordinates
ran.inf.coords <- sample(1:dim(st.pop.coords)[1], init.infect, replace=T)
st.inf.coords <- as.matrix(t(st.pop.coords[ran.inf.coords,]))
st.inf.coords
infect.mat <- spat.layer
infect.mat[st.inf.coords[1,1], st.inf.coords[1,2]] <- init.infect
not.infect.mat <- spat.init - infect.mat
infect.n <- rep(0,t.steps+1) # number infected time series
infect.n[1] <- init.infect
for (t in 2:(t.steps+1)) {
for (i in 1:mat.dim) {
for (j in 1:mat.dim) {
## total pop in cell
n.infected <- infect.mat[i,j]
n.not.infected <- not.infect.mat[i,j]
cell.pop <- n.not.infected + n.infected
prop.infected <- n.infected/cell.pop
if (cell.pop > 0) {
## new infections
pr.new.infect <- ifelse(((exp.pr*infect.pr)^(1-prop.infected)) > 1, 1, ((exp.pr*infect.pr)^(1-prop.infected))) # probability of a new infection
n.new.infect <- rbinom(n=1, size=n.not.infected, prob=pr.new.infect)
# update infection & non-infection layers
infect.mat[i,j] <- infect.mat[i,j] + n.new.infect
not.infect.mat[i,j] <- ifelse(cell.pop - infect.mat[i,j] < 0, 0, cell.pop - infect.mat[i,j])
# recalculate infected & non-infected
n.infected <- infect.mat[i,j]
n.not.infected <- not.infect.mat[i,j]
# movement
# move infected people first
if (n.infected > 0) {
move.row.rn <- sample(min.move:max.move, n.infected, replace=T) # move rows
move.col.rn <- sample(min.move:max.move, n.infected, replace=T) # move columns
dir.row.rn <- sample(c(-1,1), n.infected, replace=T) # direction rows
dir.col.rn <- sample(c(-1,1), n.infected, replace=T) # direction columns
## both directions ok
for (n in 1:n.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] < mat.dim & i + move.row.rn[n]*dir.row.rn[n] > 0) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] <- infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] + 1
infect.mat[i, j] <- ifelse((infect.mat[i, j] - 1) < 0, 0, infect.mat[i, j] - 1)
}
}
}
}
# cannot move max rows; try other direction
for (n in 1:n.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] > mat.dim & i + move.row.rn[n]*dir.row.rn[n] > 0) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] <- infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] + 1
infect.mat[i, j] <- ifelse((infect.mat[i, j] - 1) < 0, 0, infect.mat[i, j] - 1)
}
}
}
}
# cannot move min rows; try other direction
for (n in 1:n.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] < mat.dim & i + move.row.rn[n]*dir.row.rn[n] < 0) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] <- infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] + 1
infect.mat[i, j] <- ifelse((infect.mat[i, j] - 1) < 0, 0, infect.mat[i, j] - 1)
}
}
}
}
# cannot move max cols; try other direction
for (n in 1:n.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] < mat.dim & i + move.row.rn[n]*dir.row.rn[n] > 0) {
if (j + move.col.rn[n]*dir.col.rn[n] > mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] <- infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] + 1
infect.mat[i, j] <- ifelse((infect.mat[i, j] - 1) < 0, 0, infect.mat[i, j] - 1)
}
}
}
}
# cannot move min cols
for (n in 1:n.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] < mat.dim & i + move.row.rn[n]*dir.row.rn[n] > 0) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] < 0) {
infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] <- infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] + 1
infect.mat[i, j] <- ifelse((infect.mat[i, j] - 1) < 0, 0, infect.mat[i, j] - 1)
}
}
}
}
# recalculate infected & non-infected
n.infected <- infect.mat[i,j]
n.not.infected <- not.infect.mat[i,j]
}
# move non-infected people now
if (n.not.infected > 0) {
move.row.rn <- sample(min.move:max.move, n.not.infected, replace=T) # move rows
move.col.rn <- sample(min.move:max.move, n.not.infected, replace=T) # move columns
dir.row.rn <- sample(c(-1,1), n.not.infected, replace=T) # direction rows
dir.col.rn <- sample(c(-1,1), n.not.infected, replace=T) # direction columns
## both directions ok
for (n in 1:n.not.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] < mat.dim & i + move.row.rn[n]*dir.row.rn[n] > 0) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
not.infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] <- not.infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] + 1
not.infect.mat[i, j] <- ifelse((not.infect.mat[i, j] - 1) < 0, 0, not.infect.mat[i, j] - 1)
}
}
}
}
# cannot move max rows; try other direction
for (n in 1:n.not.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] > mat.dim & i + move.row.rn[n]*dir.row.rn[n] > 0) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
not.infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] <- not.infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] + 1
not.infect.mat[i, j] <- ifelse((not.infect.mat[i, j] - 1) < 0, 0, not.infect.mat[i, j] - 1)
}
}
}
}
# cannot move min rows; try other direction
for (n in 1:n.not.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] < mat.dim & i + move.row.rn[n]*dir.row.rn[n] < 0) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
not.infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] <- not.infect.mat[i + move.row.rn[n]*-1*dir.row.rn[n], j + move.col.rn[n]*dir.col.rn[n]] + 1
not.infect.mat[i, j] <- ifelse((not.infect.mat[i, j] - 1) < 0, 0, not.infect.mat[i, j] - 1)
}
}
}
}
# cannot move max cols; try other direction
for (n in 1:n.not.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (i + move.row.rn[n]*dir.row.rn[n] < mat.dim & i + move.row.rn[n]*dir.row.rn[n] > 0) {
if (j + move.col.rn[n]*dir.col.rn[n] > mat.dim & j + move.col.rn[n]*dir.col.rn[n] > 0) {
not.infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] <- not.infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] + 1
not.infect.mat[i, j] <- ifelse((not.infect.mat[i, j] - 1) < 0, 0, not.infect.mat[i, j] - 1)
}
}
}
}
# cannot move min cols
for (n in 1:n.not.infected) {
if (rbinom(1,1,move.pr) == 1) {
if (((i + move.row.rn[n]*dir.row.rn[n]) < mat.dim) & ((i + move.row.rn[n]*dir.row.rn[n]) > 0)) {
if (j + move.col.rn[n]*dir.col.rn[n] < mat.dim & j + move.col.rn[n]*dir.col.rn[n] < 0) {
not.infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] <- not.infect.mat[i + move.row.rn[n]*dir.row.rn[n], j + move.col.rn[n]*-1*dir.col.rn[n]] + 1
not.infect.mat[i, j] <- ifelse((not.infect.mat[i, j] - 1) < 0, 0, not.infect.mat[i, j] - 1)
}
}
}
}
}
}
} # end j loop
} # end i loop
# sum total infected
infect.n[t] <- sum(infect.mat)
#image(infect.mat, col=rev(heat.colors(10,1)))
#print(t)
} # end t loop
#plot(1:(t.steps+1), infect.n, type="l", xlab="time", ylab="number infected")
if (s %% itdiv==0) print(s)
tot.inf.out[s, ] <- infect.n
} # end s loop
inf.ts.med <- apply(tot.inf.out, MARGIN=2, median, na.rm=T)
inf.ts.lo <- apply(tot.inf.out, MARGIN=2, quantile, probs=0.025, na.rm=T)
inf.ts.up <- apply(tot.inf.out, MARGIN=2, quantile, probs=0.975, na.rm=T)
plot(1:(t.steps+1), inf.ts.med, type="l", xlab="time", ylab="number infected")
lines(1:(t.steps+1), inf.ts.lo, lty=2, col="red")
lines(1:(t.steps+1), inf.ts.up, lty=2, col="red")
out.dat <- data.frame(1:(t.steps+1), inf.ts.med, inf.ts.up, inf.ts.lo)
colnames(out.dat) <- c("step", "med", "up", "lo")
write.table(out.dat,file="20pcMove50pcExp.out.csv",sep=",", row.names = F, col.names = T)