-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.r
242 lines (196 loc) · 9.63 KB
/
process.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
## Copyright 2021 Neil Ferguson, Imperial College London
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
source("libs.r")
source("plots.r")
source("read_data.r")
library(gridExtra)
options(mc.cores=12)
options(max.print=10000)
### compare ORF1ab data data fits ("N" is a old tag for "New")
loo_res.N4pca=readRDS("f_ataN_4p1F_4group_voc_incvacc2_tsd4_corr1_age1_loo.rds")
loo_res.N1pc=readRDS("f_ataN_4p1F_1group_voc_incvacc2_tsd4_corr1_loo.rds")
loo_res.N4p=readRDS("f_ataN_4p1F_4group_voc_incvacc2_tsd4_corr0_loo.rds")
loo_res.N4pc=readRDS("f_ataN_4p1F_4group_voc_incvacc2_tsd4_corr1_loo.rds")
loo_res.N1t=readRDS("f_ataN_4t2F_1group_voc_incvacc2_tsd4_corr0_loo.rds")
loo_res.N1tc=readRDS("f_ataN_4t2F_1group_voc_incvacc2_tsd4_corr1_loo.rds")
loo_comp.N=loo_compare(loo_res.N4pca,loo_res.N4pc,loo_res.N1tc,loo_res.N1pc,loo_res.N4p,loo_res.N1t)
print(loo_comp.N, simplify = FALSE, digits = 3)
### compare E gene data fits
loo_res.E4pca=readRDS("f_ataE_4p1F_4group_voc_incvacc2_tsd4_corr1_age1_loo.rds")
loo_res.E1pc=readRDS("f_ataE_4p1F_1group_voc_incvacc2_tsd4_corr1_loo.rds")
loo_res.E4p=readRDS("f_ataE_4p1F_4group_voc_incvacc2_tsd4_corr0_loo.rds")
loo_res.E4pc=readRDS("f_ataE_4p1F_4group_voc_incvacc2_tsd4_corr1_loo.rds")
loo_res.E1t=readRDS("f_ataE_4t2F_1group_voc_incvacc2_tsd4_corr0_loo.rds")
loo_res.E1tc=readRDS("f_ataE_4t2F_1group_voc_incvacc2_tsd4_corr1_loo.rds")
loo_comp.E=loo_compare(loo_res.E4pca,loo_res.E4pc,loo_res.E1pc,loo_res.E4p,loo_res.E1tc,loo_res.E1t)
print(loo_comp.E, simplify = FALSE, digits = 3)
### outputs for Table S4
loo_res.N4pca
loo_res.N4pc
loo_res.N1tc
loo_res.N1pc
loo_res.N4p
loo_res.N1t
loo_res.E4pca
loo_res.E4pc
loo_res.E1pc
loo_res.E4p
loo_res.E1tc
loo_res.E1t
### load best fit models
fit.N4pca=readRDS("f_ataN_4p1F_4group_voc_incvacc2_tsd4_corr1_age1.rds")
fit.E4pca=readRDS("f_ataE_4p1F_4group_voc_incvacc2_tsd4_corr1_age1.rds")
fit.N4pc=readRDS("f_ataN_4p1F_4group_voc_incvacc2_tsd4_corr1_age1.rds")
fit.E4pc=readRDS("f_ataE_4p1F_4group_voc_incvacc2_tsd4_corr1_age1.rds")
### ORF1ab gene results
fit=fit.N4pca
# fit=fit.E4pca # for E-gene results
#extract posterior samples
posterior <- rstan::extract(fit, inc_warmup = FALSE, permuted = TRUE)
### looking at individual-level parameter estimates (not included in paper)
# assemble dataframe of individual level mean posterior estimates and 95% CrI
group_names=c("pre-Alpha","Alpha","Delta-unvacc","Delta-vacc")
PersonID=data_list$subjects$PersonID
voc=1+data_list$subjects$voc
age=data_list$subjects$age
# means
PeakVL=colMeans(posterior$p_ln_v_max)/log(10)
VLg=colMeans(posterior$p_v[,,2])/log(10)
VLd=colMeans(posterior$p_v[,,3])/log(10)
# CrI for Peak VL
tmp=as.data.frame(posterior$p_ln_v_max)/log(10)
PeakVL_r <- tmp %>%
summarise(across(.cols = everything(),~quantile(.x, probs = c(0.025,0.975))))
PeakVL_r=as.data.frame(t(PeakVL_r))
names(PeakVL_r)=c("PeakVL_0.025","PeakVL_0.975")
# CrI for growth rate
tmp=as.data.frame(posterior$p_v[,,2])/log(10)
VLg_r <- tmp %>%
summarise(across(.cols = everything(),~quantile(.x, probs = c(0.025,0.975))))
# CrI for decline rate
tmp=as.data.frame(posterior$p_v[,,3])/log(10)
VLg_r=as.data.frame(t(VLg_r))
names(VLg_r)=c("VLg_0.025","VLg_0.975")
VLd_r <- tmp %>%
summarise(across(.cols = everything(),~quantile(.x, probs = c(0.025,0.975))))
VLd_r=as.data.frame(t(VLd_r))
names(VLd_r)=c("VLd_0.025","VLd_0.975")
# put in single dataframe
results <- as.data.frame(cbind(PersonID,age,voc,PeakVL,PeakVL_r,VLg,VLg_r,VLd,VLd_r))
results$voc_group <- as.factor(group_names[results$voc])
results$voc_group <- factor(results$voc_group,levels=group_names)
# now calculate averages across VOC groups
results.sum=NULL
for(i in 1:4) {
PeakVL=mean(posterior$v_max_voc[,i])/log(10)
PeakVL_0.025=quantile(posterior$v_max_voc[,i],probs=0.025)/log(10)
PeakVL_0.975=quantile(posterior$v_max_voc[,i],probs=0.975)/log(10)
VLg=mean(posterior$v_a_voc[,i])/log(10)
VLg_0.025=quantile(posterior$v_a_voc[,i],probs=0.025)/log(10)
VLg_0.975=quantile(posterior$v_a_voc[,i],probs=0.975)/log(10)
VLd=mean(posterior$v_b_voc[,i])/log(10)
VLd_0.025=quantile(posterior$v_b_voc[,i],probs=0.025)/log(10)
VLd_0.975=quantile(posterior$v_b_voc[,i],probs=0.975)/log(10)
voc=i
tmp=as.data.frame(t(c(voc,PeakVL,PeakVL_0.025,PeakVL_0.975,VLg,VLg_0.025,VLg_0.975,VLd,VLd_0.025,VLd_0.975)))
names(tmp)=c("voc","PeakVL","PeakVL_0.025","PeakVL_0.975","VLg","VLg_0.025","VLg_0.975","VLd","VLd_0.025","VLd_0.975")
results.sum=rbind(results.sum,tmp)
}
results.sum$voc_group <- as.factor(group_names[results.sum$voc])
results.sum$voc_group <- factor(results.sum$voc_group,levels=group_names)
#plot dependence of peak VL on age
res.f=results[results$PeakVL>1.4,]
res.f$PeakVL=res.f$PeakVL-results.sum$PeakVL[res.f$voc]
res.f$PeakVL_0.025=res.f$PeakVL_0.025-results.sum$PeakVL[res.f$voc]
res.f$PeakVL_0.975=res.f$PeakVL_0.975-results.sum$PeakVL[res.f$voc]
res.f$log_age=log(res.f$age)
res.f$g="regression"
p <- ggplot(res.f, aes(x=log_age, y=PeakVL, ymin=PeakVL_0.025, ymax=PeakVL_0.975, colour=voc_group)) +
geom_point() +
# geom_smooth(data=res.f, method='lm', formula= y~x) +
geom_smooth(data=res.f, method='lm', formula= y~x, aes(x=log_age, y=PeakVL, colour=g)) +
# geom_pointrange(size = 0.3,position="jitter") +
xlab("Age") +ylab("log10(Peak VL)") + # coord_cartesian(ylim=c(6,9.5)) +
theme_classic() + theme(legend.position="right") +
theme(strip.background = element_blank())
p
ggsave(paste0("figAge.pdf"), p, width=8, height = 4.5)
cor.test(res.f$PeakVL,res.f$log_age,method = "pearson")
# nice plot of group level and individual level estimates
p1<-ggplot(results, aes(x=1, y=PeakVL, ymin=PeakVL_0.025, ymax=PeakVL_0.975, colour=voc_group)) +
facet_wrap(~voc_group) +
geom_boxplot(data=results.sum,stat = "identity",aes(lower=PeakVL_0.025,middle=PeakVL,upper=PeakVL_0.975)) +
geom_pointrange(size = 0.3, position = "jitter") +
xlab("") +ylab("log10(Peak VL)") + coord_cartesian(ylim=c(6,9.5)) +
theme_classic() + theme(legend.position="none") +
theme(strip.background = element_blank(), axis.line.x = element_blank()) +
theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())
p2<-ggplot(results, aes(x=1, y=VLg, ymin=VLg_0.025, ymax=VLg_0.975, colour=voc_group)) +
facet_wrap(~voc_group) +
geom_boxplot(data=results.sum,stat = "identity",aes(lower=VLg_0.025,middle=VLg,upper=VLg_0.975)) +
geom_pointrange(size = 0.3, position = "jitter") +
xlab("") +ylab("VL growth rate (log10 units/day)") + coord_cartesian(ylim=c(0,15)) +
theme_classic() + theme(legend.position="none") +
theme(strip.background = element_blank(), axis.line.x = element_blank()) +
theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())
p3<-ggplot(results, aes(x=1, y=VLd, ymin=VLd_0.025, ymax=VLd_0.975, colour=voc_group)) +
facet_wrap(~voc_group) +
geom_boxplot(data=results.sum,stat = "identity",aes(lower=VLd_0.025,middle=VLd,upper=VLd_0.975)) +
geom_pointrange(size = 0.3, position = "jitter") +
xlab("") +ylab("VL decline rate (log10 units/day)") + coord_cartesian(ylim=c(0,2.5)) +
theme_classic() + theme(legend.position="none") +
theme(strip.background = element_blank(), axis.line.x = element_blank()) +
theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())
pl1 = grid.arrange(p1,p2,p3,ncol=3)
pl1
ggsave(paste0("figNew.svg"), pl1, width=8, height = 4.5)
## calculate posterior probabilities of kinetic parameters differing between groups - Tables S9/S10
# .p variables are group level, .mp are within-sample averages
post.v_max.p=array(rep(0,16),dim=c(4,4))
post.v_a.p=array(rep(0,16),dim=c(4,4))
post.v_b.p=array(rep(0,16),dim=c(4,4))
post.v_max.mp=array(rep(0,16),dim=c(4,4))
post.v_a.mp=array(rep(0,16),dim=c(4,4))
post.v_b.mp=array(rep(0,16),dim=c(4,4))
for(i in 1:4)
for(j in 1:4) {
tmp=ifelse(posterior$v[,1,j] >= posterior$v[,1,i],1,0)
post.v_max.p[i,j]=sum(tmp)/length(tmp)
tmp=ifelse(posterior$v_mean[,2,j] >= posterior$v_mean[,2,i],1,0)
post.v_a.p[i,j]=sum(tmp)/length(tmp)
tmp=ifelse(posterior$v_mean[,3,j] >= posterior$v_mean[,3,i],1,0)
post.v_b.p[i,j]=sum(tmp)/length(tmp)
tmp=ifelse(posterior$v_max_voc[,j] >= posterior$v_max_voc[,i],1,0)
post.v_max.mp[i,j]=sum(tmp)/length(tmp)
tmp=ifelse(posterior$v_a_voc[,j] >= posterior$v_a_voc[,i],1,0)
post.v_a.mp[i,j]=sum(tmp)/length(tmp)
tmp=ifelse(posterior$v_b_voc[,j] >= posterior$v_b_voc[,i],1,0)
post.v_b.mp[i,j]=sum(tmp)/length(tmp)
tmp=ifelse(posterior$t_a_voc[,j] >= posterior$t_a_voc[,i],1,0)
}
post.v_max.p
post.v_a.p
post.v_b.p
post.v_max.mp
post.v_a.mp
post.v_b.mp
# posterior estimates for Tables S5/S6
# peak VL, growth rate and decline rate have been divided by ln(10) in Tables 3, S5 & S6 to put results on log10 VL scale
# within sample
print(fit, probs=c(0.025, 0.5, 0.975), pars = c("v_max_voc","v_a_voc","v_b_voc"), digits = 3)
# group level (note v[1,j] is peak VL on ln(VL) scale, v_mean[2,j] is growth rate on linear scale, v[3,j] is decline rate])
print(fit, probs=c(0.025, 0.5, 0.975), pars = c("v","v_mean"), digits = 3)
# estimates for tables S7/S8
print(fit, probs=c(0.025, 0.5, 0.975), pars = c("age_slope","v", "v_sd","Omega","v_s","fp","fp_ct_mean",
"fp_ct_sd"), digits = 3)