Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

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, JZS
    

    The 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

  • edited June 2022

    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...

    • If using a matched models approach would we compute the main and interaction effects as follows?
    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)
    
    • These seem to match relatively well with the output in JASP, however, some effects seem to have a large discrepancy. For example, imagery in JASP had a BFincl of 1.32e14 whereas when I ran it in R I got a BFincl of 5.02e29. I understand there is an expectation for some variation but it seems quite large.
    • How many iterations would we need to run to get a better match between numerical accuracy? I am currently using 100,000. Is set seed = 1 equivalent for R and JASP?

    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

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games