I’m trying to understand how brms
uses the smooth/spline functions from mgcv
to create the matrix of smooths used in model fitting.
In trying to construct the matrix myself, I can’t seem to figure out how things are happening under the hood in brms.
An example:
library(brms)
library(mgcv)
# Make some data
data <- data.frame(Time = rep(1:100, 4),
Group = rep(factor(LETTERS[1:4]), each = 100),
Y = rnorm(400, 5, 2))
# Generate Stan data from brms
Stan_Data <- make_standata(Y ~ s(Time, bs = "bs", k = 20, by = Group), data = data)
# Look at resulting Xs smooth matrix
Stan_Data[["Xs"]]
# Calculate smooths from mgcv directly
sm <- smoothCon(s(Time, bs = "bs", k = 20, by = Group),
data)
# Look at those results
view(sm)
brms
appears to be generating 18 values for each group (Zs_Group_1), whereas mgcv
is giving 20.
Does anyone know where the Z_Group_1
and the Xs
matrix are coming from and how I would go about calculating those from the mgvc
output?
I’ve tried looking through the brms
source code, but wasn’t able to figure this out.