-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix problematic argument survival in ranger models
- Loading branch information
Showing
4 changed files
with
53 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
#' Predict Function for Ranger | ||
#' | ||
#' Internal function that prepares the predictions of different types of ranger models, | ||
#' including survival models. | ||
#' Returns prediction function for different modes of ranger. | ||
#' | ||
#' @noRd | ||
#' @keywords internal | ||
#' @param model Fitted ranger model. | ||
#' @param newdata Data to predict on. | ||
#' @param treetype The value of `fit$treetype` in a fitted ranger model. | ||
#' @param survival Cumulative hazards "chf" (default) or probabilities "prob" per time. | ||
#' @param ... Additional arguments passed to ranger's predict function. | ||
#' | ||
#' @returns A vector or matrix with predictions. | ||
pred_ranger <- function(model, newdata, survival = c("chf", "prob"), ...) { | ||
#' @returns A function with signature f(model, newdata, ...). | ||
create_ranger_pred_fun <- function(treetype, survival = c("chf", "prob")) { | ||
survival <- match.arg(survival) | ||
|
||
pred <- stats::predict(model, newdata, ...) | ||
if (treetype != "Survival") { | ||
pred_fun <- function(model, newdata, ...) { | ||
stats::predict(model, newdata, ...)$predictions | ||
} | ||
return(pred_fun) | ||
} | ||
|
||
if (survival == "prob") { | ||
survival <- "survival" | ||
} | ||
|
||
if (model$treetype == "Survival") { | ||
out <- if (survival == "chf") pred$chf else pred$survival | ||
pred_fun <- function(model, newdata, ...) { | ||
pred <- stats::predict(model, newdata, ...) | ||
out <- pred[[survival]] | ||
colnames(out) <- paste0("t", pred$unique.death.times) | ||
} else { | ||
out <- pred$predictions | ||
return(out) | ||
} | ||
return(out) | ||
return(pred_fun) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
library(ranger) | ||
library(survival) | ||
library(kernelshap) | ||
|
||
set.seed(1) | ||
|
||
fit <- ranger(Surv(time, status) ~ ., data = veteran, num.trees = 20) | ||
fit2 <- ranger(time ~ . - status, data = veteran, num.trees = 20) | ||
fit3 <- ranger(time ~ . - status, data = veteran, quantreg = TRUE, num.trees = 20) | ||
fit4 <- ranger(status ~ . - time, data = veteran, probability = TRUE, num.trees = 20) | ||
|
||
xvars <- setdiff(colnames(veteran), c("time", "status")) | ||
|
||
kernelshap(fit, head(veteran), feature_names = xvars, bg_X = veteran) | ||
permshap(fit, head(veteran), feature_names = xvars, bg_X = veteran) | ||
|
||
kernelshap(fit, head(veteran), feature_names = xvars, bg_X = veteran, survival = "prob") | ||
permshap(fit, head(veteran), feature_names = xvars, bg_X = veteran, survival = "prob") | ||
|
||
kernelshap(fit2, head(veteran), feature_names = xvars, bg_X = veteran) | ||
permshap(fit2, head(veteran), feature_names = xvars, bg_X = veteran) | ||
|
||
kernelshap(fit3, head(veteran), feature_names = xvars, bg_X = veteran, type = "quantiles") | ||
permshap(fit3, head(veteran), feature_names = xvars, bg_X = veteran, type = "quantiles") | ||
|
||
kernelshap(fit4, head(veteran), feature_names = xvars, bg_X = veteran) | ||
permshap(fit4, head(veteran), feature_names = xvars, bg_X = veteran) | ||
|