# Efficient way to update a survival model Classic List Threaded 10 messages Open this post in threaded view
|

## Efficient way to update a survival model

 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[] <- update(Cox0, . ~ . + cos(1 * v), data =  pbc) Cox[] <- update(Cox[], . ~ . + cos(2 * v), data =  pbc) Cox[] <- update(Cox[], . ~ . + cos(3 * v), data =  pbc) Cox[] <- update(Cox[], . ~ . + cos(4 * v), data =  pbc) ... Cox[] <- update(Cox[], . ~ . + cos(10* v), data =  pbc) Since in practice I have to repeat above step until Cox[], 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[] <- 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[] onwards, the intermediate values of integer k are not included here (for  instance, the model Cox[] 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 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[], . ~ . + cos(3 * v), init=c(coef(Cox[]),0), data =  pbc) Hope this helps, best, vito "Frank S." <[hidden email]> 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[] <- update(Cox0, . ~ . + cos(1 * v), data =  pbc) > Cox[] <- update(Cox[], . ~ . + cos(2 * v), data =  pbc) > Cox[] <- update(Cox[], . ~ . + cos(3 * v), data =  pbc) > Cox[] <- update(Cox[], . ~ . + cos(4 * v), data =  pbc) > ... > Cox[] <- update(Cox[], . ~ . + cos(10* v), data =  pbc) > > Since in practice I have to repeat above step until Cox[], 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[] <- 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[] onwards, the intermediate values of integer k   > are not included here (for >  instance, the model Cox[] 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]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Efficient way to update a survival model

Open this post in threaded view
|

## Re: Efficient way to update a survival model

Open this post in threaded view
|

## Re: Efficient way to update a survival model

Open this post in threaded view
|

## Re: Efficient way to update a survival model

Open this post in threaded view
|

## Re: Efficient way to update a survival model

 The i^th model is included in the Cox[[ i ]] object. You can extract the formula objects with: frms <- lapply(Cox, formula) then if you want the existing and incremental terms: indeps <- lapply(frms, function(x) as.list( x[[ 3 ]] )) oldTerms <- lapply(indeps, "[[", 2) newTerms <- lapply(indeps, "[[", 3) > oldTerms[3:4] [] v + cos(1 * v) + cos(2 * v) [] v + cos(1 * v) + cos(2 * v) + cos(3 * v) > newTerms[ 3:4 ] [] cos(3 * v) [] cos(4 * v) > HTH, Chuck > On Aug 30, 2019, at 3:36 PM, Frank S. <[hidden email]> wrote: > > Chris, thank you for your elegant solution! > > Just one minor question: > I wonder how to include within the loop of your solution the 10 models, that is, writing > for (k in 1:10) so that you can get {Cox[], ..., Cox[]}. However, I'm aware that some > change has to be done due to the fact that, when computing Cox[], the term Cox[[k -1]] > does not exist. Is it possible to perform some "trick" (e.g. re-indexing) in order to achieve this? > > Best, > > Frank > ________________________________ > De: Andrews, Chris <[hidden email]> > Enviado: viernes, 30 de agosto de 2019 15:08 > Para: Frank S. <[hidden email]>; Vito Michele Rosario Muggeo <[hidden email]> > Cc: [hidden email] <[hidden email]> > Asunto: RE: [R] Efficient way to update a survival model > > The updated formula needs to have a different term rather than cos(k * v) every time.  Here is one way to explicitly change the formula. > > library("survival") > set.seed(1) > v <- runif(nrow(pbc), min = 0, max = 2) > Cox0 <- coxph(Surv(pbc\$time,pbc\$status == 2) ~ v, data =  pbc) > > Cox <- vector("list", 10) > Cox[] <- update(Cox0, . ~ . + cos(1 * v)) > for (k in 2:10) { >        form <- as.formula(sprintf(". ~ . + cos(%d * v)", k)) >        Cox[[k]] <- update(Cox[[k-1]], form) > } > > Cox > > -----Original Message----- > From: Frank S. [mailto:[hidden email]] > Sent: Friday, August 30, 2019 5:54 AM > To: Vito Michele Rosario Muggeo > Cc: [hidden email] > Subject: Re: [R] Efficient way to update a survival model > > Hi everyone, > > Vito, perhaps my previous mail was not clear.  It is true that I used a loop, but the key point is that such a loop > cannot compute the desired result. For example, for k = 3 the following loop > > Cox <- list() > Cox[] <- coxph(Surv(time,status == 2) ~ v + cos(v), data =  pbc) > for (k in 2:10) { >  Cox[[k]] <- update(Cox[[k-1]], . ~ . + cos(k * v), data =  pbc) > } > > leads to a model Cox[] which accounts for terms {v, cos(v), cos(3*v)}, but does not include the term cos(2*v). > I think that this could be one way to solve my question: > > library("survival") > set.seed(1) > v <- runif(nrow(pbc), min = 0, max = 2) > Cox0 <- coxph(Surv(pbc\$time,pbc\$status == 2) ~ v, data =  pbc) > k.max <- 9 > Z <- outer(v, 1:k.max, function (x, y) {sin(x * y)})  # Matrix with the outer product of the two arrays > > Cox <- list() > for (k in 1:k.max){ > Cox[[k]] <- >   update(Cox0, substitute(. ~ . + Z[, 1:k]), data =  pbc) >   attr(Cox[[k]]\$coefficients, "names")[2:(k+1)] <- paste0("sin(", 1:k, "* v)") > } > Cox > > Best, > > Frank > > _____ ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|