Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors "Error in Ops.Surv(observed, predicted) : Invalid operation on a survival time" #59

Closed
MosquitoFan opened this issue Feb 26, 2023 · 2 comments
Labels
bug 💣 Bug to fix

Comments

@MosquitoFan
Copy link

MosquitoFan commented Feb 26, 2023

I used the mlr3 and mlr3proba with surv.xgboost as follows, since xgboost cannot output survival matrix so I used a pipeline,
`xgb_task = TaskSurv$new("train",
backend = data.frame(train), time = "time",
event = "event")

xgb_lrn = as_learner(ppl(c("distrcompositor"),
lrn("surv.xgboost", objective = "survival:cox", nrounds=300L, eta=0.1), form = "ph"))
`

then I try to explain the model, I set the predict_survival_function and predict_cumulative_hazard_function, which could be successfully conducted when I run them,
explainer <- explain(model = xgb_lrn, data = train, y = Surv(time[!fold == i1],event[!fold == i1]), predict_function = function(model,newdata){ predict(model, newdata, predict_type = "<Prediction>")$crank }, predict_survival_function = function(model,newdata,times){ t(predict(model, newdata, predict_type = "<Prediction>")$distr$survival(times)) }, predict_cumulative_hazard_function = function(model,newdata,times){ t(predict(model, newdata, predict_type = "<Prediction>")$distr$cumHazard(times)) } )

Preparation of a new explainer is initiated
-> model label : R6 ( default )
-> data : 1498 rows 62 cols
-> target variable : 1498 values
-> predict function : function(model, newdata) { predict(model, newdata, predict_type = "")$crank }
-> predicted values : No value for predict function target column. ( default )
-> model_info : package mlr3 , ver. 0.14.0 , task regression ( default )
-> predicted values : numerical, min = -7.026592 , mean = -0.8300239 , max = 11.02893
-> residual function : difference between y and yhat ( default )
-> residuals : the residual_function returns an error when executed ( WARNING )

  • I could successfully run predict(explainer,train) to get the risk score

However, I meet error when adding output_type

Error in predict_function(model, newdata, ...) :
unused argument (output_type = "chf")

  • I also meet error when try to explain
    model_parts(explainer)

Error in Ops.Surv(observed, predicted) :
Invalid operation on a survival time

model_performance(explainer)

Error in Ops.Surv(y, predict_function(model, data)) :
Invalid operation on a survival time

I don't know what's wrong with my code.

@mikolajsp
Copy link
Collaborator

mikolajsp commented Feb 27, 2023

Hello @MosquitoFan,

I think the issue is that the wrong kind of explainer is created (from the DALEX package) as making the pipeline doesn't give the correct class to the pipeline object. (We would expect it to be LearnerSurv).

However, the possible fix is simple:

  • replace explain() function with explain_survival() when creating your explainer.
  • So the code should look like this:
explainer <- explain_survival(model = xgb_lrn,  data = train, y = Surv(time[!fold == i1],event[!fold == i1]), predict_function = function(model,newdata){ predict(model, newdata, predict_type = "<Prediction>")$crank }, predict_survival_function = function(model,newdata,times){ t(predict(model, newdata, predict_type = "<Prediction>")$distr$survival(times)) }, predict_cumulative_hazard_function = function(model,newdata,times){ t(predict(model, newdata, predict_type = "<Prediction>")$distr$cumHazard(times)) } )

I've tested it on some of my data and it works. Let me know if it solved your issue :)

P.S.
If you see residuals ... in the last line of output when creating an explainer you're creating a DALEX not a survex explainer.

@mikolajsp mikolajsp added the bug 💣 Bug to fix label Feb 27, 2023
@MosquitoFan
Copy link
Author

Hi mikolaj,

Thank you so much, it works and the plot looks beautiful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 💣 Bug to fix
Projects
None yet
Development

No branches or pull requests

2 participants