-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTNBC_v1.Rmd
2023 lines (1725 loc) · 94.4 KB
/
TNBC_v1.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: "Triple Negative Breast Cancer Data Analysis - Original Version"
author: "Anni Liu"
date: '`r format(Sys.Date(), "%B %d, %Y")`'
knit: knitautomator::knit_filename
output:
word_document:
fig_caption: no
fig_height: 6
fig_width: 6
highlight: null
toc: yes
reference_docx: manuscript_style_V0.docx
params:
date: !r format(Sys.Date(), "%Y%b%d")
plot.fig: TRUE
results.folder: FALSE
editor_options:
chunk_output_type: console
---
```{r shorcut, include=FALSE}
#################################################################
## RStudio keyboard shortcut ##
#################################################################
# Cursor at the beginning of a command line: Ctrl + A
# Cursor at the end of a command line: Ctrl + E
# Clear all the code from your console: Ctrl + L
# Create an assignment operator <-: Alt + - (Windows) or Option + - (Mac).
# Create a pipe operator %>%: Ctrl + Shift + M (Windows) or Cmd + Shift + M (Mac)
# Knit a document (knitr): Ctrl + Shift + K (Windows) or Cmd + Shift + K (Mac)
# Comment or uncomment current selection: Ctrl + Shift + C (Windows) or Cmd + Shift + C (Mac)
```
```{r attach_libraries, include=FALSE}
##################################################################
## Attach libraries ##
##################################################################
easypackages::libraries("BTKR", "multcomp", "readxl", "tidyverse",
"bannerCommenter", "parallel",
"tidycmprsk", "ggsurvfit")
```
```{r check_data, include=FALSE}
##################################################################
## Load data for checking ##
##################################################################
dat0 <- dget("../data/derived/2022Dec21_dat0.Rdata")
dat1 <- dget("../data/derived/2022Dec21_dat1.Rdata")
dat2 <- dget("../data/derived/2022Dec21_dat2.Rdata")
dat3 <- dget("../data/derived/2022Dec21_dat3.Rdata")
dat.work <- dget("../data/derived/2022Dec21_dat_TNBC.Rdata")
```
```{r analysis_plan_previous, include=FALSE}
#################################################################
## Previous analysis plan ##
#################################################################
## Variables to add:
## T2LR: time from Date.of.Diagnosis to date of local recurrence
## T2DR: time from Date.of.Diagnosis to date of distant recurrence
## T2NP: time from Date.of.Diagnosis to date of new primary
## T2Death: time from Date.of.Diagnosis to date of death
## T2LFU: time from Date.of.Diagnosis to date of last follow up
## LRFS.event: 1 if Local.Recurrence is Yes, 0 if No
## LRFS.time (local recurrence free survival time): T2LocRecur if had local recurrence,
## minimum of T2Death and T2LFU if no local recurrence
## DRFS.event: 1 if Distant.Recurrence is Yes, 0 if No
## DRFS.time (Distant recurrence free survival time): T2DisRecur if had distant recurrence,
## minimum of T2Death and T2LFU if no distant recurrence
## NPFS.event: 1 if New.Breast.Primary is Yes, 0 if No
## NPFS.time (New primary free survival time): T2NewPrim if had new Primary,
## minimum of T2Death and T2LFU if no new primary
## LR5y (5-year local recurrence): Assign all to 0 first,
## change to 1 if had local recurrence and local recurrence occurred within 5 years of disease diagnosis,
## change to NA if no recurrence and time to last follow up since diagnosis less than 5 years
## DR5y (5-year distant recurrence): defined in similar fashion as above
## NP5y (5-year new primary): defined in similar fashion as above
## Use the cumulative incidence function to estimate the incidence rate of local recurrence which treats death as the competing risk
### Add the following variables (lines starting from XXX):
### Death: "Yes" if Date.Death has the value
### "No" if Date.Death has NA
### LRDeath: NA if subjects miss information on the outcome *
### "LocRecur" if Local.Recurrence is Yes and Death is Yes or No
### "Dead" if Death is Yes and Local.Recurrence is No
### "Censored" if Local.Recurrence and Death are both No
### table(paste(dat3$Local.Recurrence,
### ifelse(is.na(dat3$T2LR), "noT2LR", "T2LR"),
### ifelse(is.na(dat3$T2Death), "noT2D", "T2D"),
### ifelse(is.na(dat3$T2LFU), "noT2LFU", "T2LFU"),
### sep = ":"))
### * NA:noT2LR:noT2D:noT2LFU 6
### NA:noT2LR:noT2D:T2LFU 75
### NA:noT2LR:T2D:T2LFU 5
### No:noT2LR:noT2D:noT2LFU 12
### Yes:noT2LR:noT2D:T2LFU 2
### 100 patients miss information on the outcome
### DRDeath: NA if subjects miss information on the outcome *
### "DisRecur" if Distant.Recurrence is Yes and Death is Yes or No
### "Dead" if Death is Yes and Distant.Recurrence is No
### "Censored" if Distant.Recurrence and Death are both No
### * NA:noT2DR:noT2D:noT2LFU 6
### NA:noT2DR:noT2D:T2LFU 69
### NA:noT2DR:T2D:T2LFU 5
### No:noT2DR:noT2D:noT2LFU 11
### Yes:noT2DR:noT2D:noT2LFU 1
### Yes:noT2DR:noT2D:T2LFU 12
### Yes:noT2DR:T2D:T2LFU 1
### 105 patients miss information on the outcome
### NPDeath: NA if subjects miss information on the outcome *
### "NBP" if New.Breast.Primary is Yes and Death is Yes or No
### "Dead" if Death is Yes and New.Breast.Primary is No
### "Censored" if New.Breast.Primary and Death are both No
### * NA:noT2NP:noT2D:noT2LFU 6
### NA:noT2NP:noT2D:T2LFU 82
### NA:noT2NP:T2D:T2LFU 6
### No:noT2NP:noT2D:noT2LFU 12
### 106 patients miss information on the outcome
### T2LRDeath: NA if LRDeath is NA
### `T2LR` if LRDeath is LocRecur
### `T2Death` if LRDeath is Dead
### `T2LFU` if LRDeath is Censored
### T2DRDeath: NA if DRDeath is NA
### `T2DR` if DRDeath is DisRecur
### `T2Death` if DRDeath is Dead
### `T2LFU` if DRDeath is Censored
### T2NPDeath: NA if NPDeath is NA
### `T2NP` if NPDeath is NBP
### `T2Death` if NPDeath is Dead
### `T2LFU` if NPDeath is Censored
## ALiu: Create event and time variables where
### 1) LR/NP with positive ER, LR/NP with negative ER, and dead are treated as competing risks; these variables are LRDeath.ER/NPDeath.ER/T2LRDeath.ER/T2NPDeath.ER
### 2) LR/NP with positive PR, LR/NP with negative PR, and dead are treated as competing risks; these variables are LRDeath.PR/NPDeath.PR/T2LRDeath.PR/T2NPDeath.PR
### 3) LR/NP with positive HER2, LR/NP with negative HER2, and dead are treated as competing risks; these variables are LRDeath.HER2/NPDeath.HER2/T2LRDeath.HER2/T2NPDeath.HER2
### LRDeath.ER: NA if subjects miss information on the outcome*
### "LRYesERPos" if Local.Recurrence is Yes and ER.LR is Positive and Death is Yes or No
### "LRYesERNeg" if Local.Recurrence is Yes and ER.LR is Negative and Death is Yes or No
### "Dead" if Death is Yes and Local.Recurrence is No
### "Censored" if Local.Recurrence and Death are both No
with(dat3, table(paste(Local.Recurrence, ER.LR, Death, # Load dat3 (refer back to the line 61)
ifelse(is.na(T2LR), "noT2LR", "T2LR"),
ifelse(is.na(T2Death), "noT2D", "T2D"),
ifelse(is.na(T2LFU), "noT2LFU", "T2LFU"),
sep = ":")))
# NA:Negative:No:noT2LR:noT2D:noT2LFU NA:Negative:No:noT2LR:noT2D:T2LFU
# 6* 75*
# NA:Negative:Yes:noT2LR:T2D:T2LFU No:NA:No:noT2LR:noT2D:T2LFU
# 5* 12*
# No:Negative:No:noT2LR:noT2D:noT2LFU No:Negative:No:noT2LR:noT2D:T2LFU
# 12* 407
# No:Negative:Yes:noT2LR:T2D:T2LFU Yes:NA:No:noT2LR:noT2D:T2LFU
# 15 1*
# Yes:NA:Yes:T2LR:T2D:T2LFU Yes:Negative:No:T2LR:noT2D:T2LFU
# 1* 54
# Yes:Negative:Yes:T2LR:T2D:T2LFU Yes:Positive:No:noT2LR:noT2D:T2LFU
# 7 1*
# Yes:Positive:No:T2LR:noT2D:T2LFU Yes:Positive:Yes:T2LR:T2D:T2LFU
# 7 1
# 113 (6+75+5+12+12+1+1+1) patients miss outcome information*
### LRDeath.PR: NA if subjects miss information on the outcome*
### "LRYesPRPos" if Local.Recurrence is Yes and PR.LR is Positive and Death is Yes or No
### "LRYesPRNeg" if Local.Recurrence is Yes and PR.LR is Negative and Death is Yes or No
### "Dead" if Death is Yes and Local.Recurrence is No
### "Censored" if Local.Recurrence and Death are both No
with(dat3, table(paste(Local.Recurrence, PR.LR, Death,
ifelse(is.na(T2LR), "noT2LR", "T2LR"),
ifelse(is.na(T2Death), "noT2D", "T2D"),
ifelse(is.na(T2LFU), "noT2LFU", "T2LFU"),
sep = ":")))
# NA:Negative:No:noT2LR:noT2D:noT2LFU NA:Negative:No:noT2LR:noT2D:T2LFU
# 6* 75*
# NA:Negative:Yes:noT2LR:T2D:T2LFU No:NA:No:noT2LR:noT2D:T2LFU
# 5* 12*
# No:Negative:No:noT2LR:noT2D:noT2LFU No:Negative:No:noT2LR:noT2D:T2LFU
# 12* 407
# No:Negative:Yes:noT2LR:T2D:T2LFU Yes:NA:No:noT2LR:noT2D:T2LFU
# 15 1*
# Yes:NA:Yes:T2LR:T2D:T2LFU Yes:Negative:No:noT2LR:noT2D:T2LFU
# 1* 1*
# Yes:Negative:No:T2LR:noT2D:T2LFU Yes:Negative:Yes:T2LR:T2D:T2LFU
# 56 8
# Yes:Positive:No:T2LR:noT2D:T2LFU
# 5
# 113 (6+75+5+12+12+1+1+1) patients miss outcome information*
### LRDeath.HER2: NA if subjects miss information on the outcome*
### "LRYesHER2Pos" if Local.Recurrence is Yes and HER2.LR is Positive and Death is Yes or No
### "LRYesHER2Neg" if Local.Recurrence is Yes and HER2.LR is Negative and Death is Yes or No
### "Dead" if Death is Yes and Local.Recurrence is No
### "Censored" if Local.Recurrence and Death are both No
with(dat3, table(paste(Local.Recurrence, HER2.LR, Death,
ifelse(is.na(T2LR), "noT2LR", "T2LR"),
ifelse(is.na(T2Death), "noT2D", "T2D"),
ifelse(is.na(T2LFU), "noT2LFU", "T2LFU"),
sep = ":")))
# NA:Negative:No:noT2LR:noT2D:noT2LFU NA:Negative:No:noT2LR:noT2D:T2LFU
# 6* 75*
# NA:Negative:Yes:noT2LR:T2D:T2LFU No:NA:No:noT2LR:noT2D:T2LFU
# 5* 12*
# No:Negative:No:noT2LR:noT2D:noT2LFU No:Negative:No:noT2LR:noT2D:T2LFU
# 12* 407
# No:Negative:Yes:noT2LR:T2D:T2LFU Yes:NA:No:noT2LR:noT2D:T2LFU
# 15 1*
# Yes:NA:No:T2LR:noT2D:T2LFU Yes:NA:Yes:T2LR:T2D:T2LFU
# 4* 2*
# Yes:Negative:No:noT2LR:noT2D:T2LFU Yes:Negative:No:T2LR:noT2D:T2LFU
# 1* 56
# Yes:Negative:Yes:T2LR:T2D:T2LFU Yes:Positive:No:T2LR:noT2D:T2LFU
# 7 1
# 118 (6+75+5+12+12+1+4+2+1) patients miss outcome information*
### NPDeath.ER: NA if subjects miss information on the outcome*
### "NPYesERPos" if New.Breast.Primary is Yes and ER.NP is Positive and Death is Yes or No
### "NPYesERNeg" if New.Breast.Primary is Yes and ER.NP is Negative and Death is Yes or No
### "Dead" if Death is Yes and New.Breast.Primary is No
### "Censored" if New.Breast.Primary and Death are both No
with(dat3, table(paste(New.Breast.Primary, ER.NP, Death,
ifelse(is.na(T2NP), "noT2NP", "T2NP"),
ifelse(is.na(T2Death), "noT2D", "T2D"),
ifelse(is.na(T2LFU), "noT2LFU", "T2LFU"),
sep = ":")))
# NA:NA:No:noT2NP:noT2D:T2LFU NA:Negative:No:noT2NP:noT2D:noT2LFU
# 4* 6*
# NA:Negative:No:noT2NP:noT2D:T2LFU NA:Negative:Yes:noT2NP:T2D:T2LFU
# 78* 6*
# No:NA:No:noT2NP:noT2D:T2LFU No:NA:No:T2NP:noT2D:T2LFU
# 54* 1*
# No:NA:Yes:noT2NP:T2D:T2LFU No:Negative:No:noT2NP:noT2D:noT2LFU
# 7* 12*
# No:Negative:No:noT2NP:noT2D:T2LFU No:Negative:Yes:noT2NP:T2D:T2LFU
# 404 14
# Yes:NA:No:T2NP:noT2D:T2LFU Yes:Negative:No:T2NP:noT2D:T2LFU
# 1* 4
# Yes:Negative:Yes:T2NP:T2D:T2LFU Yes:Positive:No:T2NP:noT2D:T2LFU
# 2 11
# 169 (4+6+78+6+54+1+7+12+1) patients miss outcome information*
### NPDeath.PR: NA if subjects miss information on the outcome*
### "NPYesPRPos" if New.Breast.Primary is Yes and PR.NP is Positive and Death is Yes or No
### "NPYesPRNeg" if New.Breast.Primary is Yes and PR.NP is Negative and Death is Yes or No
### "Dead" if Death is Yes and New.Breast.Primary is No
### "Censored" if New.Breast.Primary and Death are both No
with(dat3, table(paste(New.Breast.Primary, PR.NP, Death,
ifelse(is.na(T2NP), "noT2NP", "T2NP"),
ifelse(is.na(T2Death), "noT2D", "T2D"),
ifelse(is.na(T2LFU), "noT2LFU", "T2LFU"),
sep = ":")))
# NA:NA:No:noT2NP:noT2D:T2LFU NA:Negative:No:noT2NP:noT2D:noT2LFU
# 4* 6*
# NA:Negative:No:noT2NP:noT2D:T2LFU NA:Negative:Yes:noT2NP:T2D:T2LFU
# 78* 6*
# No:NA:No:noT2NP:noT2D:T2LFU No:NA:No:T2NP:noT2D:T2LFU
# 54* 1*
# No:NA:Yes:noT2NP:T2D:T2LFU No:Negative:No:noT2NP:noT2D:noT2LFU
# 7* 12*
# No:Negative:No:noT2NP:noT2D:T2LFU No:Negative:Yes:noT2NP:T2D:T2LFU
# 404 14
# Yes:NA:No:T2NP:noT2D:T2LFU Yes:Negative:No:T2NP:noT2D:T2LFU
# 1* 7
# Yes:Negative:Yes:T2NP:T2D:T2LFU Yes:Positive:No:T2NP:noT2D:T2LFU
# 2 8
# 169 (4+6+78+6+54+1+7+12+1) patients miss outcome information*
### NPDeath.HER2: NA if subjects miss information on the outcome*
### "NPYesHER2Pos" if New.Breast.Primary is Yes and HER2.NP is Positive and Death is Yes or No
### "NPYesHER2Neg" if New.Breast.Primary is Yes and HER2.NP is Negative and Death is Yes or No
### "Dead" if Death is Yes and New.Breast.Primary is No
### "Censored" if New.Breast.Primary and Death are both No
with(dat3, table(paste(New.Breast.Primary, HER2.NP, Death,
ifelse(is.na(T2NP), "noT2NP", "T2NP"),
ifelse(is.na(T2Death), "noT2D", "T2D"),
ifelse(is.na(T2LFU), "noT2LFU", "T2LFU"),
sep = ":")))
# NA:NA:No:noT2NP:noT2D:T2LFU NA:Negative:No:noT2NP:noT2D:noT2LFU
# 4* 6*
# NA:Negative:No:noT2NP:noT2D:T2LFU NA:Negative:Yes:noT2NP:T2D:T2LFU
# 78* 6*
# No:NA:No:noT2NP:noT2D:T2LFU No:NA:No:T2NP:noT2D:T2LFU
# 54* 1*
# No:NA:Yes:noT2NP:T2D:T2LFU No:Negative:No:noT2NP:noT2D:noT2LFU
# 7* 12*
# No:Negative:No:noT2NP:noT2D:T2LFU No:Negative:Yes:noT2NP:T2D:T2LFU
# 404 14
# Yes:NA:No:T2NP:noT2D:T2LFU Yes:Negative:No:T2NP:noT2D:T2LFU
# 2* 13
# Yes:Negative:Yes:T2NP:T2D:T2LFU Yes:Positive:No:T2NP:noT2D:T2LFU
# 2 1
# 170 (4+6+78+6+54+1+7+12+2) patients miss outcome information*
### T2LRDeath.ER: NA if LRDeath.ER is NA
### `T2LR` if LRDeath.ER is LRYesERPos or LRYesERNeg
### `T2Death` if LRDeath.ER is Dead
### `T2LFU` if LRDeath.ER is Censored
### T2LRDeath.PR: NA if LRDeath.PR is NA
### `T2LR` if LRDeath.PR is LRYesPRPos or LRYesPRNeg
### `T2Death` if LRDeath.PR is Dead
### `T2LFU` if LRDeath.PR is Censored
### T2LRDeath.HER2: NA if LRDeath.HER2 is NA
### `T2LR` if LRDeath.HER2 is LRYesHER2Pos or LRYesHER2Neg
### `T2Death` if LRDeath.HER2 is Dead
### `T2LFU` if LRDeath.HER2 is Censored
### T2NPDeath.ER: NA if NPDeath.ER is NA
### `T2NP` if NPDeath.ER is NPYesERPos or NPYesERNeg
### `T2Death` if NPDeath.ER is Dead
### `T2LFU` if NPDeath.ER is Censored
### T2NPDeath.PR: NA if NPDeath.PR is NA
### `T2NP` if NPDeath.PR is NPYesPRPos or NPYesPRNeg
### `T2Death` if NPDeath.PR is Dead
### `T2LFU` if NPDeath.PR is Censored
### T2NPDeath.HER2: NA if NPDeath.HER2 is NA
### `T2NP` if NPDeath.HER2 is NPYesHER2Pos or NPYesHER2Neg
### `T2Death` if NPDeath.HER2 is Dead
### `T2LFU` if NPDeath.HER2 is Censored
```
```{r analysis_plan_current, include=FALSE}
#################################################################
## Updated analysis plan ##
#################################################################
## ALiu: Create event variables where the dead and censored are combined as censor
### LRDeath2: NA if LRDeath is NA
### "LocRecur" if LRDeath is LocRecur
### "censor" if LRDeath is Dead or Censored
### DRDeath2: NA if DRDeath is NA
### "DisRecur" if DRDeath is DisRecur
### "censor" if DRDeath is Dead or Censored
### NPDeath2: NA if NPDeath is NA
### "NBP" if NPDeath is NBP
### "censor" if NPDeath is Dead or Censored
### LRDeath2.ER: NA if LRDeath.ER is NA
### "LRYesERPos" if LRDeath.ER is LRYesERPos
### "LRYesERNeg" if LRDeath.ER is LRYesERNeg
### "censor" if LRDeath.ER is Dead or Censored
### LRDeath2.PR: NA if LRDeath.PR is NA
### "LRYesPRPos" if LRDeath.PR is LRYesPRPos
### "LRYesPRNeg" if LRDeath.PR is LRYesPRNeg
### "censor" if LRDeath.PR is Dead or Censored
### LRDeath2.HER2: NA if LRDeath.HER2 is NA
### "LRYesHER2Pos" if LRDeath.HER2 is LRYesHER2Pos
### "LRYesHER2Neg" if LRDeath.HER2 is LRYesHER2Neg
### "censor" if LRDeath.HER2 is Dead or Censored
### NPDeath2.ER: NA if NPDeath.ER is NA
### "NPYesERPos" if NPDeath.ER is NPYesERPos
### "NPYesERNeg" if NPDeath.ER is NPYesERNeg
### "censor" if NPDeath.ER is Dead or Censored
### NPDeath2.PR: NA if NPDeath.PR is NA
### "NPYesPRPos" if NPDeath.PR is NPYesPRPos
### "NPYesPRNeg" if NPDeath.PR is NPYesPRNeg
### "censor" if NPDeath.PR is Dead or Censored
### NPDeath2.HER2: NA if NPDeath.HER2 is NA
### "NPYesHER2Pos" if NPDeath.HER2 is NPYesHER2Pos
### "NPYesHER2Neg" if NPDeath.HER2 is NPYesHER2Neg
### "censor" if NPDeath.HER2 is Dead or Censored
```
# Data preparation
Among patients with local recurrence 72 (11.92%, n = 604), 2 patients do not have the date of local recurrence. Among patients with distant recurrence 70 (11.59%, n = 604), 13 patients do not have the date of distant recurrence. Among patients with new breast primary recurrence 18 (2.98%, n = 604), no patient does not have the date of new breast primary recurrence.
Patients who were never disease free (n = 2) were counted as N/A’s in terms of recurrence and new primary endpoints.
The categorical levels of race and ethnicity variables need careful re-examination. Two spreadsheets contained race data, however the race variables were categorized in varied ways. Specially, in terms of Hisp/Latina category, it is unclear if some of the whites would have been in the Hisp/Latina category. At present, one patient with race of Arabic/Mideastern was included in the WA category.
```{r global_options, include=FALSE}
#################################################################
## Automator ##
#################################################################
if (params$plot.fig) {
dir.fig <- paste0("../report/figs_", params$date)
if (!dir.exists(dir.fig)) {
# If the figure directory does not exist, we create a new directory under the folder report using the name figs + current date passed from the params$date in YAML
dir.create(dir.fig)
}
knitr::opts_chunk$set( # Setting parameters when figures are plotted
fig.width = 6, fig.height = 6, fig.path = dir.fig, dev = "png", dpi = 300,
echo = FALSE, warning = FALSE, message = FALSE,
cache = FALSE,
comment = ""
)
} else { # Setting parameters when figures are not plotted
knitr::opts_chunk$set(
echo = FALSE, warning = FALSE, message = FALSE,
cache = FALSE,
comment = ""
)
}
if (params$results.folder) { # Suitable when the results need to be stored outside the microsoft word report
dir.result <- paste0("../report/results_", params$date)
if (!dir.exists(dir.result)) {
# If the directory does not exist, we create a new directory under the folder report using the name results + current date passed from the params$date in YAML
dir.create(dir.result)
}
}
loc.perl <- "C:/cygwin64/bin/perl"
```
```{r clean_data_start, eval=FALSE}
##################################################################
## Clean data ##
##################################################################
dat0 <- read_xlsx(
path = "../data/raw/TNBC WCM 1998-2018_Deidentified_Final_10_14_V2.xlsx",
sheet = "TNBC no recurrence or new BC",
range = "A1:BL521",
na = c("Unknown", "", "NA", "Not applicable ", "Not applicable", "Not aplicable", "n/a", "Unknown grade (not reported or unavailable)", "Unknown (not reported or unavailable)")) %>%
select(-c("ER expression (%)", "PR expression (%)", "HER2/neu (IHC)", "HER2/neu (FISH)")) %>%
mutate(ER.LR = "Negative",
PR.LR = "Negative",
HER2.LR = "Negative",
ER.NP = "Negative",
PR.NP = "Negative",
HER2.NP = "Negative") %>%
data.frame()
dat1 <- read_xlsx(
path = "../data/raw/TNBC WCM 1998-2018_Deidentified_Final_10_14_V2.xlsx",
sheet = "recurrence or new BC",
range = "A1:CE85",
na = c("Unknown", "", "NA", "Not applicable ", "Not applicable", "Not aplicable", "n/a", "Unknown grade (not reported or unavailable)", "Unknown (not reported or unavailable)")) %>%
data.frame()
var.rm <- c("Laterality.Primary",
"Laterality.local",
"Cancer.Type",
"ER.expression....",
"PR.expression....",
"HER2.neu..IHC.",
"HER2.neu..FISH.",
# "ER...60", # ER.LR
"X....61",
# "PR...62", # PR.LR
"X....63",
# "HER2...64", # HER2.LR
"IHC...65",
"FISH...66",
"Laterality.New",
"New.Path",
# "ER...74", # ER.NP
"X....75",
# "PR...76", # PR.NP
"X....77",
# "HER2...78", # HER2.NP
"IHC...79",
"FISH...80")
dat1 <- dat1 %>%
select(-var.rm) %>%
rename("Any.Local.Recurrence" = "Local.Recurrence",
"ER.LR" = "ER...60",
"PR.LR" = "PR...62",
"HER2.LR" = "HER2...64",
"ER.NP" = "ER...74",
"PR.NP" = "PR...76",
"HER2.NP" = "HER2...78") %>%
select(everything(),
"Status.Last.Follow.up",
"Any.Local.Recurrence",
"Date.Local.Recurrence",
"Any.Distant.Recurrence",
"Site.First.Distant.Recurrence",
"Date.First.Distant.Recurrence",
"Any.New..subsequent..Br.1",
"Date.New.Breast.primary",
"Date.Death")
dat2 <- rbind(dat0, dat1)
##################################################################
## Fix column names ##
##################################################################
names(dat2) <-
sapply(strsplit(names(dat2), split = "\\."), function(x)
paste(x[x != ""], collapse = "."))
#################################################################
## Clean data (continue) ##
#################################################################
# table(dat.full$Race.Ethnicity2, useNA = "always")
dat3 <- dat2 %>%
mutate(
Race.Ethnicity2 = gsub(".*Asian.*|Filipino|Korean", "Asian",
gsub("Hisp.*", "Hisp/Latina",
gsub("White|NHW|Arabic*", "WA", #xkz: added NHW and arabic
gsub("American.*|Pacific.*", "Other", Race.Ethnicity)))) %>%
factor(levels = c("WA", "AA", "Asian", "Hisp/Latina", "Other"))) %>%
mutate(
Race.Ethnicity3 = ifelse(Race.Ethnicity2 == "WA", "WA", "Other") %>%
factor(levels = c("WA", "Other"))) %>%
mutate( # NA: 86
Local.Recurrence = gsub("Never.*", NA, Any.Local.Recurrence) %>% #xkz: "Never Disease Free" needs to be counted separately probably better as NA here
factor()) %>%
mutate( # NA: 80
Distant.Recurrence = gsub("Never.*", NA, Any.Distant.Recurrence) %>%
factor()) %>%
mutate( # NA: 94
New.Breast.Primary = Any.New.subsequent.Br.1 %>%
factor()) %>%
mutate(
Clinical.T.Stage2 = gsub("T1a|T1b|T1c|T2", "T1|T2",
gsub("T3|T4", "T3|T4", Clinical.T.Stage))) %>%
mutate(
Clinical.N.Stage2 = gsub("N1|N2|N3", "N1|N2|N3", Clinical.N.Stage))
```
```{r code_complex_variable, eval=FALSE}
#################################################################
## Recode T2LR/T2DR/T2NP/T2Death/T2LFU (in days) ##
#################################################################
dat3 <- dat3 %>%
mutate( # LR: local recurrence
T2LR = as.numeric(as.Date(Date.Local.Recurrence) - as.Date(Date.of.Diagnosis))) %>%
mutate( # DR: distant recurrence
T2DR = as.numeric(as.Date(Date.First.Distant.Recurrence) - as.Date(Date.of.Diagnosis))) %>%
mutate( # NP: new breast primary
T2NP = as.numeric(as.Date(Date.New.Breast.primary) - as.Date(Date.of.Diagnosis))) %>%
mutate(
T2Death = as.numeric(as.Date(Date.Death) - as.Date(Date.of.Diagnosis))) %>%
mutate( # LFU: last follow up
T2LFU = as.numeric(as.Date(Date.Last.Follow.up) - as.Date(Date.of.Diagnosis)))
##################################################################
## Check all T2 variables ##
##################################################################
## Avoid writing the following codes in the future.
# range(dat3$T2Death, na.rm = TRUE)
# range(dat3$T2DR, na.rm = TRUE)
# range(dat3$T2NP, na.rm = TRUE)
# range(dat3$T2LFU, na.rm = TRUE)
var <- c("T2Death", "T2DR", "T2NP", "T2LFU")
mclapply(1:4,
function(i){range(dat3[var[i]], na.rm = T)},
mc.cores = 4)
# [[1]]
# [1] 106 4235
#
# [[2]]
# [1] 178 12714
#
# [[3]]
# [1] 232 4848
#
# [[4]]
# [1] 0 13573
#################################################################
## Recode LRFS.event/DRFS.event/NPFS.event ##
#################################################################
dat3 <- dat3 %>%
mutate( # LRFS: local recurrence free survival
LRFS.event = ifelse(Local.Recurrence == "Yes", 1, 0)) %>%
mutate( # DRFS: distant recurrence free survival
DRFS.event = ifelse(Distant.Recurrence == "Yes", 1, 0)) %>%
mutate( # NPFS: new breast primary free survival
NPFS.event = ifelse(New.Breast.Primary == "Yes", 1, 0))
##################################################################
## Recode LRFS.time/DRFS.time/NPFS.time ##
##################################################################
table(paste(dat3$Local.Recurrence,
ifelse(is.na(dat3$T2LR), "noT2LR", "T2LR"),
ifelse(is.na(dat3$T2Death), "noT2D", "T2D"),
ifelse(is.na(dat3$T2LFU), "noT2LFU", "T2LFU"),
sep = ":"))
# NA:noT2LR:noT2D:noT2LFU 6
# NA:noT2LR:noT2D:T2LFU 75
# NA:noT2LR:T2D:T2LFU 5
# No:noT2LR:noT2D:noT2LFU 12
# Yes:noT2LR:noT2D:T2LFU 2
# 100 patients miss outcome information
dat3 <- dat3 %>%
mutate(
LRFS.time = case_when(
Local.Recurrence == "No" & !is.na(T2Death) & T2Death <= T2LFU ~ T2Death, # No:noT2LR:T2D:T2LFU
Local.Recurrence == "No" & !is.na(T2Death) & T2Death > T2LFU ~ T2LFU, # No:noT2LR:T2D:T2LFU
Local.Recurrence == "No" & is.na(T2Death) ~ T2LFU, # No:noT2LR:noT2D:noT2LFU; No:noT2LR:noT2D:T2LFU
Local.Recurrence == "Yes" | is.na(Local.Recurrence) ~ T2LR)) # Yes:noT2LR:noT2D:T2LFU; Yes:T2LR:noT2D:T2LFU; Yes:T2LR:T2D:T2LFU; NA:noT2LR:noT2D:noT2LFU; NA:noT2LR:noT2D:T2LFU; NA:noT2LR:T2D:T2LFU
# Check the number of patients not at risk
summary(dat3$LRFS.time)
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# 0.0 557.2 1320.5 1739.5 2524.2 12694.0 100
# 100 patients miss outcome information
table(paste(dat3$Distant.Recurrence,
ifelse(is.na(dat3$T2DR), "noT2DR", "T2DR"),
ifelse(is.na(dat3$T2Death), "noT2D", "T2D"),
ifelse(is.na(dat3$T2LFU), "noT2LFU", "T2LFU"),
sep = ":"))
# NA:noT2DR:noT2D:noT2LFU 6
# NA:noT2DR:noT2D:T2LFU 69
# NA:noT2DR:T2D:T2LFU 5
# No:noT2DR:noT2D:noT2LFU 11
# Yes:noT2DR:noT2D:noT2LFU 1
# Yes:noT2DR:noT2D:T2LFU 12
# Yes:noT2DR:T2D:T2LFU 1
# 105 patients miss outcome information
dat3 <- dat3 %>%
mutate(
DRFS.time = case_when(
Distant.Recurrence == "No" & !is.na(T2Death) & T2Death <= T2LFU ~ T2Death,
Distant.Recurrence == "No" & !is.na(T2Death) & T2Death > T2LFU ~ T2LFU,
Distant.Recurrence == "No" & is.na(T2Death) ~ T2LFU,
Distant.Recurrence == "Yes" | is.na(Distant.Recurrence) ~ T2DR))
# Check the number of patients not at risk
summary(dat3$DRFS.time)
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# 0.0 579.5 1376.0 1833.1 2701.0 12714.0 105
table(paste(dat3$New.Breast.Primary,
ifelse(is.na(dat3$T2NP), "noT2NP", "T2NP"),
ifelse(is.na(dat3$T2Death), "noT2D", "T2D"),
ifelse(is.na(dat3$T2LFU), "noT2LFU", "T2LFU"),
sep = ":"))
# NA:noT2NP:noT2D:noT2LFU 6
# NA:noT2NP:noT2D:T2LFU 82
# NA:noT2NP:T2D:T2LFU 6
# No:noT2NP:noT2D:noT2LFU 12
# 106 patients miss outcome information
dat3 <- dat3 %>%
mutate(
NPFS.time = case_when(
New.Breast.Primary == "No" & !is.na(T2Death) & T2Death <= T2LFU ~ T2Death,
New.Breast.Primary == "No" & !is.na(T2Death) & T2Death > T2LFU ~ T2LFU,
New.Breast.Primary == "No" & is.na(T2Death) ~ T2LFU,
New.Breast.Primary == "Yes" | is.na(New.Breast.Primary) ~ T2NP))
# Check the number of patients not at risk
summary(dat3$NPFS.time)
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# 0.0 609.8 1351.0 1822.8 2697.2 13573.0 106
# 106 patients miss outcome information
#################################################################
## Recode LR5y/DR5y/NP5y ##
#################################################################
# dat3$LR5y <- dat3$Local.Recurrence # Factor variable
# dat3$DR5y <- dat3$Distant.Recurrence
# dat3$NP5y <- dat3$New.Breast.Primary
dat3$LR5y = dat3$DR5y = dat3$NP5y = rep(0, times = nrow(dat3))
criter1 <- with(dat3, LRFS.event == 1 & !is.na(T2LR) & T2LR < 365.25*5)
table(criter1, useNA = "ifany")
# NA is not a value; R deal with NA separately
# criter1
# FALSE TRUE
# 549 55
criter2 <- with(dat3, LRFS.event == 0 & !is.na(LRFS.time) & LRFS.time < 365.25*5)
table(criter2, useNA = "ifany")
# criter2
# FALSE TRUE
# 344 260
dat3$LR5y[criter1] <- 1
dat3$LR5y[criter2] <- NA
table(dat3$LR5y, useNA = "ifany")
# 0 1 <NA>
# 289 55 260
criter1 <- with(dat3, DRFS.event == 1 & !is.na(T2DR) & T2DR < 365.25*5)
table(criter1, useNA = "ifany")
# criter1
# FALSE TRUE
# 557 47
criter2 <- with(dat3, DRFS.event == 0 & !is.na(DRFS.time) & DRFS.time < 365.25*5)
table(criter2, useNA = "ifany")
# criter2
# FALSE TRUE
# 355 249
dat3$DR5y[criter1] <- 1
dat3$DR5y[criter2] <- NA
table(dat3$DR5y, useNA = "ifany")
# 0 1 <NA>
# 308 47 249
criter1 <- with(dat3, NPFS.event == 1 & !is.na(T2NP) & T2NP < 365.25*5)
table(criter1, useNA = "ifany")
# criter1
# FALSE TRUE
# 591 13
criter2 <- with(dat3, NPFS.event == 0 & !is.na(NPFS.time) & NPFS.time < 365.25*5)
table(criter2, useNA = "ifany")
# criter2
# FALSE TRUE
# 322 282
dat3$NP5y[criter1] <- 1
dat3$NP5y[criter2] <- NA
table(dat3$NP5y, useNA = "ifany")
# 0 1 <NA>
# 309 13 282
##################################################################
## Recode Death ##
##################################################################
dat3$Death <- ifelse(!is.na(dat3$Date.Death), "Yes", "No")
##################################################################
## Recode LRDeath ##
##################################################################
table(paste(dat3$Local.Recurrence,
ifelse(is.na(dat3$T2LR), "noT2LR", "T2LR"),
ifelse(is.na(dat3$T2Death), "noT2D", "T2D"),
ifelse(is.na(dat3$T2LFU), "noT2LFU", "T2LFU"),
sep = ":"))
# NA:noT2LR:noT2D:noT2LFU 6
# NA:noT2LR:noT2D:T2LFU 75
# NA:noT2LR:T2D:T2LFU 5
# No:noT2LR:noT2D:noT2LFU 12
# Yes:noT2LR:noT2D:T2LFU 2
# 100 patients miss outcome information
table(dat3$Local.Recurrence, dat3$Death, useNA = "ifany")
# No Yes
# No 431 15
# Yes 63 9
# <NA> 81 5
dat3[dat3$Local.Recurrence == "Yes" & !is.na(dat3$Local.Recurrence) & dat3$Death == "Yes" & !is.na(dat3$Death), c("T2LR", "T2Death")]
# T2LR T2Death
# 564 276 396
# 580 374 410
# 581 2373 4235
# 582 1922 3535
# 583 445 818
# 586 2498 3415
# 589 392 532
# 596 1116 1948
# 604 312 464
dat3 <- dat3 %>%
mutate(LRDeath = case_when(is.na(Local.Recurrence) & is.na(T2LR) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA),
is.na(Local.Recurrence) & is.na(T2LR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
is.na(Local.Recurrence) & is.na(T2LR) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
Local.Recurrence == "No" & is.na(T2LR) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA),
Local.Recurrence == "Yes" & is.na(T2LR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
Local.Recurrence == "Yes" & Death == "No" ~ "LocRecur",
Local.Recurrence == "Yes" & Death == "Yes" ~ "LocRecur",
Death == "Yes" & Local.Recurrence == "No" ~ "Dead",
Local.Recurrence == "No" & Death == "No" ~ "Censored") %>%
factor(levels = c("Censored", "LocRecur", "Dead")))
table(dat3$LRDeath, useNA = "ifany")
# Censored Dead LocRecur <NA>
# 419 15 70 100
##################################################################
## Recode DRDeath ##
##################################################################
table(paste(dat3$Distant.Recurrence,
ifelse(is.na(dat3$T2DR), "noT2DR", "T2DR"),
ifelse(is.na(dat3$T2Death), "noT2D", "T2D"),
ifelse(is.na(dat3$T2LFU), "noT2LFU", "T2LFU"),
sep = ":"))
# NA:noT2DR:noT2D:noT2LFU 6
# NA:noT2DR:noT2D:T2LFU 69
# NA:noT2DR:T2D:T2LFU 5
# No:noT2DR:noT2D:noT2LFU 11
# Yes:noT2DR:noT2D:noT2LFU 1
# Yes:noT2DR:noT2D:T2LFU 12
# Yes:noT2DR:T2D:T2LFU 1
# 105 patients miss outcome information
table(dat3$Distant.Recurrence, dat3$Death, useNA = "ifany")
# No Yes
# No 441 13
# Yes 59 11
# <NA> 75 5
dat3[dat3$Distant.Recurrence == "Yes" & !is.na(dat3$Distant.Recurrence) & dat3$Death == "Yes" & !is.na(dat3$Death), c("T2DR", "T2Death")]
# T2DR T2Death
# 237 903 1485
# 239 1120 1301
# 371 578 1091
# 520 447 550
# 564 276 396
# 581 3592 4235
# 582 3407 3535
# 583 446 818
# 586 3297 3415
# 589 392 532
# 596 NA 1948
dat3 <- dat3 %>%
mutate(DRDeath = case_when(is.na(Distant.Recurrence) & is.na(T2DR) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA),
is.na(Distant.Recurrence) & is.na(T2DR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
is.na(Distant.Recurrence) & is.na(T2DR) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
Distant.Recurrence == "No" & is.na(T2DR) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA),
Distant.Recurrence == "Yes" & is.na(T2DR) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA),
Distant.Recurrence == "Yes" & is.na(T2DR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
Distant.Recurrence == "Yes" & is.na(T2DR) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
Distant.Recurrence == "Yes" & Death == "No" ~ "DisRecur",
Distant.Recurrence == "Yes" & Death == "Yes" ~ "DisRecur",
Death == "Yes" & Distant.Recurrence == "No" ~ "Dead",
Distant.Recurrence == "No" & Death == "No" ~ "Censored") %>%
factor(levels = c("Censored", "DisRecur", "Dead"))
)
table(dat3$DRDeath, useNA = "ifany")
# Censored Dead DisRecur <NA>
# 430 13 56 105
##################################################################
## Recode NPDeath ##
##################################################################
table(paste(dat3$New.Breast.Primary,
ifelse(is.na(dat3$T2NP), "noT2NP", "T2NP"),
ifelse(is.na(dat3$T2Death), "noT2D", "T2D"),
ifelse(is.na(dat3$T2LFU), "noT2LFU", "T2LFU"),
sep = ":"))
# NA:noT2NP:noT2D:noT2LFU 6
# NA:noT2NP:noT2D:T2LFU 82
# NA:noT2NP:T2D:T2LFU 6
# No:noT2NP:noT2D:noT2LFU 12
# 106 patients miss outcome information
table(dat3$New.Breast.Primary, dat3$Death, useNA = "ifany")
# No Yes
# No 471 21
# Yes 16 2
# <NA> 88 6
dat3[dat3$New.Breast.Primary == "Yes" & !is.na(dat3$New.Breast.Primary) & dat3$Death == "Yes" & !is.na(dat3$Death), c("T2NP", "T2Death")]
# T2NP T2Death
# 564 287 396
# 582 2723 3535
dat3 <- dat3 %>%
mutate(NPDeath = case_when(is.na(New.Breast.Primary) & is.na(T2NP) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA),
is.na(New.Breast.Primary) & is.na(T2NP) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
is.na(New.Breast.Primary) & is.na(T2NP) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA),
New.Breast.Primary == "No" & is.na(T2NP) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA),
New.Breast.Primary == "Yes" & Death == "No" ~ "NBP",
New.Breast.Primary == "Yes" & Death == "Yes" ~ "NBP",
Death == "Yes" & New.Breast.Primary == "No" ~ "Dead",
New.Breast.Primary == "No" & Death == "No" ~ "Censored") %>%
factor(levels = c("Censored", "NBP", "Dead"))
)
table(dat3$NPDeath, useNA = "ifany")
# Censored Dead NBP <NA>
# 459 21 18 106
##################################################################
## Recode T2LRDeath/T2DRDeath/T2NPDeath ##
##################################################################
dat3 <- dat3 %>%
mutate(T2LRDeath = case_when(LRDeath == "LocRecur" ~ T2LR,
LRDeath == "Dead" ~ T2Death,
LRDeath == "Censored" ~ T2LFU)) %>%
mutate(T2DRDeath = case_when(DRDeath == "DisRecur" ~ T2DR,
DRDeath == "Dead" ~ T2Death,
DRDeath == "Censored" ~ T2LFU)) %>%
mutate(T2NPDeath = case_when(NPDeath == "NBP" ~ T2NP,
NPDeath == "Dead" ~ T2Death,
NPDeath == "Censored" ~ T2LFU))
## Avoid writing the following codes in the future.
# sum(is.na(T2LRDeath))
# sum(is.na(T2DRDeath))
# sum(is.na(T2NPDeath))
sum.na <- function(x) {is.na(x) %>% sum()}
var <- c("T2LRDeath", "T2DRDeath", "T2NPDeath")
mclapply(1:3,
function(i){sum.na(dat3[var[i]])},
mc.cores = 4L)
# [[1]]
# [1] 100
#
# [[2]]
# [1] 105
#
# [[3]]
# [1] 106
#################################################################
## Recode LRDeath.ER ##
#################################################################
c1 <- with(dat3, formula("is.na(Local.Recurrence) & ER.LR == 'Negative' & Death == 'No' & is.na(T2LR) & is.na(T2Death) ~ as.character(NA)")) # NA:Negative:No:noT2LR:noT2D:noT2LFU & # NA:Negative:No:noT2LR:noT2D:T2LFU
c2 <- with(dat3, formula("is.na(Local.Recurrence) & ER.LR == 'Negative' & Death == 'Yes' & is.na(T2LR) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # NA:Negative:Yes:noT2LR:T2D:T2LFU
c3 <- with(dat3, formula("Local.Recurrence == 'No' & is.na(ER.LR) & Death == 'No' & is.na(T2LR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # No:NA:No:noT2LR:noT2D:T2LFU
c4 <- with(dat3, formula("Local.Recurrence == 'No' & ER.LR == 'Negative' & Death == 'No' & is.na(T2LR) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA)")) # No:Negative:No:noT2LR:noT2D:noT2LFU
c5 <- with(dat3, formula("Local.Recurrence == 'Yes' & ER.LR %in% c(NA, 'Positive') & Death == 'No' & is.na(T2LR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # Yes:NA:No:noT2LR:noT2D:T2LFU & Yes:Positive:No:noT2LR:noT2D:T2LFU
c6 <- with(dat3, formula("Local.Recurrence == 'Yes' & is.na(ER.LR) & Death == 'Yes' & !is.na(T2LR) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # Yes:NA:Yes:T2LR:T2D:T2LFU
dat3 <- dat3 %>%
mutate(LRDeath.ER = case_when(c1, c2, c3, c4, c5, c6, # Conditions for patients not at risk
Local.Recurrence == "Yes" & ER.LR == "Positive" & Death %in% c("Yes", "No") ~ "LRYesERPos",
Local.Recurrence == "Yes" & ER.LR == "Negative" & Death %in% c("Yes", "No") ~ "LRYesERNeg",
Death == "Yes" & Local.Recurrence == "No" ~ "Dead",
Local.Recurrence == "No" & Death == "No" & !is.na(T2LFU) ~ "Censored") %>%
factor(levels = c("Censored", "LRYesERPos", "LRYesERNeg", "Dead")))
table(dat3$LRDeath.ER, useNA = "ifany")
# Censored LRYesERPos LRYesERNeg Dead
# 407 8 61 15
# <NA>
# 113
with(dat3, table(paste(LRDeath.ER,
ifelse(is.na(T2LR), "noT2LR", "T2LR"),
ifelse(is.na(T2Death), "noT2D", "T2D"),
ifelse(is.na(T2LFU), "noT2LFU", "T2LFU"),
sep = ":")))
# Censored:noT2LR:noT2D:T2LFU Dead:noT2LR:T2D:T2LFU
# 407 15
# LRYesERNeg:T2LR:noT2D:T2LFU LRYesERNeg:T2LR:T2D:T2LFU
# 54 7
# LRYesERPos:T2LR:noT2D:T2LFU LRYesERPos:T2LR:T2D:T2LFU
# 7 1
# NA:noT2LR:noT2D:noT2LFU NA:noT2LR:noT2D:T2LFU
# 18 89
# NA:noT2LR:T2D:T2LFU NA:T2LR:T2D:T2LFU
# 5 1
#################################################################
## Recode LRDeath.PR ##
#################################################################
c1 <- with(dat3, formula("is.na(Local.Recurrence) & PR.LR == 'Negative' & Death == 'No' & is.na(T2LR) & is.na(T2Death) ~ as.character(NA)")) # NA:Negative:No:noT2LR:noT2D:noT2LFU & # NA:Negative:No:noT2LR:noT2D:T2LFU
c2 <- with(dat3, formula("is.na(Local.Recurrence) & PR.LR == 'Negative' & Death == 'Yes' & is.na(T2LR) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # NA:Negative:Yes:noT2LR:T2D:T2LFU
c3 <- with(dat3, formula("Local.Recurrence == 'No' & is.na(PR.LR) & Death == 'No' & is.na(T2LR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # No:NA:No:noT2LR:noT2D:T2LFU
c4 <- with(dat3, formula("Local.Recurrence == 'No' & PR.LR == 'Negative' & Death == 'No' & is.na(T2LR) & is.na(T2Death) & is.na(T2LFU) ~ as.character(NA)")) # No:Negative:No:noT2LR:noT2D:noT2LFU
c5 <- with(dat3, formula("Local.Recurrence == 'Yes' & PR.LR %in% c(NA, 'Negative') & Death == 'No' & is.na(T2LR) & is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # Yes:NA:No:noT2LR:noT2D:T2LFU & Yes:Negative:No:noT2LR:noT2D:T2LFU
c6 <- with(dat3, formula("Local.Recurrence == 'Yes' & is.na(PR.LR) & Death == 'Yes' & !is.na(T2LR) & !is.na(T2Death) & !is.na(T2LFU) ~ as.character(NA)")) # Yes:NA:Yes:T2LR:T2D:T2LFU
dat3 <- dat3 %>%