-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
544 lines (467 loc) · 21.5 KB
/
functions.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# list of functions for plotting data
plot_effect_estimates = function(effect_ests, #list of stan outputs
plot_models, # indices of models to plot in list
my_pch=1,
mod_cols = NULL,
study_threshold){
if (length(my_pch)==1) my_pch = (1:length(plot_models))+15
if (length(my_pch)!=length(plot_models)) stop('length of my_pch needs to be the same as the number of input models')
if (is.null(mod_cols)){
mod_cols = brewer.pal(n = length(plot_models), name = 'Set1')
if(length(plot_models)>9) writeLines('too many models - supply user defined colors')
}
if(length(mod_cols)!=length(plot_models)) stop('number of colors needs to be equal to number of models to be plotted')
K_treatments = nrow(effect_ests[[plot_models[1]]])
xlims = (exp(range(c(0, range(sapply(effect_ests[plot_models], rbind)))) )-1)*100
x_points = pretty((xlims),6)
plot(NA, NA, xlim = range(x_points),
ylim = c(0.75,K_treatments+.25),
panel.first=grid(), ylab='', yaxt='n', type='n',
xlab = 'Change in rate of clearance (%)',
xaxt = 'n')
axis(1, at = x_points)
axis(2, at = 1:K_treatments,
labels = rownames(effect_ests[[plot_models[1]]]),
tick = F, cex.lab=1.5, cex.axis=1.5)
index_p = rev(seq(-.2,.2, length.out = length(plot_models)))
abline(v=0,lwd=2)
polygon(c(-1000, 100*(study_threshold-1), 100*(study_threshold-1), -1000),
c(-100, -100, 100, 100), border = NA,
col = adjustcolor('grey',.4))
for(i in 1:length(plot_models)){
points((exp(effect_ests[[plot_models[i]]][,'50%'])-1)*100,
1:K_treatments+index_p[i],pch=my_pch[i],
col=mod_cols[i],cex=1.5)
for(j in 1:K_treatments){
lines((exp(c(effect_ests[[plot_models[i]]][j,'2.5%'],
effect_ests[[plot_models[i]]][j,'97.5%']))-1)*100,
rep(j+index_p[i],2),col=mod_cols[i],lwd=1)
lines((exp(c(effect_ests[[plot_models[i]]][j,'10%'],
effect_ests[[plot_models[i]]][j,'90%']))-1)*100,
rep(j+index_p[i],2),col=mod_cols[i],lwd=3)
}
}
}
plot_baseline_data = function(input_data){
baseline_ind = input_data$Timepoint_ID==0
bvl = aggregate(log10_viral_load ~ ID, input_data[baseline_ind, ], median)
hist(bvl$log10_viral_load,
breaks = seq(1,8.5,by=.5),
xlab='Baseline viral load (RNA copies per mL)',
ylab ='Number of patients',xlim=c(1,8.5),
main='', xaxt ='n')
axis(1, at = c(2,4,6,8), labels = c(expression(10^2),
expression(10^4),
expression(10^6),
expression(10^8)))
grid();
hist(bvl$log10_viral_load,breaks = seq(1,8.5,by=.5),add=T)
}
plot_serial_data = function(xx, trt_cols){
xx$Trt_number = as.numeric(as.factor(as.character(xx$Trt)))
PCR_dat = aggregate(log10_viral_load ~ ID + Timepoint_ID +
Trt_number + Trt,
data = xx, mean)
trt_smmry = aggregate(formula = log10_viral_load ~ Timepoint_ID+Trt_number+Trt,
data = PCR_dat, FUN = median)
PCR_dat$Timepoint_ID=jitter(PCR_dat$Timepoint_ID)
gap.plot(PCR_dat$Timepoint_ID, PCR_dat$log10_viral_load,
ylab = 'RNA copies per mL', panel.first=grid(),
xlab = 'Time since randomization (days)',
gap = c(7.5,13.5), gap.axis = 'x',
yticlab = '',ytics = 2, xtics = c(0,3,6,14),
xlim = c(0,14), type='n', yaxt='n',
col = trt_cols[PCR_dat$Trt],
ylim = c(1, 8))
axis(2, at = c(2,4,6,8), labels = c(expression(10^2),
expression(10^4),
expression(10^6),
expression(10^8)))
IDs = unique(xx$ID)
writeLines(sprintf('Plotting data for %s individuals', length(IDs)))
for(id in IDs){
ind = PCR_dat$ID==id
gap.plot(PCR_dat$Timepoint_ID[ind],
PCR_dat$log10_viral_load[ind],
gap = c(7.5,13.5), gap.axis = 'x',add = T,
col=adjustcolor(trt_cols[PCR_dat$Trt[ind]],.5))
}
gap.plot(trt_smmry$Timepoint_ID, trt_smmry$log10_viral_load,
col= trt_cols[trt_smmry$Trt],
gap = c(7.5,13.5), gap.axis = 'x',add = T,
pch = 15, cex=1.5)
for(tt in unique(trt_smmry$Trt)){
ind = trt_smmry$Trt==tt
gap.plot(trt_smmry$Timepoint_ID[ind],
trt_smmry$log10_viral_load[ind],
gap = c(7.5,13.5), gap.axis = 'x',add = T,
type='l',col = trt_cols[tt],lwd=3)
}
trts = unique(PCR_dat$Trt)
for(i in 1:length(trts)){
trts[i] = paste0(trts[i],' (n=',sum(PCR_dat$Trt[!duplicated(PCR_dat$ID)]==trts[i]),')')
}
legend('topright', col=trt_cols[unique(PCR_dat$Trt)],
legend = trts,
lwd=2,pch=15,cex=1, inset = 0.03)
}
bayes_R2 = function(mod_preds, mod_residuals) {
var_pred = apply(mod_preds, 1, var)
var_res = apply(mod_residuals, 1, var)
var_pred / (var_pred + var_res)
}
make_stan_inputs = function(input_data_fit,
int_covs_base,
int_covs_full,
slope_covs_base,
slope_covs_full,
trt_frmla,
Dmax
){
## check censored values come last
if(!all(diff(input_data_fit$log10_viral_load == input_data_fit$log10_cens_vl)>=0)) stop('Data are not ordered correctly')
ind_dup = !duplicated(input_data_fit$ID)
input_data_fit$RnaseP_scaled = t(scale(40 - input_data_fit$CT_RNaseP,
scale = F))[1,]
input_data_fit$Age_scaled = (input_data_fit$Age-mean(input_data_fit$Age[ind_dup]))/sd(input_data_fit$Age[ind_dup])
# make the covariate matrix
# check no missing data
if(!all(!apply(input_data_fit[, union(int_covs_full,slope_covs_full)], 2, function(x) any(is.na(x))))){
stop('Missing data in covariate matrix!')
}
ind_contr = which(apply(input_data_fit[, int_covs_base,drop=F], 2, function(x) length(unique(x))>1))
if(length(ind_contr)>0){
X_intcpt_1 = model.matrix( ~ .,
data = input_data_fit[, int_covs_base[ind_contr],drop=F])[, -1, drop=F]
} else {
X_intcpt_1 = array(dim = c(nrow(input_data_fit),0))
}
ind_contr = which(apply(input_data_fit[, int_covs_full,drop=F], 2, function(x) length(unique(x))>1))
if(length(ind_contr)>0){
X_intcpt_2 = model.matrix( ~ .,
data = input_data_fit[, int_covs_full[ind_contr],drop=F])[, -1, drop=F]
} else {
X_intcpt_2 = array(dim = c(nrow(input_data_fit),0))
}
ind_contr = which(apply(input_data_fit[, slope_covs_base,drop=F], 2, function(x) length(unique(x))>1))
if(length(ind_contr)>0){
X_slope_1 = model.matrix( ~ .,
data = input_data_fit[, slope_covs_base[ind_contr],drop=F])[, -1, drop=F]
} else {
X_slope_1 = array(dim = c(nrow(input_data_fit),0))
}
ind_contr = which(apply(input_data_fit[, slope_covs_full,drop=F], 2, function(x) length(unique(x))>1))
if(length(ind_contr)>0){
X_slope_2 = model.matrix( ~ .,
data = input_data_fit[, slope_covs_full[ind_contr],drop=F])[, -1, drop=F]
} else {
X_slope_2 = array(dim = c(nrow(input_data_fit),0))
}
if(!nrow(X_intcpt_1) == nrow(input_data_fit)) stop()
if(!nrow(X_slope_1) == nrow(input_data_fit)) stop()
cov_matrices = list(X_int=list(X_intcpt_1, X_intcpt_2),
X_slope=list(X_slope_1, X_slope_2))
ID_map = data.frame(ID_key = input_data_fit$ID,
ID_stan = as.numeric(as.factor(input_data_fit$ID)))
writeLines(sprintf('There are a total of %s patients in the database with a total of %s PCRs analysable',
max(ID_map$ID_stan),
nrow(input_data_fit)))
ind_cens = !input_data_fit$log10_viral_load>
input_data_fit$log10_cens_vl
writeLines(sprintf('%s%% (%s out of %s) of samples are below LOD',
round(100*mean(ind_cens),digits = 2),
sum(ind_cens), length(ind_cens)))
analysis_data_stan = list(Ntot = nrow(input_data_fit),
N_obs = sum(!ind_cens),
n_id = max(ID_map$ID_stan),
id = ID_map$ID_stan,
ind_start = which(!duplicated(ID_map$ID_stan)),
obs_day = input_data_fit$Time,
log_10_vl = input_data_fit$log10_viral_load,
log10_cens_vl = input_data_fit$log10_cens_vl,
RNaseP = input_data_fit$RnaseP_scaled,
Time_max = Dmax)
ID_map = ID_map[!duplicated(ID_map$ID_key), ]
writeLines('check stan data formatting:')
all(analysis_data_stan$log_10_vl[1:analysis_data_stan$N_obs]>
analysis_data_stan$log10_cens_vl[1:analysis_data_stan$N_obs]) &
all(analysis_data_stan$log_10_vl[(1+analysis_data_stan$N_obs):analysis_data_stan$Ntot] ==
analysis_data_stan$log10_cens_vl[(1+analysis_data_stan$N_obs):analysis_data_stan$Ntot])
Trt_matrix = model.matrix(trt_frmla, data = input_data_fit)
Trt_matrix[,1]=0 # first column is dummy
analysis_inputs = list(cov_matrices=cov_matrices,
analysis_data_stan=analysis_data_stan,
Trt_matrix=Trt_matrix,
ID_map=ID_map)
return(analysis_inputs)
}
make_slopes_plot = function(stan_out,
analysis_data_stan,
ID_map,
data_summary,
trt_cols,
my_lims = c(5, 72), # hours
my_vals = c(7,24,48,72)){
slopes = extract(stan_out, pars='slope')$slope
thetas = extract(stan_out, pars='beta_0')
writeLines(sprintf('The model estimated population mean clearance half-life is %s (95%% CI %s-%s)',
round(mean(24*log10(0.5)/thetas$beta_0),1),
round(quantile(24*log10(0.5)/thetas$beta_0,.025),1),
round(quantile(24*log10(0.5)/thetas$beta_0,.975),1)))
t12_output = data.frame(t_12_med = 24*log10(.5)/(apply(slopes,2,mean)),
t_12_up = 24*log10(.5)/(apply(slopes,2,quantile,.9)),
t_12_low = 24*log10(.5)/(apply(slopes,2,quantile,.1)),
ID_stan = analysis_data_stan$id[analysis_data_stan$ind_start])
t12_output = merge(t12_output, ID_map, by = 'ID_stan')
data_summary = merge(data_summary, t12_output, by.x = 'ID', by.y = 'ID_key')
data_summary = dplyr::arrange(data_summary, Trt, t_12_med)
par(mar=c(5,2,2,2))
plot(data_summary$t_12_med, 1:nrow(data_summary),
col=trt_cols[data_summary$Trt], yaxt='n', xaxt='n',
xlim=my_lims, panel.first=grid(), xlab='',
pch=15, ylab='')
mtext(text = expression('t'[1/2] ~ ' (hours)'),side = 1,line=3)
axis(1, at = my_vals,labels = my_vals)
for(i in 1:nrow(data_summary)){
lines(c(data_summary$t_12_low[i],
data_summary$t_12_up[i]),
rep(i,2),col=trt_cols[data_summary$Trt[i]])
}
for(trt in unique(data_summary$Trt)){
ind = data_summary$Trt==trt
writeLines(sprintf('In %s the median clearance half life was %s (range %s to %s)',
trt,
round(median(data_summary$t_12_med[ind]),1),
round(min(data_summary$t_12_med[ind]),1),
round(max(data_summary$t_12_med[ind]),1)))
abline(v = median(data_summary$t_12_med[ind]), col=trt_cols[trt],lty=2,lwd=2)
}
legend('bottomright', legend = rev(unique(data_summary$Trt)),
col = trt_cols[rev(unique(data_summary$Trt))],
pch=15,lwd=2,inset=0.03)
return(data_summary)
}
make_baseline_table = function(input_data){
ind_dup = !duplicated(input_data$ID)
platcov_sm_dat = input_data[ind_dup, ]
xx_vl = aggregate(log10_viral_load ~ ID + Timepoint_ID + Trt_code, input_data, mean)
xx_vl = aggregate(log10_viral_load ~ Trt_code, xx_vl[xx_vl$Timepoint_ID==0, ],
function(x) {
paste(round(mean(x),1),' (',
round(min(x),1),'-',round(max(x),1),
')',sep='')})
xx_age = aggregate(Age ~ Trt_code, platcov_sm_dat,function(x) {
paste(round(median(x),1),' (',
round(min(x),1),'-',round(max(x),1),
')',sep='')})
xx_n = aggregate(Age ~ Trt_code, platcov_sm_dat, length)
xx_Nvac = aggregate(N_dose ~ Trt_code, platcov_sm_dat,function(x) {
paste(round(median(x),1),' (',
round(min(x),1),'-',round(max(x),1),
')',sep='')})
platcov_sm_dat$Vaccinated = as.numeric(platcov_sm_dat$Any_dose=='Yes')
xx_Any_vac = aggregate(Vaccinated ~ Trt_code, platcov_sm_dat,function(x) round(100*mean(x)))
xx_Antibody = aggregate(Antibody_test ~ Trt_code, platcov_sm_dat,function(x) round(100*mean(x)))
xx_sex = aggregate(Sex ~ Trt_code, platcov_sm_dat,function(x) round(100*mean(x)))
# Drugs allocated by Site
xx_site = merge(data.frame(Trt_code=levels(platcov_sm_dat$Trt_code)),
aggregate(Site ~ Trt_code, FUN = length,data = platcov_sm_dat,
subset = platcov_sm_dat$Site==unique(platcov_sm_dat$Site)[1]),
all.x=TRUE)
if(length(unique(platcov_sm_dat$Site)>1)){
for(ss in unique(platcov_sm_dat$Site)[-1]){
xx_site = cbind(xx_site,
merge(data.frame(Trt_code=levels(platcov_sm_dat$Trt_code)),
aggregate(Site ~ Trt_code, FUN = length,data = platcov_sm_dat,
subset = platcov_sm_dat$Site==ss),
all.x=TRUE)[,-1])
}
}
xx_site[is.na(xx_site)]=0
xx = merge(merge(merge(merge(merge(merge(
xx_n, xx_age,by = 'Trt_code'),
xx_vl,by = 'Trt_code'),
xx_Nvac,by = 'Trt_code'),
xx_Antibody,by = 'Trt_code'),
xx_sex, by = 'Trt_code'),
xx_site, by = 'Trt_code')
colnames(xx)=c('Arm','n','Age','Baseline viral load (log10)',
'Number of vaccine doses',
'Antibody+ (%)','Male (%)',
unique(platcov_sm_dat$Site))
return(xx)
}
plot_individ_data = function(mod_out, # model fits
models_plot, # which models to plot
K_plots,
mod_cols,
ID_map,
analysis_data_stan
){
# extract posterior parameters and outputs
thetas = list()
for(mm in 1:length(mod_out)){
thetas[[mm]] = rstan::extract(mod_out[[mm]])
}
counter = 1
ID_map$Trt = gsub(pattern = '\n',
replacement = '',
x = ID_map$Trt,fixed = T)
while(counter <= nrow(ID_map)){
# draw individual model fit with data
ind = analysis_data_stan$id==ID_map$ID_stan[counter]
plot(analysis_data_stan$obs_day[ind],
analysis_data_stan$log_10_vl[ind],
xlab='', ylab='',
xaxt='n', yaxt='n',
panel.first=grid(), xlim=c(0,7),
ylim = range(analysis_data_stan$log_10_vl))
if(counter %% sqrt(K_plots) == 1){
mtext(text = 'RNA copies per mL',side = 2,
line = 3,las = 3)
}
axis(1, at = c(0,3,7))
axis(2, at = c(2,4,6,8), labels = c(expression(10^2),
expression(10^4),
expression(10^6),
expression(10^8)))
if((counter%%K_plots) >= K_plots - sqrt(K_plots)){
mtext(text = 'Days',side = 1,line = 2)
}
for(mm in models_plot){
ix = order(analysis_data_stan$obs_day[ind])
my_xs = analysis_data_stan$obs_day[ind][ix]
polygon(x = c(my_xs, rev(my_xs)),
y = c(apply(thetas[[mm]]$preds[,ind],2,
quantile,probs=0.025)[ix],
rev(apply(thetas[[mm]]$preds[,ind],2,
quantile,probs=0.975)[ix])),
border = NA,
col = adjustcolor(mod_cols[mm],alpha.f = .3))
lines(my_xs,
colMeans(thetas[[mm]]$preds[,ind])[ix],
col = mod_cols[mm],lwd=2)
}
points(analysis_data_stan$obs_day[ind],
analysis_data_stan$log_10_vl[ind],pch=16)
mtext(text = paste0(ID_map$ID_key[counter],
'\n',
ID_map$Trt[counter]),
side = 3, line = -0.5, cex=0.8)
counter=counter+1
}
}
plot_coef_effects = function(stan_out, cov_mat, stan_inputs){
thetas = rstan::extract(stan_out)
alpha_coefs = apply(thetas$intercept_coefs,2,
quantile,probs=c(0.025,.1,.5,.9,0.975))
cov_names_intercept =
plyr::mapvalues(x = colnames(stan_inputs$cov_matrices$X_int[[cov_mat]]),
from = c('Age_scaled','Antibody_test',
'Symptom_onset','N_dose'),
to = c('Age','Serology RDT',
'Days since\nsymptom onset',
'Number of\nvaccine doses'))
cov_names_intercept = gsub(x = cov_names_intercept, pattern = 'Variant',replacement = 'Subvariant ', ignore.case = F)
cov_names_slope =
plyr::mapvalues(x = colnames(stan_inputs$cov_matrices$X_slope[[cov_mat]]),
from = c('Age_scaled','Antibody_test',
'Symptom_onset','N_dose'),
to = c('Age','Serology RDT',
'Days since\nsymptom onset',
'Number of\nvaccine doses'))
cov_names_slope = gsub(x = cov_names_slope, pattern = 'Variant',replacement = 'Subvariant ', ignore.case = F)
xlims=range(alpha_coefs)
x_points = axisTicks(xlims,log = T)
colnames(alpha_coefs) = cov_names_intercept
print(round(exp(alpha_coefs),2))
plot(alpha_coefs['50%', ], 1:ncol(alpha_coefs),
xlim=range(log(x_points)),yaxt='n',ylab='',bty='n',xaxt='n',
panel.first=grid(), xlab='Intercept (fold change)',pch=16,cex=1.5)
abline(v=0,lty=2,lwd=2)
for(i in 1:ncol(alpha_coefs)){
lines(c(alpha_coefs['10%',i], alpha_coefs['90%',i]),
c(i,i), lwd=3)
lines(c(alpha_coefs['2.5%',i], alpha_coefs['97.5%',i]),
c(i,i), lwd=1)
}
axis(2, at = 1:ncol(alpha_coefs), labels = cov_names_intercept,tick = F)
axis(1, at = log(x_points), labels = x_points)
beta_coefs = apply(thetas$slope_coefs,2,quantile,
probs=c(0.025,.1,.5,.9,0.975))
xlims=range(beta_coefs)
x_points = axisTicks(xlims,log = T)
colnames(beta_coefs) = cov_names_slope
print(round(exp(beta_coefs),2))
plot(beta_coefs['50%', ], 1:ncol(beta_coefs),
xlim=range(log(x_points)),yaxt='n',ylab='',bty='n',xaxt='n',
panel.first=grid(), xlab='Slope (multiplicative effect)',pch=16,cex=1.5)
abline(v=0,lty=2,lwd=2)
for(i in 1:ncol(beta_coefs)){
lines(c(beta_coefs['10%',i], beta_coefs['90%',i]),
c(i,i), lwd=3)
lines(c(beta_coefs['2.5%',i], beta_coefs['97.5%',i]),
c(i,i), lwd=1)
}
axis(2, at =1:ncol(beta_coefs), labels = cov_names_slope, tick = F)
axis(1, at = log(x_points), labels = x_points)
}
# Checks a function for use of global variables
# Returns TRUE if ok, FALSE if globals were found.
checkStrict <- function(f, silent=FALSE) {
vars <- codetools::findGlobals(f)
found <- !vapply(vars, exists, logical(1), envir=as.environment(2))
if (!silent && any(found)) {
warning("global variables used: ", paste(names(found)[found], collapse=', '))
return(invisible(FALSE))
}
!any(found)
}
calculate_fever_clearance = function(temp_dat,
window_clear = 24/24 # look ahead window to define "fever clearance"
){
if(!'temperature_ax' %in% colnames(temp_dat)) stop('needs to contain a temperature_ax column')
temp_dat$clearance_time = NA
# For interval censored data, the status indicator is 0=right censored, 1=event at time, 2=left censored, 3=interval censored.
temp_dat$clearance_time_cens = 1
temp_dat$fever_binary = temp_dat$temperature_ax>37
temp_dat = dplyr::arrange(temp_dat, ID, Time)
temp_dat = temp_dat[!is.na(temp_dat$temperature_ax), ]
for(id in unique(temp_dat$ID)){
ind = temp_dat$ID==id
if(all(!temp_dat$fever_binary[ind])){ # never fever
temp_dat$clearance_time[ind]=0
} else if(all(temp_dat$fever_binary[ind])){ # always fever
writeLines(sprintf('all fever for %s with %s FUP points',id,sum(ind)))
temp_dat$clearance_time[ind] = max(temp_dat$Time[ind])
temp_dat$clearance_time_cens[ind] = 0 #censored obs
} else { # fever cleared
j_cleared = which(ind & !temp_dat$fever_binary)
check_ahead=F
for(j in j_cleared){
if(!check_ahead){
ind_check =
which(ind &
temp_dat$Time>temp_dat$Time[j] &
temp_dat$Time<temp_dat$Time[j] + window_clear)
if(length(ind_check)>0 & all(!temp_dat$fever_binary[ind_check])){
temp_dat$clearance_time[ind]=temp_dat$Time[j]
check_ahead=T
}
}
}
if(!check_ahead){
temp_dat$clearance_time[ind]=tail(temp_dat$Time[ind],1)
temp_dat$clearance_time_cens[ind]=0
}
}
}
return(temp_dat)
}
checkStrict(make_stan_inputs)
checkStrict(plot_serial_data)
checkStrict(plot_effect_estimates)
checkStrict(plot_individ_data)
checkStrict(make_baseline_table)
checkStrict(plot_coef_effects)
checkStrict(calculate_fever_clearance)