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]]
Vito Michele Rosario Muggeo
2019-Aug-29 06:54 UTC
[R] Efficient way to update a survival model
dear Frank, update() does not update actually.. It just builds a new call which is evaluated. To speed up the procedure you could try to supply starting values via argument 'init'. The first values come from the previous fit, and the last one referring to new coefficients is set to zero (or any other appropriate value). Something like (untested), for instance update(Cox[[2]], . ~ . + cos(3 * v), init=c(coef(Cox[[1]]),0), data = pbc) Hope this helps, best, vito "Frank S." <f_j_rod at hotmail.com> ha scritto:> 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]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Vito, Thanks for your reply! Following your suggestion, I have tried: Cox[[3]] <- update(Cox[[2]], . ~ . + cos(3 * v), init=c(coef(Cox[[1]]), 0, 0), data = pbc) Cox[[3]] <- update(Cox[[2]], . ~ . + cos(3 * v), data = pbc) and both expressions lead to the same result. Is that OK? Additionally, in my original question I wondered about the possibility of reducing the 10 lines of code to one general expression or some loop. Is it possible? Best, Frank ________________________________ De: Vito Michele Rosario Muggeo <vito.muggeo at unipa.it> Enviado: jueves, 29 de agosto de 2019 8:54 Para: Frank S. <f_j_rod at hotmail.com> Cc: r-help at r-project.org <r-help at r-project.org> Asunto: Re: [R] Efficient way to update a survival model dear Frank, update() does not update actually.. It just builds a new call which is evaluated. To speed up the procedure you could try to supply starting values via argument 'init'. The first values come from the previous fit, and the last one referring to new coefficients is set to zero (or any other appropriate value). Something like (untested), for instance update(Cox[[2]], . ~ . + cos(3 * v), init=c(coef(Cox[[1]]),0), data = pbc) Hope this helps, best, vito "Frank S." <f_j_rod at hotmail.com> ha scritto:> 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)). >[[elided Hotmail spam]]> > Frank > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]