[R] Efficient way to update a survival model
Frank S.
|_j_rod @end|ng |rom hotm@||@com
Wed Sep 4 11:55:47 CEST 2019
Charles, thank you for your suggestion!
Frank S.
________________________________
De: Berry, Charles <ccberry using ucsd.edu>
Enviado: s�bado, 31 de agosto de 2019 19:21
Para: Frank S. <f_j_rod using hotmail.com>
Cc: Andrews, Chris <chrisaa using med.umich.edu>; r-help using r-project.org <r-help using r-project.org>
Asunto: 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]
[[1]]
v + cos(1 * v) + cos(2 * v)
[[2]]
v + cos(1 * v) + cos(2 * v) + cos(3 * v)
> newTerms[ 3:4 ]
[[1]]
cos(3 * v)
[[2]]
cos(4 * v)
>
HTH,
Chuck
> On Aug 30, 2019, at 3:36 PM, Frank S. <f_j_rod using hotmail.com> wrote:
>
[[elided Hotmail spam]]
>
> 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[[1]], ..., Cox[[10]]}. However, I'm aware that some
> change has to be done due to the fact that, when computing Cox[[1]], 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 <chrisaa using med.umich.edu>
> Enviado: viernes, 30 de agosto de 2019 15:08
> Para: Frank S. <f_j_rod using hotmail.com>; Vito Michele Rosario Muggeo <vito.muggeo using unipa.it>
> Cc: r-help using r-project.org <r-help using r-project.org>
> 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[[1]] <- 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:f_j_rod using hotmail.com]
> Sent: Friday, August 30, 2019 5:54 AM
> To: Vito Michele Rosario Muggeo
> Cc: r-help using r-project.org
> 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[[1]] <- 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[[3]] 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
>
> _____
[[alternative HTML version deleted]]
More information about the R-help
mailing list