Howdy, Stranger!

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

Supported by

BayesFactor anovaBF syntax

I would like to get Bayes factors for ANOVAs that are analogous to the classical F-tests, and I just want to make sure I understand correctly how to write the syntax, especially regarding subject IDs.

For example, I have between-subject independent variables a_between and b_between, and within-subject variables c_within and d_within, dependent variable values, with subject_id to identify each subject; in a dataset my_data.

If I understand correctly, for a full ANOVA, I should use:

anovaBF(values~a_between*b_between*c_within*d_within+subject_id, data = my_data, whichModels="bottom", whichRandom="subject_id") # and I assume the order of variables does not matter, e.g. it could also be d_within*a_between*c_within*b_between+subject_id

For only within-subjects ANOVA, I should use:

anovaBF(values~c_within*d_within+subject_id, data = my_data, whichModels="bottom", whichRandom="subject_id")

For only between-subjects ANOVA, I should use:

anovaBF(values~a_between*b_between, data = my_data, whichModels="bottom", whichRandom="subject_id")

So in the last case I have no +subject_id - otherwise I get Error in base::try(expression, silent = silent) : not enough observations. (Perhaps because there is only a single line per subject_id?)

Two main questions: 

 1. Regardless of reasons, are the solutions above corrects?

 2. If the solutions are correct, why do I have to specify subject ID twice (once as whichRandom and once in the beginning as +subject_id) for within-subject variables, and why not when there are only between-subject variables?


    1. Generally, yes - but I'm not sure you want to use whichModels = "bottop" - it is advised to stick with the defaults here (whichModels = "withmain") (see here). Also you can't really get a BF for an F test - as BF are always comparative, so if you want a BF for each "effect" you'll need to think which comparison of which two models might represent that (like in step-wise hierarchical regression). Or, you may want to try to compute Inclusion BFs via bayestestR::bayesfactor_inclusion()(equivalent to JASP's effects panel).
    2. anovaBF isn't really an anova at all - it is actually a linear mixed model. So you need to specify +subject_id as it is an effect in your model, but you also need to tell anovaBFthat it is a random effect (and not a fixed one).
    Thanked by 1gaspar
  • Thanks very much for the reply!

    Yes, my original interpretation was quite wrong, but after a lot of reading I think it's become clear enough.

    Let me just add that these two posts helped a lot in understanding how it works:

    Actually this too was very helpful with its "Bayesian Type II ANOVA", though I suppose (same as the author) that this is not the way BayesFactor should be used:

    In any case, I'll stick to using bayestestR::bayesfactor_inclusion() with match_models = TRUE; that seems the most straightforward to me.

  • Also, when in doubt, you can compare output from R to that of JASP.



    Thanked by 1gaspar
Sign In or Register to comment.