Pregunta

I've created this survival model (with multiple curves)..

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

..and the resulting fit object stores the results in fit$surv. In my case however, the length of the survival estimates are unequal for the different curves/groups. The length of the different curves is found in fit$strata.

Basically the data looks like this:

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

I need to extract this data to a data.table of equal group length, like so..

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

Is there an easy way to do this - or have I missed something obvious altogether?

¿Fue útil?

Solución 2

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

Otros consejos

Here is another, not very elegant, way to do it :

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

Which gives :

    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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top