KM分析中需要的包
library(survival)
library(survminer)
几个函数的作用
surv 构建对象
survfit 拟合生存曲线
survdiff 差异检验
代码鸭
#画KM图
sfit <- survfit(Surv(time, event)~group, data=dat)
ggsurvplot(sfit, conf.int=F, pval=TRUE)
#计算log rank p
sdiff <- survdiff(Surv(time, event)~group, data=dat)
p.val = 1 - pchisq(sdiff$chisq, length(sdiff$n) - 1)
补充知识点----计算KM的HR
library(survival)
data.survdiff <- survdiff(Surv(time, status) ~ group)
p.val = 1 - pchisq(data.survdiff$chisq, length(data.survdiff$n) - 1)
HR = (data.survdiff$obs[2]/data.survdiff$exp[2])/(data.survdiff$obs[1]/data.survdiff$exp[1])
up95 = exp(log(HR) + qnorm(0.975)*sqrt(1/data.survdiff$exp[2]+1/data.survdiff$exp[1]))
low95 = exp(log(HR) - qnorm(0.975)*sqrt(1/data.survdiff$exp[2]+1/data.survdiff$exp[1]))
如果出现报错
Error in pchisq(sfit$chisq, length(sfit$n) - 1) :
数学函数中用了非数值参数
网友评论