-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.R
1564 lines (1326 loc) · 58.3 KB
/
global.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
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
#### PACKAGES -----
library(shiny)
library(shinydashboard)
library(shinythemes)
library(dplyr)
library(readr)
library(here)
library(stringr)
library(DT)
library(shinycssloaders)
library(shinyWidgets)
library(gt)
library(scales)
library(kableExtra)
library(tidyr)
library(stringr)
library(ggplot2)
library(fresh)
library(plotly)
library(ggalt)
library(bslib)
library(PatientProfiles)
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)
library(CDMConnector)
library(CirceR)
library(rjson)
library(rclipboard)
library(forcats)
library(gtsummary)
library(tidyverse)
library(zoo)
mytheme <- create_theme(
adminlte_color(
light_blue = "#605ca8"
),
adminlte_sidebar(
dark_bg = "#78B7C5", # "#D8DEE9",
dark_hover_bg = "#3B9AB2", #"#81A1C1",
dark_color ="white" ,
dark_submenu_bg = "#605ca8"
),
adminlte_global(
content_bg = "#eaebea"
),
adminlte_vars(
border_color = "black",
active_link_hover_bg = "#FFF",
active_link_hover_color = "#112446",
active_link_hover_border_color = "#112446",
link_hover_border_color = "#112446",
table_border_color = "black"
)
)
# format markdown
formatMarkdown <- function(x) {
lines <- strsplit(x, "\r\n\r\n") |> unlist()
getFormat <- function(line) {
if (grepl("###", line)) {return(h3(gsub("###", "", line)))}
else {h4(line)}
}
purrr::map(lines, ~ getFormat(.))
}
# printing numbers with 1 decimal place and commas
nice.num<-function(x) {
trimws(format(round(x,1),
big.mark=",", nsmall = 1, digits=1, scientific=FALSE))}
# printing numbers with 2 decimal place and commas
nice.num2<-function(x) {
trimws(format(round(x,2),
big.mark=",", nsmall = 2, digits=2, scientific=FALSE))}
# printing numbers with 3 decimal place and commas
nice.num3<-function(x) {
trimws(format(round(x,3),
big.mark=",", nsmall = 3, digits=3, scientific=FALSE))}
# printing numbers with 4 decimal place and commas
nice.num4<-function(x) {
trimws(format(round(x,4),
big.mark=",", nsmall = 4, digits=4, scientific=FALSE))}
# for counts- without decimal place
nice.num.count<-function(x) {
trimws(format(x,
big.mark=",", nsmall = 0, digits=1, scientific=FALSE))}
#### Load and extract data -----
results <-list.files(here("data"), full.names = TRUE,
recursive = TRUE,
include.dirs = TRUE,
pattern = ".zip")
#unzip data
for (i in (1:length(results))) {
utils::unzip(zipfile = results[[i]],
exdir = here("data"))
}
#grab the results from the folders
results <- list.files(
path = here("data"),
pattern = ".csv",
full.names = TRUE,
recursive = TRUE,
include.dirs = TRUE
)
# age standization for survival
# read in ICCS_1 values (for all cancers in this study apart from prostate)
ICSS_1 <- readr::read_csv(here("www", "ICSS_1.csv"),
show_col_types = FALSE)
# # read in ICCS values (for prostate)
ICSS_prostate <- readr::read_csv(here("www", "ICSS_prostate.csv"),
show_col_types = FALSE)
age_stds <- ICSS_1 %>%
mutate(Age = case_when(
Age %in% c("0-14", "15-19") ~ "0 to 19",
Age %in% c("20-24", "25-29", "30-34", "35-39") ~ "18 to 39",
Age %in% c("40-44", "45-49") ~ "40 to 49",
Age %in% c("50-54", "55-59") ~ "50 to 59",
Age %in% c("60-64", "65-69") ~ "60 to 69",
Age %in% c("70-74", "75-79") ~ "70 to 79",
Age %in% c("80-84", "85+") ~ "80 +"
)) %>%
group_by(Age) %>%
filter(Age != "0 to 19") %>%
summarise(ICSS = sum(ICSS)/100000)
age_stds_prostate <- ICSS_prostate %>%
filter(Age != "All") %>%
mutate(Age = case_when(
Age %in% c("0-14") ~ "18 to 39",
Age %in% c("15-54 years") ~ "40 to 49",
Age %in% c("55-64 years") ~ "50 to 59",
Age %in% c("65-74 years") ~ "60 to 69",
Age %in% c("75-84 years") ~ "70 to 79",
Age %in% c("85+ years") ~ "80 +"
)) %>%
group_by(Age) %>%
summarise(ICSS = sum(ICSS)/100000)
# database details
database_details <- read_csv(here::here("www", "database_details.csv"), show_col_types = FALSE)
# clinical code lists
cohort_set <- CDMConnector::read_cohort_set(here::here(
"www", "cohorts" ))
cohort_set$markdown <- ""
for (n in row_number(cohort_set) ) {
cohort <- cohort_set$cohort_name[n]
json <- paste0(cohort_set$json[n] )
cohortExpresion <- CirceR::cohortExpressionFromJson(json)
markdown <- CirceR::cohortPrintFriendly(cohortExpresion)
cohort_set$markdown[n] <- markdown
}
# Get concept ids from a provided path to cohort json files
# in dataframe
# Get a list of JSON files in the directory
json_files <- list.files(path = here("www", "cohorts"), pattern = "\\.json$", full.names = TRUE)
concept_lists_temp <- list()
concept_lists <- list()
concept_sets <- list()
if(length(json_files > 0)){
for(i in seq_along(json_files)){
concept_lists_temp[[i]] <- fromJSON(file = json_files[[i]])
}
for(i in 1:length(concept_lists_temp)){
for(k in 1:length(concept_lists_temp[[i]]$ConceptSets[[1]]$expression$items)){
concept_sets[[k]] <- bind_rows(concept_lists_temp[[i]]$ConceptSets[[1]]$expression$items[[k]]$concept)
}
concept_lists[[i]] <- bind_rows(concept_sets) %>%
mutate(name = concept_lists_temp[[i]]$ConceptSets[[1]]$name)
}
concept_sets_final <- bind_rows(concept_lists) %>%
mutate(name = case_when(
name == "Breast" ~ "incidentbreastcancer",
name == "Colorectal" ~ "incidentcolorectalcancer" ,
name == "Head_and_neck" ~ "incidentheadneckcancer" ,
name == "Liver" ~ "incidentlivercancer" ,
name == "Lung" ~ "incidentlungcancer" ,
name == "Pancreas" ~ "incidentpancreaticcancer" ,
name == "Prostate" ~ "incidentprostatecancer" ,
name == "Stomach" ~ "incidentstomachcancer" ,
TRUE ~ name
))
}
rm(concept_lists)
rm(concept_lists_temp)
rm(concept_sets)
# survival estimates
survival_estimates_files <- results[stringr::str_detect(results, ".csv")]
survival_estimates_files <- results[stringr::str_detect(results, "survival_estimates")]
survival_estimates <- list()
for(i in seq_along(survival_estimates_files)){
survival_estimates[[i]]<-readr::read_csv(survival_estimates_files[[i]],
show_col_types = FALSE)
}
survival_estimates <- dplyr::bind_rows(survival_estimates) %>%
dplyr::mutate(Cancer = replace(Cancer, Cancer == "Head_and_neck", "Head and Neck")) %>%
dplyr::mutate(Cancer = replace(Cancer, Cancer == "Pancreatic", "Pancreas")) %>%
dplyr::mutate(Database = replace(Database, Database == "CPRD_GOLD", "CPRD GOLD")) %>%
dplyr::mutate(Database = replace(Database, Database == "HUS2000wtrunc", "HUS")) %>%
dplyr::mutate(Database = replace(Database, Database == "ECI", "ECi")) %>%
left_join(database_details %>% select(Database, database_type), by = "Database") %>%
dplyr::mutate(Database = replace(Database, Database == "CPRD GOLD", "CPRD GOLD (UK)")) %>%
dplyr::mutate(Database = replace(Database, Database == "CRN", "CRN (Norway)")) %>%
dplyr::mutate(Database = replace(Database, Database == "ECi", "ECi (Scotland)")) %>%
dplyr::mutate(Database = replace(Database, Database == "GCR", "GCR (Switzerland)")) %>%
dplyr::mutate(Database = replace(Database, Database == "HUVM", "HUVM (Spain)")) %>%
dplyr::mutate(Database = replace(Database, Database == "IMASIS", "IMASIS (Spain)")) %>%
dplyr::mutate(Database = replace(Database, Database == "IPCI", "IPCI (Netherlands)")) %>%
dplyr::mutate(Database = replace(Database, Database == "NCR", "NCR (Netherlands)")) %>%
dplyr::mutate(Database = replace(Database, Database == "SIDIAP", "SIDIAP (Spain)")) %>%
dplyr::mutate(Database = replace(Database, Database == "ULSM", "ULSM (Portugal)")) %>%
dplyr::mutate(Database = replace(Database, Database == "ULSGE", "ULSGE (Portugal)")) %>%
dplyr::mutate(Database = replace(Database, Database == "ULSEDV", "ULSEDV (Portugal)")) %>%
dplyr::mutate(Database = replace(Database, Database == "ULSRA", "ULSRA (Portugal)")) %>%
dplyr::mutate(Database = replace(Database, Database == "UTARTU", "UTARTU (Estonia)")) %>%
dplyr::mutate(Database = replace(Database, Database == "HUS", "HUS (Finland)"))
survival_estimates_prostate <- survival_estimates %>%
filter(Cancer == "Prostate") %>%
mutate(Sex = "Both")
survival_estimates_ECI <- survival_estimates %>%
filter(Database == "ECi (Scotland)") %>%
dplyr::mutate(Sex = replace(Sex, Sex == "Both", "Female"))
# survival_estimates <- bind_rows(survival_estimates,
# survival_estimates_ECI,
# survival_estimates_prostate)
survival_estimates <- bind_rows(survival_estimates,
survival_estimates_ECI,
survival_estimates_prostate) %>%
filter(Database != "ULSM (Portugal)")
# for ULSM for breast cancer
# survival_estimates <- survival_estimates %>%
# filter(!(Database == "ULSM" & Cancer == "Breast" & Sex == "Female" & Method == "Kaplan-Meier") & time <= 17)
rm(survival_estimates_prostate,
survival_estimates_ECI)
# Function to standardize survival estimates for all cancers apart from prostate
standardize_survival <- function(data_partner, cancer_type, data, weights) {
# Filter the data
filtered_data <- data %>%
filter(Database == data_partner, Cancer == cancer_type)
# Check if there is data to process
if (nrow(filtered_data) == 0) {
return(tibble(
time = numeric(0),
Cancer = character(0),
Database = character(0),
database_type = character(0),
weighted_est = numeric(0),
weighted_lcl = numeric(0),
weighted_ucl = numeric(0)
))
}
# Ensure required columns are present
required_cols <- c("time", "Age", "est", "lcl", "ucl", "Cancer", "Database", "database_type")
missing_cols <- setdiff(required_cols, colnames(filtered_data))
if (length(missing_cols) > 0) {
stop(paste("Missing columns in filtered data:", paste(missing_cols, collapse = ", ")))
}
fill_na_locf_mean <- function(x) {
# Carry forward the last observation
forward_fill <- na.locf(x, na.rm = FALSE)
# Carry backward the next observation
backward_fill <- na.locf(x, fromLast = TRUE, na.rm = FALSE)
# Calculate the mean of forward and backward fills
filled_mean <- rowMeans(cbind(forward_fill, backward_fill), na.rm = TRUE)
return(filled_mean)
}
# Perform the standardization process
result <- filtered_data %>%
select(time, Age, est, lcl, ucl, Cancer, Database, database_type) %>%
pivot_wider(names_from = Age, values_from = c(est, lcl, ucl)) %>%
arrange(time) %>%
# calculates the mean between two points (recommended)
mutate(across(starts_with("est_"), ~ fill_na_locf_mean(.x))) %>%
mutate(across(starts_with("lcl_"), ~ fill_na_locf_mean(.x))) %>%
mutate(across(starts_with("ucl_"), ~ fill_na_locf_mean(.x))) %>%
# # calculates a value based on the linear relationship between two data points
# mutate(across(starts_with("est_"), ~ na.approx(.x, na.rm = FALSE))) %>%
# mutate(across(starts_with("lcl_"), ~ na.approx(.x, na.rm = FALSE))) %>%
# mutate(across(starts_with("ucl_"), ~ na.approx(.x, na.rm = FALSE))) %>%
# original bringing last value carried forward
# mutate(across(starts_with("est_"), ~ na.locf(.x, na.rm = FALSE))) %>%
# mutate(across(starts_with("lcl_"), ~ na.locf(.x, na.rm = FALSE))) %>%
# mutate(across(starts_with("ucl_"), ~ na.locf(.x, na.rm = FALSE))) %>%
distinct(across(-c(time, Cancer, Database, database_type)), .keep_all = TRUE) %>%
pivot_longer(cols = -c(time, Cancer, Database, database_type), names_to = c(".value", "Age"), names_sep = "_") %>%
left_join(weights, by = "Age") %>%
group_by(time, Cancer, Database, database_type) %>%
summarize(
weighted_est = sum(est * ICSS, na.rm = TRUE) / sum(ICSS, na.rm = TRUE),
weighted_lcl = sum(lcl * ICSS, na.rm = TRUE) / sum(ICSS, na.rm = TRUE),
weighted_ucl = sum(ucl * ICSS, na.rm = TRUE) / sum(ICSS, na.rm = TRUE),
.groups = 'drop'
)
# Add a row with time = 0, est = 1, lcl = 1, ucl = 1
initial_row <- tibble(
time = 0,
Cancer = unique(result$Cancer),
Database = unique(result$Database),
database_type = unique(result$database_type),
weighted_est = 1,
weighted_lcl = 1,
weighted_ucl = 1
)
# Bind the initial row to the result
result <- bind_rows(initial_row, result)
# Remove rows with time > 0 but less than 0.5
result <- result %>%
filter(!(time > 0 & time < 0.5))
return(result)
}
# Preprocess the data
survival_estimates_test <- survival_estimates %>%
filter(Age != "All") %>%
filter(Sex == "Both") %>%
filter(Method == "Kaplan-Meier") %>%
filter(Cancer != "Prostate")
# Generate combinations of data partners and cancer types actually present in the data
available_combinations <- survival_estimates_test %>%
select(Database, Cancer) %>%
distinct()
# Apply the function to each combination
standardized_results <- available_combinations %>%
pmap_df(function(Database, Cancer) {
standardize_survival(Database, Cancer, survival_estimates_test, age_stds)
}) %>%
rename(est = weighted_est,
lcl = weighted_lcl,
ucl = weighted_ucl) %>%
mutate(Method = "Kaplan-Meier",
Age = "Age Standardized",
Sex = "Both",
Stratification = "None",
Adjustment = "None",
Truncated = "No")
# age standardization for prostate
# Preprocess the data
survival_estimates_p <- survival_estimates %>%
filter(Age != "All") %>%
filter(Age != "18 to 39") %>%
filter(Age != "40 to 49") %>%
filter(Sex == "Both") %>%
filter(Method == "Kaplan-Meier") %>%
filter(Cancer == "Prostate")
# Generate combinations of data partners and cancer types actually present in the data
available_combinations_p <- survival_estimates_p %>%
select(Database, Cancer) %>%
distinct()
# Apply the function to each combination
standardized_results_prostate <- available_combinations_p %>%
pmap_df(function(Database, Cancer) {
standardize_survival(Database, Cancer, survival_estimates_p, age_stds_prostate)
}) %>%
rename(est = weighted_est,
lcl = weighted_lcl,
ucl = weighted_ucl) %>%
mutate(Method = "Kaplan-Meier",
Age = "Age Standardized",
Sex = "Both",
Stratification = "None",
Adjustment = "None",
Truncated = "No")
standardized_results_prostate_m <- standardized_results_prostate %>%
mutate(Sex = "Male")
# females age stds
survival_estimates_f <- survival_estimates %>%
filter(Age != "All") %>%
filter(Sex == "Female") %>%
filter(Age != "18 to 39") %>%
filter(Method == "Kaplan-Meier") %>%
filter(Cancer != "Breast")
# Generate combinations of data partners and cancer types actually present in the data
available_combinations <- survival_estimates_f %>%
select(Database, Cancer) %>%
distinct()
# Apply the function to each combination
standardized_results_f <- available_combinations %>%
pmap_df(function(Database, Cancer) {
standardize_survival(Database, Cancer, survival_estimates_f, age_stds)
}) %>%
rename(est = weighted_est,
lcl = weighted_lcl,
ucl = weighted_ucl) %>%
mutate(Method = "Kaplan-Meier",
Age = "Age Standardized",
Sex = "Female",
Stratification = "None",
Adjustment = "None",
Truncated = "No")
# Males age stds
survival_estimates_m <- survival_estimates %>%
filter(Age != "All") %>%
filter(Sex == "Male") %>%
filter(Age != "18 to 39") %>%
filter(Method == "Kaplan-Meier") %>%
filter(Cancer != "Prostate") %>%
filter(Cancer != "Breast")
# Generate combinations of data partners and cancer types actually present in the data
available_combinations <- survival_estimates_m %>%
select(Database, Cancer) %>%
distinct()
# Apply the function to each combination
standardized_results_m <- available_combinations %>%
pmap_df(function(Database, Cancer) {
standardize_survival(Database, Cancer, survival_estimates_m, age_stds)
}) %>%
rename(est = weighted_est,
lcl = weighted_lcl,
ucl = weighted_ucl) %>%
mutate(Method = "Kaplan-Meier",
Age = "Age Standardized",
Sex = "Male",
Stratification = "None",
Adjustment = "None",
Truncated = "No")
# remove males breast cancer due to small numbers
standardized_results_m <- standardized_results_m %>%
filter(!(Cancer == "Breast" & !(Database %in% c("SIDIAP (Spain)"))))
# females age stds breast
survival_estimates_f_breast <- survival_estimates %>%
filter(Age != "All") %>%
filter(Sex == "Female") %>%
filter(Age != "18 to 39") %>%
filter(Age != "40 to 49") %>%
filter(Method == "Kaplan-Meier") %>%
filter(Cancer == "Breast")
# Generate combinations of data partners and cancer types actually present in the data
available_combinations <- survival_estimates_f_breast %>%
select(Database, Cancer) %>%
distinct()
# Apply the function to each combination
standardized_results_f_breast <- available_combinations %>%
pmap_df(function(Database, Cancer) {
standardize_survival(Database, Cancer, survival_estimates_f_breast, age_stds)
}) %>%
rename(est = weighted_est,
lcl = weighted_lcl,
ucl = weighted_ucl) %>%
mutate(Method = "Kaplan-Meier",
Age = "Age Standardized",
Sex = "Female",
Stratification = "None",
Adjustment = "None",
Truncated = "No")
# Males age stds breast
survival_estimates_m_breast <- survival_estimates %>%
filter(Age != "All") %>%
filter(Sex == "Male") %>%
filter(Age != "18 to 39") %>%
filter(Age != "40 to 49") %>%
filter(Method == "Kaplan-Meier") %>%
filter(Cancer == "Breast")
# Generate combinations of data partners and cancer types actually present in the data
available_combinations <- survival_estimates_m_breast %>%
select(Database, Cancer) %>%
distinct()
# Apply the function to each combination
standardized_results_m_breast <- available_combinations %>%
pmap_df(function(Database, Cancer) {
standardize_survival(Database, Cancer, survival_estimates_m_breast, age_stds)
}) %>%
rename(est = weighted_est,
lcl = weighted_lcl,
ucl = weighted_ucl) %>%
mutate(Method = "Kaplan-Meier",
Age = "Age Standardized",
Sex = "Male",
Stratification = "None",
Adjustment = "None",
Truncated = "No")
# males and females only ages 40+ and for prostrate and breast from 50+
survival_estimates <- bind_rows(
survival_estimates,
standardized_results,
standardized_results_f,
standardized_results_f_breast,
standardized_results_m_breast,
standardized_results_m,
standardized_results_prostate_m,
standardized_results_prostate)
# extract out ECI for females for just 50 to 59 (Both sex contains all age groups)
survival_estimates_ECI <-
standardized_results_f_breast %>%
filter(Database == "ECi (Scotland)") %>%
mutate(Sex = "Female")
survival_estimates <- bind_rows(survival_estimates,
survival_estimates_ECI
)
# risk tables ----------
survival_risk_table_files <- results[stringr::str_detect(results, ".csv")]
survival_risk_table_files <- results[stringr::str_detect(results, "risk_table")]
survival_risk_table <- list()
for(i in seq_along(survival_risk_table_files)){
survival_risk_table[[i]]<-readr::read_csv(survival_risk_table_files[[i]],
show_col_types = FALSE) %>%
mutate_if(is.double, as.character)
}
survival_risk_table <- dplyr::bind_rows(survival_risk_table) %>%
dplyr::mutate(Cancer = replace(Cancer, Cancer == "Head_and_neck", "Head and Neck")) %>%
dplyr::mutate(Cancer = replace(Cancer, Cancer == "Pancreatic", "Pancreas")) %>%
dplyr::mutate(Database = replace(Database, Database == "CPRD_GOLD", "CPRD GOLD")) %>%
dplyr::mutate(Database = replace(Database, Database == "ECI", "ECi")) %>%
dplyr::mutate(Database = replace(Database, Database == "HUS2000wtrunc", "HUS")) %>%
select(-c("Method", "Stratification", "Adjustment" )) %>%
relocate(Database, .before = 1) %>%
filter(details != "n.censor")
survival_risk_table_ECI <- survival_risk_table %>%
filter(Database == "ECi") %>%
dplyr::mutate(Sex = replace(Sex, Sex == "Both", "Female"))
survival_risk_table_prostate <- survival_risk_table %>%
filter(Cancer == "Prostate") %>%
mutate(Sex = "Both")
survival_risk_table <- bind_rows(survival_risk_table,
survival_risk_table_ECI,
survival_risk_table_prostate) %>%
mutate_all(~ ifelse(is.na(.), "-", .)) %>%
filter(Database != "ULSM")
rm(survival_risk_table_prostate,
survival_risk_table_ECI
)
# median and survival probabilities ------
survival_median_files <- results[stringr::str_detect(results, ".csv")]
survival_median_files <- results[stringr::str_detect(results, "median_mean")]
survival_median_table <- list()
for(i in seq_along(survival_median_files)){
suppressWarnings(
survival_median_table[[i]]<-readr::read_csv(survival_median_files[[i]],
show_col_types = FALSE) %>%
mutate(n = as.character(n),
events = as.character(events))
)
}
survival_median_table <- dplyr::bind_rows(survival_median_table) %>%
dplyr::mutate(Cancer = replace(Cancer, Cancer == "Pancreatic", "Pancreas")) %>%
dplyr::mutate(Database = replace(Database, Database == "CPRD_GOLD", "CPRD GOLD")) %>%
dplyr::mutate(Database = replace(Database, Database == "ECI", "ECi")) %>%
dplyr::mutate(Database = replace(Database, Database == "HUS2000wtrunc", "HUS")) %>%
left_join(database_details %>% select(Database, database_type), by = "Database") %>%
filter(Truncated != "Yes", Method == "Kaplan-Meier") %>%
relocate(Database, .before = 1) %>%
mutate(
"1-year Survival (95% CI)"= ifelse(!is.na(`surv year 1`),
paste0(paste0(nice.num(`surv year 1`)), " (",
paste0(nice.num(`lower year 1`)),"-",
paste0(nice.num(`upper year 1`)), ")"),
NA),
"5-year Survival (95% CI)"= ifelse(!is.na(`surv year 5`),
paste0(paste0(nice.num(`surv year 5`)), " (",
paste0(nice.num(`lower year 5`)),"-",
paste0(nice.num(`upper year 5`)), ")"),
NA) ,
"10-year Survival (95% CI)"= ifelse(!is.na(`surv year 10`),
paste0(paste0(nice.num(`surv year 10`)), " (",
paste0(nice.num(`lower year 10`)),"-",
paste0(nice.num(`upper year 10`)), ")"),
NA) ,
"Median Survival (95% CI)" = ifelse(!is.na(median),
paste0(paste0(nice.num(median)), " (",
paste0(nice.num(lower_median)),"-",
paste0(nice.num(upper_median)), ")"),
NA) ,
"Mean Survival (SE)" = ifelse(!is.na(rmean),
paste0(paste0(nice.num2(rmean)), " (",
paste0(nice.num2(se)), ")"),
NA),
"Mean Survival 5 years (SE)" = ifelse(!is.na(rmean5yr),
paste0(paste0(nice.num2(rmean5yr)), " (",
paste0(nice.num2(se5yr)), ")"),
NA),
"Mean Survival 10 years (SE)" = ifelse(!is.na(rmean10yr),
paste0(paste0(nice.num2(rmean10yr)), " (",
paste0(nice.num2(se10yr)), ")"),
NA)
) %>%
select(!c(Adjustment,
Stratification, Truncated
))
survival_median_table_prostate <- survival_median_table %>%
filter(Cancer == "Prostate") %>%
mutate(Sex = "Both")
survival_median_table_ECI <- survival_median_table %>%
filter(Database == "ECi") %>%
dplyr::mutate(Sex = replace(Sex, Sex == "Both", "Female"))
survival_median_table <- bind_rows(survival_median_table,
survival_median_table_ECI,
survival_median_table_prostate) %>%
filter(Database != "ULSM")
# survival_median_table <- bind_rows(survival_median_table,
# survival_median_table_ECI,
# survival_median_table_prostate)
rm(survival_median_table_prostate)
# table one ------
tableone_whole_files <- results[stringr::str_detect(results, ".csv")]
tableone_whole_files <- results[stringr::str_detect(results, "tableone")]
tableone_whole <- list()
for(i in seq_along(tableone_whole_files)){
tableone_whole[[i]] <- readr::read_csv(tableone_whole_files[[i]],
show_col_types = FALSE)
}
tableone_whole <- bind_rows(tableone_whole) %>%
dplyr::mutate(cdm_name = replace(cdm_name, cdm_name == "HUS2000", "HUS")) %>%
dplyr::mutate(cdm_name = replace(cdm_name, cdm_name == "ECI", "ECi")) %>%
dplyr::mutate(Cancer = replace(group_level, group_level == "Pancreatic", "Pancreas")) %>%
dplyr::mutate(cdm_name = replace(cdm_name, cdm_name == "CPRD_GOLD", "CPRD GOLD")) %>%
filter(estimate_type != "q05",
estimate_type != "q95",
estimate_type != "mean",
estimate_type != "sd") %>%
dplyr::mutate(variable_level = if_else(variable_level == "Obesitycharybdis",
"Obesity", variable_level)) %>%
dplyr::mutate(variable = if_else(variable == "age",
"Age", variable)) %>%
dplyr::mutate(variable = if_else(variable == "age_gr",
"Age group", variable)) %>%
dplyr::mutate(variable = if_else(variable == "cohort_end_date",
"Cohort end date", variable)) %>%
dplyr::mutate(variable = if_else(variable == "cohort_start_date",
"Cohort start date", variable)) %>%
dplyr::mutate(variable = if_else(variable == "future_observation",
"Future observation", variable)) %>%
dplyr::mutate(variable = if_else(variable == "number records",
"Number records", variable)) %>%
dplyr::mutate(variable = if_else(variable == "number subjects",
"Number subjects", variable)) %>%
dplyr::mutate(variable_level = if_else(variable_level == "18 To 39",
"18 to 39", variable_level)) %>%
dplyr::mutate(variable_level = if_else(variable_level == "40 To 49",
"40 to 49", variable_level)) %>%
dplyr::mutate(variable_level = if_else(variable_level == "50 To 59",
"50 to 59", variable_level)) %>%
dplyr::mutate(variable_level = if_else(variable_level == "60 To 69",
"60 to 69", variable_level)) %>%
dplyr::mutate(variable_level = if_else(variable_level == "70 To 79",
"70 to 79", variable_level)) %>%
dplyr::mutate(variable = if_else(variable == "sex",
"Sex", variable)) %>%
dplyr::mutate(variable = if_else(variable == "Outcome flag from 0 to 0",
"outcome", variable)) %>%
dplyr::mutate(group_level = if_else(group_level == "Overall",
"cohort_name", group_level)) %>%
dplyr::mutate(group_name = if_else(group_name == "cohort_name",
"Overall", group_name)) %>%
filter(!(variable == "Sex" & variable_level == "None")) %>%
mutate_all(~ str_replace_all(., "Head_and_neck", "Head and neck")) %>%
filter(variable != "Cohort end date" ) %>%
filter(variable != "Cohort start date" ) %>%
filter(variable != "Number records" ) %>%
filter(!(cdm_name == "HUVM" & variable == "Medications flag from -365 to 0")) %>%
filter(!(cdm_name == "NCR" & variable == "Medications flag from -365 to 0")) %>%
filter(!(cdm_name == "NCR" & variable == "Conditions flag from any time prior to 0"))
# cdm snapshot ------
snapshot_files <- results[stringr::str_detect(results, ".csv")]
snapshot_files <- results[stringr::str_detect(results, "cdm_snapshot")]
snapshotcdm <- list()
for(i in seq_along(snapshot_files)){
snapshotcdm[[i]] <- readr::read_csv(snapshot_files[[i]],
show_col_types = FALSE) %>%
mutate_all(as.character)
}
snapshotcdm <- bind_rows(snapshotcdm) %>%
select("cdm_name", "person_count", "observation_period_count" ,
"vocabulary_version", "cdm_version", "cdm_description", "StudyPeriodStartDate", "earliest_observation_period_start_date" ,) %>%
mutate(person_count = nice.num.count(person_count),
observation_period_count = nice.num.count(observation_period_count)) %>%
dplyr::mutate(cdm_name = replace(cdm_name, cdm_name == "CPRD_GOLD", "CPRD GOLD")) %>%
dplyr::mutate(cdm_name = replace(cdm_name, cdm_name == "ECI", "ECi")) %>%
dplyr::mutate(cdm_name = replace(cdm_name, cdm_name == "HUS2000", "HUS")) %>%
rename("Database" = "cdm_name",
"Persons in the cancer cohorts" = "person_count",
"Number of observation periods" = "observation_period_count",
"OMOP CDM vocabulary version" = "vocabulary_version",
"Database CDM Version" = "cdm_version",
"Database Description" = "cdm_description",
"Study Start Date" = "StudyPeriodStartDate",
"Database Start Date" = "earliest_observation_period_start_date" )
snapshotcdm <- full_join(snapshotcdm, database_details, by = "Database" ) %>%
relocate("Database Description", .after = last_col()) %>%
relocate("Full name", .after = `Database`)
snapshotcdm <- snapshotcdm %>%
mutate(`Database Description` = ifelse(`Database` == "HUVM",
"Virgen Macarena University Hospital provides hospital and community care services to 480,000 people. The hospital belongs to the Andalusian Public Health System as 3erd level hospital in Seville and Huelva areas (Spain). The hospital includes 37 medical specialties provided with state of the art technology for complex and advanced healthcare treatments. Its infrastructure includes 800 beds, 25 surgical theather distributed in 7 buildings. The hospital has 6000 professionals and its budget is more than €398 Million. The hospital currently participates in more than 435 in phase I, II and III clinical trials and produced scientific publications with 1187 impact factor points during last year. Our EHR system has been in use for more than a decade and it contains more than 10 million episodes and 1 million discharge summaries.",
`Database Description`))
snapshotcdm <- snapshotcdm %>%
distinct()
# attrition ----------
attrition_files <- results[stringr::str_detect(results, ".csv")]
attrition_files <- results[stringr::str_detect(results, "attrition")]
attritioncdm <- list()
for(i in seq_along(attrition_files)){
attritioncdm [[i]] <- readr::read_csv(attrition_files[[i]],
show_col_types = FALSE)
}
attritioncdm <- bind_rows(attritioncdm) %>%
dplyr::mutate(Cancer = replace(Cancer, Cancer == "Head_and_neck", "Head and Neck")) %>%
dplyr::mutate(Cancer = replace(Cancer, Cancer == "Pancreatic", "Pancreas")) %>%
dplyr::mutate(Database = replace(Database, Database == "CPRD_GOLD", "CPRD GOLD")) %>%
dplyr::mutate(Database = replace(Database, Database == "ECI", "ECi")) %>%
dplyr::mutate(Database = replace(Database, Database == "HUS2000", "HUS")) %>%
select(!c(cohort_definition_id))
attrition_summary <- attritioncdm %>%
group_by(Database, reason, reason_id) %>%
summarize(excluded_records = sum(excluded_records, na.rm = TRUE),
excluded_subjects = sum(excluded_subjects, na.rm = TRUE),
number_records = sum(number_records, na.rm = TRUE),
number_subjects = sum(number_subjects, na.rm = TRUE)) %>%
arrange(reason_id) %>%
mutate(Cancer = "Overall")
attritioncdm <- bind_rows(attritioncdm, attrition_summary)
# # only keep results for ECI breast
attritioncdm <- attritioncdm %>%
dplyr::filter(!(Database == "ECi" & Cancer != "Breast"))
# filter results for just km results
survival_km <- survival_estimates %>%
filter(Method == "Kaplan-Meier")
# Function to calculate age-standardized survival
calculate_age_standardized_survival <- function(data, standard_population) {
# Filter and summarize the survival rates by age groups
age_stds <- standard_population %>%
mutate(Age = case_when(
Age %in% c("0-14", "15-19") ~ "0 to 19",
Age %in% c("20-24", "25-29", "30-34", "35-39") ~ "18 to 39",
Age %in% c("40-44", "45-49") ~ "40 to 49",
Age %in% c("50-54", "55-59") ~ "50 to 59",
Age %in% c("60-64", "65-69") ~ "60 to 69",
Age %in% c("70-74", "75-79") ~ "70 to 79",
Age %in% c("80-84", "85+") ~ "80 +"
)) %>%
group_by(Age) %>%
filter(Age != "0 to 19") %>%
summarise(ICSS = sum(ICSS)/100000)
# Merge age-standardized survival to the original data
data <- data %>%
left_join(age_stds, by = c("Age")) %>%
mutate(age_standard_1year = `surv year 1` * ICSS,
age_standard_lower_1year = (`lower year 1`) * ICSS,
age_standard_upper_1year = (`upper year 1`) * ICSS,
age_standard_5year = `surv year 5` * ICSS,
age_standard_lower_5year = (`lower year 5`) * ICSS,
age_standard_upper_5year = (`upper year 5`) * ICSS,
age_standard_10year = `surv year 10` * ICSS,
age_standard_lower_10year = (`lower year 10`) * ICSS,
age_standard_upper_10year = (`upper year 10`) * ICSS,
age_standard_median = ifelse(!is.na(median) & !is.na(lower_median) & !is.na(upper_median), median * ICSS, NA),
age_standard_lower_median = ifelse(!is.na(median) & !is.na(lower_median) & !is.na(upper_median), lower_median * ICSS, NA),
age_standard_upper_median = ifelse(!is.na(median) & !is.na(lower_median) & !is.na(upper_median), upper_median * ICSS, NA),
age_standard_rmean = rmean * ICSS,
age_standard_se = se * ICSS,
age_standard_rmean5year = rmean5yr * ICSS,
age_standard_se5year = se5yr * ICSS,
age_standard_rmean10year = rmean10yr * ICSS,
age_standard_se10year = se10yr * ICSS
) # Calculate age-standardized survival
# Summarize to get total age-standardized survival
total_age_standard <- data %>%
summarise(Age = "Age Standardized",
`1-year Survival (95% CI)` =
paste0(round(sum(age_standard_1year, na.rm = TRUE), 1),
" (",
round(sum(age_standard_lower_1year, na.rm = TRUE),1),
"-",
round(sum(age_standard_upper_1year, na.rm = TRUE), 1),
")"),
`5-year Survival (95% CI)` =
paste0(round(sum(age_standard_5year, na.rm = TRUE), 1),
" (",
round(sum(age_standard_lower_5year, na.rm = TRUE),1),
"-",
round(sum(age_standard_upper_5year, na.rm = TRUE), 1),
")"),
`10-year Survival (95% CI)` =
paste0(round(sum(age_standard_10year, na.rm = TRUE), 1),
" (",
round(sum(age_standard_lower_10year, na.rm = TRUE),1),
"-",
round(sum(age_standard_upper_10year, na.rm = TRUE), 1),
")"),
`surv year 1` = sum(age_standard_1year, na.rm = TRUE) ,
`surv year 5` = sum(age_standard_5year, na.rm = TRUE) ,
`surv year 10` = sum(age_standard_10year, na.rm = TRUE) ,
`lower year 1` = sum(age_standard_lower_1year, na.rm = TRUE),
`lower year 5` = sum(age_standard_lower_5year, na.rm = TRUE),
`lower year 10` = sum(age_standard_lower_10year, na.rm = TRUE) ,
`upper year 1` = sum(age_standard_upper_1year, na.rm = TRUE),
`upper year 5` = sum(age_standard_upper_5year, na.rm = TRUE),
`upper year 10`= sum(age_standard_upper_10year, na.rm = TRUE) ,
`Median Survival (95% CI)` =
paste0(round(sum(age_standard_median, na.rm = TRUE), 1),
" (",
round(sum(age_standard_lower_median, na.rm = TRUE),1),
"-",
round(sum(age_standard_upper_median, na.rm = TRUE), 1),
")"),
`Mean Survival (SE)` =
paste0(round(sum(age_standard_rmean, na.rm = TRUE), 1),
" (",
round(
(sum(age_standard_rmean, na.rm = TRUE)) -
(sum(age_standard_se, na.rm = TRUE)) , 1 ) ,
"-",
round(
(sum(age_standard_rmean, na.rm = TRUE)) +
(sum(age_standard_se, na.rm = TRUE)) , 1 ) ,
")"),
`Mean Survival 5 years (SE)` =
paste0(round(sum(age_standard_rmean5year, na.rm = TRUE), 1),
" (",
round(
(sum(age_standard_rmean5year, na.rm = TRUE)) -
(sum(age_standard_se5year, na.rm = TRUE)) , 1 ) ,
"-",
round(
(sum(age_standard_rmean5year, na.rm = TRUE)) +
(sum(age_standard_se5year, na.rm = TRUE)) , 1 ) ,
")"),
`Mean Survival 10 years (SE)` =
paste0(round(sum(age_standard_rmean10year, na.rm = TRUE), 1),
" (",
round(
(sum(age_standard_rmean10year, na.rm = TRUE)) -
(sum(age_standard_se10year, na.rm = TRUE)) , 1 ) ,
"-",
round(
(sum(age_standard_rmean10year, na.rm = TRUE)) +
(sum(age_standard_se10year, na.rm = TRUE)) , 1 ) ,