临床研究R语言:列线图(Nomogram)绘制

如题所述

第1个回答  2024-08-08
在临床研究中,列线图作为预测模型的可视化工具备受欢迎。特别是在分析二分类结果时,它能有效地展示变量之间的关系和预测性能。以下是一段使用R语言绘制列线图(Nomogram)并进行性能检测的代码示例:

首先,确保加载必要的库和数据:

library(readxl)
data <- read_excel("亚组分析数据集.xlsx", sheet = "SampleData")
View(data)

接着,对数据进行预处理,将类别变量转换为因子:

data %>%
mutate(
Infection = factor(Group, levels = c("0", "1"), labels = c("否", "是")),
Group = factor(Group, levels = c("0", "1"), labels = c("开放", "微创")),
Age = factor(Age, levels = c("0", "1"), labels = c("<65岁", "≥65岁")),
Sex = factor(Sex, levels = c("0", "1"), labels = c("女", "男")),
Hepatitis = factor(Hepatitis, levels = c("0", "1"), labels = c("无", "有")),
Cirrhosis = factor(Cirrhosis, levels = c("0", "1"), labels = c("无", "有")),
Diabetes = factor(Diabetes, levels = c("0", "1"), labels = c("无", "有"))
) -> data

然后,使用rms库绘制列线图和进行Bootstrap校正:

library(rms)
data <- data[, -c(13)]
dd <- datadist(data)
options(datadist = 'dd')
formula <- as.formula(Infection ~ Group + Age + Sex + Hepatitis + Cirrhosis + Diabetes)
fit <- lrm(formula, data = data, x = TRUE, y = TRUE)
nom <- nomogram(fit,
fun = function(x) 1 / (1 + exp(-x)),
lp = FALSE,
fun.at = c(0, 0.2, 0.4, 0.6, 0.8, 1.0),
funlabel = 'Risk')
plot(nom)

cal <- calibrate(fit, method = 'boot', B = 1000, data = data)
plot(cal,
xlim = c(0, 1.0), ylim = c(0, 1.0),
xlab = "Predicted Probability",
ylab = "Observed Probability")
lines(cal[, c('predy', 'calibrated.corrected')],
lwd = 2, lty = 1, col = "green1") # bias-corrected
lines(cal[, c('predy', 'calibrated.orig')],
lwd = 2, lty = 1, col = "coral")
legend(0.6, 0.2,
c("Apparent", "Bias-corrected", "Ideal"),
lty = c(1, 1, 2), lwd = c(1, 1, 1), col = c("coral", "green1", "black"), bty = "n")

最后,计算训练集的ROC曲线:

library(pROC)
model <- glm(formula, data = data, family = binomial(link = "logit"))
predvalue <- predict(model, type = "response", data = data)
ROC <- roc(data$Infection, predvalue)
ci(auc(ROC))

pdf("roc.pdf", 8, 8)
plot(ROC, print.auc = TRUE, auc.polygon = T, lwd = 3, max.auc.polygon = T, print.thres = T)

这段代码展示了如何使用R语言绘制列线图,以及如何通过Bootstrap校正和计算ROC曲线来评估模型性能。
相似回答
大家正在搜