forked from Yu-Group/epistasis-cardiac-hypertrophy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval-functions.R
More file actions
278 lines (265 loc) · 8.25 KB
/
Copy patheval-functions.R
File metadata and controls
278 lines (265 loc) · 8.25 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
#' Calculate the prediction error between the predicted and observed responses
#' across various metrics
#'
#' @param y Vector, matrix, or data.frame of the true response values
#' @param yhat Vector, matrix, or data.frame of the estimated response values
#' @param metric Character vector of prediction error metrics to compute;
#' elements should be one of "RMSE", "MSE", "R2", "MAE", "Correlation",
#' "Class", "BalancedClass", "AUC", "PR".
#' @param group (Optional) vector of factors to group prediction errors by
#' @param na_rm = logical; whether or not to remove NAs
#'
#' @returns A data.frame with the following columns:
#' \describe{
#' \item{Group}{(if \code{group} is not NULL); Name of group for grouped
#' prediction errors.}
#' \item{Meric}{Prediciton error metric.}
#' \item{Value}{Predicttion error value.}
#' }
evalPreds <- function(y, yhat, metric, group = NULL, na_rm = F) {
# error checking
isvec <- is.null(dim(y))
if ((isvec & (length(y) != length(yhat))) |
(!isvec & any(dim(y) != dim(yhat)))) {
stop("y and yhat must be the same size.")
}
if (!all(metric %in% c(
"RMSE", "MSE", "R2", "MAE",
"Correlation", "Class", "BalancedClass",
"AUC", "PR"
))) {
stop("metric has not been implemented.")
}
if (("AUC" %in% metric) | ("PR" %in% metric)) {
if (length(unique(y)) != 2) {
stop("y must be binary to evaluate AUC and PR metrics.")
}
if ((min(yhat) < 0) | (max(yhat) > 1)) {
stop("yhat must give the class proportions.")
}
ylevels <- levels(as.factor(y))
Y0 <- ylevels[1]
Y1 <- ylevels[2]
}
# create (long) grouped prediction data frame with groups, y, and yhat
if (isvec) {
pred_df <- data.frame(Group = "all", y = y, yhat = yhat)
if (!is.null(group)) {
pred_df <- rbind(pred_df, data.frame(Group = group, y = y, yhat = yhat))
}
pred_df <- pred_df %>%
dplyr::group_by(Group)
} else {
y_long <- data.frame(Group = "all", y) %>%
tidyr::gather(key = "column", value = "y", -Group)
yhat_long <- data.frame(Group = "all", yhat) %>%
tidyr::gather(key = "column", value = "yhat", -Group)
if (!is.null(group)) {
y_long <- rbind(
y_long,
data.frame(Group = group, y) %>%
tidyr::gather(key = "column", value = "y", -Group)
)
yhat_long <- rbind(
yhat_long,
data.frame(Group = group, yhat) %>%
tidyr::gather(key = "column", value = "yhat", -Group)
)
}
pred_df <- dplyr::left_join(y_long, yhat_long,
by = c("Group", "column")
) %>%
dplyr::group_by(Group, column)
}
# compute error metrics between y and yhat
err_out <- NULL
for (m in metric) {
if (m == "RMSE") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = sqrt(mean((y - yhat)^2, na.rm = na_rm))
)
} else if (m == "MSE") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = mean((y - yhat)^2, na.rm = na_rm)
)
} else if (m == "R2") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = 1 - mean((y - yhat)^2, na.rm = na_rm) /
mean((y - mean(y))^2, na.rm = na_rm)
)
} else if (m == "MAE") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = mean(abs(y - yhat), na.rm = na_rm)
)
} else if (m == "Correlation") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = cor(y, yhat, use = "pairwise.complete.obs")
)
} else if (m == "Class") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = mean(y == yhat, na.rm = na_rm)
)
} else if (m == "BalancedClass") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = mean(sapply(
unique(y),
function(y0) {
mean(y[y == y0] == yhat[y == y0],
na.rm = na_rm
)
}
), na.rm = na_rm)
)
} else if (m == "AUC") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = PRROC::roc.curve(yhat[(y == Y1) & !(is.na(y))],
yhat[(y == Y0) & !(is.na(y))],
curve = F
)$auc
)
} else if (m == "PR") {
err <- pred_df %>%
dplyr::summarise(
Metric = m,
Value = PRROC::pr.curve(yhat[(y == Y1) & !(is.na(y))],
yhat[(y == Y0) & !(is.na(y))],
curve = F
)$auc.integral
)
} else {
stop("Metric has not been implemented.")
}
err_out <- rbind(err_out, err)
}
# clean up output formatting
if (!isvec) {
err_out <- err_out %>%
tidyr::spread(key = "column", value = "Value") %>%
dplyr::select(Group, Metric, tidyselect::all_of(colnames(data.frame(y))))
}
if (is.null(group)) {
err_out <- err_out %>%
dplyr::ungroup() %>%
dplyr::select(-Group)
}
return(err_out)
}
#' Evalute confusion matrix for binary classification problem.
#'
#' @param y Observed binary response vector.
#' @param yhat Predicted binary response vector.
evalConfusion <- function(y, yhat) {
conf_tab <- table(round(yhat), y)
if (nrow(conf_tab) != 2) {
if (!("0" %in% rownames(conf_tab))) {
conf_tab <- rbind(c(0, 0), conf_tab)
} else if (!("1" %in% rownames(conf_tab))) {
conf_tab <- rbind(conf_tab, c(0, 0))
}
}
rownames(conf_tab) <- c("Predicted 0", "Predicted 1")
colnames(conf_tab) <- c("Observed 0", "Observed 1")
return(conf_tab)
}
#' Evaluate AUC (for ROC or PR) between observed and predicted responses.
#'
#' @inheritParams evalConfusion
#' @param metric One of "roc" or "pr"
evalAUC <- function(y, yhat, metric = "roc") {
if (all(yhat == yhat[1])) {
warning("Predictions are all the same.")
out <- NULL
} else {
if (metric == "roc") {
out <- PRROC::roc.curve(yhat[y == 1], yhat[y == 0], curve = T)
} else if (metric == "pr") {
out <- PRROC::pr.curve(yhat[y == 1], yhat[y == 0], curve = T)
} else {
stop("metric is unknown. metric must be one of 'roc' or 'pr'.")
}
}
return(out)
}
#' Evaluate variable importance scores for a given model.
#'
#' @param res_dir Path to results directory.
#' @param method Name of method
#' @param snps_df SNP to gene mapping data frame.
evalVimp <- function(res_dir, method, snps_df) {
# load in model fit
load(file.path(res_dir, paste0(method, "_model_fits.Rdata")))
if (stringr::str_detect(method, "lasso")) {
if (stringr::str_detect(method, "std")) {
fit <- lasso_std
} else {
fit <- lasso
}
} else if (stringr::str_detect(method, "ridge") &
!stringr::str_detect(method, "kernel")) {
if (stringr::str_detect(method, "std")) {
fit <- ridge_std
} else {
fit <- ridge
}
} else if (stringr::str_detect(method, "svm")) {
fit <- svmfit
} else if (stringr::str_detect(method, "xgb")) {
fit <- xgb
} else if (stringr::str_detect(method, "irf")) {
fit <- irf
} else if (stringr::str_detect(method, "rf")) {
fit <- rang
} else if (stringr::str_detect(method, "kernel_ridge")) {
fit <- kernel_fit
} else if (stringr::str_detect(method, "logistic")) {
fit <- log_fit
}
# compute variable importance
if ((stringr::str_detect(method, "ridge")) |
(stringr::str_detect(method, "lasso"))) {
imp_df <- as.data.frame(as.matrix(fit$beta)) %>%
setNames("Importance") %>%
tibble::rownames_to_column("var") %>%
dplyr::arrange(dplyr::desc(abs(Importance)))
} else if (stringr::str_detect(method, "rf")) {
if (stringr::str_detect(method, "irf")) {
fit <- fit$rf.list[[length(fit$rf.list)]]
}
imp_df <- as.data.frame(fit$variable.importance) %>%
setNames("Importance") %>%
tibble::rownames_to_column("var") %>%
dplyr::mutate(
var = stringr::str_remove(var, "^X")
) %>%
dplyr::arrange(dplyr::desc(Importance))
} else {
imp_df <- NULL
}
# annotate SNPs
if (!is.null(imp_df)) {
imp_df <- dplyr::left_join(
x = imp_df, y = snps_df,
by = c("var" = "Name")
) %>%
dplyr::mutate(Gene = ifelse(is.na(Gene), var, Gene)) %>%
dplyr::select(-var) %>%
dplyr::relocate(Importance, .after = last_col())
}
return(imp_df)
}