﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
149	transform_to_stepfunction issue	Isabelle	Olly Betts	"I am creating a custom explainer for a model I have develop. 
I have a predict function that returns a vector of survival times. See below.
When entering the transform_to_stepfunction, the raw_prediction is simply a vector numeric values. When stepping into this function, I see where the function crashes:

transform_to_stepfunction <- function(predict_function, eval_times = NULL, ..., type = NULL, prediction_element = NULL, times_element = NULL) {
    function(model, newdata, times) {
        raw_prediction <- predict_function(model, newdata, ...)
        if (!is.null(times_element)) eval_times <- raw_prediction[[times_element]]
        if (!is.null(prediction_element)) prediction <- raw_prediction[[prediction_element]]
        n_rows <- ifelse(is.null(dim(prediction)), 1, nrow(prediction))

Since both times_element and prediction_element are null, the 'prediction' variable is null too, which creashes this function with the message:

""Error in explainer$predict_survival_function(explainer$model, newdata,  : 
  object 'prediction' not found""

How to make this work with a function that returns a vector of survival lengths?

Thank you.

See below my prediction function:

pred_cbr <- function(model, newdata, eval_times) {
  test <- newdata
  all <- alldata[rownames(test) %in% rownames(alldata)]
  test <- cbind('time' = all[,1], 'status' = all[,2], test)
  predictions <- CBR_predict(model, test)
  # list of survival predictions for all test_data samples
  survivals <- list()
  for (n in 1:length(predictions))
  {
    survivals <- c(survivals, list(mean(unlist(predictions[[n]]$`survival times`))))
  }
  vsurv <- unlist(survivals)
  return(vsurv)
}

surv_pred_cbr <- transform_to_stepfunction(pred_cbr,
                                           eval_times = times,
                                           type=""survival"")

"	defect	closed	major		Other		invalid		
