Define a model in brms as a geometric series

I have simulated data as follows:

N <- 5
x <- runif(20, -1,1)
a <- sample(1:10, N)
b <- sample(1:10, N)
y <- c()

for(i in 1:length(x)){
  s <- 0
  for(j in 1:N){
    s <- s + b[j] * x[i]^(a[j] + j)
  }
  y <- c(y, s)
}

Essentially I want to define a model to estimate a_i and b_i as a geometric expression.

\sum_{i=1}^{N} b_i x^{a_i + i}

I do not want to explicitly write the sum out because, in reality, N will be large. That is, I do not want to write something like

f <- brms::bf(y ~ a1*x^(b1 + 1) + a2*x^(b2 + 2) + ... +aN*x^(bN + N))

You should be able to build the formula as a character string programmatically and then use as.formula to convert the string to formula you can pass to bf

1 Like