-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathModelOutput.Rmd
271 lines (203 loc) · 7.69 KB
/
ModelOutput.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
---
title: "Plotting Model Output"
output: html_notebook
---
```{r}
library(tidyverse)
library(gghighlight)
library(purrr)
library(plotly)
library(cowplot)
library(scales)
```
# Read in model output files
```{r}
# this bit of code is now obsolete.
#pathname='Locations'
#dir(pathname,pattern='TotalInfections.dat',recursive=TRUE)->infectionfiles
# readinfections<-function (fname,fpath){
# # Function to read in TotalInfections.dat' type files (one column only of infecteds, Time is row number)
# d<-read_csv(file.path(fpath,fname),col_names=FALSE,cols(X1=col_integer())) %>% #
# mutate(Time=row_number()) %>%
# select(Time,Infecteds=X1)
# return(d)
# }
# data <- tibble(filename = infectionfiles) %>% # create a data frame
# holding the file names
# mutate(file_contents = map(filename, # read files into a new data column
# ~readinfections(., pathname))) %>% # use the function written above to process files
# mutate(Location=str_split(filename,'/',simplify = TRUE)[,1]) %>%
# mutate(Run=str_split(filename,'/',simplify = TRUE)[,2]) %>%
# select(-filename) %>%
# unnest(cols=file_contents)
# data %>%
# mutate(Infected=Infecteds/10^6) %>%
# filter(Time<250) %>%
# ggplot(aes(x=Time,y=Infected,colour=Run))+
# geom_line()+
# gghighlight(~Location)+
# facet_wrap(~Location)+
# theme_minimal_grid()+
# theme(strip.text.x = element_blank())+
# scale_y_continuous(labels = unit_format(unit = "M"))+
# xlab('Time (days)')
#
#
# data %>%
# group_by(Run) %>%
# summarise(TimeToPeak=which.max(Infecteds)) %>% summary()
#
#
#
# ggplot(aes(x=TimeToPeak))+
# geom_histogram()
```
# Number of Infected Wards
```{r}
read.csv(file='NumberWardsInfected.dat',sep=' ') %>%
mutate(Time=X0) %>%
select(-X,-X0,-X0.4) ->wardcurves
wardcurves %>%
pivot_longer(-Time,names_to = 'Classes',values_to = 'NumberWards')%>%
ggplot(aes(x=Time,y=NumberWards,colour=Classes))+
geom_line()
```
# Incidence curves
```{r}
readincidence<-function (fname,fpath){ # read and process a single file
# Function to read in Work/PlayInfections.dat' type files (many columns, one for each class only of infecteds, Time is row number)
d<-read_delim(file.path(fpath,fname),col_names=FALSE,delim=' ',col_types = "iiiiiii")%>% select(Time=X1,Incidence=X4)
return(d)
}
readIncidenceRecovered<-function (fname,fpath){ # read and process a single file
# Function to read in Work/PlayInfections.dat' type files (many columns, one for each class only of infecteds, Time is row number)
d<-read_delim(file.path(fpath,fname),col_names=FALSE,delim=' ',col_types = "iiiiiii")%>% select(Time=X1,Incidence=X4,Recovered=X6)
return(d)
}
readPrevalence<-function (fname,fpath){ # read and process a single file
# Function to read in Work/PlayInfections.dat' type files (many columns, one for each class only of infecteds, Time is row number)
d<-read_delim(file.path(fpath,fname),col_names=FALSE,delim=' ',col_types = "iiiiiii")%>%
transmute(Time=X1,Prevalence=X4+X5)
return(d)
}
```
```{r}
pathname='OldRuns/'
dir(pathname,pattern='PlayInfections.dat',recursive=TRUE)->pfilenames
dir(pathname,pattern='WorkInfections.dat',recursive=TRUE)->wfilenames
#PLAY infecteds
dataIP <- tibble(filename = pfilenames) %>% # create a data frame
# holding the file names
mutate(file_contents = map(filename, # read files into a new data column
~readIncidenceRecovered(., pathname))) %>% # use the function written above to process files
mutate(Location=str_split(filename,'/',simplify = TRUE)[,1]) %>%
mutate(Run=str_split(filename,'/',simplify = TRUE)[,2]) %>%
select(-filename) %>%
unnest(cols=file_contents)
# WORK infected
dataIW <- tibble(filename = wfilenames) %>% # create a data frame
# holding the file names
mutate(file_contents = map(filename, # read files into a new data column
~readIncidenceRecovered(., pathname))) %>% # use the function written above to process files
mutate(Location=str_split(filename,'/',simplify = TRUE)[,1]) %>%
mutate(Run=str_split(filename,'/',simplify = TRUE)[,2]) %>%
select(-filename) %>%
unnest(cols=file_contents)
bind_cols(dataIW,dataIP) %>%
mutate(Incidence=Incidence+Incidence1) %>%
mutate(Recovered=Recovered+Recovered1) %>%
select(Time,Incidence,Recovered,Location,Run)->dataIR
dataIR %>%
mutate(Date=as.Date(Time,origin='2020-02-10')) %>%
#mutate(Incidence=Incidence/10^6) %>%
#mutate(Recovered=Recovered/10^6) %>%
filter(Time<450) %>%
ggplot(aes(x=Date,group=Run))+
geom_line(aes(y=Incidence),alpha=0.3,size=1,colour='red')+
# geom_line(aes(y=Recovered),alpha=0.1)+
# facet_wrap(~Location)+
theme_minimal_grid()+
theme(legend.position = "none")+
#scale_y_continuous(labels = unit_format(unit = "M"))+
xlab('Time')+
scale_x_date(breaks=date_breaks("3 months"),
labels = date_format("%b"))->p
#ggplotly(p)
p
dataIR %>%
group_by(Run) %>%
summarise(PeakIncidence=max(Incidence)) %>%
summary()
```
# Time To Peak Incidence
```{r}
dataIR %>%
group_by(Run) %>%
summarise(TimeToPeak=which.max(Incidence)) %>%
ggplot(aes(x=TimeToPeak))+
geom_histogram(aes(y=..density..),bins=12)+
geom_density()+
theme_minimal_grid()+
xlab('Time to Peak Incidence')+
ylab('Density')->p2
dataIR %>%
group_by(Run) %>%
summarise(TimeToPeak=which.max(Incidence)) %>% summary()
```
# Prevalence Curves (Correct)
```{r}
dataPrevP <- tibble(filename = pfilenames) %>% # create a data frame
# holding the file names
mutate(file_contents = map(filename, # read files into a new data column
~readPrevalence(., pathname))) %>% # use the function written above to process files
mutate(Location=str_split(filename,'/',simplify = TRUE)[,1]) %>%
mutate(Run=str_split(filename,'/',simplify = TRUE)[,2]) %>%
select(-filename) %>%
unnest(cols=file_contents)
dataPrevW <- tibble(filename = wfilenames) %>% # create a data frame
# holding the file names
mutate(file_contents = map(filename, # read files into a new data column
~readPrevalence(., pathname))) %>% # use the function written above to process files
mutate(Location=str_split(filename,'/',simplify = TRUE)[,1]) %>%
mutate(Run=str_split(filename,'/',simplify = TRUE)[,2]) %>%
select(-filename) %>%
unnest(cols=file_contents)
bind_cols(dataPrevW,dataPrevP) %>%
mutate(Prevalence=Prevalence+Prevalence1) %>%
select(Time,Prevalence,Location,Run)->dataPrev
dataPrev %>%
filter(Time<450) %>%
ggplot(aes(x=Time,colour=Run))+
geom_line(aes(y=Prevalence),alpha=0.2)+
theme_minimal_grid()+
# gghighlight(Location)+
# facet_grid(~Location)+
theme(legend.position = "none")+
xlab('Time (days)')
```
# Attack Rate
```{r}
library(kableExtra)
dataIR %>%
group_by(Run) %>%
summarise(AttackRate=max(Recovered)) %>%
mutate(AttackRate2=100*AttackRate/56082077) %>%
ggplot(aes(x=AttackRate2))+
geom_histogram(aes(y=..density..),bins=10)+
geom_density()+
xlab('Attack Rate (%)')+
ylab('Density')+
theme_minimal_grid()+
theme(axis.text.x = element_text(angle = 90))->p3
dataIR %>%
group_by(Run) %>%
summarise(AttackNumbers=max(Recovered)) %>%
mutate(Deaths=AttackNumbers/100) %>%
mutate(AttackRate=100*AttackNumbers/56082077) %>%
summary()
```
```{r}
library(patchwork)
patchwork<-p+(p2/p3)
patchwork + plot_annotation(tag_levels = 'A')
```