我试图在R
中绘制KM曲线,但首先我需要适应生存对象。我有一个由100行组成的数据集,其中每行对应于A组或B组中的患者。我想要做的是能够绘制(在同一图表上)A组的KM曲线B组与A + B组(所以每个人)。我遇到的麻烦是弄清楚如何构建组变量。我假设你不能在一个变量中做到这一点,所以这就是我正在尝试的,虽然它似乎没有正常工作(我不能让A组和B组中的每个人都这样) 。
set.seed(4)
n = 100
x = runif(n,0,1500)
y = runif(n,0,5)
survival = runif(n,1,1000)
censor = rbinom(n,1,.5)
dat = data.frame(x=x,y=y,survival=survival,censor=censor)
### Create a group indicator variable
# 1: Group A
# 2: Group B
# 3: Everyone else
group = rep(3,nrow(dat))
group[which(dat$x < 730.5)] = 1
group[which(dat$y >= 2)] = 2
### Kaplan Meier curves
# Need new group indicator variables
A = ifelse(group == 1,1,0)
B = ifelse(group == 2,1,0)
AB = ifelse(group != 3,1,0)
### Overall survival
os = survfit(Surv(dat$survival,dat$censor)~A + B + AB,data=dat)
因此,如果您运行示例并键入os
,您将看到AB = 27中的样本大小,当我想要的是17 + 56 = 73时。
答案 0 :(得分:2)
一种简单的方法是创建一个新列,指示该行所属的组(A或B),并将其与整个群体(A + B)绑定。 然后只需针对该组运行模型。
# Create a new variable to indicate the group and drop the group you don't need.
dat$group = "C"
dat$group = ifelse( dat$x < 730.5, "A", dat$group )
dat$group = ifelse( dat$y >= 2, "B", dat$group )
dat = subset( dat, dat$group != "C" )
# Bind the sample with the population
dat2 = dat
dat2$group = "AB"
data = rbind( dat2, dat )
table( data$group )
# A AB B
# 17 73 56
# Plot
plot( survfit(Surv(data$survival,data$censor)~group,data=data) )