Using a spline in the longitudinal submodel of stan_jm

I’m wondering if it is possible to include a covariate in the longitudinal submodel of a joint model as a spline, probably with one or two knots at most, when using stan_jm. The longitudinal covariate I’m working with has a definite non-linear relationship with time. I haven’t had any luck googling the issue since any search with spline and stan_jm just returns hits about modeling the baseline hazard as b-splines or m-splines.

For an example, here is a slightly modified version of the model from the Estimating Joint Models vignette where logBili is just a function of time.

library(rstanarm)
mod1 <- stan_jm(formulaLong = logBili ~ year + (year | id),
                dataLong = pbcLong,
                formulaEvent = survival::Surv(futimeYears, death) ~ sex + trt,
                dataEvent = pbcSurv,
                time_var = "year",
                chains = 1, refresh = 2000, seed = 12345)

Let’s imagine that the relationship between logBili and year is non-linear and would be best modeled with a spline. I’d like to be able to fit something like:

library(rstanarm)
mod1 <- stan_jm(formulaLong = logBili ~ s(year) + (year | id),
                dataLong = pbcLong,
                formulaEvent = survival::Surv(futimeYears, death) ~ sex + trt,
                dataEvent = pbcSurv,
                time_var = "year",
                chains = 1, refresh = 2000, seed = 12345)

but it doesn’t seem possible to run that chunk as is. Is there some way to incorporate a spline in this model without writing my own custom stan code?

It’s been so long since I’ve had my head in the details, but from memory I’m pretty sure this should be possible. You should just be able to use a function in the formula to generate the splines, most likely one of the functions from the splines2 package. So just try replacing the s() in your formula with something like the B splines function from splines2. The prediction functions should also work after that I think, since they’ll use the predvars attribute of the splines in the fitted model. If that doesn’t work let us know and I’ll try take a closer look. On a phone at the moment unfortunately, so not able to test it myself.

1 Like

Oh hey this totally worked. I’ve only ever used splines from mgcv so I didn’t think to try using a spline implementation from another package. Thank you!

1 Like

No worries, glad it worked! Cheers