mdtToLong: Convert an mdt to long (time, status) format for survival...

View source: R/8_mdtFunctions.R

mdtToLongR Documentation

Convert an mdt to long (time, status) format for survival analysis

Description

mdtToLong reshapes a multiple decrement table into an aggregated long data set with one row per (exit time, cause) and a count column, ready for competing-risks tools such as survival::survfit(Surv(time, status) ~ 1, weights = count) (Aalen-Johansen cumulative incidence) or for any analysis based on survival::Surv.

Usage

mdtToLong(object, x, t, exitTime = c("end", "mid"), dropZero = TRUE)

Arguments

object

an mdt object.

x

entry age of the cohort (default: the lowest tabulated age, 0).

t

length of the follow-up in years (default: to the end of the table). Lives still in the table at x + t are right censored.

exitTime

where in the year of age decrements are placed: "end" (default, time k+1 for exits in year k) or "mid" (time k + 1/2, consistent with UDD).

dropZero

logical: drop rows with zero count (default TRUE).

Details

With exitTime = "end" the Aalen-Johansen estimate of the cumulative incidence of cause j at time k computed on the weighted long data coincides with qxt(object, x, k, decrement = j); see the multiple decrement vignette.

Value

A data.frame with columns time (years since age x), age (age at exit or censoring), status (a factor whose first level, "censored", is followed by the decrement names, as expected by survival::Surv for multi-state data) and count (number of lives, possibly non-integer).

Examples

valdezDf <- data.frame(
  x = 50:54,
  lx = c(4832555, 4821937, 4810206, 4797185, 4782737),
  heart = c(5168, 5363, 5618, 5929, 6277),
  accidents = c(1157, 1206, 1443, 1679, 2152),
  other = c(4293, 5162, 5960, 6840, 7631))
valdezMdt <- new("mdt", name = "ValdezExample", table = valdezDf)
long <- mdtToLong(valdezMdt, x = 50, t = 5)
head(long)
if (requireNamespace("survival", quietly = TRUE)) {
  fit <- survival::survfit(survival::Surv(time, status) ~ 1,
                           data = long, weights = count)
  summary(fit, times = 1:5)$pstate
}


lifecontingencies documentation built on Oct. 10, 2026, 9:06 a.m.