Hello!
I’ll define my question more precisely. Here is my data generating process :
library(tidyverse)
library(splines)
library(brms)
D <- tibble(x = rep(seq(from=0, to=15, by=2),6),
cat = rep(LETTERS[1:3],each = 16))# generating inputs
B <- t(bs(X, knots=seq(-5,5,1), degree=3, intercept = TRUE)) # creating the B-splines
num_data <- length(X); num_basis <- nrow(B)
a0 <- matrix(c(0.2,0.2,0.5,0.5,-0.2,-0.2), ncol = 6) # intercept
a_A1 <- rnorm(num_basis, 0, 1) # coefficients of B-splines
a_A2 <- rnorm(num_basis, a_A1, 0.2)
a_B1 <- rnorm(num_basis, 0, 1) # coefficients of B-splines
a_B2 <- rnorm(num_basis, a_B1, 0.2)
a_C1 <- rnorm(num_basis, 0, 1) # coefficients of B-splines
a_C2 <- rnorm(num_basis, a_C1, 0.2)
A <- matrix(c(a_A1, a_A2, a_B1, a_B2, a_C1, a_C2), ncol = 6)
Y_true <- as.vector(t(a0)%*%X + t(A)%*%B) # generating the output
D$y <- Y_true + rnorm(length(X),0,.2) # adding noise
D %>% ggplot(aes(x = x, y = y, color = cat)) +
geom_point() +
geom_smooth()
In math :
y_i \sim Normal(\mu_i, \sigma)\\
\mu_i = \beta_c x_i + f_c(x)\\
f_c(x) = \sum^K_{k=1} \alpha_{ck}b_k\\
\alpha_{ck} \sim normal(0, \tau)
Where c is the index of the category at which the observation i belongs. There is one standard deviation of weights common to each category (\tau).
However, when I fit this with brms using the following code, I get an sd estimated for each level of my category, such as :
\alpha_{ck} \sim normal(0, \tau_c)
# Store formula
form_spline <- bf(y ~ cat + s(x, by = cat, k = 3))
make_stancode(form_spline, data = D)
Is it possible to estimate a unique standard-deviation, as in my data generating process? I am afraid my real data does not contain enough information to estimate C standard-deviation without causing divergences.
I tried the following approach, based upon the “id” argument from s()
, which states
An id with a factor by variable causes the smooths at each factor level to have the same smoothing parameter.
Unfortunately, the following code still produces C standard-deviation to be estimated.
form_spline <- bf(y ~ cat + s(x, by = cat, k = 3, id = "a"))
make_stancode(form_spline, data = D)
Is someone aware of any solution?
Thank you very much!
Lucas