Skip to contents

event_time() records one observed time per subject together with how it was observed: an exact event, right-censored, left-censored, or interval-censored. The result is a vector, so it can be subset, combined, and stored as a data frame column, and as_surv() turns it into the Surv() response that the models take.

Usage

event_time(time, status, time_max = NULL)

Arguments

time

The event or censoring time, or the lower bound of an interval. Non-negative numbers (or logical NA).

status

The status codes, a character vector of "e", "r", "l", and "i" the same length as time.

time_max

The upper bound for the interval-censored rows, missing for the others. The default is missing for every row.

Value

An event_time vector the length of time. It prints with the codes as suffixes: 7 for an event, 5+ for right-censoring, 3- for left-censoring, and [2, 4] for an interval.

Details

The status codes are:

  • "e": an exact event at time.

  • "r": right-censored at time. The event happened after it.

  • "l": left-censored at time. The event happened before it.

  • "i": interval-censored. The event happened after time and at or before time_max.

time_max is given only for the interval-censored rows and is missing everywhere else. A row with a missing time is missing. Its status can be missing too.

event_time() can be used on the left-hand side of a model formula, where it's converted with as_surv(), such as event_time(time, status, time_max) ~ 1.

Errors

The checks run in a fixed order, and the first that fails is reported, naming every row where it fails. The condition has the class greenwood_event_time_error and fields check (an id such as "status_invalid_value") and locations (the rows).

See also

new_event_time() to build one from its parts without checks, extract_time() and extract_status() to take it apart, and as_surv() to convert it.

Examples

x <- event_time(
  time = c(7, 5, 3, 2, NA),
  status = c("e", "r", "l", "i", NA),
  time_max = c(NA, NA, NA, 4, NA)
)
x
#> <event_time[5]>
#> [1] 7      5+     3-     [2, 4] <NA>  
x[2:4]
#> <event_time[3]>
#> [1] 5+     3-     [2, 4]
as_surv(x)
#> <Surv[5]: interval>
#> [1] 7      5+     3-     [2, 4] <NA>  

# On the left-hand side of a formula
visits <- data.frame(
  lower = c(2, 4, 1, 6, 3, 8),
  upper = c(4, NA, 3, 9, NA, 11),
  status = c("i", "r", "i", "i", "r", "i")
)
turnbull(event_time(lower, status, time_max = upper) ~ 1, data = visits)
#> <turnbull> event_time(lower, status, time_max = upper) ~ 1
#> 
#>     n intervals ambiguous iterations converged
#> all 6         7         7         68      TRUE