README.md

censored

R-CMD-check Codecov test
coverage Lifecycle:
experimental

censored is a “parsnip-adjacent” packages with model definitions for censored regression and survival analysis models.

Installation

This package is still in early development. You need to install the developmental branch of parsnip as well.

# install.packages("pak")
pak::pak("EmilHvitfeldt/censored")

Prediction Types

The addition of censored regression comes with changes. One of these changes is the quantities we would like to predict from the model. The 3 quantities we will consider are: "time", "survival", and "linear_pred".

To showcase these the differences, here is a simple Cox regression model fitted on the lung data set.

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

cox_mod <-
  cox_reg() %>%
  set_engine("survival") %>%
  fit(Surv(time, status) ~ age + ph.ecog, data = lung)

cox_mod
#> parsnip model object
#> 
#> Fit time:  12ms 
#> Call:
#> survival::coxph(formula = Surv(time, status) ~ age + ph.ecog, 
#>     data = data, x = TRUE)
#> 
#>             coef exp(coef) se(coef)     z        p
#> age     0.011281  1.011345 0.009319 1.211 0.226082
#> ph.ecog 0.443485  1.558128 0.115831 3.829 0.000129
#> 
#> Likelihood ratio test=19.06  on 2 df, p=7.279e-05
#> n= 227, number of events= 164 
#>    (1 observation deleted due to missingness)

time

when we specify type = "time" then we get back the predicted survival time of an observation based on its predictors. The survival time is the time it takes for the observation to observe an event.

predict(cox_mod, type = "time", new_data = head(lung))
#> # A tibble: 6 x 1
#>   .pred_time
#>        <dbl>
#> 1       342.
#> 2       474.
#> 3       511.
#> 4       389.
#> 5       498.
#> 6       342.

Here we see that the first patient is predicted to have 342.36 days left.

survival

when we specify type = "survival" then we are trying to get the probabilities of survival (not observing an event) up to a given time .time.

pred_vals_survival <- predict(cox_mod, 
                              type = "survival", 
                              new_data = head(lung), 
                              .time = c(100, 200))

pred_vals_survival
#> # A tibble: 6 x 1
#>   .pred           
#>   <list>          
#> 1 <tibble [2 × 4]>
#> 2 <tibble [2 × 4]>
#> 3 <tibble [2 × 4]>
#> 4 <tibble [2 × 4]>
#> 5 <tibble [2 × 4]>
#> 6 <tibble [2 × 4]>

pred_vals_survival$.pred[[1]]
#> # A tibble: 2 x 4
#>   .time .pred_survival .pred_survival_lower .pred_survival_upper
#>   <dbl>          <dbl>                <dbl>                <dbl>
#> 1   100          0.850                0.796                0.908
#> 2   200          0.639                0.557                0.733

here we see that the first patient has a 85% probability of survival up to 100 days and 63.9% probability of survival up to 200 days.

linear_pred

when we specify type = "linear_pred" then we get back the linear predictor for the observation according to the model.

predict(cox_mod, type = "linear_pred", new_data = head(lung))
#> # A tibble: 6 x 1
#>   .pred_linear_pred
#>               <dbl>
#> 1           -0.152 
#> 2            0.359 
#> 3            0.495 
#> 4            0.0401
#> 5            0.450 
#> 6           -0.152

here we see that the linear predictor of the first observation is -0.1517.

Note that, for linear predictor prediction types, the results are formatted for all models such that the prediction increases with time. For the proportional hazards model, the sign is reversed.

Prediction type table

| alias | engine | survival | linear_pred | time | quantile | hazard | | :------------- | :------- | :------- | :----------- | :---- | :------- | :----- | | boost_tree | mboost | TRUE | TRUE | FALSE | FALSE | FALSE | | decision_tree | rpart | TRUE | FALSE | TRUE | FALSE | FALSE | | decision_tree | party | TRUE | FALSE | TRUE | FALSE | FALSE | | rand_forest | party | TRUE | FALSE | TRUE | FALSE | FALSE | | bag_tree | ipred | TRUE | FALSE | TRUE | FALSE | FALSE | | cox_reg | survival | TRUE | TRUE | TRUE | FALSE | FALSE | | cox_reg | glmnet | FALSE | TRUE | FALSE | FALSE | FALSE | | survival_reg | survival | TRUE | FALSE | TRUE | TRUE | TRUE | | survival_reg | flexsurv | TRUE | FALSE | TRUE | TRUE | TRUE |



EmilHvitfeldt/survnip documentation built on April 8, 2021, 3:52 a.m.