Per the approach in A review of survival stacking: a method to cast survival
regression analysis as a classification problem doi:10.1515/ijb-2022-0055
https://arxiv.org/abs/2107.13480, we provide df_to_survival_stacked
as a helper function for converting traditional survival data (one
observation = one row) into the survival stacked data structure, a repeated
observations data structure where multiple rows exist for each individual for
each timepoint at which they were still in the risk set up to and including
their event time.
Usage
df_to_survival_stacked(
data,
id_col = NULL,
time_col,
status_col,
covariate_cols,
period_duration = 1,
custom_times = NULL
)Arguments
- data
A data frame with survival -type outcomes including an event indicator and a time-to-event-or-censoring column
- id_col
(string) name of the id column that is unique to each observation in
data. If one is not specified, one will be created (called.id) assuming that each row is a unique observation.- time_col
(string) name of the time‐to‐event column
- status_col
(string) name of the 0/1 event indicator column
- covariate_cols
(string vector) names of your predictors
- period_duration
(numeric) length of each time-period (e.g. 1)
- custom_times
(numeric vector) (optional) A vector of the time-period breakpoints. If events could have occurred at any time after zero, this should begin with 0.
Value
a data.frame of repeated observations, one row for each time-step, that indicates which observations remain in the risk set and whether or not an event occurs.
Examples
if (requireNamespace("survival", quietly = TRUE)) {
df_to_survival_stacked(
data = survival::kidney,
id_col = "id",
time_col = "time",
status_col = "status",
covariate_cols = c("age", "sex", "disease", "frail")
)
}
#> # A tibble: 7,800 × 7
#> id t event age sex disease frail
#> <dbl> <dbl> <int> <dbl> <dbl> <fct> <dbl>
#> 1 1 0 0 28 1 Other 2.3
#> 2 1 1 0 28 1 Other 2.3
#> 3 1 2 0 28 1 Other 2.3
#> 4 1 3 0 28 1 Other 2.3
#> 5 1 4 0 28 1 Other 2.3
#> 6 1 5 0 28 1 Other 2.3
#> 7 1 6 0 28 1 Other 2.3
#> 8 1 7 0 28 1 Other 2.3
#> 9 1 8 1 28 1 Other 2.3
#> 10 1 0 0 28 1 Other 2.3
#> # ℹ 7,790 more rows
