This function allows for calculating model prediction in a unified way.

# S3 method for surv_explainer
predict(object, newdata = NULL, output_type = "survival", times = NULL, ...)

Arguments

object

an explainer object - model preprocessed by the explain() function

newdata

data used for the prediction

output_type

character, either "risk", "survival" or "chf" depending on the desired output

times

a numeric vector of times for the survival and cumulative hazard function predictions to be evaluated at. If "output_type == "risk" this argument is ignored, if left NULL then it is extracted from object$times.

...

other arguments, currently ignored

Value

A vector or matrix containing the prediction.

Examples

library(survival)
library(survex)


cph <- coxph(Surv(time, status) ~ ., data = veteran, model = TRUE, x = TRUE, y = TRUE)
rsf_ranger <- ranger::ranger(Surv(time, status) ~ .,
    data = veteran,
    respect.unordered.factors = TRUE,
    num.trees = 100,
    mtry = 3,
    max.depth = 5
)

cph_exp <- explain(cph)
#> Preparation of a new explainer is initiated 
#>   -> model label       :  coxph (  default  ) 
#>   -> data              :  137  rows  6  cols (  extracted from the model  ) 
#>   -> target variable   :  137  values ( 128 events and 9 censored , censoring rate = 0.066 ) (  extracted from the model  ) 
#>   -> times             :  50 unique time points , min = 1.5 , median survival time = 80 , max = 999 
#>   -> times             :  (  generated from y as uniformly distributed survival quantiles based on Kaplan-Meier estimator  ) 
#>   -> predict function  :  predict.coxph with type = 'risk' will be used (  default  ) 
#>   -> predict survival function  :  predictSurvProb.coxph will be used (  default  ) 
#>   -> predict cumulative hazard function  :  -log(predict_survival_function) will be used (  default  ) 
#>   -> model_info        :  package survival , ver. 3.7.0 , task survival (  default  ) 
#>   A new explainer has been created!  

rsf_ranger_exp <- explain(rsf_ranger,
    data = veteran[, -c(3, 4)],
    y = Surv(veteran$time, veteran$status)
)
#> Preparation of a new explainer is initiated 
#>   -> model label       :  ranger (  default  ) 
#>   -> data              :  137  rows  6  cols 
#>   -> target variable   :  137  values ( 128 events and 9 censored ) 
#>   -> times             :  50 unique time points , min = 1.5 , median survival time = 80 , max = 999 
#>   -> times             :  (  generated from y as uniformly distributed survival quantiles based on Kaplan-Meier estimator  ) 
#>   -> predict function  :  sum over the predict_cumulative_hazard_function will be used (  default  ) 
#>   -> predict survival function  :  stepfun based on predict.ranger()$survival will be used (  default  ) 
#>   -> predict cumulative hazard function  :  stepfun based on predict.ranger()$chf will be used (  default  ) 
#>   -> model_info        :  package ranger , ver. 0.16.0 , task survival (  default  ) 
#>   A new explainer has been created!  


predict(cph_exp, veteran[1, ], output_type = "survival")[, 1:10]
#>  [1] 0.9959425 0.9896930 0.9831475 0.9739694 0.9692183 0.9619751 0.9570515
#>  [8] 0.9494633 0.9414903 0.9358682

predict(cph_exp, veteran[1, ], output_type = "risk")
#>         1 
#> 0.7354128 

predict(rsf_ranger_exp, veteran[1, ], output_type = "chf")[, 1:10]
#>  [1] 0.005945378 0.005945378 0.018315374 0.022121901 0.024215918 0.057908648
#>  [7] 0.059175652 0.063733170 0.069066503 0.070342694