Priors and Gaussian process hyperparameters

Hi everyone,

I’m working on fitting an ODE model using brms and Stan, and I’m encountering some issues with my code. I’m new to Bayesian inference and Stan, so I’d appreciate any guidance you can provide.

The model I’m trying to fit is:

\frac{dn}{dt} = φ(t) - μ \cdot n(t), \\ n(t=0) = 0

where μ is a parameter to be inferred, and φ(t) is a non-parametric function modeled using a Gaussian process. The analytical solution to the ODE is:

n(t) = \int_0^t \exp(-\mu (t-s)) \phi(s) \, ds

I’ve attempted to implement this in brms with the following code:

data = data.frame(
  t = c(0, 1, 2, 3, 4, 5, 6, 7),
  n = c(0.00, 0.39, 0.57, 0.50, 0.12, 0.06, 0.02, 0.00),
  dndt = c(0.39, 0.28, 0.05, -0.23, -0.22, -0.05, -0.03, -0.02)
)

fit = brms::brm(
  formula = brms::bf(dndt ~ gp(t) - mu * n, mu ~ 1, nl = TRUE),
  data = data,
  prior = brms::set_prior("gamma(2, 0.1)", nlpar = "mu"),
  sample_prior = "yes",
  family = gaussian(),
  control = list(adapt_delta = 0.95),
  chains = 4,
  iter = 2000,
  cores = 4,
  algorithm = "sampling",
  silent = 0
)

summary(fit)
plot(fit)

However, I’m getting the following error:

Error: The following priors do not correspond to any model parameter: 
b_mu ~ gamma(2, 0.1)

I’m trying to enforce a positive prior on μ (hence the gamma distribution), but it seems the model isn’t recognizing the mu parameter correctly. Instead, the output only includes sigma and an intercept, which I suspect might be mu.

Additionally, I need to extract both μ and the hyperparameters of the Gaussian process (gp(t)) so that I can reconstruct the function n(t) afterwards.

Could someone kindly help me understand what’s going wrong with the priors and how I can properly specify the model to estimate μ and the GP hyperparameters? Any suggestions or corrections to my code would be greatly appreciated!

Thank you in advance for your help!

Your immediate problem is that you used mu which has a special meaning in brms. But once you replace that with something else, your next problem will be having a gp() term inside a nonlinear formula.

Compiling Stan program...
Semantic error in '/tmp/RtmpN8rJ8H/model-b7f53bf1660f.stan', line 35, column 15 to column 25:
   -------------------------------------------------
    33:      for (n in 1:N) {
    34:        // compute non-linear predictor values
    35:        mu[n] = (gp(C_1[n]) - nlp_m[n] * C_2[n]);
                        ^
    36:      }
    37:      target += normal_lpdf(Y | mu, sigma);
   -------------------------------------------------

A returning function was expected but an undeclared identifier 'gp' was supplied.
A similar known identifier is 'N'

make: *** [make/program:66: /tmp/RtmpN8rJ8H/model-b7f53bf1660f.hpp] Error 1

Error: An error occured during compilation! See the message above for more information.

I really appreciate your help! I didn’t know that mu was a reserved variable in brms, and I’ll make sure to use a different variable name in my formula. However, I’m still unclear about the limitations of using gp() in non-linear models. Is it truly not supported at all in non-linear formulas? If so, what are my options for incorporating a Gaussian process into a non-linear model?

Any advice or examples you can share would be incredibly helpful. Thank you again for your time and expertise!

I have never fitted such a model before. A key insight from the relevant vignette is that

we should think of non-linear parameters as placeholders for linear predictor terms rather than as parameters themselves

So this would be a way to go about it, I think:

bf(dndt ~ ef - mew * n, 
        ef ~ gp(t), 
        mew ~ 1, nl = TRUE)

However, this throws an opaque warning, so, you know, tread carefully:

2: In gsub("\\[[[:digit:]]+\\]", paste0("__", ind), par) :
  argument 'replacement' has length > 1 and only the first element will be used

Thank you for your response! Your suggested formula makes sense, but I’m also running into that warning. Do you have any insights into what might be causing it?

I really appreciate your help!