Some models return the survival function or cumulative hazard function prediction at the times of events present in the training data set. This is a convenient utility to allow the prediction to be evaluated at any time.
transform_to_stepfunction(
predict_function,
eval_times = NULL,
...,
type = NULL,
prediction_element = NULL,
times_element = NULL
)
a function making the prediction based on model
and newdata
arguments, the ...
parameter is also passed to this function. It has to return either a numeric vector of the same length as eval_times
, a matrix with this number of columns and the same number of rows as nrow(newdata)
. It can also return a list, with one of the elements containing such an object.
a numeric vector of times, at which the fixed predictions are made. This can be NULL
, if predict_function
returns a list which contains such a vector.
other parameters passed to predict_function
the type of function to be returned, either "survival"
, "chf"
or NULL
this chooses the value of the step function before the first prediction time. If "survival"
then it is 1, if "chf"
then 0, otherwise, it is the value of the prediction for the first time in numerical order.
if predict_function
returns a list with the matrix as one of its elements, this parameter should contain the name of this element
if predict_function
returns a list with the matrix as one of its elements, this parameter should contain the name of this element
The function returns a function with three arguments, (model
, newdata
, times
), ready to supply it to an explainer.
# \donttest{
library(survex)
library(survival)
rsf_src <- randomForestSRC::rfsrc(Surv(time, status) ~ ., data = veteran)
chf_function <- transform_to_stepfunction(predict,
type = "chf",
prediction_element = "chf",
times_element = "time.interest"
)
explainer <- explain(rsf_src, predict_cumulative_hazard_function = chf_function)
#> Preparation of a new explainer is initiated
#> -> model label : rfsrc ( 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 : sum over the predict_cumulative_hazard_function will be used ( default )
#> -> predict survival function : stepfun based on predict.rfsrc()$survival will be used ( default )
#> -> predict cumulative hazard function : chf_function
#> -> model_info : package randomForestSRC , ver. 3.2.3 , task survival ( default )
#> A new explainer has been created!
# }