简体   繁体   English

在R中为生存模型创建分组变量

[英]Creating a grouping variable for a survival model in R

I am trying to plot a KM curve in R but first I need to fit the survival object. 我试图在R绘制KM曲线,但首先我需要拟合生存对象。 I have a dataset that consists of 100 rows where each row corresponds to a patient who is either in group A or group B. What I would like to do is to be able to plot (on the same plot) KM curves for group A versus group B versus group A+B (so everyone). 我有一个包含100行的数据集,其中每行对应于A组或B组的患者。我想做的是能够绘制(在同一图上)A组与B组与A + B组(所以所有人)。 The trouble that I am having is figuring out how to construct the group variable. 我遇到的麻烦是弄清楚如何构造组变量。 I assume you can't do it in a single variable and so this is what I am trying, although it doesn't seem to be working correctly (I don't get everyone in group A and 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) 

So if you run the example and type os you will see that the sample size in AB = 27, when what I want it to be is 17+56=73. 因此,如果运行示例并输入os您将看到AB = 27的样本量,而我希望的是17 + 56 = 73。

One simple way is to create a new column indicating the group (A or B) the row belongs to, and bind this with the whole population (A+B). 一种简单的方法是创建一个新列,以指示该行所属的组(A或B),并将其与整个总体(A + B)绑定。 Then simply run the model against the group. 然后,仅对组运行模型。

# 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) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM