# Problem defining non-linear formula

**URL:** <https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341>\
**Category:** brms\
**Created:** [October 1, 2020, 9:42pm UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341 "2020-10-01T21:42:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drjhw](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/drjhw/32/9524_2.png) [@drjhw](https://discourse.mc-stan.org/u/drjhw)\
**Post date:** [October 1, 2020, 9:42pm UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341/1 "2020-10-01T21:42:56Z")

</div>

I wish to model effects of different predictors on different parameters of a logistic distribution  
y=asym/(1+exp((midy-time)/halft))

I want the asym parameter to have the lognormal distribution, the midy parameter to have a normal distribution and the halft parameter to be lognormal (to constrain asym and halft to be positive). I want to allow random intercepts for each parameter

So, I tried defining the brms formula, thus:  
bflog=nlf(asym~x1+(1|g)+lognormal()) + nlf(midy~x2+(1|g)) + nlf(halft~x3+(1|g)+lognormal()) + nlf(y~asym/(1+exp((midy-xt)/halft))) + set\_nl(TRUE)

But this gives the error:  
Error in nlf(asym ~ x1 + (1 | g) + lognormal()) + nlf(midy ~ x2 + (1 | :  
non-numeric argument to binary operator

Each component is, by itself, OK. So, R accepts the following code without error.  
f1=nlf(asym~x1+(1|g)+lognormal())  
f2=nlf(midy~x2+(1|g))  
f3=nlf(halft~x3+(1|g)+lognormal())  
f4=nlf(y~asym/(1+exp((midy-xt)/halft)))

But, bflog=f1+f2+f3+f4 gives the same error as before:-  
Error in f1 + f2 : non-numeric argument to binary operator

I’d be very grateful for guidance on how to construct the non-linear formula

I am running 2.13.0 on Windows 10

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [October 5, 2020, 6:32am UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341/2 "2020-10-05T06:32:57Z")

</div>

Can you provided a small example with the data you have so we can pinpoint this?

---

<div class="post-metadata">

**Author:** ![drjhw](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/drjhw/32/9524_2.png) [@drjhw](https://discourse.mc-stan.org/u/drjhw)\
**Post date:** [October 5, 2020, 6:57am UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341/3 "2020-10-05T06:57:46Z")

</div>

Well, I can create a toy data-set. But, the problem lies in the creation of the formula - which does not involve the data. So, I get the same error with or without data present in the environment. Here is a toy example:-

# example data and program

set.seed(111); y=round(runif(1000)\*30); table(y)

g=rep(c(1:200),each=5); xt=rep(c(1:5),200); table(xt,g)

x1=rnorm(1000); x2=rnorm(1000); x3=rnorm(1000)

data=data.frame(cbind(g,xt,y,x1,x2,x3)); data[1:10,]

# now try to set up the formula

library(brms)

bflog=nlf(asym~x1+(1|g)+lognormal()) + nlf(midy~x2+(1|g)) + nlf(halft~x3+(1|g)+lognormal()) + nlf(asym/(1+exp(midy-xt)/halft)) + set\_nl(TRUE)

#\> Error in nlf(asym ~ x1 + (1 | g) + lognormal()) + nlf(midy ~ x2 + (1 | :

#\> non-numeric argument to binary operator

# alternatively, make asym and halft positive later, by specifying lognormal (or truncated normal) priors

bflog=nlf(asym~x1+(1|g)) + nlf(midy~x2+(1|g)) + nlf(halft~x3+(1|g)) + nlf(asym/(1+exp(midy-xt)/halft)) + set\_nl(TRUE)

#\> Error in nlf(asym ~ x1 + (1 | g)) + nlf(midy ~ x2 + (1 | g)) :

#\> non-numeric argument to binary operator

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 5, 2020, 7:08am UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341/4 "2020-10-05T07:08:30Z")

</div>

You cannot mix linear and non-linear formula syntax. That is (1 | g) does not belong inside a nonlinear formula.

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [October 5, 2020, 7:08am UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341/5 "2020-10-05T07:08:49Z")

</div>

hmm, I’ve never had to do this before… Let’s see what @paul.buerkner has to say about this…

---

<div class="post-metadata">

**Author:** ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)\
**Post date:** [October 5, 2020, 7:09am UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341/6 "2020-10-05T07:09:44Z")

</div>

Thanks @paul.buerkner… Now when you say it, it’s obvious… ;)

---

<div class="post-metadata">

**Author:** ![drjhw](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/drjhw/32/9524_2.png) [@drjhw](https://discourse.mc-stan.org/u/drjhw)\
**Post date:** [October 5, 2020, 7:10am UTC](https://discourse.mc-stan.org/t/problem-defining-non-linear-formula/18341/7 "2020-10-05T07:10:47Z")

</div>

Yes - that’s fixed it  
Thanks
