Is it possible to have a linear (smoothing spline) and non-linear components in a single model in brms?

Hello,

I’m hoping to build a model (ideally via brms) which is able to use both linear (with smoothing splines) and nonlinear components but I am having some isues with specifying the model. The model I am hoping to make is:

r_{t,i} \sim Normal(\mu_i, \sigma)
\mu_i = f(\rho_i)
\rho_i = T_i + \frac{1}{\phi}D_i
D_i = sin(\theta)X_i + cos(\theta)Y_i

where I want to estimate parameters \theta and \phi and variables D and \rho, with \rho eventually being fed into a smoothing spline.

Simulated dataset:

phi <- 2
theta <- 90 * pi/180

x <- 1:10
y <- 1:10
t <- 1:10
map <- expand.grid(x, y, t)
df <- data.frame(
  x = map$Var1, # x coord
  y = map$Var2, # y coord
  t = map$Var3  # time
)
df$D <- sin(theta) * df$x+ cos(theta) * df$y
df$rho <- df$t+ (1/phi) * D
df$rt <- sin(df$rho)

Based on a reply to another ~similar question about having a model with both linear and nonlinear components, I think it’s possible to have both together in brms, but I may be misunderstanding the context (most questions I’ve seen are related to non-linear models with linear random/varying effects).

The syntax I have tried is:

linear <- bf(
           rt ~ s(rho, k = 12, bs = "tp"),
)
nonlinear <- bf(
           rho ~ t + 1/phi * D,
           D ~ sin(theta) * x + cos(theta) * y,
           phi ~ 1,
           theta ~ 1,
           nl = TRUE
)
priorz <- c(
  prior(normal(5, 10), nlpar = "phi"),
  prior(uniform(0, 2*pi), nlpar = "theta")
)
fit <- brms(linear + nonlinear,
            prior = priorz,
            family = gaussian(),
            ...
)

When I try this I get a warning that e.g. theta is not recognised as a distribution and a suggestion that I forgot to include nl = TURE. Based on another post, I tried separating each component into its own bf():

linear <- bf(
  rt ~ s(rho, k = 12, bs = "tp")
)

nonlinear <- bf(
  rho ~ t + 1/phi * D,
  phi ~ 1,
  nl = TRUE
)

nonlinear2 <- bf(
  D ~ sin(theta) * x + cos(theta) * y,
  theta ~ 1,
  nl = TRUE
)

But I get an error stating that The following variables are missing in 'data': 'rho', 'D'.

I’m not really sure where the problem is. Is it possible to mix linear and nonlinear together in this way? I’d really appreciate it if someone could give some advice.

Thanks,

  • Operating System: Windows 10
  • brms Version: brms_2.13.5

Hey Deon! Welcome to the Stan forum!

I think the way brms does splines it’s not possible to do what you want to do. Even in Stan it would be pretty hard to do, because you’d need to build the basis function matrix in an autodiff-able way. Maybe that is possible, but I wouldn’t know how.

How big is you data? How many i are there? IF your data is small you can maybe get away with a GP for f. But I think that would have to be done in Stan.

Maybe someone else has other ideas.

Cheers,
Max