View source: R/pa_predict_coxph.R
| pa_predict_coxph | R Documentation |
This function calculates a set of measures, $R^2$ and $L^2$, $Pseudo-R^2$, for a Cox proportional hazards model. When predict = TRUE, it returns the predicted survival times alongside observed values and event status. R-squared quantifies the proportion of variability in the observed survival times explained by the model's predictions, while L-squared measures the proportion of prediction error explained by a corrected prediction function. Together, these metrics provide a comprehensive evaluation of the predictive power of the Cox model.
pa_predict_coxph(model, covs, tau = 1e+11, new_data = NULL, predict = TRUE)
model |
An object of class 'coxph' representing a fitted Cox proportional hazards regression model. The model must be fitted with 'x = TRUE' and 'y = TRUE' to include the design matrix and response vector. |
covs |
A character vector specifying the names of the covariates used in the model. |
tau |
(Optional) A restriction time for computing restricted survival time. If provided, the function evaluates predictions up to this time point. Default is 10e10. |
new_data |
Optional evaluation data. Its outcomes are returned for metric computation and define the evaluation time grid; they never estimate the baseline hazard. All prediction parameters come from the training fit. |
predict |
A logical value indicating whether to return individual predictions. Default is TRUE. |
Probabilities use the fitted training baseline, including its case weights, strata, tie method and offsets. Offset centering follows the weighted training mean used by [survival::survfit()]. With 'predict = FALSE', R-squared metrics return missing values with a warning if censoring weights cannot be normalized, for example when the training censoring survival is zero at an observed evaluation event.
A **named list** of model-specific prediction objects. Each element of the returned list corresponds to one fitted model and contains:
model – the fitted survival model object.
times – numeric vector of observed follow-up times for the evaluation dataset.
status – event indicators (1 = event, 0 = censored).
surv_prob – an n \times K matrix of predicted
subject-specific survival probabilities on a common time grid.
pred – numerical integral of the training-model survival curve
on the evaluation grid, up to its maximum time or tau, whichever is
smaller. This preserves the legacy integration rule; it is not an
identified unrestricted mean. Fix the grid and tau for comparisons.
covs – character vector of covariate names used for prediction.
new_data – dataset on which the predictions were computed.
Li, G., & Wang, X. (2016). Prediction Accuracy Measures for a Nonlinear Model and for Right-Censored Time-to-Event Data. arXiv preprint arXiv:1611.03063. Available at https://arxiv.org/abs/1611.03063.
data(pbc, package = "survival")
pbc <- pbc[!is.na(pbc$trt), ]
pbc$log_albumin <- log(pbc$albumin)
pbc$log_bili <- log(pbc$bili)
pbc$log_protime <- log(pbc$protime)
pbc$status <- ifelse(pbc$status == 2, 1, 0)
covs <- c("age", "log_albumin", "log_bili", "log_protime", "edema")
df <- pbc[, c("time", "status", covs)]
df <- df[stats::complete.cases(df), ]
set.seed(2025)
idx <- sample(seq_len(nrow(df)), round(2 / 3 * nrow(df)))
train_data <- df[idx, ]
test_data <- df[-idx, ]
m.cox <- survival::coxph(survival::Surv(time, status) ~ .,
data = train_data, x = TRUE, y = TRUE)
cox_pred <- pa_predict_coxph(model = m.cox, covs = covs,
new_data = test_data)
str(cox_pred$pred)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.