Predictions and coefficients of a piecewise exponential model
Source:R/piecewise-exponential.R
piecewise_exponential_predict.Rdpredict() gives, for each row of newdata (or of the data the model was fit to), the
survival or cumulative hazard curve, the linear predictor \(x^\top \beta\), or the risk
score \(\exp(x^\top \beta)\). The cumulative hazard is
\(H(t \mid x) = \exp(x^\top \beta) \sum_j \lambda_j \ell_j(t)\), where \(\ell_j(t)\) is the
time spent in interval \(j\) by time \(t\), and survival is \(\exp(-H)\). Their
standard errors use the delta method with the variance of all the parameters, and the limits
are built on the scale of \(\log H\), so survival stays within 0 and 1.
Usage
# S3 method for class 'piecewise_exponential'
predict(
object,
newdata,
type = c("survival", "cumhaz", "lp", "risk"),
times,
conf_level = object$conf_level,
...
)
# S3 method for class 'piecewise_exponential'
as.data.frame(
x,
row.names = NULL,
optional = FALSE,
...,
conf_level = x$conf_level
)Arguments
- object, x
A
piecewise_exponential()fit.- newdata
A data frame with the covariates of the subjects to predict for. The default is the data the model was fit to.
- type
What to predict:
"survival"(the default),"cumhaz","lp", or"risk".- times
For
"survival"and"cumhaz", the times to read the curves at.- conf_level
The confidence level. The default is the level the model was fit with.
- ...
Unused.
- row.names, optional
Unused. They're part of the
as.data.frame()generic.
Value
For "lp" and "risk", a numeric vector with one value per row of newdata. For
"survival" and "cumhaz", a data frame with one row per subject and time: subject (the
row of newdata), time, estimate, std_error, conf_low, and conf_high.
as.data.frame() gives a data frame with the columns term, estimate, std_error,
statistic, p_value, conf_low, and conf_high.
Details
as.data.frame() gives the covariates' log hazard ratios, with Wald tests and limits.
Examples
lung <- data.frame(
time = c(306, 455, 1010, 210, 883, 1022, 310, 361, 218, 166, 170, 654, 728, 71, 567, 144,
613, 707, 61, 88),
status = c(2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2),
age = c(74, 68, 56, 57, 60, 74, 68, 71, 53, 61, 57, 68, 68, 60, 57, 67, 70, 63, 56, 57),
sex = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1)
)
fit <- piecewise_exponential(Surv(time, status) ~ age + sex, data = lung, breaks = c(180, 365))
as.data.frame(fit)
#> term estimate std_error statistic p_value conf_low conf_high
#> 1 age -0.05500287 0.04465546 -1.23171657 0.2180550 -0.142526 0.03252022
#> 2 sex 0.01384684 0.72114306 0.01920124 0.9846806 -1.399568 1.42726126
newdata <- data.frame(age = c(55, 70), sex = c(1, 2))
predict(fit, newdata = newdata, type = "survival", times = c(100, 300, 500))
#> subject time estimate std_error conf_low conf_high
#> 1 1 100 0.7899052 0.09648341 0.52161141 0.9180878
#> 2 1 300 0.4085405 0.16093008 0.11995204 0.6853247
#> 3 1 500 0.2157642 0.13487256 0.03306608 0.5016564
#> 4 2 100 0.9005128 0.06696322 0.65634748 0.9742577
#> 5 2 300 0.6718333 0.16139661 0.27271399 0.8853646
#> 6 2 500 0.5059057 0.20066820 0.11853613 0.8043443
predict(fit, newdata = newdata, type = "risk")
#> [1] 0.04922710 0.02187289