# Incorporating Splines and Offset in MixedModels.jl

**URL:** <https://discourse.julialang.org/t/incorporating-splines-and-offset-in-mixedmodels-jl/136025>\
**Category:** Statistics\
**Tags:** mixed-models\
**Created:** [March 5, 2026, 1:38am UTC](https://discourse.julialang.org/t/incorporating-splines-and-offset-in-mixedmodels-jl/136025 "2026-03-05T01:38:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imantha](https://avatars.discourse-cdn.com/v4/letter/i/6a8cbe/32.png) [@imantha](https://discourse.julialang.org/u/imantha)\
**Post date:** [March 5, 2026, 1:38am UTC](https://discourse.julialang.org/t/incorporating-splines-and-offset-in-mixedmodels-jl/136025/1 "2026-03-05T01:38:20Z")

</div>

Hi,

I am working on project that required doing multilevel models and the dataset is quite large (7 million rows). My current implementation is in R but takes quite a bit of time using `glmmTMB` package. So, I was wondering whether this can be replicated using `MixedModels.jl` with the hopes that it will be faster.

My main issue is how to add an offset term and a spline. As for the offset I saw some old posts stating that it hasn’t been implemented but wondering if it has by now.

Here is my current R implementation

```julia-auto
library(glmmTMB)
glmm_formula_hosp <- hospitalisation_cum_lead7 ~  
  publicholiday +
  dow +
  splines::ns(date) +
  hw_mean_ehf_severity +
  age_band3 +
  sex +
  (1 | sa2_2021_code) +
  stats::offset(log(population_interp))

m_glmm <- glmmTMB::glmmTMB(
  formula = glmm_formula_hosp,
  data = df,
  family = glmmTMB::nbinom1(link = "log")
)

```

---

<div class="post-metadata">

**Author:** ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)\
**Post date:** [March 5, 2026, 2:59am UTC](https://discourse.julialang.org/t/incorporating-splines-and-offset-in-mixedmodels-jl/136025/2 "2026-03-05T02:59:03Z")

</div>

[Post from four years ago discusses offsets.](https://discourse.julialang.org/t/how-do-i-fit-generalised-linear-multilevel-models-including-offsets/74286/10). [Offsets are available.](https://github.com/JuliaStats/MixedModels.jl/pull/482) Where you will have to make a decision is whether to use `Poisson()` as an approximation to the negative binomial if overdispersion is mild. `Splines2` provides an equivalent to R’s `ns`. If negative binomial is a must-have, there’s `Turing` but performance will be slow.

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [March 5, 2026, 10:40pm UTC](https://discourse.julialang.org/t/incorporating-splines-and-offset-in-mixedmodels-jl/136025/3 "2026-03-05T22:40:45Z")

</div>

As @technocrat mentioned, offsets are supported for GLMMs in MixedModels.jl. We don’t have support for negative binomial models and it’s not currently on my list of things that I have enough personal interest in to implement. (If somebody else were to contribute code adding support for negative binomial models, that would be most welcome.) I am more interested in splines support, but we don’t have support built into the package. Splines2.jl doesn’t support the `@formula` macro out of the box, but the package maintainer [does provide some information on how to add it](https://github.com/mclements/Splines2.jl?tab=readme-ov-file#using-splines2-with-formula). If you did that, then splines should just (🤞) work.

It’s been a while since I looked at Splines2.jl, but a few things have happened in Julia since then, which would make adding official spline support much more straightforward. Realistically, I won’t have time to try this for at least a few days (which can easily turn into much longer since this is a hobby project of mine and I have a day job).

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [March 12, 2026, 4:23am UTC](https://discourse.julialang.org/t/incorporating-splines-and-offset-in-mixedmodels-jl/136025/4 "2026-03-12T04:23:57Z")

</div>

I have opened a [PR](https://github.com/mclements/Splines2.jl/pull/8) to Splines2.jl that shows the minimal chunk of code to add spline support with safe predictions.
