[R] Efficient way to update a survival model

Frank S. |_j_rod @end|ng |rom hotm@||@com
Wed Aug 28 21:51:40 CEST 2019


Hello everybody, I come with a question which I do not know how to conduct in an efficient way. In order to
provide a toy example, consider the dataset "pbc" from the package "survival". First, I fit the Cox model "Cox0":

library("survival")
set.seed(1)
v <- runif(nrow(pbc), min = 0, max = 2)
Cox0 <- coxph(Surv(pbc$time,pbc$status == 2) ~ v, data =  pbc)

Then, from the above model, I can fit recursively 10 additional models as:

Cox <- list()

Cox[[1]] <- update(Cox0, . ~ . + cos(1 * v), data =  pbc)
Cox[[2]] <- update(Cox[[1]], . ~ . + cos(2 * v), data =  pbc)
Cox[[3]] <- update(Cox[[2]], . ~ . + cos(3 * v), data =  pbc)
Cox[[4]] <- update(Cox[[3]], . ~ . + cos(4 * v), data =  pbc)
...
Cox[[10]] <- update(Cox[[9]], . ~ . + cos(10* v), data =  pbc)

Since in practice I have to repeat above step until Cox[[100]], say, do you know an efficient way to
wrap this code chunk in a loop or similar?

I had tried:

set.seed(1)
v <- runif(nrow(pbc), min = 0, max = 2)
Cox0 <- coxph(Surv(pbc$time,pbc$status == 2) ~ v, data =  pbc)

Cox <- list()
Cox[[1]] <- update(Cox0, . ~ . + cos(1 * v), data =  pbc)
for (k in 1:10) {
  Cox[[k + 1]] <- update(Cox[[k]], . ~ . + cos((k + 1) * v), data =  pbc)
}

However, from Cox[[3]] onwards, the intermediate values of integer k are not included here (for
 instance, the model Cox[[10]] would only include the cosinus terms for cos(1*v) and cos(10*v)).

Thanks in advance for any help!

Frank

	[[alternative HTML version deleted]]



More information about the R-help mailing list