-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfit_predict_smlc.R
More file actions
316 lines (276 loc) · 9.41 KB
/
fit_predict_smlc.R
File metadata and controls
316 lines (276 loc) · 9.41 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
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
get_rand_foldid <- function(response, nfold = 10) {
data.table::data.table(
resp = response
)[,
foldid := sample(rep(1:nfold, length.out = .N)),
by = resp
]$foldid
}
#' Hidden genome sparse multinomial logistic classifier (smlc)
#' @param X data design matrix with observations across rows and predictors across
#' columns. For a typical hidden genome classifier each row represents a tumor and
#' the columns represent (possibly normalized by some functions of
#' the total mutation burden in tumors) binary 1-0 presence/absence indicators
#' of raw variants, counts of mutations at specific genes and counts of mutations
#' corresponding to specific mutation signatures etc.
#' @param Y character vector or factor denoting the cancer type of tumors whose
#' mutation profiles are listed across the rows of \code{X}.
#' @param grouped logical. Use group-lasso penalty instead of the ordinary lasso
#' penalty? Defaults to TRUE.
#' @param alpha The elasticnet mixing parameter. Passed to {cv.glmnet}
#' @param ... additional arguments passed to \code{cv.glmnet}.
#' @param normalize_rows vector of the same length as \code{nrow(X)} to be used
#' to normalize the rows of \code{X}. If NULL (default), no normalization is performed.
#'
#'
#' @note
#' The function is a light wrapper around cv.glmnet with
#' \code{family = "multinomial"}, and \code{type.multinomial = "grouped"} if
#' \code{grouped} = TRUE. \code{cv.glmnet} tunes the sparsity hyper-parameter using
#' cross-validation. \code{fit_smlc} by default uses a 10-fold cross-validation
#' similar to the default of \code{cv.glmnet} (can be changed by supplying
#' \code{nfolds} in \code{...}); however with a stratified random partition
#' (based on the categories of \code{Y}), instead of the default simple random
#' partition used in \code{cv.glmnet}. Override this by supplying \code{foldid} to
#' \code{cv.glmnet} in the \code{...}. In addition, \code{fit_smlc}
#' sets \code{maxit = 1e6}, \code{trace.it = TRUE} in \code{...} by default
#' (instead of the default
#' \code{maxit = 1e5} set in glmnet).
#'
#' @return
#' Returns a list containing the cv.glmnet fitted object,
#' the original X and Y and the estimated
#' intercept vector alpha and regression coefficients matrix beta.
#'
#' @examples
#' data("impact")
#' top_v <- variant_screen_mi(
#' maf = impact,
#' variant_col = "Variant",
#' cancer_col = "CANCER_SITE",
#' sample_id_col = "patient_id",
#' mi_rank_thresh = 50,
#' return_prob_mi = FALSE
#' )
#' var_design <- extract_design(
#' maf = impact,
#' variant_col = "Variant",
#' sample_id_col = "patient_id",
#' variant_subset = top_v
#' )
#'
#' canc_resp <- extract_cancer_response(
#' maf = impact,
#' cancer_col = "CANCER_SITE",
#' sample_id_col = "patient_id"
#' )
#' pid <- names(canc_resp)
#' # create five stratified random folds
#' # based on the response cancer categories
#' set.seed(42)
#' folds <- data.table::data.table(
#' resp = canc_resp
#' )[,
#' foldid := sample(rep(1:5, length.out = .N)),
#' by = resp
#' ]$foldid
#'
#' # 80%-20% stratified separation of training and
#' # test set tumors
#' idx_train <- pid[folds != 5]
#' idx_test <- pid[folds == 5]
#'
#' # train a classifier on the training set
#' # using only variants (will have low accuracy
#' # -- no meta-feature information used
#' fit0 <- fit_mlogit(
#' X = var_design[idx_train, ],
#' Y = canc_resp[idx_train]
#' )
#'
#' pred0 <- predict_mlogit(
#' fit = fit0,
#' Xnew = var_design[idx_test, ]
#' )
#'
#'
#' @export
fit_smlc <- function(X,
Y,
grouped = TRUE,
alpha = 1,
normalize_rows = NULL,
...) {
type.multinomial <- ifelse(
grouped,
"grouped",
"ungrouped"
)
dots <- list(...)
dots$alpha <- NULL
dots$type.multinomial <- NULL
if (is.null(dots$nfolds)) {
dots$nfolds <- 10
}
if (is.null(dots$foldid)) {
dots$foldid <- get_rand_foldid(Y, dots$nfolds)
}
if (is.null(dots$maxit)) {
dots$maxit <- 1e6
}
if (is.null(dots$keep)) {
dots$keep <- TRUE
}
if (is.null(dots$trace.it)) {
dots$trace.it <- TRUE
if (!is.null(dots$parallel)) {
if (dots$parallel) {
dots$trace.it <- FALSE
}
}
}
if (!is.null(normalize_rows)) {
X <- X %>% divide_rows(normalize_rows)
}
logis <- do.call(
glmnet::cv.glmnet,
c(
list(
x = Matrix::Matrix(X, sparse = TRUE),
y = Y,
family = "multinomial",
alpha = alpha,
type.multinomial = type.multinomial
),
dots
)
)
tmp <- coef(logis)
icept_idx <- "(Intercept)"
alpha_vec <- sapply(tmp, function(x) x[icept_idx, ])
beta_mat <- do.call(
cbind,
lapply(tmp, function(xx) xx[colnames(X), ])
)
list(
alpha = alpha_vec,
beta = beta_mat,
X = X,
Y = Y,
fit = logis,
method = "mlogit",
glmnet_keep = dots$keep,
glmnet_alpha = alpha,
glmnet_type.multinomial = type.multinomial,
normalize_rows = normalize_rows
)
}
#' @rdname fit_smlc
#' @export
fit_mlogit <- fit_smlc
#' adjust Xnew by discarding columns not in Xold_colnames
#' and by adding 0-valued columns that are in Xold_colnames
#' but not in Xnew
adjust_Xnew <- function(Xnew, Xold_colnames) {
all_preds <- union(colnames(Xnew), Xold_colnames)
Xnew_adj <- Matrix::Matrix(
0, nrow = nrow(Xnew),
ncol = length(all_preds),
dimnames = list(rownames(Xnew),
all_preds),
sparse = TRUE)
if (!is.null(rownames(Xnew))) {
Xnew_adj[rownames(Xnew), colnames(Xnew)] <- Xnew
} else {
Xnew_adj[, colnames(Xnew)] <- Xnew
}
Xnew_adj <- Matrix::Matrix(Xnew_adj, sparse = TRUE)
Xnew_adj
}
#' Prediction based on the hidden genome sparse multinomial
#' logistic classifier
#'
#' @param Xnew test data design (or meta-design) matrix (observations
#' across rows and variables predictors/features across columns)
#' for which predictions are to be made from a fitted model. For a typical hidden
#' genome classifier this will be a matrix whose rows correspond to the test set
#' tumors, and columns correspond to (normalized by some functions of
#' the total mutation burdens in tumors) binary 1-0 presence/absence of
#' raw variants, counts of mutations at specific genes and counts of mutations
#' corresponding to specific mutation signatures etc.
#' @param fit fitted hidden genome mlogit classifier, an output of fit_smlc.
#' @param Ynew the actual cancer categories for the test samples.
#' This is not used in computation, but is return as a component in the output,
#' for possibly easier post-processing.
#' @param normalize_rows vector of the same length as \code{nrow(Xnew)} to be used
#' to normalize the rows of \code{Xnew}. If NULL (default), no normalization is performed.
#'
#' @note Predictors in \code{Xnew} that are not present in the
#' training set design matrix (stored in \code{fit}) are dropped, and predictors
#' not included in \code{Xnew} but present in training set design matrix are
#' all assumed to have zero values. This is convenient for a typical
#' hidden genome classifier where most predictors are (some normalized versions
#' of) counts (e.g. for gene and mutation signatures) or
#' binary presence/absence indicators (e.g., for raw variants) so that a zero
#' predictor value essentially indicates some form of "absence".
#' However, care must be taken for predictors whose 0 values
#' do not indicate absence.
#' @seealso fit_mlogit
#'
#' @return a list with entries (a) probs_predicted:
#' a \code{ncol(Xnew)} by n_cancer (determined from \code{fit})
#' matrix of multinomial probabilities, providing
#' the predicted probability of each sample unit in Xnew
#' being classified into each cancer site,
#' and (b) predicted : a character vector listing
#' hard classes based on the predicted multinomial
#' probabilities (obtained by assigning individuals to
#' the classes with the highest predicted probabilities), and
#' optionally, (c) observed: if Ynew is supplied, then it
#' is returned as is.
#'
#' @export
predict_smlc <- function(fit,
Xnew,
Ynew = NULL,
return_lin_pred = FALSE,
normalize_rows = NULL,
...) {
fit_glmnet <- fit$fit
beta <- fit$beta
alpha <- fit$alpha
if (is.null(normalize_rows) & !is.null(fit$normalize_rows)) {
msg <- "'normalize_rows' supplied for training but not for prediction"
warning(msg)
} else if (!is.null(normalize_rows) & is.null(fit$normalize_rows)) {
msg <- "'normalize_rows' supplied for prediction but not for training"
warning(msg)
}
if (!is.null(normalize_rows)) {
Xnew <- Xnew %>% divide_rows(normalize_rows)
}
Xnew_adj <- adjust_Xnew(Xnew, rownames(beta))
n_new <- nrow(Xnew_adj)
Xbeta_new <- (Xnew_adj[, rownames(beta), drop = FALSE] %*%
Matrix::Matrix(beta, sparse = TRUE) +
tcrossprod(rep(1, n_new), alpha))
predict_prob <- t(apply(Xbeta_new, 1, softmax))
out <- apply(predict_prob, 1, function(x) names(x)[which.max(x)])
res <- list("predicted" = out,
"probs_predicted" = predict_prob,
"observed" = Ynew)
if (return_lin_pred) {
res$lin_pred <- Xbeta_new
}
res
}
#' @rdname predict_smlc
#' @export
predict_mlogit <- predict_smlc
#' Extract the cv.glmnet object from the output of fit_mlogit
#' @inheritParams predict_smlc
#' @export
extract_glmnet <- function(fit, ...) {
out <- fit$fit
out
}