pmcalibration: Create a calibration curve

View source: R/pmcalibration.R

pmcalibrationR Documentation

Create a calibration curve

Description

Assess calibration of clinical prediction models (agreement between predicted and observed probabilities) via different smooths. Binary and time-to-event outcomes are supported.

Usage

pmcalibration(
  y,
  p,
  smooth = c("none", "ns", "bs", "rcs", "gam", "lowess", "loess"),
  time = NULL,
  ci = c("sim", "boot", "pw", "none"),
  n = 1000,
  transf = NULL,
  eval = 100,
  ...
)

Arguments

y

a binary or a right-censored time-to-event outcome. Latter must be an object created via survival::Surv.

p

predicted probabilities from a clinical prediction model. For a time-to-event object time must be specified and p are predicted probabilities of the outcome happening by time units of time follow-up.

smooth

what smooth to use. Available options:

  • 'rcs' = restricted cubic spline using rms::rcs. Optional arguments for this smooth are nk (number of knots; defaults to 5) and knots (knot positions; set by Hmisc::rcs.eval if not specified)

  • 'ns' = natural spline using splines::ns. Optional arguments are df (default = 6), knots, Boundary.knots (see ?splines::ns)

  • 'bs' = B-spline using splines::bs. Optional arguments are df (default = 6), knots, Boundary.knots (see ?splines::bs)

  • 'gam' = generalized additive model via mgcv::gam and mgcv::s. Optional arguments are bs, k, fx, method (see ?mgcv::gam and ?mgcv::s)

  • 'lowess' = uses lowess(x, y, iter = 0) based on rms::calibrate. Only for binary outcomes.

  • 'loess' = uses loess with all defaults. Only for binary outcomes.

  • 'none' = logistic or Cox regression with single predictor variable (for binary outcome performs logistic calibration when transf = "logit"). See logistic_cal

'rcs', 'ns', 'bs', and 'none' are fit via glm or survival::coxph and 'gam' is fit via mgcv::gam with family = Binomial(link="logit") for a binary outcome or mgcv::cox.ph when y is time-to-event.

time

what follow up time do the predicted probabilities correspond to? Only used if y is a Surv object

ci

what kind of confidence intervals to compute?

  • 'sim' = simulation based inference. Note this is currently only available for binary outcomes. n samples are taken from a multivariate normal distribution with mean vector = coef(mod) and variance covariance = vcov(model).

  • 'boot' = bootstrap resampling with n replicates. y and p are sampled with replacement and the calibration curve is reestimated. If knots are specified the same knots are used for each resample (otherwise they are calculated using resampled p or transformation thereof)

  • 'pw' = pointwise confidence intervals calculated via the standard errors produced by relevant predict methods. Only for plotting curves; if selected, CIs are not produced for metrics (not available for smooth = 'lowess')

Calibration metrics are calculated using each simulation or boot sample. For both options percentile confidence intervals are returned.

n

number of simulations or bootstrap resamples

transf

transformation to be applied to p prior to fitting calibration curve. Valid options are 'logit', 'cloglog', 'none', or a function (must retain order of p). If unspecified defaults to 'logit' for binary outcomes and 'cloglog' (complementary log-log) for time-to-event outcomes.

eval

number of points (equally spaced between min(p) and max(p)) to evaluate for plotting (0 or NULL = no plotting). Can be a vector of probabilities.

...

additional arguments for particular smooths. For ci = 'boot' the user is able to run samples in parallel (using the parallel package) by specifying a cores argument

Value

a pmcalibration object containing calibration metrics and values for plotting

References

Austin P. C., Steyerberg E. W. (2019) The Integrated Calibration Index (ICI) and related metrics for quantifying the calibration of logistic regression models. Statistics in Medicine. 38, pp. 1–15. https://doi.org/10.1002/sim.8281

Van Calster, B., Nieboer, D., Vergouwe, Y., De Cock, B., Pencina M., Steyerberg E.W. (2016). A calibration hierarchy for risk models was defined: from utopia to empirical data. Journal of Clinical Epidemiology, 74, pp. 167-176. https://doi.org/10.1016/j.jclinepi.2015.12.005

Austin, P. C., Harrell Jr, F. E., & van Klaveren, D. (2020). Graphical calibration curves and the integrated calibration index (ICI) for survival models. Statistics in Medicine, 39(21), 2714-2742. https://doi.org/10.1002/sim.8570

Examples

# binary outcome -------------------------------------
library(pmcalibration)
# simulate some data
n <- 500
dat <- sim_dat(N = n, a1 = .5, a3 = .2)
head(dat)
# predictions
p <- with(dat, invlogit(.5 + x1 + x2 + x1*x2*.1))

# fit calibration curve
cal <- pmcalibration(y = dat$y, p = p, smooth = "gam", k = 20, ci = "pw")

summary(cal)

plot(cal)

# time to event outcome -------------------------------------
library(pmcalibration)
if (requireNamespace("survival", quietly = TRUE)){
library(survival)

data('transplant', package="survival")
transplant <- na.omit(transplant)
transplant = subset(transplant, futime > 0)
transplant$ltx <- as.numeric(transplant$event == "ltx")

# get predictions from coxph model at time = 100
# note that as we are fitting and evaluating the model on the same data
# this is internal calibration (see vignette("internal-validation", package = "pmcalibration"))
cph <- coxph(Surv(futime, ltx) ~ age + sex + abo + year, data = transplant)

time <- 100
newd <- transplant; newd$futime <- time; newd$ltx <- 1
p <- 1 - exp(-predict(cph, type = "expected", newdata=newd))
y <- with(transplant, Surv(futime, ltx))

cal <- pmcalibration(y = y, p = p, smooth = "rcs", nk=5, ci = "pw", time = time)

summary(cal)

plot(cal)

}

pmcalibration documentation built on Sept. 8, 2023, 5:10 p.m.