I am currently trying to reproduce results I found on this post in `brms`

. The goal is to see if I can recover adstock rates, coefficients, and intercept using a non-linear model from simulated data.

The only non-linear models I have tried are the ones in `vignette(brms_nonlinear)`

so I am betting I am making an embarrassingly simple mistake.

The error I am getting is

Error in stanc(model_code = paste(program, collapse = “\n”), model_name = model_cppname, :

failed to parse Stan model ‘file290d3c2c341a’ due to the above error.

I am guessing this is due to the `adstock`

function being part of my model.

My code is as follows:

```
# load libraries
library(purrr)
library(tidyverse)
# positive_round function
positive_round <- function(...) round(pmax(..., 0), 0)
# adstock function
adstock<-function(x,rate=0){
return(as.numeric(stats::filter(x=x,filter=rate,method="recursive")))
}
# base and ad constants
base <- 100
n_weeks <- 104
avg <- 20
sdev <- 10
# adstock rates
ad1_rate <- .7
ad2_rate <- .4
ad3_rate <- .5
set.seed(42)
# 3 ads with mean
fake_df <- rep(avg, 3) %>%
# normal distribution with 2 years of observations
map(~ rnorm(n = n_weeks, mean = .x, sd = sdev)) %>%
# make sure there are no 0s and make values integers
map(~ positive_round(0,.x)) %>%
# convert lists to data frame
map_dfc(~as_tibble(.x)) %>%
# sensible names
setNames(c("ad1", "ad2", "ad3")) %>%
# sales = sum of adstock and some noise
mutate(sales = base +
adstock(ad1, ad1_rate) +
adstock(ad2, ad2_rate) +
adstock(ad3, ad3_rate) +
rnorm(n(), sd = 5)) %>%
# round sales to whole number
mutate(sales = round(sales, 0))
# Model building
library(brms)
prior1 <- prior(normal(.5, .5), nlpar = "rate1") +
prior(normal(.5, .5), nlpar = "rate2") +
prior(normal(.5, .5), nlpar = "rate3") +
prior(normal(1, .5), nlpar = "b1") +
prior(normal(1, .5), nlpar = "b2") +
prior(normal(1, .5), nlpar = "b3") +
prior(normal(100, 50), nlpar = "basesales")
fit1 <- brm(bf(sales ~ basesales +
b1 * adstock(ad1, rate1) +
b2 * adstock(ad2, rate2) +
b3 * adstock(ad3, rate3),
basesales + b1 + b2 + b3 + rate1 + rate2 + rate3 ~ 1,
nl = TRUE),
data = fake_df,
prior = prior1)
summary(fit1)
```