-
Notifications
You must be signed in to change notification settings - Fork 0
/
PANCOVID.Rmd
1951 lines (1777 loc) · 89.9 KB
/
PANCOVID.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Pancovid"
author: "A.Amstutz"
date: "2024-02-17"
output:
html_document:
keep_md: yes
toc: yes
toc_float: yes
code_folding: hide
pdf_document:
toc: yes
---
# Load packages
```{r load packages, echo=TRUE, message=FALSE, warning=FALSE}
library(tidyverse)
library(readxl)
library(writexl)
library(tableone)
library(haven) # Read sas files
library(here)
library(kableExtra)
library(jtools) # for summ() and plot_summs
library(sjPlot) # for tab_model
library(ggplot2) # survival/TTE analyses and other graphs
library(ggsurvfit) # survival/TTE analyses
library(survival) # survival/TTE analyses
library(gtsummary) # survival/TTE analyses
library(ggfortify) # autoplot
library(tidycmprsk) # competing risk analysis
library(ordinal) # clinstatus ordinal regression
library(mosaic) # OR for 0.5-corrected 2x2 table in case of rare events
library(logistf) # Firth regression in case of rare events
library(finalfit) # missing data exploration
library(mice) # multiple imputation
library(jomo) # multiple imputation
library(mitools) # multiple imputation
```
# Load Data
```{r, include=FALSE}
df <- read_xlsx("/Users/amstutzal/Library/CloudStorage/OneDrive-usb.ch/Dokumente - JAKi IPDMA data source management/General/PANCOVID/JAKi_IPDMA_codebook_pancovid v4.xlsx", sheet = "data")
df_clinstatus <- read_xlsx("/Users/amstutzal/Library/CloudStorage/OneDrive-usb.ch/Dokumente - JAKi IPDMA data source management/General/PANCOVID/JAKi_IPDMA_codebook_pancovid v4.xlsx", sheet = "clinical status")
df_ae <- read_xlsx("/Users/amstutzal/Library/CloudStorage/OneDrive-usb.ch/Dokumente - JAKi IPDMA data source management/General/PANCOVID/JAKi_IPDMA_codebook_pancovid v4.xlsx", sheet = "Total AEs")
df_comorb <- read_xlsx("/Users/amstutzal/Library/CloudStorage/OneDrive-usb.ch/Dokumente - JAKi IPDMA data source management/General/PANCOVID/JAKi_IPDMA_codebook_pancovid v4.xlsx", sheet = "comorbidites")
```
# Define ITT set
```{r}
df <- df %>%
filter(!is.na(trt))
```
# Baseline Characteristics
```{r echo=TRUE}
## add trial indicators and basic characteristics
df$trial <- "PANCOVID"
df$JAKi <- "Baricitinib"
df$country <- "Spain"
df$icu <- NA # not available
df$ethn <- NA # not available
# addmargins(table(df$trt, useNA = "always")) # Corresponds to publication
# addmargins(table(df$sex, df$trt, useNA = "always"))
df <- df %>% # Corresponds to publication
mutate(sex = case_when(sex == "1" ~ "female",
sex == "2" ~ "male"))
## Age
# df %>% # Corresponds to publication
# filter(trt == 1) %>%
# select(age) %>%
# summary()
df %>%
ggplot(aes(x = age)) +
geom_density(fill = "blue", color = "black") +
labs(title = "Density Plot of Age",
x = "Age",
y = "Density")
## Symptom duration
# df %>% # Corresponds to publication
# filter(trt == 1) %>%
# select(sympdur) %>%
# summary()
# table(df$sympdur) # change -21 to +21
df <- df %>% mutate(sympdur = case_when(sympdur == -21 ~ 21,
TRUE ~ sympdur))
df %>%
drop_na(sympdur) %>%
ggplot(aes(x = sympdur)) +
geom_density(fill = "blue", color = "black") +
labs(title = "Density Plot of Symptom Duration",
x = "Symptom Duration",
y = "Density")
## Clinical status at baseline
df_clinstatus <- df_clinstatus %>%
rename(clinstatus = clinstatus_baseline)
df <- left_join(df, df_clinstatus[, c("clinstatus", "id_pat")], by = join_by(id_pat == id_pat))
# addmargins(table(df$clinstatus, df$clinstatus_baseline, useNA = "always")) # identical, good.
# addmargins(table(df$clinstatus_baseline, df$trt, useNA = "always")) # Corresponds to publication
df <- df %>%
mutate(clinstatus_baseline = case_when(clinstatus_baseline == 0 ~ 2,
clinstatus_baseline == 1 | clinstatus_baseline == 9 ~ 3,
clinstatus_baseline == 2 | clinstatus_baseline == 3 ~ 4))
df$clinstatus_baseline <- factor(df$clinstatus_baseline, levels = 1:6) ## no missing data
addmargins(table(df$clinstatus_baseline, df$trt, useNA = "always"))
df <- df %>%
mutate(vbaseline = case_when(clinstatus_baseline == "2" | clinstatus_baseline == "3" ~ 0,
clinstatus_baseline == "4" | clinstatus_baseline == "5" ~ 1))
df$randdate <- as_date(df$randdate) # randomisation date (baseline date)
## Comorbidities
df_comorb <- df_comorb %>%
rename(comorb_dm = "Diabetes",
comorb_aht = "Hypertension",
comorb_obese = "Obesity",
id_pat = "Label")
df <- left_join(df, df_comorb[, c("comorb_dm", "comorb_aht", "comorb_obese", "id_pat")], by = join_by(id_pat == id_pat))
df$immunosupp <- 0 # see protocol
df$comorb_autoimm <- 0 # see protocol
df <- df %>%
mutate(any_comorb = case_when(comorb_lung == 1 | comorb_liver == 1 | comorb_cvd == 1 |
comorb_aht == 1 | comorb_dm == 1 | comorb_obese == 1 | comorb_smoker == 1
| immunosupp == 1 | comorb_cancer == 1 | comorb_autoimm == 1 | comorb_kidney == 1
~ 1,
comorb_lung == 0 & comorb_liver == 0 & comorb_cvd == 0 &
comorb_aht == 0 & comorb_dm == 0 & comorb_obese == 0 & comorb_smoker == 0
& immunosupp == 0 & comorb_cancer == 0 & comorb_autoimm == 0 & comorb_kidney == 0
~ 0))
# addmargins(table(df$any_comorb, df$trt, useNA = "always")) # no missing
# group them for the subgroup analysis, according to protocol // count all pre-defined comorbidities per patient first
comorb <- df %>%
select(id_pat, comorb_lung, comorb_liver, comorb_cvd, comorb_aht, comorb_dm, comorb_obese, comorb_smoker, immunosupp, comorb_kidney, comorb_autoimm, comorb_cancer)
comorb$comorb_count <- NA
for (i in 1:dim(comorb)[[1]]) {
comorb$comorb_count[i] <- ifelse(
sum(comorb[i, ] %in% c(1)) > 0,
sum(comorb[i, ] %in% c(1)),
NA
)
}
comorb <- comorb %>%
mutate(comorb_count = case_when(comorb_lung == 0 & comorb_liver == 0 & comorb_cvd == 0 &
comorb_aht == 0 & comorb_dm == 0 & comorb_obese == 0 & comorb_smoker == 0
& immunosupp == 0 & comorb_cancer == 0 & comorb_autoimm == 0 & comorb_kidney == 0 ~ 0,
TRUE ~ comorb_count))
# addmargins(table(comorb$comorb_count, useNA = "always")) # no missing
df <- left_join(df, comorb[, c("comorb_count", "id_pat")], by = join_by(id_pat == id_pat)) ## merge imputed variable back
df <- df %>%
mutate(comorb_cat = case_when(immunosupp == 1 ~ 4, # immunocompromised
comorb_count == 0 ~ 1, # no comorbidity
comorb_count == 1 ~ 2, # one comorbidity
comorb_count >1 & (immunosupp == 0 | is.na(immunosupp)) ~ 3)) # multiple comorbidities
# table(df$comorb_cat, useNA = "always")
df <- df %>%
mutate(comorb_any = case_when(comorb_count == 0 ~ 0, # no comorbidity
comorb_count >0 ~ 1)) # any comorbidity
# addmargins(table(df$comorb_any, df$trt, useNA = "always")) # no missing
## COVID-19 comedication
df$comed_ab <- NA
df$comed_acoa <- NA
df$comed_interferon <- NA
df$comed_other <- df$comed_tdf # add tenofovir as "other covid-19 medication" to be consistent with other trials
# table(df$comed_dexa, df$trt) # was part of intervention and SOC
# table(df$comed_rdv, df$trt) # corresponds to publication
# table(df$comed_toci, df$trt) # CAVE: this is from the follow-up (part of disease progression)!! -> use toxi_2nd_random
df$comed_toci <- df$toxi_2nd_random
# table(df$comed_toci, df$trt, useNA = "always")
# GROUP them for the subgroup analysis, according to protocol
df <- df %>% # there are no missings in comed_dexa and comed_toci
mutate(comed_cat = case_when(comed_dexa == 0 & comed_toci == 0 ~ 1, # patients without Dexa nor Toci // NA: all have Dexa by design
comed_dexa == 1 & comed_toci == 0 ~ 2, # patients with Dexa but no Toci
comed_dexa == 1 & comed_toci == 1 ~ 3, # patients with Dexa and Toci
comed_dexa == 0 & comed_toci == 1 ~ 4)) # patients with Toci but no Dexa // NA: all have Dexa by design
# addmargins(table(df$comed_cat, df$trt, useNA = "always"))
## CRP at baseline
# df %>% # Corresponds more or less to publication
# filter(trt == 1) %>%
# select(crp) %>%
# summary()
# table(df$sympdur) # 11374 is probably an entry error -> NA
df <- df %>% mutate(crp = case_when(crp == 11374 ~ NA, TRUE ~ crp))
df %>%
drop_na(crp) %>%
ggplot(aes(x = crp)) +
geom_density(fill = "blue", color = "black") +
labs(title = "Density Plot of CRP",
x = "CRP",
y = "Density")
# addmargins(table(df$vacc, df$trt, useNA = "always"))
### Serology at baseline // not available
### Variant // not available
```
# Endpoints
```{r echo=TRUE}
## Clinical status follow-up data
df_clinstatus$Date_Progression <- as_date(df_clinstatus$Date_Progression)
df <- left_join(df, df_clinstatus[, c("Ventilatory_Support_Progression", "Date_Progression","Progression","Type_Ventilation", "id_pat")], by = join_by(id_pat == id_pat))
# "Type_Ventilation":
# 1 = Nasal goggles
# 2 = High-flow oxygen devices
# 3 = Mask with reservoir
# 4 = Non-invasive ventilation
# 5 = Invasive mechanical ventilation or ECMO
# 9 = Other
## CREATE the most important time to event variables (death, discharge, withdrawal) # no LTFU, no readmission
df <- df %>%
mutate(death_date = as_date(death_date)) %>%
mutate(discharge_date = as_date(discharge_date)) %>%
mutate(withdrawal_date = as_date(withdrawal_date)) %>%
mutate(withdrawal_invest_date = as_date(withdrawal_invest_date)) %>%
mutate(death_d = as.numeric(death_date - randdate)) %>%
rename(withdraw_date = withdrawal_date,
withdrawi_date = withdrawal_invest_date) %>%
mutate(withdraw_d = as.numeric(withdraw_date - randdate)) %>%
mutate(withdrawi_d = as.numeric(withdrawi_date - randdate)) %>%
mutate(discharge_d = as.numeric(discharge_date - randdate)) %>%
mutate(progression_d = as.numeric(Date_Progression - randdate)) %>%
mutate(death_reached = case_when(!is.na(death_date) ~ 1,
TRUE ~ 0)) %>% # death_reached over entire study period (mort_28 defined below)
mutate(discharge_reached = case_when(is.na(death_date) & is.na(withdraw_date) & is.na(withdrawi_date)
& discharge_d <= 28 ~ 1,
TRUE ~ 0)) # discharge_reached only within 28d by definition
# (i) Primary outcome: Mortality at day 28
df <- df %>% # 2 died after window 28 (TT: "Yes, but we took it into account for the main endpoint.")
mutate(mort_28 = case_when(death_reached == 1 & death_d <29 ~ 1, # this includes those discharged and then died (as long as within 28 days)
death_reached == 1 & death_d >28 ~ 0, # alive at day 28 but died later, i.e. info available
discharge_d >= 28 ~ 0, # discharged at day 28 or later, proof of still alive at day 28
withdraw_d >= 28 | withdrawi_d >= 28 ~ 0, # still in contact after day 28, proof to be alive
progression_d >= 28 ~ 0, # still in contact after day 28, proof to be alive
is.na(death_date) & is.na(withdraw_date) & is.na(withdrawi_date) & death_reached == 0 & discharge_d <29 ~ 0)) # all discharged were discharged alive and not to hospice
# df %>% # only left with the withdrawals (10 by participants, 1 by investigator)
# select(id_pat, trt, randdate, first_randdate, clinstatus_baseline, mort_28, death_d, death_date, death_reached, discharge_d, discharge_date, discharge_reached, withdraw_d, withdraw_date, withdrawi_d, withdrawi_date, Ventilatory_Support_Progression, progression_d, Date_Progression, Progression, Type_Ventilation) %>%
# filter(is.na(mort_28)) %>%
# View()
addmargins(table(df$mort_28, df$trt, useNA = "always"))
# First, keep mort_28 as complete case
# Second, use multiple imputation (see below)
# Third, apply a deterministic imputation (see notes): we use the same rules as ACTT2 => No transfer to hospice happened -> assign "alive"
df <- df %>%
mutate(mort_28_dimp = case_when(is.na(mort_28) ~ 0,
TRUE ~ c(mort_28)))
# addmargins(table(df$mort_28_dimp, df$trt, useNA = "always"))
# (ii) Mortality at day 60
df <- df %>%
mutate(mort_60 = case_when(death_reached == 1 & death_d <61 ~ 1,
death_reached == 1 & death_d >60 ~ 0,
discharge_d >= 60 ~ 0,
progression_d >= 60 ~ 0,
is.na(death_date) & is.na(withdraw_date) & is.na(withdrawi_date) & death_reached == 0 & discharge_d <61 ~ 0))
# df %>% # only left with the withdrawals (10 by participants, 1 by investigator; 2 more deaths than day 28)
# select(id_pat, trt, randdate, first_randdate, clinstatus_baseline, mort_28, mort_60, death_d, death_date, death_reached, discharge_d, discharge_date, discharge_reached, withdraw_d, withdraw_date, withdrawi_d, withdrawi_date, Ventilatory_Support_Progression, progression_d, Date_Progression, Progression, Type_Ventilation) %>%
# filter(is.na(mort_60)) %>%
# View()
addmargins(table(df$mort_60, df$trt, useNA = "always"))
# (iii) Time to death within max. follow-up time
df <- df %>% # first, death_d, then withdraw_d, then withdrawi_d, then discharge_d, then max follow-up
mutate(death_time = case_when(!is.na(death_d) ~ death_d,
!is.na(withdraw_d) ~ withdraw_d,
!is.na(withdrawi_d) ~ withdrawi_d,
!is.na(discharge_d) ~ discharge_d))
# (iv) New mechanical ventilation among survivors within 28 days. PANCOVID included across clinstatus 2-4.
df <- df %>%
mutate(new_mv_28 = case_when((clinstatus_baseline %in% c("2","3","4")) & (mort_28 == 0)
& Type_Ventilation == "Invasive Ventilation" ~ 1,
(clinstatus_baseline %in% c("2","3","4")) & (mort_28 == 0)
& Type_Ventilation %in% c("No ventilation", "Other", "Non-Invasive Ventilation") ~ 0))
addmargins(table(df$new_mv_28, df$trt, useNA = "always")) # NA: same 11 as mort_28 + 8 deaths
# (iv) Alternative definition/analysis: New mechanical ventilation OR death within 28 days => include all in denominator.
df <- df %>%
mutate(new_mvd_28 = case_when(new_mv_28 == 1 | mort_28 == 1 ~ 1,
new_mv_28 == 0 | mort_28 == 0 ~ 0))
addmargins(table(df$new_mvd_28, df$trt, useNA = "always")) # NA: same 11 as mort_28
# (v) Clinical status at day 28
df <- df %>% # merge into our score
mutate(clinstatus_fup = case_when(Ventilatory_Support_Progression == "1" ~ 3,
Ventilatory_Support_Progression %in% c("2", "3", "4") ~ 4,
Ventilatory_Support_Progression == "5" ~ 5,
Ventilatory_Support_Progression == "9" ~ 9))
df <- df %>%
mutate(clinstatus_28 = case_when(mort_28 == 1 ~ 6, # died within 28d
discharge_d <29 & !is.na(mort_28) ~ 1, # discharged alive / exclude withdrawals
!is.na(mort_28) & (progression_d == 28 | progression_d == 29 | progression_d == 30 | progression_d == 31)
& clinstatus_fup == 3 ~ 3, # exclude withdrawals (!is.na(mort_28)), progression score around day 28
!is.na(mort_28) & (progression_d == 28 | progression_d == 29 | progression_d == 30 | progression_d == 31) # exclude withdrawals
& clinstatus_fup == 4 ~ 4, # exclude withdrawals (!is.na(mort_28)), progression score around day 28
!is.na(mort_28) & (progression_d == 28 | progression_d == 29 | progression_d == 30 | progression_d == 31) # exclude withdrawals
& clinstatus_fup == 5 ~ 5, # exclude withdrawals (!is.na(mort_28)), progression score around day 28
!is.na(mort_28) & progression_d <28
& (discharge_d == 29 | discharge_d == 30 | discharge_d == 31 | discharge_d == 32 | discharge_d == 33)
& (is.na(clinstatus_fup) | clinstatus_fup == 3) ~ 2, # discharge around day 28, progression score before, reduce progression score
!is.na(mort_28) & progression_d <28
& (discharge_d == 29 | discharge_d == 30 | discharge_d == 31 | discharge_d == 32 | discharge_d == 33)
& clinstatus_fup == 4 ~ 3, # discharge around day 28, progression score before, reduce progression score
!is.na(mort_28) & progression_d <28
& (discharge_d == 29 | discharge_d == 30 | discharge_d == 31 | discharge_d == 32 | discharge_d == 33)
& clinstatus_fup == 5 ~ 4, # discharge around day 28, progression score before, reduce progression score
!is.na(mort_28) & is.na(clinstatus_fup)
& (discharge_d == 29 | discharge_d == 30 | discharge_d == 31 | discharge_d == 32 | discharge_d == 33)
~ 2, # discharge around day 28, left with no progression score
))
# df %>% # left with discharges beyond day 35 (and withdrawals)
# select(id_pat, trt, randdate, first_randdate, clinstatus_baseline, clinstatus_28, mort_28, mort_60, death_d, death_date, death_reached, discharge_d, discharge_date, discharge_reached, withdraw_d, withdraw_date, withdrawi_d, withdrawi_date, Ventilatory_Support_Progression, progression_d, Date_Progression, Progression, Type_Ventilation) %>%
# filter(is.na(clinstatus_28)) %>%
# View()
## Imputation according to protocol: If there was daily data for the ordinal score available but with missing data for single days, then we carried last observed value forward unless for day 28, whereby we first considered data from the window (+/-3 days)
df <- df %>%
mutate(clinstatus_28_imp = case_when(is.na(clinstatus_28) & !is.na(mort_28) & progression_d <29 & !is.na(clinstatus_fup) ~ clinstatus_fup,
TRUE ~ clinstatus_28)) # take last value from progression score during follow-up
df$clinstatus_baseline_n <- as.numeric(df$clinstatus_baseline)
df <- df %>%
mutate(clinstatus_28_imp = case_when(is.na(clinstatus_28_imp) & !is.na(mort_28) ~ clinstatus_baseline_n,
TRUE ~ clinstatus_28_imp)) # take from baseline score
df$clinstatus_28 <- factor(df$clinstatus_28, levels = 1:6)
df$clinstatus_28_imp <- factor(df$clinstatus_28_imp, levels = 1:6)
# table(df$clinstatus_28, useNA = "always")
# table(df$clinstatus_28_imp, useNA = "always")
# table(df$clinstatus_28_imp, df$discharge_reached, useNA = "always") # correct
# table(df$clinstatus_28_imp, df$mort_28, useNA = "always") # correct
# (vi) Time to discharge or reaching discharge criteria up to day 28
# table(df$discharge_reached, useNA = "always") # this is already within 28 days
df <- df %>% # first, discharge_d, then death_d, then withdraw_d, then max follow-up
mutate(discharge_time = case_when(!is.na(discharge_d) ~ discharge_d,
!is.na(death_d) ~ death_d,
!is.na(withdraw_d) ~ withdraw_d,
!is.na(withdrawi_d) ~ withdrawi_d))
# table(df$discharge_time, useNA = "always")
# table(df$discharge_reached, df$discharge_time, useNA = "always")
df <- df %>% # restrict to max fup time 28d
mutate(discharge_time = case_when(discharge_time >28 ~ 28,
TRUE ~ discharge_time))
df <- df %>% # add 28d for those that died - as a sens-variable
mutate(discharge_time_sens = case_when(mort_28 == 1 ~ 28,
TRUE ~ discharge_time))
# (vi) Sens-analysis: Alternative definition/analysis of outcome: time to sustained discharge within 28 days -> cannot be differentiated in dataset
df$discharge_reached_sus <- df$discharge_reached
df$discharge_time_sus <- df$discharge_time
# (vii) Viral clearance up to day 5, day 10, and day 15 (Viral load value <LOQ and/or undectectable) // not available in PANCOVID
# (viii) Quality of life at day 28 // not available in PANCOVID
# (ix) Participants with an adverse event grade 3 or 4, or a serious adverse event, excluding death, by day 28
# extract AE28
df_ae28 <- df_ae %>%
mutate(ae_date = as.Date(AE_STDTC, format = "%d/%m/%Y")) %>%
rename(id_pat = Label)
df_ae28 <- left_join(df_ae28, df[, c("id_pat", "randdate", "trt", "death_date", "death_d", "mort_28")], by = join_by(id_pat == id_pat))
df_ae28 <- df_ae28 %>%
filter(!is.na(trt)) %>%
mutate(ae_d = as.numeric(ae_date - randdate)) %>%
filter(ae_d < 29) %>%
filter(mort_28 == 0 | is.na(mort_28)) %>%
filter(AE_SEV == 1 | AE_SEV == 2 | AE_SER == 1) %>%
distinct(id_pat) %>%
mutate(ae34 = 1)
df <- left_join(df, df_ae28[, c("id_pat", "ae34")], by = join_by(id_pat == id_pat))
# the remaining missing have no (S)AE(SI) grade 34 -> recode as 0 (and exclude deaths, again)
df <- df %>%
mutate(ae_28 = case_when(is.na(ae34) ~ 0,
TRUE ~ ae34)) %>%
mutate(ae_28 = case_when(mort_28 == 1 ~ NA,
TRUE ~ ae_28))
# table(df$ae_28, df$mort_28, useNA = "always")
# addmargins(table(df$ae_28, df$trt, useNA = "always"))
# (ix) Sens-analysis: Alternative definition/analysis of outcome: incidence rate ratio (Poisson regression) -> AE per person by d28
df_ae_npp <- df_ae %>%
mutate(ae_date = as.Date(AE_STDTC, format = "%d/%m/%Y")) %>%
rename(id_pat = Label)
df_ae_npp <- left_join(df_ae_npp, df[, c("id_pat", "randdate", "trt", "death_date", "death_d", "mort_28")], by = join_by(id_pat == id_pat))
df_ae_npp <- df_ae_npp %>%
filter(!is.na(trt)) %>%
mutate(ae_d = as.numeric(ae_date - randdate)) %>%
filter(ae_d < 29) %>%
filter(mort_28 == 0 | is.na(mort_28)) %>%
filter(AE_SEV == 1 | AE_SEV == 2 | AE_SER == 1)
df_ae_npp <- df_ae_npp %>%
group_by(id_pat)%>%
summarise(ae28_sev = n())
df <- left_join(df, df_ae_npp[, c("ae28_sev", "id_pat")], by = join_by(id_pat == id_pat))
# the remaining missing have no (S)AE(SI) grade 34 -> recode as 0 (and exclude deaths, again)
df <- df %>%
mutate(ae_28_sev = case_when(is.na(ae28_sev) ~ 0,
TRUE ~ ae28_sev)) %>%
mutate(ae_28_sev = case_when(mort_28 == 1 ~ NA,
TRUE ~ ae_28_sev))
# addmargins(table(df$ae_28_sev, useNA = "always"))
# (ix) Sens-analysis: Alternative definition/analysis of outcome: time to first (of these) adverse event, within 28 days, considering death as a competing risk -> COP
# (x) Adverse events of special interest within 28 days: a) thromboembolic events (venous thromboembolism, pulmonary embolism, arterial thrombosis), b) secondary infections (bacterial pneumonia including ventilator-associated pneumonia, meningitis and encephalitis, endocarditis and bacteremia, invasive fungal infection including pulmonary aspergillosis), c) Reactivation of chronic infection including tuberculosis, herpes simplex, cytomegalovirus, herpes zoster and hepatitis B, d) serious cardiac events (excl. hypertension), e) events related to signs of bone marrow suppression (anemia, lymphocytopenia, thrombocytopenia, pancytopenia), f) malignancy, g) gastrointestinal perforation (incl. gastrointestinal bleeding/diverticulitis), h) liver dysfunction/hepatotoxicity (grade 3 and 4), i) Multiple organ dysfunction syndrome and septic shock
df_aesi_list <- df_ae %>%
mutate(ae_date = as.Date(AE_STDTC, format = "%d/%m/%Y")) %>%
rename(id_pat = Label)
df_aesi_list <- left_join(df_aesi_list, df[, c("id_pat", "randdate", "trt", "death_date", "death_d", "mort_28")], by = join_by(id_pat == id_pat))
df_aesi_list <- df_aesi_list %>%
filter(!is.na(trt)) %>%
mutate(ae_d = as.numeric(ae_date - randdate)) %>%
filter(ae_d < 29)
df_thrombo <- df_aesi_list %>% # a) thromboembolic events (venous thromboembolism, pulmonary embolism, arterial thrombosis)
filter(grepl("thrombos|embo|occl", AETerm, ignore.case = TRUE)) %>%
mutate(aesi = "thrombo")
df_sec_inf <- df_aesi_list %>% # b) secondary infections (bacterial pneumonia including ventilator-associated pneumonia, meningitis and encephalitis, endocarditis and bacteremia, invasive fungal infection including pulmonary aspergillosis), but not COVID-19 pneumonia!
filter(SOC_Name %in% c("Infections and infestations") & !grepl("shock|herpe|COVID-19|sinusitis|dendritic", AETerm, ignore.case = TRUE)) %>%
mutate(aesi = "sec_inf")
df_reactivate <- df_aesi_list %>% # c) Reactivation of chronic infection including tuberculosis, herpes simplex, cytomegalovirus, herpes zoster and hepatitis B
filter(grepl("hepatitis|zoster|herpe|cytome|tuber|tb|dendritic", AETerm, ignore.case = TRUE)) %>%
mutate(aesi = "reactivate")
df_cardiac <- df_aesi_list %>% # d) serious cardiovascular and cardiac events (including stroke and myocardial infarction) (excl. hypertension)
filter(SOC_Name %in% c("Cardiac disorders") | grepl("stroke|cerebrovascular|infarction|ischaemia|ischemia", AETerm, ignore.case = TRUE)) %>%
mutate(aesi = "cardiac")
df_penia <- df_aesi_list %>% # e) events related to signs of bone marrow suppression (anemia, lymphocytopenia, thrombocytopenia, pancytopenia)
filter(grepl("penia|anemia|anaemia", AETerm, ignore.case = TRUE)) %>%
mutate(aesi = "penia")
# df_malig <- df_aesi_list %>% # f) malignancy
# filter(SOC_Name %in% c("Neoplasms benign, malignant and unspecified (incl cysts and polyps)") | grepl("cancer|neopl|malig", AETerm, ignore.case = TRUE)) %>%
# mutate(aesi = "malig")
# df_git_bl <- df_aesi_list %>% # g) gastrointestinal perforation (incl. gastrointestinal bleeding/diverticulitis)
# filter(SOC_Name %in% c("Hepatobiliary disorders","Gastrointestinal disorders") & grepl("hemor|haemor|bleed", AETerm, ignore.case = TRUE)) %>%
# mutate(aesi = "git_bl")
df_hepatox <- df_aesi_list %>% # h) liver dysfunction/hepatotoxicity (grade 3 and 4)
filter(SOC_Name %in% c("Hepatobiliary disorders") & grepl("hepatox|liver injury|damage|failure|hypertrans|abnormal|hyperbili", AETerm, ignore.case = TRUE)) %>%
mutate(aesi = "hepatox")
df_mods <- df_aesi_list %>% # i) Multiple organ dysfunction syndrome and septic shock
filter(grepl("Multiple organ dysfunction syndrome|mods|shock", AETerm, ignore.case = TRUE)) %>%
mutate(aesi = "mods")
df_aesi <- rbind(df_mods, df_hepatox, df_penia, df_cardiac, df_reactivate, df_sec_inf, df_thrombo)
df_aesi <- df_aesi %>%
select(id_pat, trt, aesi, AETerm, SOC_Name)
# table(df_aesi$trt, df_aesi$aesi)
# double-check if there are any duplicate AEs within the same person and if it is the same event or distinct ones
df_aesi <- df_aesi %>%
group_by(id_pat) %>%
mutate(duplicate_id = duplicated(SOC_Name) & !is.na(SOC_Name)) %>%
ungroup()
# df_aesi <- df_aesi %>%
# filter(duplicate_id == F)
# Save
saveRDS(df_aesi, file = "df_aesi_pancovid.RData")
# (xi) Adverse events, any grade and serious adverse event, excluding death, within 28 days, grouped by organ classes
df_ae_list <- df_ae %>%
mutate(ae_date = as.Date(AE_STDTC, format = "%d/%m/%Y")) %>%
rename(id_pat = Label)
df_ae_list <- left_join(df_ae_list, df[, c("id_pat", "randdate", "trt", "death_date", "death_d", "mort_28")], by = join_by(id_pat == id_pat))
df_ae_list <- df_ae_list %>%
filter(!is.na(trt)) %>%
mutate(ae_d = as.numeric(ae_date - randdate)) %>%
filter(ae_d < 29)
df_ae_list <- df_ae_list %>%
group_by(id_pat) %>%
mutate(duplicate_id = duplicated(SOC_Name) & !is.na(SOC_Name)) %>%
ungroup()
# df_ae_list <- df_ae_list %>%
# filter(duplicate_id == F)
# Save
saveRDS(df_ae_list, file = "df_ae_pancovid.RData")
```
# Define final datasets
```{r echo=TRUE}
# keep the overall set
df_all <- df
# reduce the df set to our standardized set across all trials
df <- df_all %>%
select(id_pat, trt, sex, age, trial, JAKi,
# ethn,
country,
# icu,
sympdur,
vacc,
clinstatus_baseline, vbaseline,
comed_dexa,
comed_rdv,
comed_toci, comed_ab, comed_acoa, comed_interferon, comed_other,
comed_cat,
comorb_lung, comorb_liver, comorb_cvd, comorb_aht, comorb_dm, comorb_obese, comorb_smoker, immunosupp,
any_comorb, comorb_cat, comorb_any, comorb_count,
comorb_autoimm, comorb_cancer, comorb_kidney,
crp,
# sero,
# vl_baseline,
# variant,
mort_28, mort_28_dimp,
mort_60, death_reached, death_time,
new_mv_28, new_mvd_28,
clinstatus_28_imp,
discharge_reached, discharge_time, discharge_time_sens, discharge_reached_sus, discharge_time_sus,
ae_28, ae_28_sev,
# vir_clear_5, vir_clear_10, vir_clear_15
)
# export for one-stage model, i.e., add missing variables
df_os <- df
df_os$ethn <- NA
df_os$icu <- NA
df_os$sero <- NA
df_os$vl_baseline <- NA
df_os$variant <- NA
df_os$vir_clear_5 <- NA
df_os$vir_clear_10 <- NA
df_os$vir_clear_15 <- NA
# Save
saveRDS(df_os, file = "df_os_pancovid.RData")
```
# Missing data plot: One-stage dataset
```{r echo=TRUE}
# Bar plot, missing data, each data point, standardized one-stage dataset
original_order <- colnames(df_os)
missing_plot <- df_os %>%
summarise_all(~ mean(is.na(.))) %>%
gather() %>%
mutate(key = factor(key, levels = original_order)) %>%
ggplot(aes(x = key, y = value)) +
geom_bar(stat = "identity") +
labs(x = "Columns", y = "Proportion of Missing Values", title = "Missing Data - standardized one-stage dataset") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ylim(0, 1)
print(missing_plot)
```
Discussion points
1. Missing variables:
* Baseline:
- Variant, Serology, VL
- comed_ab, comed_acoa, comed_interferon
- ethn, icu
* Outcomes:
- qol_28
- vir_clear
2. Missing data:
- mort_28/60/new_mv/new_mvd
- crp
- ae_28/_sev: by design-> deaths
# Missing data: Explore for MI
```{r message=FALSE, warning=FALSE}
# keep the core df
df_core <- df_all %>%
select(id_pat, trt, sex, age, trial, JAKi,
# ethn,
country,
# icu,
sympdur,
vacc,
clinstatus_baseline, vbaseline,
comed_dexa, comed_rdv, comed_toci,
# comed_ab, comed_acoa, comed_interferon,
comed_other,
comed_cat,
comorb_lung, comorb_liver, comorb_cvd, comorb_aht, comorb_dm, comorb_obese, comorb_smoker, immunosupp,
comorb_autoimm, comorb_cancer, comorb_kidney,
any_comorb, comorb_cat, comorb_any, comorb_count,
crp,
# sero, variant,
# vl_baseline,
clinstatus_fup,
clinstatus_28_imp,
mort_28, mort_28_dimp, mort_60, death_reached, death_time,
new_mv_28, new_mvd_28,
discharge_reached, discharge_time, discharge_time_sens, discharge_reached_sus, discharge_time_sus,
# vir_clear_5, vir_clear_10, vir_clear_15,
ae_28, ae_28_sev
)
# str(df_core)
# Convert character variables to factors
char_vars <- c("id_pat", "sex", "trial", "JAKi", "country", "vacc", "clinstatus_baseline", "vbaseline",
"comed_dexa", "comed_rdv", "comed_toci", "comed_other", "comed_cat",
"comorb_lung", "comorb_liver", "comorb_cvd", "comorb_aht", "comorb_dm", "comorb_obese", "comorb_smoker", "immunosupp", "any_comorb", "comorb_cat", "comorb_any", "comorb_autoimm","comorb_cancer", "comorb_kidney",
"clinstatus_fup",
"clinstatus_28_imp", "mort_28", "mort_28_dimp", "mort_60", "death_reached", "new_mv_28", "new_mvd_28","discharge_reached", "discharge_reached_sus", "ae_28")
df_core <- df_core %>%
mutate(across(all_of(char_vars), factor))
# Bar plot, missing data, each data point, core dataset
original_order <- colnames(df_core)
missing_plot <- df_core %>%
summarise_all(~ mean(is.na(.))) %>%
gather() %>%
mutate(key = factor(key, levels = original_order)) %>%
ggplot(aes(x = key, y = value)) +
geom_bar(stat = "identity") +
labs(x = "Columns", y = "Proportion of Missing Values", title = "Missing Data - core dataset") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ylim(0, 1)
print(missing_plot)
# Bar plot, missing data, each data point, core dataset, by arm
df_core_int <- df_core %>%
filter(trt == 1)
original_order <- colnames(df_core_int)
missing_plot <- df_core_int %>% # Intervention arm
summarise_all(~ mean(is.na(.))) %>%
gather() %>%
mutate(key = factor(key, levels = original_order)) %>%
ggplot(aes(x = key, y = value)) +
geom_bar(stat = "identity") +
labs(x = "Columns", y = "Proportion of Missing Values", title = "Missing Data - core dataset, intervention") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ylim(0, 1)
print(missing_plot)
df_core_cont <- df_core %>%
filter(trt == 0)
original_order <- colnames(df_core_cont)
missing_plot <- df_core_cont %>% # Control arm
summarise_all(~ mean(is.na(.))) %>%
gather() %>%
mutate(key = factor(key, levels = original_order)) %>%
ggplot(aes(x = key, y = value)) +
geom_bar(stat = "identity") +
labs(x = "Columns", y = "Proportion of Missing Values", title = "Missing Data - core dataset, control") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ylim(0, 1)
print(missing_plot)
### Baseline table, by individuals with no missing data vs any missing data (or only in mort_28)
# df_core <- df_core %>% mutate(complete = ifelse(rowSums(is.na(.)) > 0, 0, 1));table(df_core$complete) # ANY missing
df_core$resp<-ifelse(is.na(df_core$mort_28), 0, 1);table(df_core$resp) # only mort_28 missing
# Assign variable list
vars.list <- c("resp", "age", "sympdur"
,"trt", "sex", "country", "vacc", "clinstatus_baseline", "vbaseline",
"comed_dexa", "comed_rdv", "comed_toci", "comed_other", "comed_cat",
"comorb_lung", "comorb_liver", "comorb_cvd", "comorb_aht", "comorb_dm", "comorb_obese", "comorb_smoker", "immunosupp", "any_comorb", "comorb_cat", "comorb_any", "comorb_count","comorb_autoimm","comorb_cancer", "comorb_kidney", "crp"
, "mort_28", "mort_28_dimp", "mort_60", "death_reached","death_time", "new_mv_28", "new_mvd_28","discharge_reached", "discharge_time", "discharge_reached_sus", "discharge_time_sus", "ae_28", "ae_28_sev")
# By completeness (only mort_28)
table_resp <- CreateTableOne(data = df_core, vars = vars.list[!vars.list %in% c("resp")], strata = "resp", includeNA = T, test = T, addOverall = TRUE)
# Print and display the table
capture.output(
table_resp <- print(
table_resp,
nonnormal = vars.list,
catDigits = 1,
SMD = TRUE,
showAllLevels = TRUE,
test = TRUE,
printToggle = FALSE,
missing = TRUE))
kable(table_resp, format = "markdown", table.attr = 'class="table"', caption = "By completeness (only mort_28)") %>%
kable_styling(bootstrap_options = "striped", full_width = FALSE)
### Define variables to be included in imputation set
df_imp <- df_core %>%
select("id_pat"
, "trt", "sex", "age"
# , "ethn" # no info
, "sympdur"
# , "country" # only Spain, no info
, "vacc"
# , "trial", "JAKi" # only 0
,"clinstatus_baseline"
# , "vbaseline" # derived
, "comed_rdv"
, "comed_toci"
# , "comed_interferon" # no info
#, "comed_cat", # derived
# , "comed_dexa" # all received dexa
# , "comed_ab", "comed_acoa" # no info
# , "comed_other" # does not make sense
# , "comorb_lung", "comorb_liver", "comorb_cvd", "comorb_aht", "comorb_dm", "comorb_obese",
# "comorb_smoker", "immunosupp", "comorb_autoimm", "comorb_cancer", "comorb_kidney", "any_comorb",
# "comorb_count",
# "comorb_any",
,"comorb_cat" # derived from above, contains most information, and needed as interaction term
,"crp"
# , "vl_baseline" # no info
# , "sero" , "variant" # no info
, "clinstatus_fup"
# , "clinstatus_28_imp" # imputed via LOVCF above
, "mort_28"
# , "mort_28_dimp" # imputed deterministically
# , "mort_60" # does not contain any additional information compared to death reached
, "death_reached", "death_time", "new_mv_28", "new_mvd_28", "discharge_reached", "discharge_time"
# , "discharge_reached_sus", "discharge_time_sus" # same as discharge, does not contain any addition information
, "ae_28", "ae_28_sev"
# , "vir_clear_5", "vir_clear_10", "vir_clear_15" # no info
)
# str(df_imp)
# First, table and visualize missing data in various ways
# df_imp %>%
# ff_glimpse() # from finalfit package
df_imp %>%
missing_plot() # from finalfit package
explanatory = c("age", "clinstatus_baseline", "sex", "vacc", "sympdur", "comorb_cat", "comed_rdv", "comed_toci", "crp", "ae_28")
dependent = "mort_28"
df_imp %>% # from finalfit package, missing plot
missing_pairs(dependent, explanatory, position = "fill", )
# Second, let's explore the missingness patterns
md.pattern(df_imp[,c("mort_28", "age", "clinstatus_baseline", "sex", "vacc", "sympdur", "comorb_cat", "comed_rdv", "comed_toci", "crp", "ae_28")], rotate.names = T)
# Third, let's explore if the variables from my substantive model plus auxiliary variables are associated with mort_28
mort28.aux <- glm(mort_28 ~ trt
+ age
+ clinstatus_baseline
+ sex
+ vacc
+ sympdur
+ comorb_cat
+ comed_toci
+ comed_rdv
+ crp
# + ae_28
,family="binomial"
,data=df_imp)
summary(mort28.aux)
# Fourth, let's explore if they are associated with missingness of mort_28:
df_imp %>%
missing_compare(dependent, explanatory) %>%
knitr::kable(row.names=FALSE, align = c("l", "l", "r", "r", "r"))
# Fifth, check age
summary(df_imp$age)
hist(df_imp$age, breaks=50) # looks fine
# Sixth, check sympdur
summary(df_imp$sympdur)
hist(df_imp$sympdur, breaks=50) # skewed -> transform
df_imp$sqsympdur=sqrt(df_imp$sympdur)
hist(df_imp$sqsympdur) # looks fine
# Seventh, check crp
summary(df_imp$crp)
hist(df_imp$crp, breaks=50) # outliers
df_imp <- df_imp %>% # truncate outliers > 500
mutate(crptrunc = case_when(crp > 500 ~ 500,
TRUE ~ crp))
hist(df_imp$crptrunc)
df_imp$sqcrptrunc=sqrt(df_imp$crptrunc)
hist(df_imp$sqcrptrunc) # looks fine
### Reshape to long format // not needed since we don't have daily clinical score
### We will impute separately by treatment arm, since we have to expect an effect modification between outcome x trt over time
df_imp_int <- df_imp %>%
filter(trt == 1)
df_imp_cont <- df_imp %>%
filter(trt == 0)
```
# Multiple imputation
```{r eval = FALSE}
#### INTERVENTION group
## jomo only accepts numeric or factors, check and adapt
# str(df_imp_int)
attach(df_imp_int)
Y<-data.frame(mort_28
, age
, sex
, vacc
, sqsympdur
, comorb_cat
, clinstatus_baseline
, sqcrptrunc
, clinstatus_fup
, comed_rdv
, comed_toci
)
nimp<-30 # set number of iterations
## run jomo
# dry run
imputed_int_mcmc<-jomo.MCMCchain(Y=Y, nburn=2)
# plot(c(1:2),imputed_int_mcmc$collectbeta[1,1,1:2],type="l")
# plot(c(1:2),imputed_int_mcmc$collectcovu[5,5,1:2],type="l")
set.seed(1569)
imputed_int <- jomo(Y=Y, nburn=1000, nbetween=1000, nimp=nimp)
# nburn<-1000
# imputed_int_mcmc<-jomo.MCMCchain(Y=Y, nburn=nburn)
# plot(c(1:nburn),imputed_int_mcmc$collectbeta[1,1,1:nburn],type="l")
# plot(c(1:nburn),imputed_int_mcmc$collectcovu[5,5,1:nburn],type="l")
# convert to jomo object, split imputations, and exclude original data (imputation "0")
imp.list_int <- imputationList(split(imputed_int, imputed_int$Imputation)[-1])
# checks
round(prop.table(table(imp.list_int[[1]]$`1`$mort_28, useNA = "always"))*100,1) # first imputed dataset
round(prop.table(table(imp.list_int[[1]]$`2`$mort_28, useNA = "always"))*100,1) # second imputed dataset
round(prop.table(table(df_imp_int$mort_28, useNA = "always"))*100,1) # original data
#### CONTROL group
## jomo only accepts numeric or factors, check and adapt
# str(df_imp_cont)
attach(df_imp_cont)
Y<-data.frame(mort_28
, age
, sex
, vacc
, sqsympdur
, comorb_cat
, clinstatus_baseline
, sqcrptrunc
# , clinstatus_fup
, comed_rdv
, comed_toci
)
nimp<-30 # set number of iterations
## run jomo
# dry run
imputed_cont_mcmc<-jomo.MCMCchain(Y=Y, nburn=2)
# plot(c(1:2),imputed_cont_mcmc$collectbeta[1,1,1:2],type="l")
# plot(c(1:2),imputed_cont_mcmc$collectcovu[5,5,1:2],type="l")
set.seed(1569)
imputed_cont <- jomo(Y=Y, nburn=1000, nbetween=1000, nimp=nimp)
# nburn<-1000
# imputed_cont_mcmc<-jomo.MCMCchain(Y=Y, nburn=nburn)
# plot(c(1:nburn),imputed_cont_mcmc$collectbeta[1,1,1:nburn],type="l")
# plot(c(1:nburn),imputed_cont_mcmc$collectcovu[5,5,1:nburn],type="l")
# convert to jomo object, split imputations, and exclude original data (imputation "0")
imp.list_cont <- imputationList(split(imputed_cont, imputed_cont$Imputation)[-1])
# checks
round(prop.table(table(imp.list_cont[[1]]$`1`$mort_28, useNA = "always"))*100,1) # first imputed dataset
round(prop.table(table(imp.list_cont[[1]]$`2`$mort_28, useNA = "always"))*100,1) # second imputed dataset
round(prop.table(table(df_imp_cont$mort_28, useNA = "always"))*100,1) # original data
#### Add trt back, change from long to wide format, and finally combine the two data frames
imputed_int$trt <- 1
imputed_int_s <- imputed_int %>% # remove imputation variables, not needed anymore
select(trt, age, sqsympdur, mort_28, sex, vacc, comorb_cat, comed_rdv, comed_toci, sqcrptrunc, clinstatus_baseline, Imputation)
imputed_cont$trt <- 0 # treatment variable
imputed_cont_s <- imputed_cont %>% # remove imputation variables, not needed anymore
select(trt, age, sqsympdur, mort_28, sex, vacc, comorb_cat, comed_rdv, comed_toci, sqcrptrunc, clinstatus_baseline, Imputation)
imputed_combined <- rbind(imputed_cont_s, imputed_int_s)
#### Convert combined df to jomo object, split imputations, and exclude original data (imputation "0")
imp.list <- imputationList(split(imputed_combined, imputed_combined$Imputation)[-1])
### Checks
round(prop.table(table(imp.list[[1]]$`1`$mort_28, imp.list[[1]]$`1`$trt, useNA = "always"),2)*100,1) # first imputed dataset
round(prop.table(table(imp.list[[1]]$`2`$mort_28, imp.list[[1]]$`2`$trt, useNA = "always"),2)*100,1) # second imputed dataset
round(prop.table(table(df_imp$mort_28, df_imp$trt, useNA = "always"),2)*100,1) # original data
summary(imp.list[[1]]$`2`$sqcrptrunc)
```
# (i) Primary endpoint: Mortality at day 28
```{r warning=FALSE}
addmargins(table(df$mort_28, df$trt, useNA = "always"))
df$clinstatus_baseline_n <- as.numeric(df$clinstatus_baseline)
# Complete case analysis, substantive model
mort.28 <- df %>%
glm(mort_28 ~ trt
+ age
+ clinstatus_baseline
, family = "binomial", data=.)
summ(mort.28, exp = T, confint = T, model.info = T, model.fit = F, digits = 2)
# Deterministic imputation
mort.28.dimp <- df %>%
glm(mort_28_dimp ~ trt
+ age + clinstatus_baseline
, family = "binomial", data=.)
summ(mort.28.dimp, exp = T, confint = T, model.info = T, model.fit = F, digits = 2)
# Multiple imputation analysis under MAR; use mitools package to fit imputed and combined data list and apply Rubin's rules
# mort.28.mi <- imp.list %>%
# with(glm(mort_28 ~ trt
# + age
# + clinstatus_baseline
# , family = binomial)) %>%
# pool() %>%
# summary(conf.int = T, exponentiate = T)
# mort.28.mi
```
# (i.i) Covariate adjustment for primary endpoint: Mortality at day 28
```{r warning=FALSE}
# unadjusted estimator for the (absolute) risk difference
mort.28.prop.test <- prop.test(x = with(df, table(trt, mort_28)))
# print(mort.28.prop.test)
# Estimate
-diff(mort.28.prop.test$estimate)
# Confidence Interval
mort.28.prop.test$conf.int
# P-Value
mort.28.prop.test$p.value
# Covariate-Adjusted Analysis
# Fit the `glm` object
# Same as Complete case analysis, substantive model // but don't use piping, otherwise problem in margins::margins
df_mort28_comp <- df %>% filter(!is.na(mort_28))
mort.28.cov.adj <-
glm(formula = mort_28 ~ trt + age + clinstatus_baseline,
data = df_mort28_comp,
family = binomial(link = "logit")
)
# Print a summary of the `glm` object
summary(mort.28.cov.adj)
# Predict Pr{Y = 1 | Z = 1, X} // equals: E(Y|Z=1,X)
pr_y1_z1 <-
predict(
object = mort.28.cov.adj,
newdata =
df_mort28_comp %>%
dplyr::mutate(
trt = 1
),
type = "response"
)
# Predict Pr{Y = 1 | Z = 0, X} // equals: E(Y|Z=0,X)
pr_y1_z0 <-
predict(
object = mort.28.cov.adj,
newdata =
df_mort28_comp %>%
dplyr::mutate(
trt = 0
),
type = "response"
)
# Estimate RD
adj_mean = mean(pr_y1_z1) - mean(pr_y1_z0)
print(adj_mean)
# Standard Error RD
# The variance/standard error can be calculted as 1/n times the sample variance of:
# Z/P(Z=1)*[Y-E(Y|Z=1,X)] + E(Y|Z=1,X) - ((1-Z)/(1-P(1=Z))*[Y-E(Y|Z=0,X)] + E(Y|Z=0,X))
p_arm = mean(df_mort28_comp$trt==1)
adj_se = sqrt(
var((df_mort28_comp$trt==1)/p_arm * (df_mort28_comp$mort_28 - pr_y1_z1) + pr_y1_z1 -
((df_mort28_comp$trt==0)/(1-p_arm) * (df_mort28_comp$mort_28-pr_y1_z0) + pr_y1_z0))/
nrow(df_mort28_comp))
print(adj_se)
# Confidence Interval
c(adj_mean-qnorm(0.975)*adj_se, adj_mean+qnorm(0.975)*adj_se)
# Or, we can obtain the standard error of the estimate two ways. The first way is using the margins::margins() command, using the robust standard errors from sandwich::vcovHC // The second way to obtain these would be the bias corrected and accelerated (BCa) non-parametric bootstrap
# You’ll see that we now have a standard error, p-value under the hypothesis that the marginal effect is 0, and a 95% Confidence Interval for the estimate.
library(sandwich)
library(margins)
mort.28.cov.adj.ame <-
margins::margins(
model = mort.28.cov.adj,
# Specify treatment variable
variables = "trt",
# Convert to outcome scale, not link scale
type = "response",
# Obtain robust standard errors
vcov = sandwich::vcovHC(x = mort.28.cov.adj, type = "HC3")
)
summary(object = mort.28.cov.adj.ame, level = 0.95)
mort.28.ame <- summary(object = mort.28.cov.adj.ame, level = 0.95)
```
# (ii) Mortality at day 60
```{r warning=FALSE}
table(df$mort_60, df$trt, useNA = "always")
mort.60 <- df %>%