Skip to contents

Surv() builds the response for a survival model from event times and an event indicator, typically on the left-hand side of a formula such as Surv(time, status == 2) ~ age.

Usage

Surv(time, time2, event, type, origin = 0)

Arguments

time

For right-censored data, the follow-up time. For interval data, the first time (the lower bound). For counting-process data, the start of the interval.

time2

The stop time for counting-process data, or the upper bound for interval data. With two arguments and no type, the second is taken as the status, so Surv(time, status) works positionally.

event

The status indicator: logical, 0/1, or 1/2 for right, left, and counting data, codes 0 (right), 1 (exact), 2 (left), and 3 (interval) for type = "interval", or a factor for multi-state data.

type

One of "right", "left", "interval", "counting", "interval2", or "mstate". Inferred from the arguments when not given. With "interval2", time and time2 are the bounds, and a missing or infinite bound is open. With "mstate", the status is converted to a factor with sorted levels.

origin

A value subtracted from every time.

Value

A Surv object: a numeric matrix with columns time and status (right, left, and multi-state), start, stop, and status (counting process), or time1, time2, and status (interval), with the type in attr(, "type") and the state names of multi-state data in attr(, "states").

Details

The type is inferred from the arguments when type isn't given:

  • time only: right-censored, with every row an event.

  • time and event: right-censored.

  • time, time2, and event: counting process, with time the start and time2 the stop.

  • A factor event: multi-state, with the first level as censoring.

Status may be logical, 0/1, or 1/2. When the largest status is 2, one is subtracted, so 1/2 coding becomes 0/1. Any other value becomes missing with a warning.

Missing values are kept rather than dropped. Missing inputs, invalid status codes, start times that are not before their stop times, and backwards intervals all give missing rows.

Examples

Surv(c(5, 6, 4, 9), c(1, 0, 1, 0))
#> <Surv[4]: right>
#> [1] 5  6+ 4  9+

# 1/2 status coding, as in the lung data, becomes 0/1
Surv(c(5, 6, 4), c(2, 1, 2))
#> <Surv[3]: right>
#> [1] 5  6+ 4 

# Counting process (start, stop]
Surv(c(0, 2, 1), c(5, 6, 4), c(1, 0, 1))
#> <Surv[3]: counting>
#> [1] (0,5]  (2,6+] (1,4] 

# Interval censoring from bounds, with open bounds as NA
Surv(c(1, 2, NA), c(1, NA, 4), type = "interval2")
#> <Surv[3]: interval>
#> [1] 1  2+ 4-

# Multi-state, with the first level as censoring
Surv(c(5, 6, 7), factor(c("censor", "pcm", "death"), levels = c("censor", "pcm", "death")))
#> <Surv[3]: mright> states: pcm, death
#> [1] 5+      6:pcm   7:death