R/calc_interval.R

Defines functions interval_ptime interval_atrisk

# Indicates whether a person (row) was at risk at the beginning of
# the survival interval (column)
interval_atrisk <- function(x, surv_interval = c(0, Inf)) {

  k <- length(x)
  t(matrix(rep(surv_interval, k), ncol=k)) <= x

}

# Observed person-time for each person (row) across each
# survival interval (column)
interval_ptime <- function(x, surv_interval = c(0, Inf)) {

  atrisk <- interval_atrisk(x, surv_interval)
  t(diff(
    t(atrisk) * c(utils::head(surv_interval, -1), 0) +
      t((1-atrisk) * x)
  ))

}

Try the npsurvSS package in your browser

Any scripts or data that you put into this service are public.

npsurvSS documentation built on May 29, 2024, 11:23 a.m.