How to input matrix data into brms formula (for signal regression using smooths)?

Hi everyone, I am trying to input matrix data into the brm function to run a signal regression. brm is from the brms package, which provides an interface to fit Bayesian models using Stan. Signal regression is when you model one covariate using another within the bigger model, and you use the by parameter like this: model <- brm(response ~ s(matrix1, by = matrix2) + ..., data = Data). The problem is, I cannot input my matrices using the data parameter because it only allows one data.frame object to be inputted.

Here are my code and the errors I obtained from trying to get around that constraint…

First off, my reproducible code leading up to the model-building:

library(brms) 
#100 rows, 4 columns. Each cell contains a number between 1 and 10 
Data <- data.frame(runif(100,1,10),runif(100,1,10),runif(100,1,10),runif(100,1,10)) 
#Assign names to the columns names(Data) <- c("d0_10","d0_100","d0_1000","d0_10000") Data$Density <- as.matrix(Data)%*%c(-1,10,5,1) 
#the coefficients we are modelling 
d <- c(-1,10,5,1) 
#Made a matrix with 4 columns with values 10, 100, 1000, 10000 which are evaluation points. 
Bins <- 10^matrix(rep(1:4,times = dim(Data)[1]),ncol = 4,byrow =T) 
Bins

As mentioned above, since data only allows one data.frame object to be inputted, I’ve tried other ways of inputting my matrix data. These methods include:

1) making the matrix within the brm() function using as.matrix()

signalregression.brms <- brm(Density ~ s(Bins,by=as.matrix(Data[,c(c("d0_10","d0_100","d0_1000","d0_10000"))])),data = Data) #Error in is(sexpr, "try-error") : argument "sexpr" is missing, with no default

2) making the matrix outside the formula, storing it in a variable, then calling that variable inside the brm() function

Donuts <- as.matrix(Data[,c(c("d0_10","d0_100","d0_1000","d0_10000"))]) signalregression.brms <- brm(Density ~ s(Bins,by=Donuts),data = Data) #Error: The following variables can neither be found in 'data' nor in 'data2': 'Bins', 'Donuts'
3) inputting a list containing the matrix using the ‘data2’ parameter

signalregression.brms <- brm(Density ~ s(Bins,by=donuts),data = Data, data2=list(Bins = 10^matrix(rep(1:4,times = dim(Data)[1]), ncol = 4, byrow =T), donuts=as.matrix(Data[,c(c("d0_10","d0_100","d0_1000","d0_10000"))]))) #Error in names(dat) <- object$term: 'names' attribute [1] must be the same length as the vector [0]

None of the above worked; each had their own errors and it was difficult troubleshooting them because I couldn’t find answers or examples online that were of a similar nature in the context of brms.

I was able to use the above techniques just fine for gam(), in the mgcv package - you don’t have to define a data.frame using ‘data’, you can call on variables defined outside of the gam() formula, and you can make matrices inside the gam() function itself. See below:

library(mgcv) signalregression2 <- gam(Data$Density ~ s(Bins,by = as.matrix(Data[,c("d0_10","d0_100","d0_1000","d0_10000")]), k=3)) #This works!

It seems like brms is less flexible… Maybe I am inputting the list into data2 incorrectly?

My question: does anyone have any suggestions on how to make my brm function run?

Thank you very much!

Perhaps you could try?

brm(Density ~ s(Bins, by=cbind(d0_10, d0_100, d0_1000, d0_10000)), data = Data)

Thank you for your response. No, unfortunately all of the variables used in brm need to be defined in Data . Thus, I have also tried the tidyverse way:
signalregression.brms <- brm(Density~ s(cbind(d0_10_bin,d0_100_bin,d0_1000_bin,d0_10000_bin), by = cbind(d0_10,d0_100,d0_1000,d0_10000),k=3,), data = Data %>% mutate(d0_10_bin=10, d0_100_bin = 100, d0_1000_bin = 1000, d0_10000_bin = 10000))
But this still does not work for me. This error shows up:
Error in is(sexpr, "try-error") : argument "sexpr" is missing, with no default

What stops you from adding d0_10 etc. to your data set as additional variables?

Good news! This code works! In the tidyverse code above I accidentally added a comma , after the k = 3. Oh so the d0_10, d0_100, etc. are already stored within Data when the dataframe was created. Thank you for your help!

I also tried defining all of the variables within Data and using cbind:
signalregression.brms <- brm(Density~ s(cbind(d0_10_bin,d0_100_bin,d0_1000_bin,d0_10000_bin), by = cbind(d0_10,d0_100,d0_1000,d0_10000),k=3), data = Data)
This also works, yet strangely I got different plots compared to the tidyverse way. I ruled this out as a solution before because it didn’t work when I ran it with k = 20. But now that I’m trying it with k = 3, it works. So the moral of the story is that I couldn’t get as.matrix to work inside brm but cbind works just fine. Thanks again.