-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreproducibility_script.R
More file actions
252 lines (187 loc) · 8.71 KB
/
reproducibility_script.R
File metadata and controls
252 lines (187 loc) · 8.71 KB
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
## ----setup, include=FALSE---------------------------------------------------------------------------------
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
## ---------------------------------------------------------------------------------------------------------
library(survival)
library(coxme)
library(frailtyEM)
library(tidyverse)
## ---------------------------------------------------------------------------------------------------------
dat <- read.csv("EORTC_10854.csv", stringsAsFactors = FALSE)
dat$periop <-
factor(dat$periop,
levels=c("no periop chemo","periop chemo"))
dat$surgery <-
factor(dat$surgery,
levels=c("mastectomy with RT","mastectomy without RT","breast conserving"))
dat$tusi <-
factor(dat$tusi,
levels=c("<2 cm","2-5 cm",">5 cm"))
dat$nodal <-
factor(dat$nodal,
levels=c("node negative","node positive"))
dat$age50 <-
factor(dat$age50,
levels=c("<=50",">50"))
dat$adjchem <-
factor(dat$adjchem,
levels=c("no adj chemo","adj chemo"))
dat$tam <-
factor(dat$tam,
levels=c("no tam","tam"))
dat$hospno <- factor(dat$hospno)
## ---------------------------------------------------------------------------------------------------------
dat %>%
group_by(hospno) %>%
summarize(npat = n()) %>%
ggplot(aes(x = npat)) + geom_histogram() +
labs(x = "Center size") +
# scale_x_continuous(breaks = c(0, 50, 100, 182, 304, 602, 880)) +
scale_x_continuous(breaks = seq(0, 900, by=50)) +
theme_bw()
## ---------------------------------------------------------------------------------------------------------
# data set with Kaplan-Meier curves per center
sf <- survfit(Surv(survyrs, survstat) ~ strata(hospno), dat)
dd <- data.frame(time = sf$time, surv = sf$surv, center = as.factor(rep(1:length(sf$strata), sf$strata)))
startpoint <- data.frame(center = unique(dd$center), time = 0, surv = 1)
dd <- rbind(dd, startpoint)
# data set with overall Kaplan-Meier curve
sf2 <- survfit(Surv(survyrs, survstat) ~ 1, dat)
dd2 <- data.frame(time = sf2$time, surv = sf2$surv)
dd %>%
ggplot(aes(x = time, y = surv)) +
geom_step(aes(group = center), show.legend = FALSE, alpha = 0.2) +
geom_step(data = dd2) +
theme_classic() +
labs(x = "Years since randomisation", y = "Survival")
## ---------------------------------------------------------------------------------------------------------
m_cph <- coxph(Surv(survyrs, survstat) ~ surgery + tusi + nodal + age50 + adjchem +
tam + periop + cluster(hospno), dat, ties = "breslow")
m_cph
m_fixed <- coxph(Surv(survyrs, survstat) ~ surgery + tusi + nodal + age50 +
adjchem + tam + periop + hospno, dat, ties = "breslow")
m_fixed
m_fr_cov <- coxph(Surv(survyrs, survstat) ~ frailty(hospno) + surgery + tusi + nodal + age50 +
adjchem + tam + periop, data = dat, ties = "breslow")
m_fr_cov
m_emfrail <- emfrail(Surv(survyrs, survstat) ~ cluster(hospno) + surgery + tusi + nodal + age50 +
adjchem + tam + periop,
data = dat)
summary(m_emfrail)
# -5450.928 likelihood
m_fr_cov_lnorm <- coxme(Surv(survyrs, survstat) ~ (1|hospno) + surgery + tusi + nodal + age50 +
adjchem + tam + periop, data = dat, ties = "breslow")
m_fr_cov_lnorm
# -5449.445
## ---------------------------------------------------------------------------------------------------------
# Obtain frailty estimates
sm <- summary(m_emfrail)
frailties <- sm$frail
# Obtain center estimates and calculate standard error
centers <- grep("hospno", names(m_fixed$coefficients))
nc <- length(centers)
var_centers <- m_fixed$var[centers, centers]
varmean <- sum(var_centers)/ nc^2
newse <- sqrt(c(varmean, diag(var_centers) + varmean - 2/nc * apply(var_centers, 1, sum)))
# Arrange frailty estimates and fixed effects estimates
logz <- frailties %>%
mutate_if(is.numeric, log) %>%
rename(estimate = z, ymin = lower_q, ymax = upper_q) %>%
mutate(type = "frailty")
fe <- data.frame(beta = c(hospnoA = 0, m_fixed$coefficients[centers]), sd = newse) %>%
rownames_to_column("Center") %>%
mutate(Center = substr(Center, 7, 9)) %>%
mutate(sbeta = sum(beta) / 15) %>%
mutate(beta = beta - sbeta) %>%
arrange(beta) %>%
mutate(ord = 1:n()) %>%
mutate(Center = as.character(Center)) %>%
mutate(ymin = beta - 1.96 * sd, ymax = beta + 1.96 * sd) %>%
rename(id = Center, estimate = beta) %>%
select(-sd, -sbeta, -ord) %>%
mutate(type = "fixed effects") %>%
mutate(ord = 1:n())
ord_fe <- data.frame(id = fe$id, ord = fe$ord)
logz <- logz %>%
right_join(ord_fe)
datt <- bind_rows(logz, fe)
# Finally, the plot
datt %>%
mutate_at(vars(estimate, ymin, ymax), .funs = exp) %>% # make it exp()
ggplot(aes(x = ord, y = estimate)) +
geom_point(aes(colour = type),
position = position_dodge(.9)) +
geom_errorbar(aes(ymin = ymin, ymax = ymax, colour = type, linetype = type),
position = position_dodge()) +
scale_x_continuous(labels = as.character(datt$id)[1:15],
breaks = seq_along(datt$estimate)[1:15]) +
scale_y_continuous(trans = "log",
breaks = seq(from = 0.5, to = 2.5, by = .5)) +
labs(x = "Center",
y="Center effect (hazard ratio)") +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
guides(colour = guide_legend(title = element_blank()), linetype = guide_legend(title = element_blank())) +
scale_colour_brewer(palette = "Set1")
## ---------------------------------------------------------------------------------------------------------
# checking proportional hazards assumption
# in Cox model
cox.zph(m_cph)
# in Cox model with log-frailties as offset
logz_long <- log(m_emfrail$frail)[dat$hospno]
m_cph_offset_gamma <- coxph(Surv(survyrs, survstat) ~ surgery + tusi + nodal + age50 + adjchem +
tam + periop + offset(logz_long), dat, ties = "breslow")
cox.zph(m_cph_offset_gamma)
## ---------------------------------------------------------------------------------------------------------
data(cgd)
# head(cgd)
# rebrand id
cgd <- cgd %>%
mutate(id = as.numeric(as.factor(id)))
## ---------------------------------------------------------------------------------------------------------
cgd %>%
ggplot() +
geom_segment(aes(x = tstart, xend = tstop, y = id, yend = id)) +
geom_point(data = filter(cgd, status == 1), aes(x = tstop, y = id)) +
theme_bw() +
labs(x = "time", y = "patient")
## ---------------------------------------------------------------------------------------------------------
cgd %>%
group_by(id) %>%
summarize(n_events = sum(status)) %>%
ggplot(aes(x = n_events)) +
geom_bar() +
labs(x= "Number of events / individual") +
theme_bw()
## ---------------------------------------------------------------------------------------------------------
mvals <- seq(from = 0.1, to = 2, by = 0.2)
models <- lapply(mvals, function(x) emfrail(formula = Surv(tstart, tstop, status) ~ sex +
treat + age + propylac + inherit + steroids + cluster(id), data = cgd,
distribution = emfrail_dist(dist = "pvf", pvfm = x)) )
likelihoods <- sapply(models, function(x) x$loglik[2])
mvals[which.max(likelihoods)]
## ---------------------------------------------------------------------------------------------------------
m_cph <- coxph(Surv(tstart, tstop, status) ~ treat + age +inherit + steroids, cgd)
# summary(m_cph)
m_gamma_emf <- emfrail(Surv(tstart, tstop, status) ~ treat + sex + age +inherit + steroids + cluster(id), cgd)
# summary(m_gamma_emf)
mod_ig <- emfrail(formula = Surv(tstart, tstop, status) ~ treat + sex +
age + inherit + steroids + cluster(id),
distribution = emfrail_dist(dist = "pvf"),
data = cgd)
mod_cp <- emfrail(formula = Surv(tstart, tstop, status) ~ treat + sex +
age + inherit + steroids + cluster(id),
distribution = emfrail_dist(dist = "pvf", pvfm = 0.5),
data = cgd)
mod_cp_11 <- emfrail(formula = Surv(tstart, tstop, status) ~ treat + sex +
age + inherit + steroids + cluster(id),
distribution = emfrail_dist(dist = "pvf", pvfm = 1.1),
data = cgd)
mod_stab <- emfrail(formula = Surv(tstart, tstop, status) ~ treat + sex +
age + inherit + steroids + cluster(id),
distribution = emfrail_dist(dist = "stable"),
data = cgd)
## ---------------------------------------------------------------------------------------------------------
autoplot(m_gamma_emf, type = "hist") +
theme_bw()
## ---------------------------------------------------------------------------------------------------------
sessionInfo()