不使用survcomp,补充完善以下代码,添加两两之间C-INDEX的对比是否有意义。library(foreign) library(survival) # 1. 导入数据集 my_data <- read.csv(file="D:/5放射诊断/R生存分析/nafld.csv") # 2. 转换分级变量 my_data$CACSgrades <- factor(my_data$CACSgrades) levels(my_data$CACSgrades) <- c("1", "2", "3", "4") my_data$CACSgrades <- relevel(my_data$CACSgrades, ref = "1") my_data$CADRADS <- factor(my_data$CADRADS) levels(my_data$CADRADS) <- c("0","1", "2", "3", "4", "5") my_data$CADRADS <- relevel(my_data$CADRADS, ref = "0") # 3.单因素Cox回归模型拟合 # 定义生存时间和事件结果变量 surv <- with(my_data, Surv(time, MACE==1)) #Cox回归模型拟合,多因素,CACSgrades fit_1 <- coxph(Surv(time, MACE==1) ~ age + Diabetes + Hypertension + CACSgrades + CADRADS + SIS + SSS, data = my_data) summary(fit_1) fit_2 <- coxph(Surv(time, MACE==1) ~ age + Diabetes + Hypertension + CACSgrades + CADRADS + SIS + SSS + NAFLD, data = my_data) summary(fit_2) fit_3 <- coxph(Surv(time, MACE==1) ~ age + Diabetes + Hypertension + CACSgrades + CADRADS + SIS + SSS + PCATgrade, data = my_data) summary(fit_3) fit_4 <- coxph(Surv(time, MACE==1) ~ age + Diabetes + Hypertension + CACSgrades + CADRADS + SIS + SSS + PCATgrade + NAFLD, data = my_data) summary(fit_4)
时间: 2024-04-22 09:23:52 浏览: 161
# 4. 添加两两之间C-INDEX的对比是否有意义
# 定义一个函数,计算C-INDEX
c_index <- function(model1, model2, data, time, event) {
# 获取模型1和模型2的预测值
pred1 <- predict(model1, newdata = data, type = "lp")
pred2 <- predict(model2, newdata = data, type = "lp")
# 计算C-INDEX
concordance <- survival::concordance.index(time, -pred1, event, weights = NULL,
strata = NULL, robust = FALSE,
cluster = NULL, method = "ipcw",
R = 100, var.type = "C", conf.int = FALSE,
conf.level = 0.95, ties = "efron")$concordance
concordance2 <- survival::concordance.index(time, -pred2, event, weights = NULL,
strata = NULL, robust = FALSE,
cluster = NULL, method = "ipcw",
R = 100, var.type = "C", conf.int = FALSE,
conf.level = 0.95, ties = "efron")$concordance
# 输出结果
if (concordance > concordance2) {
cat("The first model has a higher C-INDEX than the second model: ", concordance - concordance2, "\n")
} else if (concordance < concordance2) {
cat("The second model has a higher C-INDEX than the first model: ", concordance2 - concordance, "\n")
} else {
cat("The two models have the same C-INDEX: ", concordance, "\n")
}
}
# 两两比较模型的C-INDEX
c_index(fit_1, fit_2, my_data, "time", "MACE")
c_index(fit_1, fit_3, my_data, "time", "MACE")
c_index(fit_1, fit_4, my_data, "time", "MACE")
c_index(fit_2, fit_3, my_data, "time", "MACE")
c_index(fit_2, fit_4, my_data, "time", "MACE")
c_index(fit_3, fit_4, my_data, "time", "MACE")
阅读全文