# Non-integer time covariate for correlated residuals?

**URL:** <https://discourse.mc-stan.org/t/non-integer-time-covariate-for-correlated-residuals/6328>\
**Category:** brms\
**Created:** [November 3, 2018, 12:29pm UTC](https://discourse.mc-stan.org/t/non-integer-time-covariate-for-correlated-residuals/6328 "2018-11-03T12:29:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![idopen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/idopen/32/2821_2.png) [@idopen](https://discourse.mc-stan.org/u/idopen)\
**Post date:** [November 3, 2018, 12:29pm UTC](https://discourse.mc-stan.org/t/non-integer-time-covariate-for-correlated-residuals/6328/1 "2018-11-03T12:29:17Z")

</div>

Is there a way to model autocorrelated residuals with non-integer covariates with brms?

Something like autocor = cor\_??(~ time | subject), where time is an irregularly spaced continuous covariate. The grouping variable subject also has random intercepts and slopes with respect to the time variable.

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [November 4, 2018, 8:43am UTC](https://discourse.mc-stan.org/t/non-integer-time-covariate-for-correlated-residuals/6328/2 "2018-11-04T08:43:38Z")

</div>

One option are continuous autoregressive models, but they are rather complex and not implemented in brms. You can find those in the ctsem package.

How many time values per subject do you have? Any how irregularily spaced are they?

---

<div class="post-metadata">

**Author:** ![idopen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/idopen/32/2821_2.png) [@idopen](https://discourse.mc-stan.org/u/idopen)\
**Post date:** [November 4, 2018, 11:44am UTC](https://discourse.mc-stan.org/t/non-integer-time-covariate-for-correlated-residuals/6328/3 "2018-11-04T11:44:19Z")

</div>

Hi Paul,

Thanks for the advice regarding ctsem. I am not familiar with it and will check it out.

The data are quite messy, with 14 the median number of measurements per subject (a little over 500 subjects), ranging from 2 to 56. The median amount of time between measurements is 107 days, ranging all the way from 1 to 4500 days.

To make things even more messy, the treatments (4 kinds of anti-diabetes medication) are probably not independent of the response variable (blood glucose level), since the physicians prescribing them must have taken the current glucose level into account.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Charles\_Driver](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charles_driver/32/6524_2.png) [@Charles\_Driver](https://discourse.mc-stan.org/u/Charles_Driver)\
**Post date:** [November 4, 2018, 1:58pm UTC](https://discourse.mc-stan.org/t/non-integer-time-covariate-for-correlated-residuals/6328/4 "2018-11-04T13:58:25Z")

</div>

That’s all manageable with ctsem, for the treatment effect dependency you’d have to use the (new and not well documented) nonlinear estimator, which will slow things down, but if you’ve only one dependent variable then it should still be reasonable. The paper here: [https://www.researchgate.net/publication/323457904\_Understanding\_the\_time\_course\_of\_interventions\_with\_continuous\_time\_dynamic\_models](https://www.researchgate.net/publication/323457904_Understanding_the_time_course_of_interventions_with_continuous_time_dynamic_models)  
might give some ideas, and the model spec can be seen in one of the vignettes on CRAN:  
[https://cran.r-project.org/web/packages/ctsem/vignettes/hierarchical.pdf](https://cran.r-project.org/web/packages/ctsem/vignettes/hierarchical.pdf)  
The attached script includes an example with an intervention effect, and also conveniently (way down the bottom) includes a state dependent intervention. There’s a few things still in flux at the moment, the github version with rstan 2.18.1 is recommended.  
[ctsem\_script.R](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/4/406ac277c4a49bedd87c7542148b89223b3b80fe.R) (10.3 KB)

---

<div class="post-metadata">

**Author:** ![idopen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/idopen/32/2821_2.png) [@idopen](https://discourse.mc-stan.org/u/idopen)\
**Post date:** [November 4, 2018, 2:38pm UTC](https://discourse.mc-stan.org/t/non-integer-time-covariate-for-correlated-residuals/6328/5 "2018-11-04T14:38:31Z")

</div>

Thank you very much, Charles! That sounds very promising and I will check it out.
