Function to compute Bayes factors for ANOVA designs
Description
This function computes Bayes factors for all maineffects and interaction contrasts in an ANOVA design.
Usage
1 2 3 4 5 
Arguments
formula 
a formula containing all factors to include in the analysis (see Examples) 
data 
a data frame containing data for all factors in the formula 
whichRandom 
a character vector specifying which factors are random 
whichModels 
which set of models to compare; see Details 
iterations 
How many Monte Carlo simulations to generate, if relevant 
progress 
if 
rscaleFixed 
prior scale for standardized, reduced fixed effects. A number of preset values can be given as strings; see Details. 
rscaleRandom 
prior scale for standardized random effects 
multicore 
if 
method 
approximation method, if needed. See

noSample 
if 
Details
Models, priors, and methods of computation are provided in Rouder et al. (2012).
The ANOVA model for a vector of observations y is
y = μ + X_1 θ_1 + … + X_pθ_p +ε,
where θ_1,…,θ_p are vectors of maineffect and interaction effects, X_1,…,X_p are corresponding design matrices, and ε is a vector of zerocentered noise terms with variance σ^2. Zellner and Siow (1980) inspired gpriors are placed on effects, but with a separate gprior parameter for each covariate:
θ_1~N(0,g_1σ^2), …, θ_p~N(0,g_p σ^2).
A Jeffries prior is placed on μ and
σ^2. Independent scaled inversechisquare
priors with one degree of freedom are placed on
g_1,…,g_p. The squareroot of the scale for
g's corresponding to fixed and random effects is given by
rscaleFixed
and rscaleRandom
, respectively.
When a factor is treated as random, there are as many main effect terms in the vector θ as levels. When a factor is treated as fixed, the sumstozero linear constraint is enforced by centering the corresponding design matrix, and there is one fewer main effect terms as levels. The CornfieldTukey model of interactions is assumed. Details are provided in Rouder et al. (2012)
Bayes factors are computed by integrating the likelihood
with respect to the priors on parameters. The
integration of all parameters except g_1,…,g_p
may be expressed in closedform; the integration of
g_1,…,g_p is performed through Monte Carlo
sampling, and iterations
is the number of
iterations used to estimate the Bayes factor.
anovaBF
computes Bayes factors for either all
submodels or select submodels missing a single main
effect or covariate, depending on the argument
whichModels
. If no random factors are specified,
the null model assumed by anovaBF
is the
grandmean only model. If random factors are specified,
the null model is the model with an additive model on all
random factors, plus a grand mean. Thus, anovaBF
does not currently test random factors. Testing random
factors is possible with lmBF
.
The argument whichModels
controls which models are
tested. Possible values are 'all', 'withmain', 'top', and
'bottom'. Setting whichModels
to 'all' will test
all models that can be created by including or not
including a main effect or interaction. 'top' will test
all models that can be created by removing or leaving in
a main effect or interaction term from the full model.
'bottom' creates models by adding single factors or
interactions to the null model. 'withmain' will test all
models, with the constraint that if an interaction is
included, the corresponding main effects are also
included.
For the rscaleFixed
and rscaleRandom
arguments, several named values are recognized: "medium",
"wide", and "ultrawide", corresponding to r scale
values of 1/2, sqrt(2)/2, and 1,
respectively. In addition, rscaleRandom
can be set
to the "nuisance", which sets r=1 (and is thus
equivalent to "ultrawide"). The "nuisance" setting is for
mediumtolargesized effects assumed to be in the data
but typically not of interest, such as variance due to
participants.
Value
An object of class BFBayesFactor
, containing the
computed model comparisons
Note
The function anovaBF
will compute Bayes factors
for all possible combinations of fixed factors and
interactions, against the null hypothesis that all
effects are 0. The total number of tests computed will be
2^(2^K  1) for K fixed factors.
This number increases very quickly with the number of
factors. For instance, for a fiveway ANOVA, the total
number of tests exceeds two billion. Even though each
test takes a fraction of a second, the time taken for all
tests could exceed your lifetime. An option is included
to prevent this: options('BFMaxModels')
, which
defaults to 50,000, is the maximum number of models that
'anovaBF' will analyze at once. This can be increased by
increasing the option value.
It is possible to reduce the number of models tested by
only testing the most complex model and every restriction
that can be formed by removing one factor or interaction
using the whichModels
argument. Setting this
argument to 'top' reduces the number of tests to
2^K1, which is more manageable. The Bayes factor
for each restriction against the most complex model can
be interpreted as a test of the removed
factor/interaction. Setting whichModels
to
'withmain' will not reduce the number of tests as much as
'top' but the results may be more interpretable, since an
interaction is only allowed when all interacting effects
(main or interaction) are also included in the model.
Author(s)
Richard D. Morey (richarddmorey@gmail.com)
References
Gelman, A. (2005) Analysis of Variance—why it is more important than ever. Annals of Statistics, 33, pp. 153.
Rouder, J. N., Morey, R. D., Speckman, P. L., Province, J. M., (2012) Default Bayes Factors for ANOVA Designs. Journal of Mathematical Psychology. 56. p. 356374.
Zellner, A. and Siow, A., (1980) Posterior Odds Ratios for Selected Regression Hypotheses. In Bayesian Statistics: Proceedings of the First Interanational Meeting held in Valencia (Spain). Bernardo, J. M., Lindley, D. V., and Smith A. F. M. (eds), pp. 585603. University of Valencia.
See Also
lmBF
, for testing specific models, and
regressionBF
for the function similar to
anovaBF
for linear regression models.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  ## Classical example, taken from t.test() example
## Student's sleep data
data(sleep)
plot(extra ~ group, data = sleep)
## traditional ANOVA gives a p value of 0.00283
summary(aov(extra ~ group + Error(ID/group), data = sleep))
## Gives a Bayes factor of about 11.6
## in favor of the alternative hypothesis
anovaBF(extra ~ group + ID, data = sleep, whichRandom = "ID",
progress=FALSE)
## Demonstrate topdown testing
data(puzzles)
result = anovaBF(RT ~ shape*color + ID, data = puzzles, whichRandom = "ID",
whichModels = 'top', progress=FALSE)
result
## In orthogonal designs, the top down Bayes factor can be
## interpreted as a test of the omitted effect
