# Mean in Bayesian formula for non-linear modeling with brms

I am trying to fit non-linear models with `brms`. My model is:

``````y = intercept + b1 * (x1 * mean(x2^b2))
``````

where:

• `x1` is a measured continuous variable,
• `x2` is also a continuous variable nested in `x1`. For more context, `x1` is a population size and `x2` is the mass of a subset of individuals of that population.
• `intercept`, `b1` and `b2` are parameters to be estimated

A colleague of mine coded it in `rjags` this way:

``````y1[i] <- x1[i]*(mean(x2_matrix[1:no_per_site[i],i]^b2))
y2[i] <- intercept + y1[i]*b1
``````

Where `i` is the site index, `x2_matrix` has one row per individual mass (`x2`) values and one column per site, and where `no_per_site` is the number of `x2` measured in each site.

Is there a way to fit this model in `brms`? How do I manage the `x2` matrix?

Here is the structure of the data:

``````'data.frame':   31 obs. of  3 variables:
\$ population: chr  "p1" "p2" "p3" "p4" ...`
\$ y2        : num  592 551 1720 5135 3710 ...
\$ x1        : int  145 145 72 3173 3173 1262 1262 504 504 777 ...
\$ no_per_site: num 104 55 187 102 ...
``````

and the x2 matrix:

``````num [1:187, 1:31] 530 600 460 510 325 420 490 430 450 350 ...
``````

Thank you!

1 Like

Sounds like you might look through the non-linear vignette: Estimating Non-Linear Models with brms