More on Cox model with time-varying covariates

310 views
Skip to first unread message

John Molitor

unread,
Nov 15, 2023, 11:14:50 PM11/15/23
to R-inla discussion group
Hello,

I'm struggling to fit a Cox model with time varying covariates, where the time-varying covariates enter at a variety of time points. In particular, I am trying to replicate the GCD example presented in:


After using "tmerge", the data is formatted as (page 7):

> newcgd[1:5,c(1, 4:6, 13:17)]
id treat sex age futime tstart tstop infect enum
1 1 1 2 12 414 0 219 1 1
2 1 1 2 12 414 219 373 1 2
3 1 1 2 12 414 373 414 0 3
4 2 0 1 15 439 0 8 1 1
5 2 0 1 15 439 8 26 1 2

And the model is run with:

> coxph(Surv(tstart, tstop, infect) ~ treat + inherit + steroids,
data =newcgd, cluster = id)

Note that the data are (after manipulation) represented via disjoint time intervals within each id (interval). Could I fit a piecewise Poisson with separate baseline hazards for each time interval within each id, but then fit a random effect ("cluster") for id?

I did look at the advice regarding weighing time-varying covariates via midpoints of time intervals, but I'm not sure how this applies here.

Thank  you very much in advance!

John Molitor

Denis Rustand

unread,
Nov 16, 2023, 10:22:13 AM11/16/23
to R-inla discussion group
Hi John,

I am not sure I see what should be considered as a time-varying covariate in this example, it looks more like a recurrent events survival model.
You can use the data as it is to replicate the model, by using truncation to continue follow-up for an individual after an event is observed. Then you can also add an individual random effect if needed. See the attached code.
If you want to include a time-varying covariate measured at specific time points, you can adapt the baseline risk to decompose the follow-up with cutpoints matching the measurement times but in this example, the 3 covariates are not time-varying.

Best,
Denis Rustand
he...@r-inla.org 
coxTV.R

John Molitor

unread,
Nov 17, 2023, 8:50:30 PM11/17/23
to R-inla discussion group
Dennis,

Thank you very much! This was very helpful.

John

John Molitor

unread,
Mar 3, 2024, 12:02:42 AM3/3/24
to R-inla discussion group
I'm still having trouble with time-varying covariates with the Cox model. I'm trying to use Model B located here: "https://stats.oarc.ucla.edu/r/examples/alda/r-applied-longitudinal-data-analysis-ch-15/

The code give error: "Error in max(sapply(y.surv, length)) : invalid 'type' (list) of argument"

Any help would be greatly appreciated:

------------------------
rm(list=ls())

library(survival)
library(INLA)

## https://stats.oarc.ucla.edu/r/examples/alda/r-applied-longitudinal-data-analysis-ch-15/

cocaine <- read.csv("https://stats.idre.ucla.edu/stat/examples/alda/firstcocaine.csv")
cocaine$EVENT <- 1 - cocaine$CENSOR
cocaine <- cocaine[, c("ID", "BIRTHYR", "COKEAGE", "EARLYMJ", "EARLYOD",
                       "EVENT", "MJAGE", "ODAGE", "SELLMJAGE", "SDAGE")]

###############
### Model B ###
###############

longModBDF <- tmerge(
    cocaine,
    cocaine,
    id = ID,
    start_using = event(COKEAGE, EVENT),
    USEMJ = tdc(MJAGE),
    USEOD = tdc(ODAGE)
)
head(longModBDF[order(longModBDF$ID),])

modB <- coxph(Surv(tstart, tstop, start_using) ~ BIRTHYR + USEMJ + USEOD,
              data= longModBDF, ties="efron")
summary(modB)

### INLA ###

y.surv <- inla.surv(
    time=longModBDF$tstop,
    event=longModBDF$start_using,
    truncation=longModBDF$tstart
)

f <- y.surv ~ 1 + BIRTHYR + USEMJ + USEOD
mod.inla.cox <- inla.coxph(
    f,
    data=longModBDF,
    control.hazard=list(n.intervals=30)
)
## mod <- inla(
##     mod.inla.cox$formula,
##     family=mod.inla.cox$family,
##     E=mod.inla.cox$E,
##     data=c(as.list(mod.inla.cox$data),mod.inla.cox$data.list)
## )
   


Denis Rustand

unread,
Mar 3, 2024, 1:34:17 AM3/3/24
to R-inla discussion group
Hi John,

You can just include the inla.surv() call in the formula object, as follows:

f <- inla.surv(
  time=tstop,
  event=start_using,
  truncation=tstart
) ~ -1 + BIRTHYR + USEMJ + USEOD

Best,
Denis Rustand
he...@r-inla.org

Denis Rustand

unread,
Mar 3, 2024, 2:37:34 AM3/3/24
to Molitor, John T, R-inla discussion group
In your example, the time points are equally spaced and there are not so many so yes, you can just set cutpoints for integers between 1 and 42 (i.e., max observed time). When there are many event times, and some of them are very close to each other, it is easier to just set more intervals (just increase the n.interval so you have a fine resolution). An alternative approach is to impute the time-dependent covariate values for the corresponding intervals if you want to keep the cutpoints at specific values but it's more involved and likely less accurate.

Best,
Denis

Le dim. 3 mars 2024 à 10:00, Molitor, John T <John.M...@oregonstate.edu> a écrit :
Wow!  Thanks for the quick reply. One extra bit, should I manually adjust the cutpoints somehow to accommodate the time dependent covariants?

John

Sent from my iPhone

On Mar 2, 2024, at 10:37 PM, Denis Rustand <denis....@gmail.com> wrote:


You don't often get email from denis....@gmail.com. Learn why this is important

[This email originated from outside of OSU. Use caution with links and attachments.]

--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/403d9d24-e37c-4c0e-be46-bc303ed9e989n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages