Skip to content

Fix prediction with glmnet after fit_xy()#251

Merged
hfrick merged 4 commits into
mainfrom
fit_xy-glmnet
Apr 6, 2023
Merged

Fix prediction with glmnet after fit_xy()#251
hfrick merged 4 commits into
mainfrom
fit_xy-glmnet

Conversation

@hfrick

@hfrick hfrick commented Apr 6, 2023

Copy link
Copy Markdown
Member

closes #250

For coxnet models, we need to save the training data to be available at prediction time. We save the inputs to fit_xy() in a fit_xy.proportional_hazards() method. However, we saved them as-is, it worked for at fit time (since fit_xy() can deal with data frame inputs) but failed at prediction time (because survival_prob_coxnet() expects the training data as a matrix.

This PR now saves the training data as a matrix (for x) and Surv object (for y) directly in fit_xy.proportional_hazards().

This came up in tune_grid().

library(censored)
#> Loading required package: parsnip
#> Loading required package: survival

lung2 <- lung[-14, ]
lung_x <- lung2[, c("age", "ph.ecog")]
lung_y <- data.frame(surv = Surv(lung2$time, lung2$status))
lung_pred <- lung2[1:5, ]

spec <- proportional_hazards(penalty = 0.1) %>%
  set_engine("glmnet")
xy_fit <- fit_xy(spec, x = lung_x, y = lung_y)

xy_pred_time <- predict(xy_fit, new_data = lung_pred, type = "time")

Created on 2023-04-06 with reprex v2.0.2

@hfrick hfrick merged commit d198771 into main Apr 6, 2023
@hfrick hfrick deleted the fit_xy-glmnet branch April 6, 2023 18:00
@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions Bot locked and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prediction with glmnet model fitted via fit_xy() fails for data frame input

1 participant