简体   繁体   English

如何将矩阵数据输入到 brms 公式中?

[英]How to input matrix data into brms formula?

I am trying to input matrix data into the brm() function to run a signal regression.我正在尝试将矩阵数据输入brm()函数以运行信号回归。 brm is from the brms package, which provides an interface to fit Bayesian models using Stan. brm来自 brms 包,它提供了一个使用 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) .信号回归是当您在更大的模型中使用另一个协变量对一个协变量进行建模时,您可以像这样使用by参数: 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.问题是,我无法使用 'data' 参数输入我的矩阵,因为它只允许输入一个data.frame对象。

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. Rows are repeats of the same column numbers
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.如上所述,由于“数据”只允许输入一个 data.frame 对象,因此我尝试了其他输入矩阵数据的方法。 These methods include:这些方法包括:

1) making the matrix within the brm() function using as.matrix() 1) 使用 as.matrix() 在 brm() 函数内制作矩阵

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 2) 在公式外制作矩阵,将其存储在一个变量中,然后在 brm() 函数内调用该变量

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 3)使用'data2'参数输入包含矩阵的列表

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.每个人都有自己的错误,很难对它们进行故障排除,因为我无法在网上找到在 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.我能够在 mgcv 包中对 gam() 使用上述技术 - 您不必使用“data”定义 data.frame,您可以调用在 gam() 公式之外定义的变量,并且您可以在 gam() 函数本身内部制作矩阵。 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))
#Works!

It seems like brms is less flexible... :(似乎 brms 不太灵活... :(

My question : does anyone have any suggestions on how to make my brm() function run?我的问题:有人对如何让我的 brm() 函数运行有任何建议吗?

Thank you very much!非常感谢你!

My understanding of signal regression is limited enough that I'm not convinced this is correct, but I think it's at least a step in the right direction.我对信号回归的理解有限,我不相信这是正确的,但我认为这至少是朝着正确方向迈出的一步。 The problem seems to be that brm() expects everything in its formula to be a column in data .问题似乎是brm()期望其公式中的所有内容都是data一列。 So we can get the model to compile by ensuring all the things we want are present in data :所以我们可以通过确保我们想要的所有东西都存在于data来编译模型:

library(tidyverse)
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))

Writing out each column by hand is a little annoying;手动写出每一列有点烦人; I'm sure there are more general solutions.我相信有更通用的解决方案。

For reference, here are my installed package versions:作为参考,这是我安装的软件包版本:

map_chr(unname(unlist(pacman::p_depends(brms)[c("Depends", "Imports")])), ~ paste(., ": ", pacman::p_version(.), sep = ""))
 [1] "Rcpp: 1.0.6"           "methods: 4.0.3"        "rstan: 2.21.2"         "ggplot2: 3.3.3"       
 [5] "loo: 2.4.1"            "Matrix: 1.2.18"        "mgcv: 1.8.33"          "rstantools: 2.1.1"    
 [9] "bayesplot: 1.8.0"      "shinystan: 2.5.0"      "projpred: 2.0.2"       "bridgesampling: 1.1.2"
[13] "glue: 1.4.2"           "future: 1.21.0"        "matrixStats: 0.58.0"   "nleqslv: 3.3.2"       
[17] "nlme: 3.1.149"         "coda: 0.19.4"          "abind: 1.4.5"          "stats: 4.0.3"         
[21] "utils: 4.0.3"          "parallel: 4.0.3"       "grDevices: 4.0.3"      "backports: 1.2.1"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 brms 中正确使用 set_prior() 以及从矩阵中提取的值,例如prior(normal(priors[i,1],priors[i,2])) - How to correctly use set_prior() in brms with values extracted from a matrix, e.g. prior(normal(priors[i,1], priors[i,2])) 如何用rpy2估计Brms模型? - How estimate a brms model with rpy2? 如何将更新的 Shiny 矩阵输入值保存到数据帧(或工作存储器)中,以便进一步访问 R 代码中的矩阵输入数据? - How to save updated Shiny matrix input values into a data frame (or working memory), for further accessing the matrix input data in R code? 使用R中数据帧的元素基于公式创建系数矩阵 - create a coefficient matrix based on a formula using elements of a data frame in R 如何在R中使用BRMS进行贝叶斯有序回归的假设检验 - How to hypothesis test in Bayesian ordinal regression with BRMS in R brms:如何在分类变量上设置先验? - brms: how do I set prior on categorical variable? 如何迭代拟合Brms回归模型并将平均值和sigma提取到数据框 - How to iteratively fit a brms regression model and extract means and sigma to the dataframe 如何将公式粘贴到R中的model.matrix函数中? - How to paste formula into model.matrix function in R? 如何在RHS上使用矩阵项来理解/扩展r公式 - How to understand/expand r formula with matrix term on RHS 重新运行公式以生成多列数据(使用函数rep()和Matrix()的矩阵) - Rerun of formula to make many columns of data (matrix using functions rep() & Matrix())
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM