richarddmorey / BayesFactor

BayesFactor R package for Bayesian data analysis with common statistical models.
https://richarddmorey.github.io/BayesFactor/
131 stars 48 forks source link

Bayes factor for a single parameter in multilevel regression #142

Open jen265 opened 4 years ago

jen265 commented 4 years ago

Hello, Is it possible to compute a bayes factor against the null for a single parameter in multilevel regression? For example, in the model below, Subject is the grouping variable, IV1 is the level-1 predictor with a random slope, and IV2 to IV6 are level-2 predictors. I want to know if a bayes factor is in favor of a specific cross-level interaction being unequal to zero, rather than being equal to zero.

full_BF = lmBF(DV ~ IV1 IV2 + IV1 IV3 + IV1 IV4 + IV1 IV5 + IV1 * IV6 + Subject + IV1:Subject, data = data, whichRandom = c('Subject','IV1:Subject'))

I have found an example on how to compute a bayes factor for a single parameter in multiple linear regression in Heck, D. W. (2019). A caveat on the Savage–Dickey density ratio: The case of computing Bayes factors for regression parameters. British Journal of Mathematical and Statistical Psychology, 72(2), 316-333. The relevant code of that example is:

full_model <- cranium_capacity ~ parasites + temp_variation + isosd + population_dens select_pred <- 1 # = test the effect of "parasites"

DV and design matrix

y <- bailey2009$cranium_capacity X_unscaled <- model.matrix(full_model, data = bailey2009)[,-1, drop = FALSE] X <- scale(X_unscaled, scale = FALSE) attr(X, "scaled:center") <- NULL P <- ncol(X)

BayesFactor package (Morey & Rouder, 2015)

t_bayesfactor <- system.time({ bf.full <- regressionBF(full_model, bailey2009)

select relevant Bayes factor for "select_pred == 1"

idx <- 0
for(i in 1:(P-1)) 
  idx <- idx + choose(P, i) 
bf_BF <- 1 / extractBF(bf.full / bf.full[length(bf.full)])[idx,"bf"]

})

Is it possible to adjust the code such that it can be used for the lmBF function? Or is there any other possibility to compute a bayes factor against the null for a single parameter in multilevel regression?

I am thankful for any idea on this.