-
Notifications
You must be signed in to change notification settings - Fork 3
/
ALSFRS_R.R
357 lines (263 loc) · 13 KB
/
ALSFRS_R.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
## ----data ------------------------------------------------------------
### data preparation ##########################
load("../data/RALSfinal.rda")
ALSFRSdata <- data
hya <- grep(".halfYearAfter", names(ALSFRSdata), value = TRUE)
del <- hya[-grep("ALSFRS", hya)]
st <- grep(".Start", names(ALSFRSdata), value = TRUE)
del <- c(del, st[-grep("ALSFRS", hya)])
del <- c(del, "survival.time", "cens")
ALSFRSdata <- ALSFRSdata[ , !(names(ALSFRSdata) %in% del)]
ALSFRSdata <- ALSFRSdata[complete.cases(ALSFRSdata[, c("ALSFRS.halfYearAfter",
"ALSFRS.Start",
"Riluzole")]),]
### delete all of non-usefull variables
delete <- grepl("Delta|delta|SubjectID|Unit|Onset|treatment.group", names(ALSFRSdata))
ALSFRSdata <- ALSFRSdata[ , -which(delete)]
### delete Basophil count and Hematocrit (non-explicable values)
ALSFRSdata$Value_Absolute_Basophil_Count <- NULL
# ALSFRSdata$Value_Hematocrit <- NULL
sm1 <- ALSFRSdata$Value_Hematocrit < 1 & !is.na(ALSFRSdata$Value_Hematocrit)
ALSFRSdata$Value_Hematocrit[sm1] <- ALSFRSdata$Value_Hematocrit[sm1] * 100
### delete extremely high Phosphorus value
ALSFRSdata$Value_Phosphorus[!is.na(ALSFRSdata$Value_Phosphorus) &
ALSFRSdata$Value_Phosphorus > 5] <- NA
### delete columns with more than 50% NAs
### except scores, t.onsettrt, Riluzole
keepvarnames <- c("ALSFRS", "t.onsettrt", "Riluzole")
keepvars <- grepl(paste0(keepvarnames, collapse = "|"), names(ALSFRSdata))
pNA <- 0.5 * nrow(ALSFRSdata)
ALSFRSdata <- ALSFRSdata[ , keepvars | (colSums(is.na(ALSFRSdata)) < pNA)]
# ### Rename variables for plotting
names(ALSFRSdata)[names(ALSFRSdata) == "t.onsettrt"] <- "time_onset_treatment"
# names(ALSFRSdata)[names(ALSFRSdata) == "SubjectLiters_fvc"] <- "FVC"
# names(ALSFRSdata)[names(ALSFRSdata) == "Value_Phosphorus"] <- "phosphorus"
Z <- names(ALSFRSdata)[!(names(ALSFRSdata) %in% c("ALSFRS.halfYearAfter",
"Riluzole",
"ALSFRS.Start"))]
names(ALSFRSdata)[names(ALSFRSdata) %in% Z] <-
tolower(names(ALSFRSdata)[names(ALSFRSdata) %in% Z])
names(ALSFRSdata) <- gsub("fam.hist.", "family_history_", names(ALSFRSdata))
dim(ALSFRSdata)
save(ALSFRSdata, file = "../data/ALSFRSdata.rda")
## ---- fittingfunction --------------------------------------------------------------
##' fitting function for glm with offset and log-link
##' @param data ALSFRS data
##' @param weights weights
##' @param parm which parameters are we interested in. c(1,2) corresponds to intercept and Riluzole parameter.
my.lmlog <- function(data, weights, parm = c(1,2)) {
tb <- table(data[["Riluzole"]][weights > 0])
## only one treatment arm left; we don't want to split further...
# if (any(tb == 0)) return(matrix(0, nrow = nrow(data), ncol = length(parm)))
if (any(tb < 5)) return(matrix(0, nrow = nrow(data), ncol = length(parm)))
mod <- glm(ALSFRS.halfYearAfter ~ Riluzole + offset(log(ALSFRS.Start)),
weights = weights, data = data, subset = weights > 0,
family = gaussian(link = "log"), start = c(-0.159, 0.009)) # start from base model
ef <- as.matrix(estfun(mod)[, parm])
ret <- matrix(0, nrow = nrow(data), ncol = ncol(ef))
ret[weights > 0,] <- ef
ret
}
## ----forest ------------------------------------------------------------
message("forest")
set.seed(1234)
### formula
Z <- names(ALSFRSdata)[!(names(ALSFRSdata) %in% c("ALSFRS.halfYearAfter", "Riluzole", "ALSFRS.Start"))]
fm <- as.formula(paste("ALSFRS.halfYearAfter + Riluzole + ALSFRS.Start ~ ", paste(Z, collapse = "+")))
### forest
## with cores != NULL, not reproducible (seed depends on parallel)
forest <- cforest(fm, data = ALSFRSdata, ytrafo = my.lmlog,
ntree = 100, cores = NULL,
perturb = list(replace = FALSE),
control = ctree_control(teststat = "max", testtype = "Univ",
mincriterion = 0.95, minsplit = 40, minbucket = 30))
forest <- prune_forest(forest, endpoint = "numeric")
### base model
bmod <- glm(ALSFRS.halfYearAfter ~ Riluzole + offset(log(ALSFRS.Start)),
data = ALSFRSdata, family = gaussian(link = "log"))
save(bmod, file = "ALSFRS_bmod.rda")
## ---- pm ----
message("personalised models")
### personalized models
mods <- person_mods(forest, basemod = "glm", newdata = NULL, OOB = TRUE,
offset = "log(ALSFRS.Start)", family = gaussian(link = "log"),
parallel = TRUE)
cf <- t(sapply(mods, coef))
summary(cf)
colnames(cf) <- gsub("\\(|\\)|Yes", "", colnames(cf))
save(cf, file = "ALSFRS_personalModels.rda")
cf <- cbind(cf, ALSFRSdata)
ggplot(cf, aes(x = Intercept, y = Riluzole, color = log(time_onset_treatment))) + geom_point()
ggplot(cf, aes(x = Intercept, y = Riluzole, color = speech)) + geom_point()
ggplot(cf, aes(x = Intercept, y = Riluzole, color = log(value_phosphorus))) + geom_point()
ggplot(cf, aes(x = Intercept, y = Riluzole, color = subjectliters_fvc)) + geom_point()
ggplot(cf, aes(x = Intercept, y = Riluzole, color = age)) + geom_point()
ggplot(cf, aes(x = Intercept, y = Riluzole, color = height)) + geom_point()
ggplot(cf, aes(x = Intercept, y = Riluzole, color = weakness)) + geom_point()
## ----logLiks------------------------------------------------------------
message("logliks")
set.seed(5)
## forest
logLiks <- sapply(1:nrow(ALSFRSdata), comp_loglik, mods = mods, dat = ALSFRSdata,
basemod = "glm", loglik = comp_loglik.ALSFRS)
## base model
logLik_bmod <- sum(comp_loglik.ALSFRS(mod = bmod, ndat = ALSFRSdata))
(logLik_rf <- sum(logLiks))
## forest with splits in alpha
my.lmlog_alpha <- function(data, weights, parm = c(1)) {
my.lmlog(data, weights, parm)
}
forest_alpha <- cforest(fm, data = ALSFRSdata, ytrafo = my.lmlog_alpha,
ntree = 100, cores = NULL,
perturb = list(replace = FALSE),
control = ctree_control(teststat = "max", testtype = "Univ",
mincriterion = 0.95, minsplit = 40, minbucket = 30))
forest_alpha <- prune_forest(forest_alpha, endpoint = "numeric")
mods_alpha <- person_mods(forest_alpha, basemod = "glm", newdata = NULL, OOB = TRUE,
offset = "log(ALSFRS.Start)", family = gaussian(link = "log"),
parallel = TRUE)
logLiks_alpha <- sapply(1:nrow(ALSFRSdata), comp_loglik, mods = mods_alpha, dat = ALSFRSdata,
basemod = "glm", loglik = comp_loglik.ALSFRS)
logLik_rf_alpha <- sum(logLiks_alpha)
## forest with splits in beta
my.lmlog_beta <- function(data, weights, parm = c(1)) {
my.lmlog(data, weights, parm)
}
forest_beta <- cforest(fm, data = ALSFRSdata, ytrafo = my.lmlog_beta,
ntree = 100, cores = NULL,
perturb = list(replace = FALSE),
control = ctree_control(teststat = "max", testtype = "Univ",
mincriterion = 0.95, minsplit = 40, minbucket = 30))
forest_beta <- prune_forest(forest_beta, endpoint = "numeric")
mods_beta <- person_mods(forest_beta, basemod = "glm", newdata = NULL, OOB = TRUE,
offset = "log(ALSFRS.Start)", family = gaussian(link = "log"),
parallel = TRUE)
logLiks_beta <- sapply(1:nrow(ALSFRSdata), comp_loglik, mods = mods_beta, dat = ALSFRSdata,
basemod = "glm", loglik = comp_loglik.ALSFRS)
logLik_rf_beta <- sum(logLiks_beta)
save(logLik_bmod, logLik_rf, logLik_rf_alpha, logLik_rf_beta,
file = "ALSFRS_logLiks.rda")
rm(mods)
rm(mods_alpha)
rm(mods_beta)
## ----bootstrapLogliks ------------------------------------------------------------
message("bootstrapLogliks")
set.seed(12)
# number of bootstrap samples
B <- 50
# get info to get parametric bootstrap samples
cfbmod <- coef(bmod)
sdbmod <- sqrt(summary(bmod)$dispersion)
mmbmod <- model.matrix(bmod)
y0 <- ALSFRSdata$ALSFRS.Start
yhat <- exp(mmbmod %*% cfbmod) * y0
# all_ynew <- t(sapply(yhat, rnorm, n = B, sd = sdbmod))
### bootstrap sample without negative values
all_ynew <- t(sapply(yhat, function(yh) {
bss <- rnorm(n = B, mean = yh, sd = sdbmod)
while(any(bss < 0)) bss[bss < 0] <- rnorm(n = sum(bss < 0), mean = yh, sd =sdbmod)
return(bss)
}))
# how many observations have a probability of more than 1% to have a negative bootstrap
# sample
table(pnorm(0, mean = yhat, sd = sdbmod) > 0.01)
ggplot(ALSFRSdata, aes(x = Riluzole)) +
geom_jitter(aes(y = ALSFRS.halfYearAfter), color = 2, alpha = 0.3, position=position_jitter(height = 0)) +
geom_jitter(aes(y = all_ynew[,1]), alpha = 0.3, position=position_jitter(height = 0))
get_bslogliks <- function(ynew, start) {
bsdata <- ALSFRSdata
bsdata$ALSFRS.halfYearAfter <- ynew
bsforest <- cforest(fm, data = bsdata, ytrafo = my.lmlog,
ntree = 100, cores = NULL,
perturb = list(replace = FALSE),
control = ctree_control(teststat = "max", testtype = "Univ",
mincriterion = 0.95, minsplit = 40, minbucket = 30))
bsforest <- prune_forest(bsforest, endpoint = "numeric")
bsmods <- person_mods(bsforest, basemod = "glm", newdata = NULL, OOB = TRUE,
offset = "log(ALSFRS.Start)", family = gaussian(link = "log"),
parallel = TRUE, start = start)
bootstrapped.logliks_pm <- sum(sapply(1:nrow(bsdata), comp_loglik, mods = bsmods, dat = bsdata,
basemod = "glm", loglik = comp_loglik.ALSFRS))
## compute base model log-likelihood on bootstrap sample
bsbmod <- glm(ALSFRS.halfYearAfter ~ Riluzole + offset(log(ALSFRS.Start)),
data = bsdata, family = gaussian(link = "log"), start = cfbmod)
bootstrapped.logliks_bm <- sum(comp_loglik.ALSFRS(mod = bsbmod, ndat = bsdata))
c(base_model = bootstrapped.logliks_bm,
forest = bootstrapped.logliks_pm)
}
# bootstrapped.logliks <- as.data.frame(t(apply(all_ynew, 2, get_bslogliks)))
bootstrapped.logliks <- adply(all_ynew, 2, get_bslogliks, start = cfbmod,
.progress = "text")
ggplot(bootstrapped.logliks) +
geom_line(aes(forest), stat = "density") +
geom_line(aes(base_model), stat = "density", linetype = 2) +
geom_rug(aes(x = logLik_bmod), linetype = 2) +
annotate("text", x = logLik_bmod, y = 0,
label = "base model") +
geom_rug(aes(x = logLik_rf)) +
annotate("text", x = logLik_rf, y = 0,
label = "forest") +
xlab("log-likelihood")
save(bootstrapped.logliks, file = "ALSFRS_bootstrapLogLiks.rda")
## ----varimp ------------------------------------------------------------
message("varimp")
set.seed(122)
VI <- varimp(forest = forest, basemod = "glm", loglik = comp_loglik.ALSFRS,
OOB = TRUE, parallel = TRUE,
offset = "log(ALSFRS.Start)", family = gaussian(link = "log"))
VI$variable <- factor(VI$variable, levels = VI$variable[order(VI$VI)])
VI[order(VI$VI), ]
ggplot(VI, aes(y = VI, x = variable)) + geom_bar(stat = "identity", width = .1) +
coord_flip() + theme(panel.grid.major.y = element_blank(), axis.text.y = element_text(size = 13))
save(VI, file = "ALSFRS_varimp.rda")
## ----rankplot--------------------------------------------------------------
## get data
V <- as.character(VI$variable[order(VI$VI, decreasing = TRUE)][1:5])
rk <- cbind(cf, ALSFRSdata[, V])
rk$Rank <- rank(rk$Riluzole)
## Plot treatment effect against rank
p_ril <- ggplotGrob(
ggplot(aes(x = Rank, y = Riluzole), data = rk) +
geom_point() + theme_bw() +
ylab(bquote(hat(beta)))
)
## Plot variables with highest VI against rank
make_ggplotgrob <- function(z) {
p <- ggplot(aes_string(x = "Rank", y = z), data = rk) +
theme_bw() + theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
legend.position = "none")
if(is.numeric(rk[,z])) {
p <- p + geom_point(alpha = I(0.2))
} else {
p <- p + geom_point(alpha = I(0.2), aes_string(colour = z))
}
ggplotGrob(p)
}
p_z <- lapply(V, make_ggplotgrob)
names(p_z) <- V
## arrange plots in list, make sure they align
p_all <- list()
p_all[[1]] <- p_ril
p_all[2:(length(p_z) + 1)] <- p_z
maxWidth <- do.call(grid:::unit.pmax, lapply(p_all, function(p) p$widths[2:3]))
p_all <- lapply(p_all, function(p) {
p$widths[2:3] <- maxWidth
p
})
## Plot
do.call(grid.arrange, c(p_all, ncol = 1))
## ----pdplot ------------------------------------------------------------
library("plyr")
#load(file = "../data/ALSFRSdata.rda")
#load(file = "ALSFRS_forest.rda")
#load(file = "ALSFRS_personalModels.rda")
source("basis/dependence_plots.R")
### partial dependency plots
pd <- cbind(cf, ALSFRSdata)
library("ggplot2")
a <- lapply(Z, dependenceplot, treatment = "Riluzole", pd = pd)
print(a)
b <- lapply(Z, dependenceplot, treatment = "Riluzole", pd = pd, nmean = TRUE)
print(b)