R Kaplan-Meier曲线是一种非参数统计方法,用于估计生存函数,即在不同时间点上生存的概率。当数据中包含分类变量(如性别)或其他危险因素时,我们可以根据这些变量来划分曲线,以观察不同组之间的生存差异。
以下是如何在R中使用survival
和survminer
包来绘制按性别和其他危险因素划分的Kaplan-Meier曲线:
install.packages("survival")
install.packages("survminer")
library(survival)
library(survminer)
假设你有一个数据框df
,其中包含生存时间time
、事件指示status
、性别gender
和其他危险因素(例如risk_factor
)。
# 示例数据
df <- data.frame(
time = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
status = c(1, 1, 0, 1, 0, 1, 0, 1, 0, 1),
gender = c("Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female"),
risk_factor = c("High", "Low", "High", "Low", "High", "Low", "High", "Low", "High", "Low")
)
# 拟合基本的Kaplan-Meier模型
km_model <- survfit(Surv(time, status) ~ 1, data = df)
# 绘制按性别划分的Kaplan-Meier曲线
ggsurvplot(km_model, data = df, risk.table = TRUE, pval = TRUE, break.time.by = 20,
ggtheme = theme_minimal(), legend.title = "Gender",
facet.by = "gender", palette = "jco")
# 绘制按危险因素划分的Kaplan-Meier曲线
ggsurvplot(km_model, data = df, risk.table = TRUE, pval = TRUE, break.time.by = 20,
ggtheme = theme_minimal(), legend.title = "Risk Factor",
facet.by = "risk_factor", palette = "jco")
# 绘制按性别和危险因素划分的Kaplan-Meier曲线
ggsurvplot(km_model, data = df, risk.table = TRUE, pval = TRUE, break.time.by = 20,
ggtheme = theme_minimal(), legend.title = "Gender and Risk Factor",
facet.by = c("gender", "risk_factor"), palette = "jco")
survival
和survminer
包是最新版本。通过以上步骤,你可以轻松地在R中绘制按性别和其他危险因素划分的Kaplan-Meier曲线。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云