2

我创建了这个生存模型(带有多条曲线)..

obj <- with(subscriptions, Surv(time=life_time, event=status, type="right"))
fit <- survfit(obj~sales_month, data=subscriptions)

..生成的fit对象将结果存储在fit$surv. 然而,就我而言,不同曲线/组的生存估计长度是不相等的。不同曲线的长度见fit$strata

基本上数据是这样的:

fit$surv <- 1:10
1  2  3  4  5  6  7  8  9 10

fit$strata <- c(5,3,2)
names(fit$strata) <- LETTERS[1:3]
A B C 
5 3 2

我需要将这些数据提取到data.table相同的组长度,就像这样..

strata   surv
A        1
A        2
A        3
A        4
A        5
B        6
B        7
B        8
B        NA
B        NA
C        9
C        10
C        NA
C        NA
C        NA

有没有一种简单的方法可以做到这一点 - 或者我完全错过了一些明显的东西?

4

2 回答 2

3

这是另一种不太优雅的方法:

n <- max(strata)
miss <- n-strata
newsurv <- c(surv, rep(NA,sum(miss)))
newnames <- c(rep(names(strata),strata), rep(names(strata), miss))
data.table(strata=newnames, surv=newsurv, key="strata")

这使 :

    strata surv
 1:      A    1
 2:      A    2
 3:      A    3
 4:      A    4
 5:      A    5
 6:      B    6
 7:      B    7
 8:      B    8
 9:      B   NA
10:      B   NA
11:      C    9
12:      C   10
13:      C   NA
14:      C   NA
15:      C   NA
于 2013-09-24T10:04:04.847 回答
1

I am not aware of any predefined function that does what you are looking for, but you can hack together a solution for it fairly easily. It might not qualify as simple, but it does get the job done efficiently.

attach(fit)
n <- max(strata)
rbindlist(mapply(function(st, su){
    data.table(strata = rep(st, n),
               surv = c(su, rep(NA, n - length(su))))
}, names(strata), split(surv, rep(names(strata), strata)), SIMPLIFY=FALSE))

Basically what it does is to split the values of surv into separate vectors based on strata, then make a data.table for each with a fixed number of n rows, and finally stacks them all together with rbindlist.

    strata surv
 1:      A    1
 2:      A    2
 3:      A    3
 4:      A    4
 5:      A    5
 6:      B    6
 7:      B    7
 8:      B    8
 9:      B   NA
10:      B   NA
11:      C    9
12:      C   10
13:      C   NA
14:      C   NA
15:      C   NA
于 2013-09-24T09:55:38.370 回答