-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_post_admission_mortality.R
142 lines (107 loc) · 4.03 KB
/
09_post_admission_mortality.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
# Load packages ----
library(tidyverse)
library(finalfit)
library(survival)
library(survminer)
library(gtsummary)
# Source parameters ----
source("timestamp.R")
source("resource_parameters.R")
source("myfunctions.R")
# Create folder for outputs -----
dir.create(here::here("02_outputs", "05_postadm_mortality"), recursive = TRUE,
showWarnings = FALSE)
# Create folder rds plots -----
dir.create(here::here("03_plots_rds", "postadm_mortality"), recursive = TRUE,
showWarnings = FALSE)
# Load datasets ----
cohort_index = read_rds(
here::here("01_data", paste0("cohort_index_", timestamp, ".rds")))
cluster_labeled = read_rds(
here::here("01_data", paste0("cluster_labeled_", timestamp, ".rds")))
resource_emerg_2yr_prior = read_rds(
here::here("01_data", paste0("resource_emerg_2yr_prior_", timestamp, ".rds")))
## Join to cohort cluster and prior emergency data ----
cohort_index = cohort_index %>%
left_join(cluster_labeled, by = "PatientID") %>%
left_join(resource_emerg_2yr_prior, by = "PatientID")
# Calculate days to mortality and mortality flag -----
cohort_index = cohort_index %>%
mutate(
mort_status = case_when(
date_of_death <= censor_date ~ 1, # Death event
TRUE ~ 0), # Alive and censored
mort_days = (censor_date - admission_date) %>% as.numeric()
)
# Extract lables -----
vlabel = extract_variable_label(cohort_index)
# Create overall group ----
cohort_index = cohort_index %>%
mutate(overall = "Overall" %>%
ff_label(" "))
# Post discharge mortality Kaplan Meier ----------------
## Stratifying variables ----
var_statifaction = c(
"overall", "age.factor", "sex",
"n_comorb_charl.factor", "any_icu", "wave", "vacc_status_index",
"class_factor", "prior_emergency_beddays_factor"
)
## KM parameters ----
ftime = cohort_index$mort_days
fstatus = cohort_index$mort_status
## Create Kaplan Meire plots ----
var_statifaction %>%
walk(function(var_strat){
# Vector of group variable
group = cohort_index %>% pull(all_of(var_strat))
if(var_strat == "class_factor" | var_strat == "prior_emergency_beddays_factor"){
legend_rows = 2
} else {legend_rows = 1}
# Create K-M plot
plot_km = ff_km_plot(ftime, fstatus, group, legend_rows = legend_rows)
# Save plot as jpeg
ggsave(filename = here::here("02_outputs", "05_postadm_mortality",
paste0("plot_postadm_km_", var_strat, ".jpeg")),
plot = plot_km,
width = 7, height = 5, dpi = 600)
# Save plot as rds
write_rds(plot_km,
here::here("03_plots_rds", "postadm_mortality",
paste0("plot_postadm_km_", var_strat, ".rds")))
})
## Create Kaplan Meire table ----
tbl_postadm_km = var_statifaction %>%
map(function(var_strat){
# Vector of group variable
group = cohort_index %>% pull(all_of(var_strat))
# Temp tibble ----
data_temp = tibble(
ftime = cohort_index$mort_days,
fstatus = cohort_index$mort_status,
group = group
)
# Survfit model ----
survfit = surv_fit(Surv(ftime, fstatus) ~ group, data = data_temp)
# Summary of survfit -----
summary_survfit = summary(survfit, times = c(0, 30, 60, 90, 180, 365))
if(var_strat == "overall"){summary_survfit$strata = "Overall"}
# Output table ------
tbl_output = tibble(
level = summary_survfit$strata %>% str_replace("^group=", ""),
time = summary_survfit$time,
n_risk = summary_survfit$n.risk,
cumulative_event = cumsum(summary_survfit$n.event),
cumulative_censor = cumsum(summary_survfit$n.censor),
surv = summary_survfit$surv,
surv_lower = summary_survfit$lower,
surv_upper = summary_survfit$upper
) %>%
mutate(var = var_strat,
var_desc = vlabel[var_strat]
) %>%
relocate(var, var_desc)
}) %>%
bind_rows()
## Save table ----
write_csv(tbl_postadm_km,
here::here("02_outputs", "05_postadm_mortality", "tbl_postadm_km.csv"))