我尝试使用我喜欢的方法来创建四分位指标:
library(survival)
datalung <- lung
detach(datalung) # Agree with Zheyuan Li that attach()-ing is dangerous practice.
fit3<- survfit(Surv(time,status) ~ findInterval(age, quantile(age)[-5]),
data=datalung, type = "kaplan-meier")
需要删除向量中的第五个项目是拆分值,因为 findInterval 的拆分在左侧关闭,并且会得到第五个只有最大年龄的组。请注意,我们的四分位数结果是不同的。他的方法丢失了案例,而不仅仅是最小或最大组。他们去了哪里……我还不确定:
> fit3
Call: survfit(formula = Surv(time, status) ~ findInterval(age, quantile(age)[-5]),
data = datalung, type = "kaplan-meier")
n events median 0.95LCL 0.95UCL
findInterval(age, quantile(age)[-5])=1 49 32 320 226 533
findInterval(age, quantile(age)[-5])=2 57 41 340 245 433
findInterval(age, quantile(age)[-5])=3 55 39 310 267 524
findInterval(age, quantile(age)[-5])=4 67 53 285 229 363
您向李哲元提出的关于 ggplot 中级别顺序的问题暴露了使用 cut 的另一个陷阱,至少如果不提供带有“标签”参数的名称的话。级别是按词法排序的,“[”是>而不是“(”:
> levels(datalung$fage)
[1] "[39,56]" "(56,63]" "(63,69]" "(69,82]"
> "[" < "("
[1] FALSE
要解决我使用分位数与@ZheyuanLi 使用以及他对我的方法的错误描述的问题,只需检查一下:
> quantile(datalung$age)
0% 25% 50% 75% 100%
39 56 63 69 82
> with( datalung, table( findInterval(age, quantile(datalung$age)[-5] )))
1 2 3 4
49 57 55 67
所以大部分区别在于 56 岁的处理方式:
> sum(lung$age==56)
[1] 9
在使用 cut() 时试图解决标签问题(这不是我的责任,对吗?):
> library(ggplot2) # checked to make sure I have the most recent version per CRAN
> autoplot(fit2)
Error: Objects of type survfit not supported by autoplot.