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!