It may be that I have not entirely understood what I have researched online but is there an example of how to include a user defined function in brms, where the function is of the predictor variable(s) and there is a parameter to be optimized. The function f() consumes one of the predictor variables in a regression and there is a parameter p1 that I need to optimize during fitting. Is this possible?

y~f(x,p1)

Specifically, if it helps, I am looking to optimize the adstock function:

adstock_loop<-function(x,rate)

{

adstock_x<- vector(mode=“numeric”, length=length(x))

adstock_x[1]<-x[1]

for (i in 2:length(x))

{

```
adstock_x[i]<-x[i]+rate*adstock_x[i-1]
```

}

return(adstock_x)

}

Is the best way to generate the stan code and then try and alter it to add this function in?

Could be one option if you don’t want to write the Stan code from scratch.