generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_Strata_Summary.R
186 lines (144 loc) · 4.9 KB
/
04_Strata_Summary.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
######################################
# This script:
# - imports fitted stratified Cox model
# - reports cumulative baseline hazard for each strata
######################################
## Import libraries
library('tidyverse')
library('survival')
## Create output directory
dir.create(here::here("output", "models", "final"), showWarnings = FALSE, recursive=TRUE)
# function to get survival estimates for each strata over time
# basically like broom::tidy.coxph() but with a bit more stuff
tidy_surv <-
function(
survfit, # fitted survival model using coxph()
times = NULL, # return estimates for these times only. If NULL then uses all event times
addtimezero=FALSE # add estimates at time zero (ie where survival=1 and hazard=0)
) {
# tidy post-fit survival dataset, with extra estimates than provided by broom::tidy.coxph
strata <- names(survfit$strata)
mintime <- min(survfit$time)
timezero <- min(0, mintime-1)
output0 <- broom::tidy(survfit)
if (is.null(strata)){
output0$strata = "1"
}
output <- output0 %>%
group_by(strata) %>%
transmute(
strata,
time,
leadtime = lead(time),
interval = leadtime - time,
n.risk,
n.event,
n.censor,
estimate,
std.error,
conf.low, conf.high,
haz = -(estimate-lag(estimate, n=1L, default=1))/lag(estimate, n=1L, default=1),
cml.haz = cumsum(haz),
cml.haz2 = -log(estimate),
haz2 = cml.haz2-lag(cml.haz2, n=1L, default=0),
# log(-log()) scale
llsurv = log(-log(estimate)),
llsurv.low = log(-log(conf.low)),
llsurv.high = log(-log(conf.high)),
) %>%
ungroup()
if(!is.null(times)){
# this fills in (with constant interpolation) estimates for times where no events occurred
# it does NOT re-estimate hazard over the smaller time period
output <-
output %>%
group_by(strata) %>%
complete(
time = times,
fill = list(n.event = 0, n.censor = 0)
) %>%
fill(n.risk, .direction = c("up")) %>%
fill(
estimate, std.error, conf.low, conf.high,
haz, cml.haz, haz2, cml.haz2,
llsurv, llsurv.low, llsurv.high
) %>%
ungroup()
}
if(addtimezero){
time0row <- tibble(
time = timezero,
leadtime = mintime,
interval = leadtime-time,
estimate=1,
conf.low=1,
conf.high=1,
std.error=0,
haz=0,
cml.haz=0,
) %>%
right_join(
tibble(strata=strata, time=timezero),
by="time"
)
output <- bind_rows(
output,
time0row
) %>%
arrange(strata, time) %>%
fill(estimate, std.error, conf.low, conf.high, haz, cml.haz)
}
return(output)
}
## Import processed data
data_tte <- read_rds(here::here("output", "data", "data_modelling.rds"))
## Converts logical to integer so that model coefficients print nicely in gtsummary methods
data_cox <- data_tte %>%
mutate(
across(
where(is.logical),
~.x*1L
)
)
mod.strat.coxph.adj <- read_rds(here::here("output", "models", "final", "mod_strat_coxph_adj.rds"))
# get strata-specific estimates based on mean-centered covariates
# mean-centered doesn't make much sense but strata-specific cumulative hazard is multiplicative wrt covariates,
# so relative differences between strata are invariant
fit <- survfit(mod.strat.coxph.adj, conf.type="log-log")
# tidy output, with one estimate per time point
strata_estimates <- tidy_surv(fit, times=1:max(fit$time), addtimezero=TRUE)
plot_strata_cmlhaz <- ggplot(strata_estimates)+
geom_step(aes(x=time, y=cml.haz, group=strata), alpha=0.2)+
#geom_ribbon(aes(x=time, ymin=conf.low, ymax=conf.high, group=strata), colour="transparent", alpha=0.2)+
theme_bw()+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
ggsave(
here::here("output", "models", "final", "plot_strata_cmlhaz.svg"),
plot_strata_cmlhaz,
units = "cm", width = 20, height = 20
)
plot_strata_llsurv <- ggplot(strata_estimates)+
geom_step(aes(x=time, y=llsurv, group=strata), alpha=0.2)+
#geom_ribbon(aes(x=time, ymin=llsurv.low, ymax=llsurv.high, group=strata), colour="transparent", alpha=0.2)+
scale_x_log10()+
theme_bw()+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
ggsave(
here::here("output", "models", "final", "plot_strata_llsurv.svg"),
plot_strata_llsurv,
units = "cm", width = 20, height = 20
)
plot_strata_cmlhaz30 <- ggplot(strata_estimates %>% filter(time==30))+
geom_histogram(aes(x=cml.haz), alpha=0.2)+
theme_bw()
ggsave(
here::here("output", "models", "final", "plot_strata_cmlhaz30.svg"),
plot_strata_cmlhaz30,
units = "cm", width = 20, height = 20
)