Bayesian RM ANOVA w/ random slopes
Kia ora,
I was reading the recent pre-print on including random slopes in Bayesian ANOVAs. In the paper, it notes that the specification can be done using the lmBF function from BayesFactor. I would like to implement this in my analyses but am having a bit of trouble with model comparisons and obtaining BFincl for the effects
Say we have a two-way RM ANOVA with the factors F1 & F2, would the formulae for the models (w/ random slopes) be specified as follows?
full <- DV~F1 + F2 + ID + F1*F2 + F1*ID + F2*ID
main <- DV~F1 + F2 + ID + F1*ID + F2*ID
F1 <- DV~F1 + ID + F1*ID + F2*ID
F2 <- DV~F2 + ID + F1*ID + F2*ID
Null <- DV~ID + F1*ID + F2*ID
where the R input is: mdl <- lmBF(formula, data=data, whichRandom="id").
When previously using anovaBF, the mdl output would give a summary of BFincl for the analysis of effects. However, the lmBF outputs give the BF a single model. I imagine we would need to do model comparisons to obtain the BFincl values but I am unsure of this process, or rather, how this is done in JASP.
Generally, it would be nice to have a way to reproduce the model comparison JASP output in R. I would appreciate it greatly if someone could point me in the right direction.
Thanks,
Corey
Comments
Hi Corey,
Happy to hear someone read the preprint 😊
That is correct, you need to use lmBF which only fits one model at a time (anovaBF does the wrong thing, or well, the wrong thing in this scenario). Let's look at an example in the jasp data library, namely Alcohol attitudes.
In jasp 0.16.3 I get this output:
To replicate this in R you can run this code:
library(tidyr) library(BayesFactor) # read the data file directly from GitHub (https://github.com/jasp-stats/jasp-desktop/blob/stable/Resources/Data%20Sets/Data%20Library/3.%20ANOVA/Alcohol%20Attitudes.csv) dat <- read.csv("https://raw.githubusercontent.com/jasp-stats/jasp-desktop/stable/Resources/Data%20Sets/Data%20Library/3.%20ANOVA/Alcohol%20Attitudes.csv") # reshape the data to match bayes factor input dat_long <- dat |> pivot_longer(!participant, names_to = c("Drink", "Imagery"), names_pattern = "(beer|wine|water|)(pos|neg|neu)", values_to = "DV") # these formulas are indeed the full and null model full <- DV ~ Drink + Imagery + participant + Drink*Imagery + Drink*participant + Imagery*participant null <- DV ~ participant + Drink*participant + Imagery*participant # however, there is an easier way to generate the entire modelspace # generate all formulas without random slopes formulas <- BayesFactor::enumerateGeneralModels(DV ~ Drink + Imagery + participant + Drink*Imagery, whichModels = "withmain", neverExclude = "participant") # add the random slopes formulas <- lapply(formulas, function(f) { update.formula(f, ~ . + Drink:participant + Imagery:participant) }) # fit all models bfs <- lapply(formulas, lmBF, data = dat_long) # reduce the list to vector type inside BayesFactor bfs_c <- Reduce(c, bfs) # divide by the best performing model an sort decreasingly, as in the Jasp output sort(bfs_c / max(bfs_c), decreasing = TRUE) #> Bayes factor analysis #> -------------- #> [1] Drink + Imagery + participant + Drink:Imagery + Drink:participant + Imagery:participant : 1 ±0% #> [2] Drink + Imagery + participant + Drink:participant + Imagery:participant : 6.211023e-08 ±3.21% #> [3] Imagery + participant + participant:Drink + Imagery:participant : 3.469623e-12 ±2.89% #> [4] Drink + participant + Drink:participant + participant:Imagery : 2.777261e-41 ±3.49% #> [5] participant + participant:Drink + participant:Imagery : 6.836036e-42 ±2.72% #> #> Against denominator: #> DV ~ Drink + Imagery + participant + Drink:Imagery + Drink:participant + Imagery:participant #> --- #> Bayes factor type: BFlinearModel, JZSThe order of the models is exactly the same as in Jasp. Note that there are small differences in the values of the Bayes factor due to approximation error. If you increase the numerical accuracy in both Jasp and R then the differences should disappear, although that will (significantly) increase the running time.
Let me know if anything is unclear!
Cheers,
Don
Hi Don,
That is very helpful! Thanks for sharing the code. My next question would be how to progress from the model comparison output to the analysis of effects output...
mainEff <- c(bfs_c[1]/bfs_c[5], bfs_c[2]/bfs_c[5]) # drink, imagery interEff <- bfs_c[4]/bfs_c[3] # drink * imagery effects <- c("drink", "imagery", "drink * imagery") BFincl <- c(exp(mainEff@bayesFactor[["bf"]][1]), exp(mainEff@bayesFactor[["bf"]][2]), exp(interEff@bayesFactor[["bf"]])) error <- c(mainEff@bayesFactor[["error"]][1], mainEff@bayesFactor[["error"]][2], interEff@bayesFactor[["error"]]) effTbl <- data.frame(effects,BFincl,error)Also, how would we get the Analysis of Effects table using the 'Across all models' approach? Based on my understanding, it has something to do with summing across models but not sure what the actual order of operations is.
Thanks,
Corey