-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample-AllFigures.Rmd
443 lines (366 loc) · 20.5 KB
/
Example-AllFigures.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
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
---
title: "Competing Heterogeneities in Vaccine Efficacy Estimation: Create Example Figures"
author: "Ariel Nikas"
date: "2/27/2023"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## This code is intended to go along with the paper "Competing Heterogeneities in Vaccine Efficacy Estimation" and provides example code for the figures or, for compound figures, a single panel. Note that this reads in csv files generated from Julia which should also be in the code repository named by figure number (i.e. Figure1.jl makes Figure 1's panels) and due to the stochastic nature of the models may vary somewhat (though does not alter results) between computers and versions.
```{r}
#Load In Packages
library("survminer")
library("survival")
```
#--Generate Figure 1: Heterogeneity in Underlying Susceptibility ONLY (VE is at 50%)
```{r}
#--Here we: 1) reorganize the read in data, 2) create our bins and solve the Cox model, 3) solve for the TVI method (biggest time), 4) plot all panels
Scenario=read.csv("~/Desktop/ExampleFigure1-1.csv")
originalVE<-rep(50,365)
#Parameters
spread=1 #How many days is vaccination spread over?
mindays=14 #Minimum number of days in bin (the smaller this is the longer it takes to run)
minevents=100 #Minimum number of events in bin (<100 is generally a bad estimate)
D=365 #How many days in the simulation?
an=Scenario3=Scenario #re-name for reorganization
##---------------------------------------------First: Reorganize Data
an$vac_status2=an$original_infectors=NULL
x1=subset(an,vac_status==0)
x1$time1=1; x1$time2=x1$infected_time+0.5; x1$event=x1$ever_infected; x1$vac=0
if( length(which(an$vac_status==1&an$vac_time>1))>=1){
x2=subset(an,vac_status==1&vac_time>1)
x2$time1=1; x2$time2=x2$vac_time; x2$event=0; x2$vac=0
}
x3=subset(an,vac_status==1)
x3$time1=x3$vac_time; x3$time2=x3$infected_time+0.5; x3$event=x3$ever_infected; x3$vac=1
if(length(which(an$vac_status==1&an$vac_time>1))>=1){
x=rbind(x1,x2,x3)}else{x=rbind(x1,x3)}
x=x[order(x$id,x$time1),]
x$time3=1; x$time4=x$time2-x$time1+1
x$vac_status=x$ever_infected=x$infected_time=x$vac_time=NULL #x is dataset rearranged to work with coxph
##---------------------------------------------Second: Make Bins and Solve Cox Model
fit=coxph(Surv(time1,time2,event)~vac,x)
zph=cox.zph(fit,'identity')
ut=c(unique(zph$x),Inf); LL=ut[1]; i=1; j=length(ut); mind=mindays; minn=minevents
while(TRUE){
if(ut[j]-ut[i]<mind|sum(ut[i]<=zph$x)<minn){LL[length(LL)]=Inf; break}
for(newi in i:j) if(ut[newi]-ut[i]>=mind&sum(ut[i]<=zph$x&zph$x<ut[newi])>=minn) break
i=newi; LL=c(LL,ut[newi])
}
bins=cut(zph$x,LL,FALSE,right=FALSE); nbins=length(LL)-1
day<-tapply(zph$x,bins,mean); event.level3.SR<-(1-exp(tapply(zph$y,bins,mean)))*100
##---------------------------------------------Third: Solve For TVI Method
ru=residuals(fit,'schoenfeld') #unscaled schoenfeld residuals
tk=as.numeric(names(ru)); Bm=coef(fit); rh=exp(Bm); V=(ru>0)+0 #tk is infection times, V indicates whether infected was vaccinated (0=unvaccinated, 1=vaccinated)
Z=sapply(unique(rank(tk,ties.method='min')),function(i) optimize(function(Z) (V[i]-exp(Bm)*Z/(exp(Bm)*Z+1-Z) - ru[i])^2 ,c(0,1),tol=1e-15)$min )
Z=Z[as.numeric(factor(tk))]
y=survSplit(Surv(time1,time2,event)~.,x,cut=tapply(zph$x,bins,max)[-nbins],zero=1,episode='tcat')
vzm=sapply(1:nbins,function(i) coxph(Surv(time1,time2,event)~vac,subset(y,tcat==i))$coef[[1]] )
fit2=coxph(Surv(time1,time2,event)~vac:factor(tcat),y)
TVI<-exp(coef(fit2))#hazard ratio estimates using time vaccine interaction term
bins=as.numeric(factor(bins))
event.level3.TVI<-(1-TVI)*100 #actual mVE from this method
##---------------------------------------------Fourth: Plot
#For Panel A: Histogram of Underlying Chance to Get Infected
who.u1<-which(Scenario$vac_status==0)
dist.u1<-Scenario$RR[who.u1]
who.v1<-which(Scenario$vac_status==1)
dist.v1<-Scenario$RR[who.v1]#*0.5
pdf(file = "~/Desktop/ExampleFigure1A.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
hist(dist.u1, main="Initial Underlying Risk", col="steelblue", xlab="Chance to Get Infected", xlim=c(0, 0.04), ylim=c(0,50000), breaks =40)
hist(dist.v1, add=TRUE, col="purple", breaks=40)
legend("topright", legend=c("Vaccinated", "Unvaccinated"), col=c("purple", "steelblue"), lty=1, lwd=4, bty="n")
dev.off()
#For Panel B: Fraction of Individuals who are Still Susceptible in Each Group
infvax<-infunv<-rep(NA,365)
for(k in 1:364){
infvax[k]<-length(which(Scenario$infected_time>k & Scenario$vac_status==1))
infunv[k]<-length(which(Scenario$infected_time>k & Scenario$vac_status==0))
}
pdf(file = "~/Desktop/ExampleFigure1B.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
plot(seq(1:365), infunv/100000, type="l", col="steelblue", ylab="Fraction Susceptible", xlab="Day of Epidemic", cex.lab=1.4, cex.main=1.5, cex.axis=1.4, ylim=c(0.2,0.7), lwd=2)
lines(seq(1:365), infvax/100000, col="purple", lwd=2)
legend("topright", legend=c("Vaccinated", "Unvaccinated"), col=c("purple", "steelblue"), lty=1, bty="n", lwd=2)
dev.off()
#For Panel C: mVE compared to VE_NE
pdf(file = "~/Desktop/ExampleFigure1C.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
plot(seq(1:365), originalVE, lwd=2, col="black", main="Gamma(0.2,80)", ylab="VE %", xlab="Day of Epidemic", type="l", ylim=c(0,60))
lines(day, event.level3.TVI, col="forestgreen", lwd=2, type="b")
legend("topright", legend=c("VE", "mVE (TVI)"), col=c("black", "forestgreen"), lwd=2, lty=c(1,1,2,2,2),bty="n")
dev.off()
```
#--Generate Figure 2: Heterogeneity in Vaccine-Induced Protection (mean = 0.5) ONLY
```{r}
#--Here we: 1) reorganize the read in data, 2) create our bins and solve for the SR method, 3) solve for the TVI method (biggest time), 4) plot all panels
Scenario=read.csv("~/Desktop/ExampleFigure2-1.csv")
originalVE<-rep(50,365)
#Parameters
spread=1 #How many days is vaccination spread over?
mindays=14 #Minimum number of days in bin (the smaller this is the longer it takes to run)
minevents=100 #Minimum number of events in bin (<100 is generally a bad estimate)
D=365 #How many days in the simulation?
an=Scenario3=Scenario #re-name for reorganization
##---------------------------------------------First: Reorganize Data
an$vac_status2=an$original_infectors=NULL
x1=subset(an,vac_status==0)
x1$time1=1; x1$time2=x1$infected_time+0.5; x1$event=x1$ever_infected; x1$vac=0
if( length(which(an$vac_status==1&an$vac_time>1))>=1){
x2=subset(an,vac_status==1&vac_time>1)
x2$time1=1; x2$time2=x2$vac_time; x2$event=0; x2$vac=0
}
x3=subset(an,vac_status==1)
x3$time1=x3$vac_time; x3$time2=x3$infected_time+0.5; x3$event=x3$ever_infected; x3$vac=1
if(length(which(an$vac_status==1&an$vac_time>1))>=1){
x=rbind(x1,x2,x3)}else{x=rbind(x1,x3)}
x=x[order(x$id,x$time1),]
x$time3=1; x$time4=x$time2-x$time1+1
x$vac_status=x$ever_infected=x$infected_time=x$vac_time=NULL #x is dataset rearranged to work with coxph
##---------------------------------------------Second: Make Bins and Solve the Cox Model
fit=coxph(Surv(time1,time2,event)~vac,x)
zph=cox.zph(fit,'identity')
ut=c(unique(zph$x),Inf); LL=ut[1]; i=1; j=length(ut); mind=mindays; minn=minevents
while(TRUE){
if(ut[j]-ut[i]<mind|sum(ut[i]<=zph$x)<minn){LL[length(LL)]=Inf; break}
for(newi in i:j) if(ut[newi]-ut[i]>=mind&sum(ut[i]<=zph$x&zph$x<ut[newi])>=minn) break
i=newi; LL=c(LL,ut[newi])
}
bins=cut(zph$x,LL,FALSE,right=FALSE); nbins=length(LL)-1
day<-tapply(zph$x,bins,mean); event.level3.SR<-(1-exp(tapply(zph$y,bins,mean)))*100
##---------------------------------------------Third: Solve For TVI Method
ru=residuals(fit,'schoenfeld') #unscaled schoenfeld residuals
tk=as.numeric(names(ru)); Bm=coef(fit); rh=exp(Bm); V=(ru>0)+0 #tk is infection times, V indicates whether infected was vaccinated (0=unvaccinated, 1=vaccinated)
Z=sapply(unique(rank(tk,ties.method='min')),function(i) optimize(function(Z) (V[i]-exp(Bm)*Z/(exp(Bm)*Z+1-Z) - ru[i])^2 ,c(0,1),tol=1e-15)$min )
Z=Z[as.numeric(factor(tk))]
y=survSplit(Surv(time1,time2,event)~.,x,cut=tapply(zph$x,bins,max)[-nbins],zero=1,episode='tcat')
vzm=sapply(1:nbins,function(i) coxph(Surv(time1,time2,event)~vac,subset(y,tcat==i))$coef[[1]] )
fit2=coxph(Surv(time1,time2,event)~vac:factor(tcat),y)
TVI<-exp(coef(fit2))#hazard ratio estimates using time vaccine interaction term
bins=as.numeric(factor(bins))
event.level3.TVI<-(1-TVI)*100 #This is mVE
##---------------------------------------------Fourth: Plot
#For Panel A: Vaccine Protection Histogram
who.u1<-which(Scenario$vac_status==0)
dist.u1<-Scenario$RR[who.u1]
who.v1<-which(Scenario$vac_status==1)
dist.v1<-Scenario$RR[who.v1]#*0.5
pdf(file = "~/Desktop/ExampleFigure2A.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
hist(Scenario$RRVE, main="", col="purple", xlab="Vaccine Protection", ylim=c(0,15000))
dev.off()
#For Panel B: Fraction Susceptible in Each Group
infvax<-infunv<-rep(NA,365)
for(k in 1:364){
infvax[k]<-length(which(Scenario$infected_time>k & Scenario$vac_status==1))
infunv[k]<-length(which(Scenario$infected_time>k & Scenario$vac_status==0))
}
pdf(file = "~/Desktop/ExampleFigure2B.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
plot(seq(1:365), infunv/100000, type="l", col="steelblue", ylab="Fraction Susceptible", xlab="Day of Epidemic", cex.lab=1.4, cex.main=1.5, cex.axis=1.4, ylim=c(0.2,0.7), lwd=2)
lines(seq(1:365), infvax/100000, col="purple", lwd=2)
legend("topright", legend=c("Vaccinated", "Unvaccinated"), col=c("purple", "steelblue"), lty=1, bty="n", lwd=2)
dev.off()
#For Panel C: mVE vs. VE_NE (50%)
pdf(file = "~/Desktop/ExampleFigure2C.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
plot(seq(1:365), originalVE, lwd=2, col="black", main="Beta(2,2)", ylab="VE %", xlab="Day of Epidemic", type="l", ylim=c(40,100))
lines(day, event.level3.TVI, col="forestgreen", lwd=2, type="b")
legend("topright", legend=c("VE", "mVE (TVI)"), col=c("black", "forestgreen"), lwd=2, lty=c(1,1,2,2,2),bty="n")
dev.off()
```
#--Generate Figure 3 Panels: Heterogeneity in Susceptibility and Vaccine-Induced Protection + Predictor
```{r}
#--This makes an example (Panel C) from Figure 3
Scenario=read.csv("~/Desktop/ExampleFigure3-PanelC-1.csv")
originalVE<-rep(50,365)
##--Finding the Prediction Amount Over Time
predictor.1<-rep(NA,365)
for (k in 1:364){ #Due to right censoring on day 365, only predict on days 1-364
rvax<-which(Scenario$vac_status==1 &Scenario$infected_time>k)
runv<-which(Scenario$vac_status==0&Scenario$infected_time>k)
muU<-mean(Scenario$RR[runv]) #mean frailty in unvaccinated
muV<-mean(Scenario$RR[rvax]*(1-Scenario$RRVE[rvax])) #mean frailty in vaccinated (both protection and susceptibility)
varU<-var(Scenario$RR[runv]) #variance of frailty in unvaccinated
varV<-var(Scenario$RR[rvax]*(1-Scenario$RRVE[rvax])) #variance of frailty in vaccinated (both protection and susceptibility)
predictor.1[k]<-(-varV*muU+muV*varU)/muU^2}
#Parameters
spread=1 #How many days is vaccination spread over?
mindays=14 #Minimum number of days in bin (the smaller this is the longer it takes to run)
minevents=100 #Minimum number of events in bin (<100 is generally a bad estimate)
D=365 #How many days in the simulation?
an=Scenario3=Scenario #re-name for reorganization
##---------------------------------------------First: Reorganize Data
an$vac_status2=an$original_infectors=NULL
x1=subset(an,vac_status==0)
x1$time1=1; x1$time2=x1$infected_time+0.5; x1$event=x1$ever_infected; x1$vac=0
if( length(which(an$vac_status==1&an$vac_time>1))>=1){
x2=subset(an,vac_status==1&vac_time>1)
x2$time1=1; x2$time2=x2$vac_time; x2$event=0; x2$vac=0
}
x3=subset(an,vac_status==1)
x3$time1=x3$vac_time; x3$time2=x3$infected_time+0.5; x3$event=x3$ever_infected; x3$vac=1
if(length(which(an$vac_status==1&an$vac_time>1))>=1){
x=rbind(x1,x2,x3)}else{x=rbind(x1,x3)}
x=x[order(x$id,x$time1),]
x$time3=1; x$time4=x$time2-x$time1+1
x$vac_status=x$ever_infected=x$infected_time=x$vac_time=NULL #x is dataset rearranged to work with coxph
##---------------------------------------------Second: Make Bins and Solve the Cox Model
fit=coxph(Surv(time1,time2,event)~vac,x)
zph=cox.zph(fit,'identity')
ut=c(unique(zph$x),Inf); LL=ut[1]; i=1; j=length(ut); mind=mindays; minn=minevents
while(TRUE){
if(ut[j]-ut[i]<mind|sum(ut[i]<=zph$x)<minn){LL[length(LL)]=Inf; break}
for(newi in i:j) if(ut[newi]-ut[i]>=mind&sum(ut[i]<=zph$x&zph$x<ut[newi])>=minn) break
i=newi; LL=c(LL,ut[newi])
}
bins=cut(zph$x,LL,FALSE,right=FALSE); nbins=length(LL)-1
day<-tapply(zph$x,bins,mean); event.level3.SR<-(1-exp(tapply(zph$y,bins,mean)))*100
##---------------------------------------------Third: Solve For Level 3 TVI Method
ru=residuals(fit,'schoenfeld') #unscaled schoenfeld residuals
tk=as.numeric(names(ru)); Bm=coef(fit); rh=exp(Bm); V=(ru>0)+0 #tk is infection times, V indicates whether infected was vaccinated (0=unvaccinated, 1=vaccinated)
Z=sapply(unique(rank(tk,ties.method='min')),function(i) optimize(function(Z) (V[i]-exp(Bm)*Z/(exp(Bm)*Z+1-Z) - ru[i])^2 ,c(0,1),tol=1e-15)$min )
Z=Z[as.numeric(factor(tk))]
y=survSplit(Surv(time1,time2,event)~.,x,cut=tapply(zph$x,bins,max)[-nbins],zero=1,episode='tcat')
vzm=sapply(1:nbins,function(i) coxph(Surv(time1,time2,event)~vac,subset(y,tcat==i))$coef[[1]] )
fit2=coxph(Surv(time1,time2,event)~vac:factor(tcat),y)
TVI<-exp(coef(fit2))#hazard ratio estimates using time vaccine interaction term
bins=as.numeric(factor(bins))
event.level3.TVI<-(1-TVI)*100
##---------------------------------------------Fourth: Create Original and Predicted Lines
originalVE=rep( mean(Scenario$RRVE),365)*100
prediction=rep(NA,364)
for(i in 1:364){
prediction[i]<-(mean(Scenario$RRVE))-(sum(predictor.1[1:i]))
}
##---------------------------------------------Fifth: Plot
pdf(file = "~/Desktop/ExampleFigure3C.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
plot(seq(1:365), originalVE, lwd=2, col="black", main="Gamma(2,800), Beta(0.2,0.2)", ylab="VE %", xlab="Day of Epidemic", type="l", ylim=c(0,100))
lines(seq(1:364), prediction*100, lty=2, col="purple")
lines(day, event.level3.TVI, col="forestgreen", lwd=2, type="b")
legend("topright", legend=c("VE", "TVI", "Prediction"), col=c("black", "forestgreen", "purple"), lwd=2, lty=c(1,1,2,2,2),bty="n")
dev.off()
```
###---Generate Figure 4: Risk-Correlate Model
```{r}
#This generates an example Panel C from Figure 4. To create other panels, run the accompanying Julia file and change as directed.
#Read File
Scenario=read.csv("~/Desktop/Example-RiskCorrelate-1.csv") #Read In File, May Need to Change File Path
Scenario2=read.csv("~/Desktop/Example-RiskCorrelate-2.csv")
#Parameters
spread=1
mindays=14
minevents=100
D=365
an=Scenario3=Scenario
##---------------------------------------------First: Reorganize Data
an$vac_status2=an$original_infectors=NULL
x1=subset(an,vac_status==0)
x1$time1=1; x1$time2=x1$infected_time+0.5; x1$event=x1$ever_infected; x1$vac=0
if( length(which(an$vac_status==1&an$vac_time>1))>=1){
x2=subset(an,vac_status==1&vac_time>1)
x2$time1=1; x2$time2=x2$vac_time; x2$event=0; x2$vac=0
}
x3=subset(an,vac_status==1)
x3$time1=x3$vac_time; x3$time2=x3$infected_time+0.5; x3$event=x3$ever_infected; x3$vac=1
if(length(which(an$vac_status==1&an$vac_time>1))>=1){
x=rbind(x1,x2,x3)}else{x=rbind(x1,x3)}
x=x[order(x$id,x$time1),]
x$time3=1; x$time4=x$time2-x$time1+1
x$vac_status=x$ever_infected=x$infected_time=x$vac_time=NULL #x is dataset rearranged to work with coxph
##---------------------------------------------Second: Make Bins and Solve For the Level 3 SR Method
fit=coxph(Surv(time1,time2,event)~vac,x)
zph=cox.zph(fit,'identity')
ut=c(unique(zph$x),Inf); LL=ut[1]; i=1; j=length(ut); mind=mindays; minn=minevents
while(TRUE){
if(ut[j]-ut[i]<mind|sum(ut[i]<=zph$x)<minn){LL[length(LL)]=Inf; break}
for(newi in i:j) if(ut[newi]-ut[i]>=mind&sum(ut[i]<=zph$x&zph$x<ut[newi])>=minn) break
i=newi; LL=c(LL,ut[newi])
}
bins=cut(zph$x,LL,FALSE,right=FALSE); nbins=length(LL)-1
day<-tapply(zph$x,bins,mean); event.level3.SR<-(1-exp(tapply(zph$y,bins,mean)))*100
##---------------------------------------------Third: Solve For Level 3 TVI Method
ru=residuals(fit,'schoenfeld') #unscaled schoenfeld residuals
tk=as.numeric(names(ru)); Bm=coef(fit); rh=exp(Bm); V=(ru>0)+0 #tk is infection times, V indicates whether infected was vaccinated (0=unvaccinated, 1=vaccinated)
Z=sapply(unique(rank(tk,ties.method='min')),function(i) optimize(function(Z) (V[i]-exp(Bm)*Z/(exp(Bm)*Z+1-Z) - ru[i])^2 ,c(0,1),tol=1e-15)$min )
Z=Z[as.numeric(factor(tk))]
y=survSplit(Surv(time1,time2,event)~.,x,cut=tapply(zph$x,bins,max)[-nbins],zero=1,episode='tcat')
vzm=sapply(1:nbins,function(i) coxph(Surv(time1,time2,event)~vac,subset(y,tcat==i))$coef[[1]] )
fit2=coxph(Surv(time1,time2,event)~vac:factor(tcat),y)
TVI<-exp(coef(fit2))#hazard ratio estimates using time vaccine interaction term
bins=as.numeric(factor(bins))
event.level3.TVI<-(1-TVI)*100
##---------------------------------------------Fourth: Create Original Lines
originalVE=rep( mean(Scenario$RRVE),365)*100 #Just in caase original mean was slightly off 50%, e.g. 49.99469%
##---------------------------------------------Fifth: Plot
pdf(file = "~/Desktop/ExampleFigure4C.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
plot(seq(1:365),(1-Scenario2$DailyVE)*100, lwd=2, type="l", main="", ylab="VE %", xlab="Day of Epidemic", ylim=c(50,100), cex.lab=1.4, cex.main=1.5, cex.axis=1.4,)
lines(seq(1:365), rep(((1-Scenario2$DailyVE)*100)[365], 365), lty=2, col="black")
lines(seq(1:365), rep(mean(event.level3.TVI[22:26]), 365), lty=2, col="forestgreen")
lines(day, event.level3.TVI, col="forestgreen", lwd=2, type="b")
legend("topright", legend=c("VE", "mVE (TVI)"), col=c("black", "forestgreen"), lwd=2, lty=c(1,1,1,2,2),bty="n")
dev.off()
```
##--Generate Figure 5:
```{r}
Scenario=read.csv("~/Desktop/WithinHost-1.csv") #Read In File, May Need to Change File Path
Scenario2=read.csv("~/Desktop/WithinHost-2.csv")
#Parameters
spread=1
mindays=14
minevents=100
D=365
an=Scenario3=Scenario
##---------------------------------------------First: Reorganize Data
an$vac_status2=an$original_infectors=NULL
x1=subset(an,vac_status==0)
x1$time1=1; x1$time2=x1$infected_time+0.5; x1$event=x1$ever_infected; x1$vac=0
if( length(which(an$vac_status==1&an$vac_time>1))>=1){
x2=subset(an,vac_status==1&vac_time>1)
x2$time1=1; x2$time2=x2$vac_time; x2$event=0; x2$vac=0
}
x3=subset(an,vac_status==1)
x3$time1=x3$vac_time; x3$time2=x3$infected_time+0.5; x3$event=x3$ever_infected; x3$vac=1
if(length(which(an$vac_status==1&an$vac_time>1))>=1){
x=rbind(x1,x2,x3)}else{x=rbind(x1,x3)}
x=x[order(x$id,x$time1),]
x$time3=1; x$time4=x$time2-x$time1+1
x$vac_status=x$ever_infected=x$infected_time=x$vac_time=NULL #x is dataset rearranged to work with coxph
##---------------------------------------------Second: Make Bins and Solve For the Level 3 SR Method
fit=coxph(Surv(time1,time2,event)~vac,x)
zph=cox.zph(fit,'identity')
ut=c(unique(zph$x),Inf); LL=ut[1]; i=1; j=length(ut); mind=mindays; minn=minevents
while(TRUE){
if(ut[j]-ut[i]<mind|sum(ut[i]<=zph$x)<minn){LL[length(LL)]=Inf; break}
for(newi in i:j) if(ut[newi]-ut[i]>=mind&sum(ut[i]<=zph$x&zph$x<ut[newi])>=minn) break
i=newi; LL=c(LL,ut[newi])
}
bins=cut(zph$x,LL,FALSE,right=FALSE); nbins=length(LL)-1
day<-tapply(zph$x,bins,mean); event.level3.SR<-(1-exp(tapply(zph$y,bins,mean)))*100
##---------------------------------------------Third: Solve For Level 3 TVI Method
ru=residuals(fit,'schoenfeld') #unscaled schoenfeld residuals
tk=as.numeric(names(ru)); Bm=coef(fit); rh=exp(Bm); V=(ru>0)+0 #tk is infection times, V indicates whether infected was vaccinated (0=unvaccinated, 1=vaccinated)
Z=sapply(unique(rank(tk,ties.method='min')),function(i) optimize(function(Z) (V[i]-exp(Bm)*Z/(exp(Bm)*Z+1-Z) - ru[i])^2 ,c(0,1),tol=1e-15)$min )
Z=Z[as.numeric(factor(tk))]
y=survSplit(Surv(time1,time2,event)~.,x,cut=tapply(zph$x,bins,max)[-nbins],zero=1,episode='tcat')
vzm=sapply(1:nbins,function(i) coxph(Surv(time1,time2,event)~vac,subset(y,tcat==i))$coef[[1]] )
fit2=coxph(Surv(time1,time2,event)~vac:factor(tcat),y)
TVI<-exp(coef(fit2))#hazard ratio estimates using time vaccine interaction term
bins=as.numeric(factor(bins))
event.level3.TVI<-(1-TVI)*100
##---------------------------------------------Fourth: Create Original Lines
originalVE=rep( mean(Scenario$RRVE),365)*100
##---------------------------------------------Fifth: Plot
pdf(file = "~/Desktop/ExampleFigure5C.pdf", width = 6, height =4.75 )
par(mfrow=c(1,1))
plot(seq(1:365),(1-Scenario2$DailyVE)*100, lwd=2, type="l", main="", ylab="VE %", xlab="Day of Epidemic", ylim=c(0,100), cex.lab=1.4, cex.main=1.5, cex.axis=1.4,)
lines(seq(1:365), rep(((1-Scenario2$DailyVE)*100)[365], 365), lty=2, col="black")
lines(seq(1:365), rep(mean(event.level3.TVI[22:26]), 365), lty=2, col="forestgreen")
lines(day, event.level3.TVI, col="forestgreen", lwd=2, type="b")
legend("topright", legend=c("VE", "mVE (TVI)"), col=c("black", "forestgreen"), lwd=2, lty=c(1,1,1,2,2),bty="n")
dev.off()
```