-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-compare-distributions.R
296 lines (257 loc) · 13.3 KB
/
plot-compare-distributions.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
rm(list = ls())
library(reshape)
setwd('~/Dropbox/R/2018_seasonal_flu/')
## Plot observed age distributions of infection using the AZ data set
## MAIN TEXT PLOTS:
## - Age dists from AZ, overall and panels with all seasons with cocirculation
## SUPPLEMENTARY TEXT PLOTS
## - Lower smoothing paramters
## - Higher smoothing paramters
## - All countries or all seasons
## Set smoothing paramter for smoothing splines
spar.in = .8
spar.low = .6
spar.high = 1
#### OUTPUTS
outfile6 = 'figures/age-dists-ARIZONA-all-seasons.tiff' ## Supplement
outfile7 = 'figures/age-dists-by-season-ARIZONA-ages0-90.tiff' ## Supplement
outfile8 = 'figures/age-dists-by-season-ARIZONA-ages18-90.tiff' ## Main text, Fig. 2
outfile9 = 'figures/age-dists-by-season-ARIZONA-hilowsmoothpar.tiff' ## Supplement
## Write a function to output transparent colors
## Credit to: http://www.dataanalytics.org.uk/Data%20Analysis/TipsAndTricks/TTR-20150531.htm
tns <- function(colname, percent = 70) {
rgb.val <- col2rgb(colname)
## Make new color using input color as base and alpha set by transparency
tnscol <- rgb(rgb.val[1], rgb.val[2], rgb.val[3], max = 255, alpha = (100-percent)*255/100)
## return
invisible(tnscol)
}
###################################
## AZ data
###################################
setwd('2017_AZ/')## Change directories to import AZ data
source('00-Inputs_multinomial.R')
setwd('../') ## Change back
## Rename seasons for plotting
raw.dat$season = gsub(pattern = '(\\d{4})(\\d{2})', replacement = '\\1-20\\2', x = raw.dat$season)
### Plot age distributions
agerange = 0:90
## Define a function to tabluate
age.tab = function(agevec){sapply(agerange, FUN = function(aa) sum(agevec == aa))}
## Supplementary figure
## Plot data from all seasons in which 10 or more cases of a given subtype were observed
## Plot splines only if 50 or more cases were observed
tb = (table(raw.dat$season, raw.dat$subtype)) # Get counts for each country, H1N1 = 1, H3N2 = 2
include = rownames(tb)[which(rowSums(tb)>0)]
include = include[-which(include %in% c('2008-2009', '2009-2010'))]
## Extract data from countries to be included
azplotdat = subset(raw.dat, season %in% include & age %in% agerange)
ssns = unique(azplotdat$season)
{
tiff(outfile6, width = 6.5, height = 10, units = 'in', res = 400)
## Set smoothing paramter
par(mfrow = c(5,3))
par(mar = c(2,1.5,2,1)+1.5)
labs = LETTERS[1:15]; names(labs) = ssns
for(ss in ssns){
valid1 = subset(azplotdat, season == ss & subtype == "H1")
valid2 = subset(azplotdat, season == ss & subtype == "H3")
## Determine y limits for each plot
if(nrow(valid1) > 10 & nrow(valid2)>10){
mxs = c(age.tab(valid1$age)/nrow(valid1), age.tab(valid2$age)/nrow(valid2))
}else if (nrow(valid1) > 10){
mxs = age.tab(valid1$age)/nrow(valid1)
}else if (nrow(valid2) > 10){
mxs = age.tab(valid2$age)/nrow(valid2)
}
mxs = mxs[which(is.finite(mxs))] # Extract finite proportions and then use the maximum to set the y limits
ymax = max(mxs)*1.1
## If less than 10 cases observd, plot white
c.in = ifelse(test = nrow(valid1)>10, tns('dodgerblue'), 'white')
plot(agerange, age.tab(valid1$age)/nrow(valid1), xlab = '', ylab = '', xlim = c(0, 90), ylim = c(0, ymax), main = '', col = c.in)
mtext(text = 'age', side = 1, line = 1.9, cex = .8)
mtext(text = 'fraction', side = 2, line = 1.9, cex = .8)
mtext(text = paste('AZ,', ss), side = 3, line = .25, font = 1, cex = .9)
mtext(labs[ss], side = 3, line = 1.2, at = -10, font = 2)
legend(20, par('usr')[4]*.9, legend = paste(c('H1N1: n = ', 'H3N2: n = '), c(nrow(valid1), nrow(valid2)), sep = ''), col = c('dodgerblue', 'firebrick'), pch = 15, bty = 'n')
if(nrow(valid1) >50){# If cases of this subtype were observed in the country of interest...
## Add smoothing spline
zz = smooth.spline(agerange, age.tab(valid1$age)/nrow(valid1), spar = spar.in)
lines(zz$x, zz$y, col = 'dodgerblue')
}
## Plot H3N2 age spline
c.in = ifelse(test = nrow(valid2)>10, tns('firebrick'), tns('white', percent = 100))
points(agerange, age.tab(valid2$age)/nrow(valid2), col = c.in)
if(nrow(valid2)>50){
zz = smooth.spline(agerange, age.tab(valid2$age)/nrow(valid2), spar = spar.in)
lines(zz$x, zz$y, col = 'firebrick')
}
}
dev.off()
}
## Alternate version of the main text figure, except
## Plot with all ages (0-90)
## Extract data from seasons wtih more than 50 observations of each subtype
{
tiff(outfile7, width = 6.5, height = 4.5, units = 'in', res = 400)
## Set smoothing paramter
#layout(matrix(c(1,8,2, 9,3,4, 5,6,7), byrow = T, nrow = 3))
par(mfrow = c(2,3),mar = c(1,3,2.5,0))
## Overall
valid1 = subset(azplotdat,subtype == "H1")
valid2 = subset(azplotdat,subtype == "H3")
plot(agerange, age.tab(valid1$age)/nrow(valid1), xlab = '', ylab = '', xlim = c(0, 90), ylim = c(0, .085), main = '', col = tns('dodgerblue'), xpd = NA)
mtext(text = 'age', side = 1, line = 1.9, cex = .8)
mtext(text = 'fraction', side = 2, line = 1.9, cex = .8)
mtext(text = 'Overall', side = 3, line = .25, font = 1)
mtext(labs[1], side = 3, line = 1.2, at = -10, font = 2)
#text(x = 75, y = .08, paste( 'H1N1, n = ', nrow(valid1), '\nH3N2, n = ', nrow(valid2)), cex = .9)
legend('topright', legend = paste(c('H1N1: n = ', 'H3N2: n = '), c(nrow(valid1), nrow(valid2)), sep = ''), col = c('dodgerblue', 'firebrick'), pch = 15, bty = 'n')
## add smoothed density
ss = smooth.spline(agerange, age.tab(valid1$age)/nrow(valid1), spar = spar.in)
lines(ss$x, ss$y, col = 'dodgerblue')
points(agerange, age.tab(valid2$age)/nrow(valid2), col = tns('firebrick'))
ss = smooth.spline(agerange, age.tab(valid2$age)/nrow(valid2), spar = spar.in)
lines(ss$x, ss$y, col = 'firebrick')
## Extract focal seasons
tb = (table(raw.dat$season, raw.dat$subtype)) # Get counts for each country, H1N1 = 1, H3N2 = 2
include = rownames(tb)[which(tb[,1]>=50 & tb[,2]>=50)]
include = include[-which(include %in% c('2008-2009', '2009-2010'))] # exclude pandemic seasons
## Extract data from countries to be included
azplotdat = subset(raw.dat, season %in% include & age %in% agerange)
ssns = unique(azplotdat$season)
labs = LETTERS[1:10]; names(labs) = c(NA, ssns)
par(mar = c(2,2,2,0)+1.5)
for(ss in ssns){
valid1 = subset(azplotdat, season == ss & subtype == "H1")
valid2 = subset(azplotdat, season == ss & subtype == "H3")
plot(agerange, age.tab(valid1$age)/nrow(valid1), xlab = '', ylab = '', xlim = c(0, 90), ylim = c(0, .085), main = '', col = tns('dodgerblue'))
mtext(text = 'age', side = 1, line = 1.9, cex = .8)
mtext(text = 'fraction', side = 2, line = 1.9, cex = .8)
mtext(text = paste('AZ,', ss), side = 3, line = .25, font = 1, cex = .9)
mtext(labs[ss], side = 3, line = 1.2, at = -10, font = 2)
legend('topright', legend = paste(c('H1N1: n = ', 'H3N2: n = '), c(nrow(valid1), nrow(valid2)), sep = ''), col = c('dodgerblue', 'firebrick'), pch = 15, bty = 'n')
if(nrow(valid1) >0){# If cases of this subtype were observed in the country of interest...
## Add smoothing spline
zz = smooth.spline(agerange, age.tab(valid1$age)/nrow(valid1), spar = spar.in)
lines(zz$x, zz$y, col = 'dodgerblue')
}
if(nrow(valid2)>0){
## Plot H3N2 age spline
points(agerange, age.tab(valid2$age)/nrow(valid2), col = tns('firebrick'))
zz = smooth.spline(agerange, age.tab(valid2$age)/nrow(valid2), spar = spar.in)
lines(zz$x, zz$y, col = 'firebrick')
}
}
dev.off()
}
########## repeat with a higher and lower smoothing paramter (sparlow = .6, sparhigh = 1)
{
tiff(outfile9, width = 6.5, height = 8.5, units = 'in', res = 400)
## Set smoothing paramter
par(mfrow = c(4,3))
## Overall
valid1 = subset(azplotdat,subtype == "H1")
valid2 = subset(azplotdat,subtype == "H3")
################ Plot season-specific observations with a low smoothing par
par(mar = c(2,1.5,2,1)+1.5)
labs = LETTERS[1:6]; names(labs) = ssns
for(ss in ssns){
valid1 = subset(azplotdat, season == ss & subtype == "H1")
valid2 = subset(azplotdat, season == ss & subtype == "H3")
plot(agerange, age.tab(valid1$age)/nrow(valid1), xlab = '', ylab = '', xlim = c(0, 90), ylim = c(0, .085), main = '', col = tns('dodgerblue'))
mtext(text = 'age', side = 1, line = 1.9, cex = .8)
mtext(text = 'fraction', side = 2, line = 1.9, cex = .8)
mtext(text = paste('AZ,', ss), side = 3, line = .25, font = 1, cex = .9)
mtext(labs[ss], side = 3, line = 1.2, at = -10, font = 2)
legend('topright', legend = paste(c('H1N1: n = ', 'H3N2: n = '), c(nrow(valid1), nrow(valid2)), sep = ''), col = c('dodgerblue', 'firebrick'), pch = 15, bty = 'n')
if(nrow(valid1) >0){# If cases of this subtype were observed in the country of interest...
## Add smoothing spline
zz = smooth.spline(agerange, age.tab(valid1$age)/nrow(valid1), spar = spar.low)
lines(zz$x, zz$y, col = 'dodgerblue')
}
if(nrow(valid2)>0){
## Plot H3N2 age spline
points(agerange, age.tab(valid2$age)/nrow(valid2), col = tns('firebrick'))
zz = smooth.spline(agerange, age.tab(valid2$age)/nrow(valid2), spar = spar.low)
lines(zz$x, zz$y, col = 'firebrick')
}
}
plot.new()
## Repeat with a high smoothpar value
labs = LETTERS[7:12]; names(labs) = ssns
for(ss in ssns){
valid1 = subset(azplotdat, season == ss & subtype == "H1")
valid2 = subset(azplotdat, season == ss & subtype == "H3")
plot(agerange, age.tab(valid1$age)/nrow(valid1), xlab = '', ylab = '', xlim = c(0, 90), ylim = c(0, .085), main = '', col = tns('dodgerblue'))
mtext(text = 'age', side = 1, line = 1.9, cex = .8)
mtext(text = 'fraction', side = 2, line = 1.9, cex = .8)
mtext(text = paste('AZ,', ss), side = 3, line = .25, font = 1, cex = .9)
mtext(labs[ss], side = 3, line = 1.2, at = -10, font = 2)
legend('topright', legend = paste(c('H1N1: n = ', 'H3N2: n = '), c(nrow(valid1), nrow(valid2)), sep = ''), col = c('dodgerblue', 'firebrick'), pch = 15, bty = 'n')
if(nrow(valid1) >0){# If cases of this subtype were observed in the country of interest...
## Add smoothing spline
zz = smooth.spline(agerange, age.tab(valid1$age)/nrow(valid1), spar = spar.high)
lines(zz$x, zz$y, col = 'dodgerblue')
}
if(nrow(valid2)>0){
## Plot H3N2 age spline
points(agerange, age.tab(valid2$age)/nrow(valid2), col = tns('firebrick'))
zz = smooth.spline(agerange, age.tab(valid2$age)/nrow(valid2), spar = spar.high)
lines(zz$x, zz$y, col = 'firebrick')
}
}
dev.off()
}
##### repeat wtih ages 18:90, which matches the INSIGHT data
agerange = 18:90
## Extract data from seasons wtih more than 50 observations of each subtype
tb = (table(raw.dat$season, raw.dat$subtype)) # Get counts for each country, H1N1 = 1, H3N2 = 2
include = rownames(tb)[which(tb[,1]>=50 & tb[,2]>=50)]
include = include[-2] # Exclude pandemic seasons
## Extract data from countries to be included
azplotdat = subset(raw.dat, season %in% include & age %in% agerange)
ssns = unique(azplotdat$season)
{
tiff(outfile8, width = 6.5, height = 6, units = 'in', res = 400)
## Set smoothing paramter
#layout(matrix(c(1,8,2, 9,3,4, 5,6,7), byrow = T, nrow = 3))
par(mfrow = c(2,3), mar = c(1.5,3,2.5,0))
## Overall
valid1 = subset(azplotdat,subtype == "H1")
valid2 = subset(azplotdat,subtype == "H3")
plot(agerange, age.tab(valid1$age)/nrow(valid1), xlab = '', ylab = '', xlim = c(18, 90), ylim = c(0, .085), main = '', col = tns('dodgerblue'), xpd = NA)
mtext(text = 'age', side = 1, line = 1.9, cex = .8)
mtext(text = 'fraction', side = 2, line = 1.9, cex = .8)
mtext(text = 'Overall', side = 3, line = .25, font = 2)
#text(x = 75, y = .08, paste( 'H1N1, n = ', nrow(valid1), '\nH3N2, n = ', nrow(valid2)), cex = .9)
legend('topright', legend = paste(c('H1N1: n = ', 'H3N2: n = '), c(nrow(valid1), nrow(valid2)), sep = ''), col = c('dodgerblue', 'firebrick'), pch = 15, bty = 'n')
## add smoothed density
ss = smooth.spline(agerange, age.tab(valid1$age)/nrow(valid1), spar = spar.in)
lines(ss$x, ss$y, col = 'dodgerblue')
points(agerange, age.tab(valid2$age)/nrow(valid2), col = tns('firebrick'))
ss = smooth.spline(agerange, age.tab(valid2$age)/nrow(valid2), spar = spar.in)
lines(ss$x, ss$y, col = 'firebrick')
par(mar = c(2,2,2,0)+1.5)
for(ss in ssns){
valid1 = subset(azplotdat, season == ss & subtype == "H1")
valid2 = subset(azplotdat, season == ss & subtype == "H3")
plot(agerange, age.tab(valid1$age)/nrow(valid1), xlab = '', ylab = '', xlim = c(18, 90), ylim = c(0, .085), main = '', col = tns('dodgerblue'))
mtext(text = 'age', side = 1, line = 1.9, cex = .8)
mtext(text = 'fraction', side = 2, line = 1.9, cex = .8)
mtext(text = paste(ss), side = 3, line = .25, font = 2, cex = .9)
legend('topright', legend = paste(c('H1N1: n = ', 'H3N2: n = '), c(nrow(valid1), nrow(valid2)), sep = ''), col = c('dodgerblue', 'firebrick'), pch = 15, bty = 'n')
if(nrow(valid1) >0){# If cases of this subtype were observed in the country of interest...
## Add smoothing spline
zz = smooth.spline(agerange, age.tab(valid1$age)/nrow(valid1), spar = spar.in)
lines(zz$x, zz$y, col = 'dodgerblue')
}
if(nrow(valid2)>0){
## Plot H3N2 age spline
points(agerange, age.tab(valid2$age)/nrow(valid2), col = tns('firebrick'))
zz = smooth.spline(agerange, age.tab(valid2$age)/nrow(valid2), spar = spar.in)
lines(zz$x, zz$y, col = 'firebrick')
}
}
dev.off()
}