| ggcalibrate | R Documentation |
ggcalibrate plots the stats::predicted events against the actual event rate
ggcalibrate(
x1,
x2 = NULL,
y = NULL,
n_knots = 5,
ci_level = 0.95,
alpha_level = 0.25,
actuals = FALSE,
smooth_method = NULL,
smooth_span = NULL
)
x1 |
Either a logistic regression fitted using glm (base package) or lrm (rms package) or calculated probabilities (eg through a logistic regression model) of the baseline model. Must be between 0 & 1 |
x2 |
Either a logistic regression fitted using glm (base package) or lrm (rms package) or calculated probabilities (eg through a logistic regression model) of the new (alternative) model. Must be between 0 & 1 |
y |
Binary of outcome of interest. Must be 0 or 1 (if fitted models are provided this is extracted from the fit which for an rms fit must have x = TRUE, y = TRUE). |
n_knots |
The curves are made by fitting a restricted cubic spline (rms package). The default 5-knots is usually enough. |
ci_level |
Confidence interval of the curve (default = 0.95). |
alpha_level |
Transparency (alpha) of the shaded confidence interval (default = 0.25). |
actuals |
Logical, whether to also plot the actual events (0 or 1) against the predictions as short vertical marks, like a rug plot, at 0 and 1 (default = FALSE). |
smooth_method |
Deprecated and ignored. The curves are no longer drawn with geom_smooth(). |
smooth_span |
Deprecated and ignored. The curves are no longer drawn with geom_smooth(). |
The calibration curve for each model is a logistic regression of the outcome on a restricted cubic spline of the predicted probability. The confidence interval is calculated from the standard error of that fit on the log-odds scale and back-transformed, so it is bounded by 0 and 1.
a ggplot
[ggcalibrate_BA()] for the same curves plotted as deviations from perfect calibration.
# Quick example with subset of data
data(data_risk)
data_subset <- data_risk[1:100, ] # Use first 100 rows for speed
complete_cases <- complete.cases(data_subset)
data_clean <- data_subset[complete_cases, ]
y <- data_clean$outcome
x1 <- data_clean$baseline
x2 <- data_clean$new
output <- ggcalibrate(x1, x2, y, n_knots = 3, ci_level = 0.95)
# Full dataset example
data(data_risk)
complete_cases <- complete.cases(data_risk)
data_clean <- data_risk[complete_cases, ]
y <- data_clean$outcome
x1 <- data_clean$baseline
x2 <- data_clean$new
output <- ggcalibrate(x1, x2, y, n_knots = 5, ci_level = 0.95)
# Show the actual events and a darker confidence interval
output <- ggcalibrate(x1, x2, y, alpha_level = 0.5, actuals = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.