7 files

Aladins Bayes Factor in R

posted on 05.06.2017, 09:15 by Stefan Wiens
Contains R scripts (created with https://www.r-project.org/) to calculate the Bayes Factor "a la Dienes". It also produces plots (inspired by JASP, https://jasp-stats.org/).

Information and tutorial:

Zoltan Dienes has several excellent papers on the use of Bayes Factor with informed priors. He distinguishes between uniform, half-normal, and normal priors. These can be used in the context of t tests with his online calculator (see below). This tool is simple to use and helpful, but it does not produce illustrations of the results. Also, to increase reproducibility in open science, it would be beneficial to have scripted analyses. With scripts, it would also be easier to check the robustness of the results by comparing effects of different priors, as suggested by Dienes. Although some R scripts have already been provided earlier (e.g., Baguley & Kaye, 2010, see Dienes' website), the present scripts are a humble attempt to improve these scripts.


The present scripts are called "Aladins Bayes Factor in R". If you read "Aladins" out loud, it sounds like "a la Dienes", to give credit to Zoltan Dienes and his work. The script also fulfills my little wish to have a visualization of the BF (see JASP for beautiful figures, https://jasp-stats.org/ ).

BF_t.R and BF_U.R
The guide shows how to use these functions.

Phrase everything on the same, unstandardized units. That is, mean and SEM of the data, and the prediction of the H1 must all be in the same units. If you want to use standardized units, see Dienes' website on how to convert the scores. Alternatively, use JASP or the BayesFactor package in R (see links).

When defining your H1, phrase it so that you would mainly expect a positive effect. This makes more sense intuitively. (Another reason is that if you want to test one-tailed with tail = 1, the script assumes that you mean that the effect cannot be negative and cuts off the negative tail.)

If your data are opposite (i.e., negative) to the predicted direction (which is positive), enter a negative mean difference.

Example (with many more in the user guide):
BF_t(10, 5, 10000, -3, 8, 119)
The BF10 = 0.35 and BF01 = 2.86. The data tend to support the null hypothesis.
In the calculation, the H1 is modeled as a t distribution centered at 10 (and an SEM of 5); however, because the df = 10000, the t distribution is close enough to a normal distribution centered at 10 with an SD = 5.
The actual data had a mean effect of -3 (i.e., opposite to the prediction), SEM = 8, and df = 119.
BF_t(10, 5, 10000, -3, 8, 119) is identical to BF_t(10, 5, 10000, -3, 8, 119, tail = 2). Thus, H1 allows for both positive and negative effects.

Use BF_t.R to run Dienes' half-normal or normal prior. The script is already designed to run according to Dienes & Mclatchie (2017)--that is, with a t distribution rather than a normal distribution--but if you want a normal distribution, simply defines the degrees of freedom (dftheory and/or dfobtained) as 10000. With this setting, the t distribution is like a normal distribution.

Use BF_U.R to run Dienes' uniform prior.

If you do not like the resulting figures, just open the scripts and change them. To facilitate this process, I have marked some sections. It should be easy to:
> change the title (which is maybe too informative now)
> plot without the pie chart
> plot the legend ("prior" etc.) on the left rather than right

If the scripts are helpful, please cite them with reference to this deposit.

P.S. The present scripts follow up on my tutorial on how to transition from null hypothesis significance testing to Bayesian analyses.

Wiens, S., & Nilsson, M. E. (2016). Performing contrast analysis in factorial designs: From NHST to confidence intervals and beyond. Educational and Psychological Measurement, in press. doi: 10.1177/0013164416668950

Wiens, Stefan (2017): Supplement for Performing contrast analysis in factorial designs: From NHST to confidence intervals and beyond. Educational and Psychological Measurement. figshare.




Stockholm University