pa_evaluate_two_phase: Evaluate Survival Model Metrics for Two-Phase Sampling...

View source: R/pa_evaluate_two_phase.R

pa_evaluate_two_phaseR Documentation

Evaluate Survival Model Metrics for Two-Phase Sampling Designs

Description

This function constructs a standardized evaluation dataset ('dat1') from the analysis dataset, predicted survival probabilities, and sampling weights, and computes key performance metrics in a tidy table format. Works for common two-phase sampling designs: - Case-cohort (unstratified or stratified) - Nested case-control (matched or unmatched)

Usage

pa_evaluate_two_phase(
  pred_results,
  t_star = NULL,
  tau = 1e+11,
  km_cens_fit,
  case_weights,
  metrics = c("pseudo_r2", "harrell_c", "uno_c", "brier_score", "td_auc", "ici"),
  calibration_args = list()
)

Arguments

pred_results

A prediction list, such as the output of pa_predict_coxph(), with times (or legacy time), status (0 = censored, 1 = event), pred (predicted mean survival times), and surv_prob (subjects by sorted observed times).

t_star

(Optional) Evaluation time for Brier score, time-dependent AUC, and calibration. If NULL, it defaults to the median survival time.

tau

(Optional) A numeric value specifying the truncation time for calculating explained variation metrics. If NULL, it defaults to the maximum observed survival time.

km_cens_fit

Kaplan-Meier estimate of the censoring distribution, a 'survival::survfit' object. Not required when only calibration is requested.

case_weights

Numeric sampling weights in the same subject order as pred_results. Prediction helpers sort subjects by observed time; reorder weights using order(new_data$time) as well.

metrics

Character vector of metrics. NULL uses the default metrics, including ICI; "all" additionally returns r_square, l_square, e50, and e90.

calibration_args

Optional named list of calibration settings, e.g. list(nk = 3). Sampling weights are supplied by case_weights, not through this list.

Details

Calibration uses the event risk from the last prediction-grid column at or before t_star, with the exact requested outcome horizon. Both the Cox spline calibration model and absolute-error summaries use sampling weights; see [pa_calibration()]. Calibration-only inputs need times, status, and surv_prob, without fitted models or mean time predictions.

Value

A tibble with columns: - 'Metric': name of metric - 'Value': numeric value

Examples

d <- pa_sim_cox_weibull(n = 150, pi_c = 0.3, v = 2,
                        beta = c(0.5, -0.5), seed = 2025)
d <- d[, c("time", "status", "x1", "x2")]
fit <- survival::coxph(survival::Surv(time, status) ~ x1 + x2,
                       data = d, x = TRUE, y = TRUE)
pred <- pa_predict_coxph(model = fit, covs = c("x1", "x2"), new_data = d)

# case-cohort sampling weights, and the censoring distribution they need
set.seed(2025)
in_subcohort <- stats::rbinom(nrow(d), 1, 0.4) == 1
w <- pa_case_cohort_weights(d$time, d$status, subcohort = in_subcohort)
keep <- which(in_subcohort | d$status == 1)
keep <- keep[order(d$time[keep])]
pred <- pa_predict_coxph(fit, c("x1", "x2"), new_data = d[keep, ])
w <- w[keep]
km <- survival::survfit(survival::Surv(d$time, 1 - d$status) ~ 1)

pa_evaluate_two_phase(pred_results = pred, km_cens_fit = km,
                      case_weights = w)

PAmeasures documentation built on Oct. 29, 2026, 1:08 a.m.