Skip to contents

split_episodes() merges a table of subjects with a table of their visits into the counting-process layout, one row per interval (tstart, tstop] over which a subject's covariates stay the same. The result can be fit with a response such as Surv(tstart, tstop, event), so the covariates measured at each visit enter a cox_ph() model as time-varying covariates.

Usage

split_episodes(
  baseline,
  visits,
  id,
  time,
  event,
  visit_time,
  covariates = NULL,
  baseline_covariates = NULL,
  carry_forward = TRUE
)

Arguments

baseline

A data frame of the subjects, with the id, time, and event columns. It can have one row per subject, or be the same long table as visits when that table repeats each subject's follow-up time and event on every row. Then the first row of each subject is used, after checking that time, event, and the baseline_covariates don't change within it.

visits

A data frame with one row per visit, with the id and visit_time columns.

id

The name of the column that identifies the subjects in both tables.

time

The name of the column of baseline with the end of follow-up.

event

The name of the column of baseline with the event indicator: logical, numeric, or a factor whose first level means censored.

visit_time

The name of the column of visits with the time of each visit.

covariates

The names of the columns of visits to carry as time-varying covariates. The default is every column other than id, visit_time, time, event, and the baseline_covariates.

baseline_covariates

The names of the columns of baseline to carry as fixed covariates. The default is every column other than id, time, and event when baseline has one row per subject, and none when it repeats subjects (its other columns then usually change from visit to visit).

carry_forward

Whether to carry each subject's last measured values to the end of follow-up. The default is TRUE. With FALSE, follow-up stops at the last visit, so the interval after it is dropped (and with it the event), and a subject with no visits is dropped. A subject whose only interval is the one after its last visit keeps it.

Value

A data frame with the columns id, tstart, tstop, event (named as in baseline), then the fixed covariates and the time-varying covariates. The rows are ordered by subject, in the order of baseline, and then by time.

Details

A value measured at a visit applies from that visit to the next one. The event of a subject is kept only on its last interval, and every earlier interval is censored. A visit at the end of follow-up doesn't start a new interval: the interval that ends there keeps the event and the values measured before it. Before a subject's first visit (when it's after time 0) the time-varying covariates are missing, and so are those of a subject with no visits.

See also

cox_ph() for fitting the result, and its predict() method's trajectory argument for the survival of a subject along a path of covariates.

Examples

baseline <- data.frame(id = 1:6, time = c(10, 8, 12, 6, 15, 9), event = c(1, 0, 1, 1, 0, 1))
visits <- data.frame(
  id = c(1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6),
  day = c(0, 5, 0, 0, 4, 8, 0, 3, 0, 7, 0),
  bili = c(1.2, 2.4, 0.8, 3.1, 2.9, 4, 2.2, 3.5, 0.9, 1.1, 1.8)
)
episodes <- split_episodes(baseline,
  visits = visits, id = "id", time = "time", event = "event", visit_time = "day"
)
episodes
#>    id tstart tstop event bili
#> 1   1      0     5     0  1.2
#> 2   1      5    10     1  2.4
#> 3   2      0     8     0  0.8
#> 4   3      0     4     0  3.1
#> 5   3      4     8     0  2.9
#> 6   3      8    12     1  4.0
#> 7   4      0     3     0  2.2
#> 8   4      3     6     1  3.5
#> 9   5      0     7     0  0.9
#> 10  5      7    15     0  1.1
#> 11  6      0     9     1  1.8

cox_ph(Surv(tstart, tstop, event) ~ bili, data = episodes)
#> <cox_ph> Surv(tstart, tstop, event) ~ bili
#> Efron ties. n = 11, events = 4
#> 
#>      estimate hazard_ratio std_error statistic p_value
#> bili   0.4192        1.521    0.4532     0.925   0.355
#> 
#> Likelihood ratio test: 0.93 on 1 df, p = 0.335
#> Wald test:             0.856 on 1 df, p = 0.355
#> Score test:            0.918 on 1 df, p = 0.338
#> Concordance: 0.727 (std. error 0.219)