pa_plot_pred: Quick Diagnostic Plot for Survival Model Predicted...

View source: R/plot.R

pa_plot_predR Documentation

Quick Diagnostic Plot for Survival Model Predicted Probabilities

Description

Produces a quick diagnostic plot visualizing predicted survival or event probabilities (e.g., from a Weibull AFT model) against the subject-specific linear predictors (risk scores). The plot combines a smoothed line of predicted values and points representing observed times, using shape to indicate censoring status. This visualization provides a simple check of how predicted risks correspond to observed outcomes.

Usage

pa_plot_pred(
  data,
  title = NULL,
  xlab = "Risk Score",
  ylab = "Days",
  label_name = c("censored", "event"),
  levels = NULL,
  shape_style = NULL,
  legend_name = "status",
  invert_linear = TRUE,
  sample_index = NULL,
  restrict_time = NULL
)

Arguments

data

A data frame (or list-like object) containing at least the following columns:

'linear.pred'

Model linear predictor (risk score).

'pred'

Predicted probability or median survival value.

'times'

Observed event or censoring time.

'status'

Event indicator (0 = censored, 1 = event; optionally can include more levels if defined in label_name).

title

Optional plot title (default = NULL).

xlab, ylab

Character strings specifying the axis labels (defaults are "Risk Score" for x and "Days" for y).

label_name

Character vector of labels corresponding to event status codes (e.g., c("censored", "event")); used in the legend.

levels

Numeric or character vector giving the ordering of status levels; defaults inferred from the length of label_name.

shape_style

Numeric vector of plotting symbols (pch values) corresponding to the levels of status. If NULL, defaults to c(1, 19) for two levels (censored vs event) or c(2, 19, 1) for three levels.

legend_name

Character string for the legend title (default = "status").

invert_linear

Logical; if TRUE (default), plots the negative of linear.pred so that higher values correspond to higher risk. Set to FALSE if you want higher values correspond to lower risk.

sample_index

Optional integer vector selecting which subjects to plot, e.g. sample(seq_len(n), 200) to thin a crowded chart. If NULL (default), every subject is plotted.

restrict_time

Optional numeric. If supplied, observed times above this value are capped at it and a dashed horizontal reference line is drawn, which keeps a few long follow-up times from compressing the plot.

Details

The function is primarily designed for quick visual diagnostics in survival model development. The line (pred vs linear.pred) shows the fitted or predicted trend, while the overlaid points (times vs linear.pred) illustrate observed event times, distinguished by censoring status. For more elaborate visualization (e.g., confidence bands, multiple models), use faceting or combine plots with patchwork.

Value

A ggplot object that can be further customized.

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)

# observed time against predicted risk score, shaped by event status
pa_plot_pred(pred, title = "Cox model")

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