richarddmorey / BayesFactor

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

Random "slopes" are not tagged as such (maybe not treated as such?) #160

Open mattansb opened 2 years ago

mattansb commented 2 years ago

When adding a random slope into a model, it does not appear in the dataTypes slot (https://github.com/easystats/insight/pull/566):

library(BayesFactor)

mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)

model1 <- lmBF(mpg ~ cyl + gear + cyl:gear, mtcars, 
              progress = FALSE, whichRandom = c("gear", "cyl:gear"))

model1@numerator[[1]]@dataTypes
#>      cyl     gear 
#>  "fixed" "random"
richarddmorey commented 2 years ago

This is because the slopes are inferred from the types of the individual constituents. Adding cyl:gear to random won't do anything on top of making "gear" random. If gear is random and cyl is fixed, the constraints on the interaction are already set due to the way the interaction is produced using the Kronecker product.

bwiernik commented 1 year ago

That makes sense. It might be good to error or at least give a warning that the product term was ignored in whichRandom

grocio commented 1 year ago

A relevant topic is "Mixed models" of the tutorial. https://cran.r-project.org/web/packages/BayesFactor/vignettes/manual.html#mixed

In the tutorial, a classical ANOVA example is

summary(aov(RT ~ shape*color + Error(ID/(shape*color)), data=puzzles))

and an anovaBF equivalent is

bf = anovaBF(RT ~ shape*color + ID, data = puzzles, 
             whichRandom="ID")

When I read it, I thought anovaBF would be (for consistency with the classical ANOVA code)

bf = anovaBF(RT ~ shape*color + ID + ID:shape + ID:color, data = puzzles, 
             whichRandom=c("ID", "ID:shape", "ID:color"))

It may be good to mention that BayesFactor automatically infers slopes in the tutorial for novices as well as giving a warning message sugested by @bwiernik.