Survival distribution container for efficient storage, management, and interpolation of survival model predictions.
Installation
Install the development version from GitHub:
# install.packages("pak")
pak::pak("mlr-org/survdistr")Examples
Linear interpolation of a survival matrix using the survDistr R6 class:
library(survdistr)
# generate survival matrix
mat = matrix(data = c(0.9,0.6,0.4,0.8,0.8,0.7), nrow = 2,
ncol = 3, byrow = TRUE)
x = survDistr$new(x = mat, times = c(12, 34, 42), method = "linear_surv")
x## A [2 x 3] survival matrix
## Number of observations: 2
## Number of time points: 3
## Interpolation method: Piecewise Constant Density (Linear Survival)
# stored survival matrix
x$data()
# S(t) at requested time points (linear interpolation)
x$survival(times = c(5, 30, 42, 50))
# Cumulative hazard H(t)
x$cumhazard(times = c(5, 42))
# Probability density f(t)
x$density(times = c(5, 30, 42))
# Hazard h(t)
x$hazard(times = c(5, 30, 42))Interpolation of a Kaplan-Meier survival curve using exported R function that calls C++ code:
library(survival)
fit = survfit(formula = Surv(time, status) ~ 1, data = veteran)
tab = data.frame(time = fit$time, surv = fit$surv)
head(tab)## time surv
## 1 1 0.9854015
## 2 2 0.9781022
## 3 3 0.9708029
## 4 4 0.9635036
## 5 7 0.9416058
## 6 8 0.9124088
tail(tab)## time surv
## 96 411 0.045022553
## 97 467 0.036018043
## 98 553 0.027013532
## 99 587 0.018009021
## 100 991 0.009004511
## 101 999 0.000000000
# linear S(t) interpolation
interp(
x = tab$surv,
times = tab$time,
eval_times = c(0, 3.5, 995),
method = "linear_surv"
)Code of Conduct
Please note that the survdistr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.