Syntax error with brms hierarquical non-linear model

Dear Stan/brms community,

I am currently having trouble when mixing multilevel with nonlinear models.
I want to introduce harmonic terms (sine wave) in the model. The following works:

pois_form <- bf(y ~ a*sin(b*t) + sex, a+b~1+sex,nl=T)
ab_priors <- prior(cauchy(0, 1), nlpar = "a", lb = 0) +
  prior(cauchy(0, 1), nlpar = "b",lb = 0)
poiss_fit <- brm(pois_form, prior = ab_priors,
                 data = gp_df, family = poisson())

However, when introducing a random effects term (either with (1 |gr(ParticipantID)) or (1 |ParticipantID))), I get a parsing error which seems to be related to the multilevel term in the Stan output code:

My formula becomes:

pois_form <- bf(y ~ a*sin(b*t) + sex + (1 |gr(ParticipantID)), a+b~1+sex,nl=T)

Which returns the follwowing error

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'model24a9763592c2_file24a96eaf1d9c' at line 36, column 59
  -------------------------------------------------
    34:   for (n in 1:N) {
    35:     // compute non-linear predictor values
    36:     mu[n] = nlp_a[n] * sin(nlp_b[n] * C_1[n]) + C_2[n] + (1|gr(C_3[n]));
                                                                  ^
    37:   }
  -------------------------------------------------
PARSER EXPECTED: ")"
Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname,  : 
  failed to parse Stan model 'file24a96eaf1d9c' due to the above error.

Am I missing something? Any ideas on how to fix it? I would also welcome any material on spectral decomposition (Fourier terms) for multilevel modeling considering covariates.

Welcome to the Stan forums! This question has been asked in differnt form in multiple threads already. Essentially, a formula can even be linear or non-linear with respect to how it is parsed. Multilevel terms only work in linear formulas, in your case in formulas for a and b not in the formula of y, which is nonlinear.

1 Like

Thank you, Paul.

I suppose I’ll have to work from make_stancode then.