-
Notifications
You must be signed in to change notification settings - Fork 11
/
25-predict_response.Rmd
266 lines (207 loc) · 7.55 KB
/
25-predict_response.Rmd
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
---
title: "First pass at predicting response from baseline samples in RTX trial"
output: html_notebook
---
**J. Taroni 2018**
## Install `caret`
```{r}
# devtools::install_github('topepo/caret/pkg/caret',
# ref = "6546939345fe10649cefcbfee55d58fb682bc902")
# devtools::install_version("e1071", version = "1.6-8")
```
## Functions and directory set up
```{r}
# magrittr pipe
`%>%` <- dplyr::`%>%`
```
```{r}
# plot and result directory setup for this notebook
plot.dir <- file.path("plots", "25")
dir.create(plot.dir, recursive = TRUE, showWarnings = FALSE)
results.dir <- file.path("results", "25")
dir.create(results.dir, recursive = TRUE, showWarnings = FALSE)
```
## Read in data
#### Covariates
```{r}
covariate.df <- readr::read_tsv(file.path("data", "rtx",
"RTX_full_covariates.tsv"))
```
#### Gene expression data
This is gene-level expression data that has been vst-transformed and filtered
to only genes that are in the recount2 PLIER model.
```{r}
exprs <- readRDS(file.path("data", "rtx", "VST_blind_filtered.RDS"))
```
#### recount2 `B`
The multiPLIER approach
```{r}
recount.b <- readRDS(file.path("data", "rtx", "RTX_recount2_B.RDS"))
```
#### RTX PLIER model
```{r}
rtx.plier <- readRDS(file.path("data", "rtx", "RTX_PLIER_model.RDS"))
rtx.b <- rtx.plier$B
```
## LASSO
### Prep data
First, we'll change the sample names to match the barcodes in the covariates.
The first six characters of the current column/sample names should correspond
to a barcode.
```{r}
# in the expression data
colnames(exprs) <- substr(colnames(exprs), start = 1, stop = 6)
all(covariate.df$barcode == colnames(exprs))
```
```{r}
# in the recount B data
colnames(recount.b) <- substr(colnames(recount.b), start = 1, stop = 6)
all(covariate.df$barcode == colnames(recount.b))
```
```{r}
# in the RTX B
colnames(rtx.b) <- substr(colnames(rtx.b), start = 1, stop = 6)
all(covariate.df$barcode == colnames(rtx.b))
```
The `mainclass` column in `covariate.df` is what we are interested in
predicting; it contains whether or not a patient is a _nonresponder_ or
_responder_ (divided into _tolerant_ or _nontolerant_ depending on, I believe,
long-term outcome) to treatment.
(We'll exclude samples with `NA` in this column.)
We'll want to try and predict this from baseline samples
(`covariate.df$timepoint == "BL"`).
We will not be adjusting for covariates at this point.
The earlier publications on this trial suggest that the majority of
covariates have no significant association with response.
Let's take a look at the sample size and class balance.
```{r}
table(covariate.df$mainclass, covariate.df$timepoint)
```
We can see that there are `r sum(covariate.df$timepoint == "BL")` baseline
samples and that the three classes (`Nonresponder`, `Nontolerant`, and
`Tolerant`) are pretty balanced.
If we use these three classes, we can likely use a metric like total accuracy
to evaluate performance.
Also, the small sample size lends itself to leave-one-out cross-validataion
(LOOCV).
```{r}
# Do all baseline samples have response labels? No, one is NA
baseline.covariate.df <- covariate.df %>%
dplyr::filter(timepoint == "BL") %>%
dplyr::select(c("barcode", "timepoint", "mainclass")) %>%
dplyr::filter(complete.cases(.))
```
```{r}
# we only want the baseline samples with a class label
baseline.samples <- baseline.covariate.df$barcode
```
```{r}
baseline.exprs <- t(exprs[, which(colnames(exprs) %in% baseline.samples)])
dim(baseline.exprs)
```
```{r}
recount.baseline.b <-
t(recount.b[, which(colnames(recount.b) %in% baseline.samples)])
dim(recount.baseline.b)
```
```{r}
rtx.baseline.b <- t(rtx.b[, which(colnames(rtx.b) %in% baseline.samples)])
dim(rtx.baseline.b)
```
```{r}
all(rownames(recount.baseline.b) == baseline.covariate.df$barcode)
```
```{r}
all(rownames(baseline.exprs) == baseline.covariate.df$barcode)
```
```{r}
all(rownames(rtx.baseline.b) == baseline.covariate.df$barcode)
```
### Prediction
```{r}
set.seed(12345)
```
#### Expression data
```{r}
exprs.results <- glmnet::cv.glmnet(x = baseline.exprs,
y = baseline.covariate.df$mainclass,
type.measure = "class",
family = "multinomial",
nfolds = nrow(baseline.exprs)) # LOOCV
saveRDS(exprs.results, file.path(results.dir, "expression_cv.glmnet.RDS"))
```
```{r}
exprs.predicted.labels <- stats::predict(exprs.results,
baseline.exprs,
s = exprs.results$lambda.1se,
type = "class")
caret::confusionMatrix(data = as.factor(exprs.predicted.labels),
reference = as.factor(baseline.covariate.df$mainclass))
```
#### recount2 `B`
```{r}
recount.b.results <- glmnet::cv.glmnet(x = recount.baseline.b,
y = baseline.covariate.df$mainclass,
type.measure = "class",
family = "multinomial",
nfolds = nrow(recount.baseline.b)) # LOOCV
saveRDS(recount.b.results, file.path(results.dir, "recount2_B_cv.glmnet.RDS"))
```
```{r}
recount.b.predicted.labels <- stats::predict(recount.b.results,
recount.baseline.b,
s = recount.b.results$lambda.1se,
type = "class")
caret::confusionMatrix(data = as.factor(recount.b.predicted.labels),
reference = as.factor(baseline.covariate.df$mainclass))
```
#### RTX `B`
```{r}
rtx.b.results <- glmnet::cv.glmnet(x = rtx.baseline.b,
y = baseline.covariate.df$mainclass,
type.measure = "class",
family = "multinomial",
nfolds = nrow(rtx.baseline.b)) # LOOCV
saveRDS(rtx.b.results, file.path(results.dir, "RTX_B_cv.glmnet.RDS"))
```
```{r}
rtx.b.predicted.labels <- stats::predict(rtx.b.results,
rtx.baseline.b,
s = rtx.b.results$lambda.1se,
type = "class")
caret::confusionMatrix(data = as.factor(rtx.b.predicted.labels),
reference = as.factor(baseline.covariate.df$mainclass))
```
### Plotting accuracy
```{r}
acc.df <- data.frame(Model = c("Expression", "RTX LVs", "multiPLIER LVs"),
Accuracy = c(0.9444, 1, 0.3889),
Lower = c(0.8134, 0.9026, 0.2314),
Upper = c(0.9932, 1, 0.5654))
```
```{r}
acc.df %>%
ggplot2::ggplot() +
ggplot2::geom_pointrange(mapping = ggplot2::aes(x = Model, y = Accuracy,
ymin = Lower, ymax = Upper)) +
ggplot2::theme_bw() +
ggplot2::labs(title = "Predicting response with LASSO") +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5,
face = "bold")) +
ggplot2::theme(text = ggplot2::element_text(size = 15))
```
```{r}
ggplot2::ggsave(file.path(plot.dir, "total_accuracy_CI.pdf"),
plot = ggplot2::last_plot())
```
I wonder if the poor performance in the case of the multiPLIER LVs could be due
to a smaller range of values.
```{r}
summary(as.vector(baseline.exprs))
```
```{r}
summary(as.vector(recount.baseline.b))
```
```{r}
summary(as.vector(rtx.baseline.b))
```