# Divergent transitions fitting smooths in brms

**URL:** https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517
**Category:** Modeling
**Tags:** fitting-issues, brms
**Created:** [August 13, 2026, 4:04pm UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517 "2026-08-13T16:04:19Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![PaulVBell](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@PaulVBell](https://discourse.mc-stan.org/u/PaulVBell)
#### Post date: [August 13, 2026, 4:04pm UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517/1 "2026-08-13T16:04:19Z")

</div>

I’m trying to get my head around using smoothing splines with brms.

I generated a simple dataset (sin wave plus normal noise):

 ![dataset1](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/e/a/ea824d047c0dadece085dde897303a8a3a97d96b.png)

And fit the following brms model, for B-splines with 10 knots:

```r
fit_brms <- brm(
  y ~ s(x, bs="bs", k=10),
  data = df, 
  family = gaussian,
  backend = "cmdstanr",
  refresh = 0
)

```

This fits quickly and the posterior looks reasonable (see plots below), but I ended up with 27 of 4000 divergent transitions. I would like to understand and eliminate these transitions!

In order to understand what was going on I decided to code the model myself in stan:

```stan
data {
    int n;
    int len_a;
    vector[n] y;
    matrix[n, len_a] X;
    matrix[len_a, len_a] S;
}

parameters {
    vector[len_a] a_tilde;
    real<lower=0> sigma_s;
    real<lower=0> sigma;
}

transformed parameters {
    vector[len_a] a = a_tilde * sigma_s;
    vector[n] mu = X * a;
}

model {
    target+= - quad_form(S, a_tilde);
    target+= normal_lpdf(y | mu, sigma);
}

```

I’m including the penalisation via the prior: p(a|\sigma\_s) = exp(- \frac{a^T S a}{2 \sigma\_s^2}). The a\_tilde term is to avoid divergent transitions by using a non-centred parametrisation.

I get the model matrix, X, and the penalisation matrix, S, using mgcv’s function smoothCon in R:

```r
sm_spec <- s(x, bs = "bs", k = 10)

sm_obj <- smoothCon(sm_spec, data = df, absorb.cons = TRUE)[[1]]

X <- sm_obj$X
S <- sm_obj$S[[1]]

```

Fitting this stan model, again the posterior looks reasonable and now I only get 1 to 2 divergent transitions in 4000 which I am more comfortable with getting rid of by increasing adapt\_delta.

I know of two big differences between my parametrisation and brms’s. First, I have flat priors on all parameters, whereas brms has a set of default priors (I’m not sure how to replicate equivalent priors to the brms ones in mine).

Second, I understand (from this blog by Tristan Mahr: [Random effects and penalized splines are the same thing - Higher Order Functions](https://www.tjmahr.com/random-effects-penalized-splines-same-thing/) ), that under the hood brms uses mgcv to convert the penalized spline problem to a mixed effects model.

My questions are as follows:

1. How should I go about trying to reduce divergent transitions for brms in this example?
2. brms have chosen a sensible parametrisation for computational reasons, if I used my stan code for more complex problems in the future, would I run into problems?

Fit using my stan code:

 ![stan_gam1](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/3/6/36f0a75ba48b99335dde3345a96e2cd96ef4aba2.png)

Fit using brms:

 ![brms_gam1](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/2/2/22c267593e9bc71b4cafc02c6568bccd51370d0b.png)

Fit using mgcv (REML):

 ![mgcv_gam1](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/b/2/b2e39e1448d26f8a84568c483fb3fa4cc5e09c5c.png)

---

<div class="post-metadata">

### Author: ![PaulVBell](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@PaulVBell](https://discourse.mc-stan.org/u/PaulVBell)
#### Post date: [August 14, 2026, 1:03pm UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517/2 "2026-08-14T13:03:38Z")

</div>

Quick update to this, I forgot to include an intercept term, new model is:

```stan
transformed parameters {
    vector[len_a] a = a_tilde * sigma_s;
    vector[n] mu = a_0 + X * a;
}

model {
    target+= cauchy_lpdf(sigma_s |0, 2.5);
    target+= - quad_form(S, a_tilde);
    target+= normal_lpdf(y | mu, sigma);
}

```

I had to include a prior on sigma\_s to avoid divergences but otherwise this didn’t change the previous comparison.

---

<div class="post-metadata">

### Author: ![js592](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@js592](https://discourse.mc-stan.org/u/js592)
#### Post date: [August 14, 2026, 1:38pm UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517/3 "2026-08-14T13:38:03Z")

</div>

I’ve had similar issues implementing various types of smooths – a few divergent transitions but the overall fit looks fine. I suspect it has something to do with either potential collinearity between the basis columns and/or scaling of the design matrix. Can you post a pairs plot of your posterior draws? It might be also worth looking into doing a QR decomposition of X, but in that case I’m not sure how the penalty matrix S would be derived.

---

<div class="post-metadata">

### Author: ![PaulVBell](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@PaulVBell](https://discourse.mc-stan.org/u/PaulVBell)
#### Post date: [August 14, 2026, 3:11pm UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517/4 "2026-08-14T15:11:42Z")

</div>

Thanks for the reply. Here is the pairs plot for a selection of brms parameters. It looks OK to me.

 ![pairs](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/3X/4/1/41b20109559dc80683e4ad8ff89390fda7ec156d.jpeg)

> [@js592](#):
>
> It might be also worth looking into doing a QR decomposition of X 𝑋, but in that case I’m not sure how the penalty matrix S 𝑆 would be derived.

Do you mean for the version I wrote or brms?

---

<div class="post-metadata">

### Author: ![js592](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@js592](https://discourse.mc-stan.org/u/js592)
#### Post date: [August 14, 2026, 4:19pm UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517/5 "2026-08-14T16:19:09Z")

</div>

I’m not sure what `brms` does under the hood. I meant for the Stan implementation – if you’re doing an unpenalized spline then you can just swap the design matrix into something like [Regression Models](https://mc-stan.org/docs/stan-users-guide/regression.html#QR-reparameterization.section), which could help if collinearity between the basis functions is an issue. But since you’re then doing inference on the transformed parameters, I don’t know if the penalty matrix formulation of the likelihood would still behave as intended.

---

<div class="post-metadata">

### Author: ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)
#### Post date: [August 15, 2026, 3:41pm UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517/6 "2026-08-15T15:41:10Z")

</div>

Smooths tend to have the problem that you get funnels with both centered and non-centered parameterizations as illustrated by @Niko in his blog post [**Divergent transitions in Hilbert Space Gaussian process posteriors and how to avoid them**](https://www.generable.com/post/hsgp-reparam). Although the blog post uses HSGP as the example, the same problem happens with the usual spline implementations. Sometimes increasing adapt\_delta a little bit can help, but sometimes it may require switching to adaptive parameterization, but as it is not automatically used by brms, it may require some effort.

---

<div class="post-metadata">

### Author: ![PaulVBell](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@PaulVBell](https://discourse.mc-stan.org/u/PaulVBell)
#### Post date: [August 18, 2026, 8:48am UTC](https://discourse.mc-stan.org/t/divergent-transitions-fitting-smooths-in-brms/41517/7 "2026-08-18T08:48:47Z")

</div>

Thanks for the answer and the link to the blog post, that’s very helpful for my understanding.
