# Install packages
if (!requireNamespace("survival", quietly = TRUE)) {
install.packages("survival")
}if (!requireNamespace("rms", quietly = TRUE)) {
install.packages("rms")
}if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}
# Load packages
library(survival)
library(rms)
library(ggplotify)
Calibration Curve
The calibration curve is used to evaluate the consistency / calibration, i.e.Β the difference between the predicted value and the real value.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
survival
;rms
;ggplotify
Data Preparation
Data frame of multi columns data (Numeric allow NA). i.e the survival data (status with 0 and 1).
# Load data
<- read.table("files/Hiplot/018-calibration-curve-data.txt", header = T)
data
# convert data structure
<- lrm(as.formula(paste(
res.lrm "status ~ ",
paste(colnames(data)[3:length(colnames(data))], collapse = "+"))),
data = data, x = TRUE, y = TRUE)
<- calibrate(res.lrm, method = "boot", B = length(rownames(data)))
lrm.cal
# View data
head(data)
time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss
1 306 2 74 1 1 90 100 1175 NA
2 455 2 68 1 0 90 90 1225 15
3 1010 1 56 1 0 90 90 NA 15
4 210 2 57 1 1 90 60 1150 11
5 883 2 60 1 0 100 90 NA 0
6 1022 1 74 1 1 50 80 513 0
Visualization
# Calibration Curve
<- as.ggplot(function() {
p plot(lrm.cal,
xlab = "Nomogram Predicted Survival",
ylab = "Actual Survival",
main = "Calibration Curve"
) })
n=168 Mean absolute error=0.065 Mean squared error=0.00493
0.9 Quantile of absolute error=0.096
p
