Analysis of Factorial Experiments

Convenience functions for analyzing factorial experiments using ANOVA or mixed models. aov_ez(), aov_car(), and aov_4() allow specification of between, within (i.e., repeated-measures), or mixed between-within (i.e., split-plot) ANOVAs for data in long format (i.e., one observation per row), aggregating multiple observations per individual and cell of the design. mixed() fits mixed models using lme4::lmer() and computes p-values for all fixed effects using either Kenward-Roger or Satterthwaite approximation for degrees of freedom (LMM only), parametric bootstrap (LMMs and GLMMs), or likelihood ratio tests (LMMs and GLMMs). afex uses type 3 sums of squares as default (imitating commercial statistical software).


                ** afex VERSION 0.19-x **
                Changes in afex Version 0.19-0
                Released Januray 2018
Significant User Visible Changes and New Features

o   afex now works with emmeans (predecessor of lsmeans pckage).


o   lsmeans did not work with mixed objects if type=2 and method used nested
    model comparions (e.g., LRT or PB).
o   removed warning 'Error() model is singular' when units of observations 
    have missing values for ANOVAs.

                ** afex VERSION 0.18-x **
                Changes in afex Version 0.18-0
                Released May 2017

o   aov_ez did not work with more than one covariate 

                ** afex VERSION 0.17-x **
                Changes in afex Version 0.17-x
                Released April 2017

Significant User Visible Changes and New Features

o   Added Satterthwaite approximation (method="S") to mixed(), which is 
    implemented via lmerTest. Satterthwaite approximates the degrees-of-
    freedom of an F-test, similar to the default Kenward-Roger method, but
    requires less RAM.

o   mixed() methods "KR" and "S" are now obtained via tests on the full 
    model directly. No restricted models are calculated for these methods. 
    The former KR method is still available: method="nested-KR"

o   mixed() now per default exports and uses lmerTest::lmer and not 
    lme4::lmer(). This can be changed via calling 
    afex_options(lmer_function = "lme4")
o   afex does not depend on reshape2 any more. It is only imported.
o   All arguments with "." in the name have been renamed to use "_". The old
    names have been deprecated (i.e., still work, but should not be used).
    check.contrasts = check_contrasts, test.intercept = test_intercept,
    args.test = args_test, ...
o   Names of slots of mixed model objects with "." have been renamed to have
    a "_" instead: full.model = full_model and restricted.models = 
o   afex_options() now uses the global R options accessible via options() 
    (with prefix afex.) and allows to reset all options via list (i.e.,:
    aop <- afex_options(); ...; afex_options(aov); works).
o   renamed allFit() to all_fit() and argument to meth_tab. 
    Added method dfoptim::nmkb() to all_fit(). To use nmkb, dfoptim needs to
    be loaded explicitly via library (see examples). 
o   Added argument all_fit to mixed(). If TRUE, each model is fitted with 
    all available optimizers and the best fit in each case selected.
o   mixed objects have an additional slot, "data", that contains the data 
    used for fitting.
o   Added vignette with extensive mixed model example.

o   Created forum for providing afex support:

o   Sphericity correction now correctly displayed as none if all within-
    factors have only two levels (bug removed by Frederik Aust).
o   removed bug when calling nice() on afex_aov objects created with 
    explicit effect size.
o   aov_...(): anova_table = list(intercept = TRUE) now correctly suppresses
    the intercept (bug removed by Frederik Aust).
o   aov_...(): return = "nice" did not work (bug removed by Frederik Aust).

o   aov_...() now supports column names with spaces (using backticks), see: Thx to Jonathan Love for fix.
o   changed license to GPL >=2 (as it call some function from GPL >=2 
o   removed bug that mixed() object with type=2 could not be printed.

o   removed bug that model names for anova.mixed with multiple models were 
o   all_fit() now uses a data argument correctly and passes it update(). 
    This again allows to use it for data with suppressed correlations. 
    all_fit(... verbose = TRUE) now correctly signals if a fit failed.

                ** afex VERSION 0.16-x **
                Changes in afex Version 0.16-x
                Released April 2016

Significant User Visible Changes and New Features

o   added "correction" and "observed" attributes to the anova_table slot of
    afex_aov objects, which is used per default by nice(). This ensures 
    that nice(object) and object always return the same data.frame.
    Also added "nice_table" class to data.frame returned by nice which 
    print the value of the slots for information.
    Both contributed by Frederik Aust (who was also promoted to author).
o   added return="data" for mixed. This allows to obtain the data set used
    for fitting a model. Can be useful in combination with 
    and allFit() or for obtaining predictions. See ?allFit for a simple 

o   increased required version of R (>= 3.1.0) and the following packages
    to ensure afex runs with older versions of R: lme4 (>= 1.1-8), 
    pbkrtest (>= 0.4-1), and Matrix (>= 1.1.1).

                ** afex VERSION 0.15-x **
                Changes in afex Version 0.15-x
                Released October 2015

Significant User Visible Changes and New Features

o   added p.adjust.method argument for ANOVA functions (anova and nice 
    methods). Can be used to control for multiple comparisons in 
    exploratory ANOVAs (see Cramer, et al., 2015; PB&R). Functionality 
    contributed by Frederik Aust (

o   ANOVA functions work with dplyr data.frames now (data is transformed
    via See:

o   formulas for mixed can now be of a maximum length of 500 characters 
    (was 60 chars previously):
o   aov_car et al. did not work with within-subject factors that were also
    included outside the Error term. This was caused by the use of regular
    expressions not appropriate for the new stringi backend of stringr.
    Thanks to Tom Wenseleers for reporting this bug.

                ** afex VERSION 0.14-x **
                Changes in afex Version 0.14-x
                Released August 2015

Significant User Visible Changes and New Features

o   new default return argument for ANOVA functions, afex_aov, 
    an S3 object containing the following:
    (1) ANOVA table of class "anova"
    (2) ANOVA fitted with base R's aov (can be passed to lsmeans 
        for post-hoc tests)
    (3) output from car::Anova (for tests of effects),
        ANOVA table (1) is based on this model
    (4) lm object passed to car::Anova
    (5) data used for estimating (2) and (4)
o   added support for lsmeans: objects of class afex_aov can be 
    passed to lsmeans directly. afex now depends on lsmeans.

o   added afex_options() functionality for setting options globally.

o   added expand_re argument to mixed which, if TRUE, correctly interprets 
    the || notation in random effects with factors (i.e., suppresses 
    estimation of correlation among random effects). lmer_alt is a wrapper
    for mixed which uses expand_re = TRUE, returns an object of class
    merMod (i.e., does not calculate p-values), and otherwise behaves 
    like g/lmer (i.e., does not enforce certain contrasts)

o   added three new data sets (Singmann & Klauer, 2011; Klauer & 
    Singmann, 2013) and a vignette showing how to calculate 
    contrasts after ANOVA.

o   ANOVA functions renamed to aov_car, aov_ez, and aov_4. Old 
    functions are now deprecated.

o   first element in mixed object renamed to anova_table.

o   nice.anova renamed to nice. nice() can be called for afex_aov
    and mixed objects and returns a nicely formatted (numbers 
    converted to characters) results table (which is also the 
    default print method for both objects).
o   anova() can be called for afex_aov and mixed objects and returns
    the numeric anova table (i.e., the first element of each object).
    There also exists print methods for those data.frames.

o   summary method for mixed objects now calls summary.merMod on 
    full model.
o   afex does not depend on car package anymore, it is only imported.
o   afex is now hosted on github:

o   ANOVA: for "aov"-objects, contrasts are only set for factors.

o   compare.2.vector failed when the two means were exactly equal 
    (due to an issue with median_test). This only throws a warning 
o   compare.2.vector documentation updated for coin 1-1.0.

                ** afex VERSION 0.13-x **
                Changes in afex Version 0.13-x
                Released January 2015

Significant User Visible Changes and New Features

o   added ems() function for deriving the expected values of the 
    mean squares for factorial designs (contributed by Jake 


o et al. stop with error message if a factor has only one
o transforms id variable to factor which ensures that 
    return = "aov" provides equivalent results.
o   changed regex for detecting "observed" variables to work with 
    the new version of stringr which uses stringi.

                ** afex VERSION 0.12-x **
                Changes in afex Version 0.12-x
                Released November 2014

Significant User Visible Changes and New Features

o   ANOVA functions give informative error if some parameters are
    not estimable (most likely due to structural missings, i.e. 
    empty cells).


o   mixed(..., method = "PB") does not fail anymore when only 
    having a single fixed effect (thanks to Kiyoshi Sasaki for
    reporting it). 
o failed when a within-subject factor had empty levels.
    Unused factor levels are now dropped. This bug was probably 
    introduced in Rev 126 as part of an attempt to solve a bug. 
    (thanks to Will Bowditch for reporting it).

                ** afex VERSION 0.11-x **
                Changes in afex Version 0.11-x
                Released October 2014

Significant User Visible Changes and New Features

o   added allFit() function (written by Ben Bolker).

o   mixed() gives warning if nested model provides worse fit 
    (logLik) than full model (when fitted with ML).

o   print, summary, and anova method for mixed objects are now 
o   description of returned object from mixed() extended (Thanks
    to Ben Bolker, see
o   added return = "aov" to which returns the ANOVA 
    fitted with aov (with correct Error strata) so that it can be
    passed to lsmeans for post-hoc tests or plotting (lsmip).

o   all required functions are now correctly imported avoiding 
    CRAN warnings and better functioning.
o   data argument to lmer calls in mixed set correctly. Note that
    still contrasts added to the data in mixed may prohibit use of 
    predict.merMod() or similar functions. It is recommended to 
    set the contrasts globally to "contr.sum", e.g., 
    via set_sum_contrasts(), for correct functioning
    (disable via argument for mixed).

                ** afex VERSION 0.10-x **
                Changes in afex Version 0.10-x
                Released August 2014

Significant User Visible Changes and New Features

o   afex does not change the global contrasts anymore when loading 
    the package (due to popular demand).

o   new functions to globally set contrasts: set_sum_contrasts & 
    set_treatment_contrasts (and some more wrappers).
o   Added more mixed model examples from Maxwell & Delaney, Chapter 
    15 ("Multilevel Models for within subjects designs"), see ?mixed 
    and ?md_15.1. Thanks to Ulf Mertens. 

o   removed bug when factor levels of within-subject factors
    contained "+" or "-" and were not converted correctly. Added 
    tests for known bugs in

                ** afex VERSION 0.9-x **
                Changes in afex Version 0.9-x
                Released April 2014

Significant User Visible Changes and New Features

o   added function aov4: another wrapper of car::Anova for ANOVA
    models specified via lme4::lmer syntax.

o   added return="marginal" to which returns the marginal 
    means of all effects (i.e., grand mean and mean of main 
    effects and interactions). Is also returned if return="full".
o   testing the intercept in mixed models in now only optional.
    The default is that the new argument test.intercept = FALSE.

o   removed return="lme4" from

o   added argument intercept to nice.anova (default FALSE) which
    allows to selective display the intercept in an ANOVA table.
o   added method = "F" to mixed() which only returns the F-value 
    but no ddf (and hence no p-value). Experimental, not documented.
o   renamed colname "stat" to "F" when method = "KR" (mixed).

o   added tests (currently mainly for mixed()) via pkg testthat, 
    see tests/testthat.

o   increased requirement of R version and lme4 version.    

o   print.mixed should now propagate all warnings from lme4 (i.e., 
    also the new convergence warnings).
o   lme4 is now loaded at worker nodes for mixed (default, 
    turn of via check.contrasts)


                ** afex VERSION 0.8-x **
                Changes in afex Version 0.8-x
                Released February 2014

Significant User Visible Changes and New Features

o   removed renaming of within subject factor levels to names
    with length 1 (which was introduced in 0.6).
o   helper function round_ps which nicely rounds p-values
    is now exported.
o   warning about numerical non-centered variables in mixed
    is now a message only.
o   added examples data sets from Maxwell & Delaney (2004) for 
    within-subjects ANOVA and mixed models.
o   reshape2 is now again in depends but coin only imported.

o   the default effect size (ges: generalized eta-squared) was 
    calculated wrong (the error term for the intercept was not 
    included). Sorry. This is now corrected (with new examples).

o   removed bug that didn't work when running some ANCOVAs
    (thanks to Gang Chen for reporting this).


                ** afex VERSION 0.7-x **
                Changes in afex Version 0.7-x
                Released December 2013

Significant User Visible Changes and New Features

o   nicer output of print.mixed


o   mixed() correctly converts all non-formula arguments to 
    formula correctly to a formula (gave error if formula
    was a different object and not-available on cluster nodes).
    Prints message if formula is converted to a formula.
o   Using multicore for fitting the models prodcued erroneous 
    results (did not use correct contrasts at nodes). Now the
    current contrasts are also set at the nodes.
o   mixed() sets REML = FALSE if method = "LRT" and no 
    family argument is present (i.e., for LMMs) as LRTs
    for models fitted with REML are not recommended.
o   on the cluster nodes now only the lme4 namespace is 
    loaded (instead of using library) to avoid a CRAN note.

                ** afex VERSION 0.6-x **
                Changes in afex Version 0.6-x
                Released September 2013

Significant User Visible Changes and New Features

o   added LRT tests to mixed() which should replicate
    the recommended test by Barr et al. (2013, JML).
o   multicore estimation of (g)lmer models now available
    through package parallel (argument cl) for mixed().
o   added experimental "lme4" return method for and ez.glm (which fits the data using lmer).
o   reshape2 and stringr are not any more loaded when
    loading afex (are now only imported via Imports)


o   Type 2 tests of mixed() were implemented incorrectly (i.e.,
    they did not give what they should have given according
    to the description in the help file).
o and ez.glm() now convert factor levels of within 
    subjects factors to be of length one so that long levels 
    do not lead to problems when constructing the call to lm. 
    Thanks to Isaac Schwabacher for noticing this, see also:

o   mixed should now work with missing values as it construes
    a new data object if the model.matrix from which the
    different versions are fitted has nrow different to 
    nrow(data) (with warning). Thanks to Daniel Bunker
    for reporting this.
o   mixed should work with the newest version of lme4 (>= 1.0.4)
    and the newst version of pbkrtest (>= 0.3-5.1), but not 
    with older versions (due to CRAN policy to disallow :::).

                ** afex VERSION 0.5-x **
                Changes in afex Version 0.5-x
                Released May 2013

Significant User Visible Changes and New Features

o   added argument factorize (default: TRUE) to and ez.glm:
    all variables are now factorized per default. (now it is 
    necessary to set factorize=FALSE for ANCOVAs!)

o   added argument per.parameter to mixed() which allows to specify
    variables for which each parameter is tested (not only the 
    overall effect). Can be useful to test for ordered factors. 
    (only implemented for "Type 3" tests)

o   added more informative startup message (thanks to Robert Volcic 
    and Lars Konieczny)
o   mixed, ez.glm and now check for correct contrasts and set 
    factors to contr.sum if other contrasts are found (can be tunred 
    off via check.contrasts argument). Resetting the default contrast
    to contr.treatment should therefore not interfere with afex.
    (this is not intensly tested, so please report anything)

o   mixed checks numeric variables if they are centered on 0 (and
    gives warning if not).


o   checks if observed variable is in the data and throws
    an error if not (nice.anova)

                ** afex VERSION 0.4-x **
                Changes in afex Version 0.4-x
                Released February 2013

Significant User Visible Changes and New Features

o   added generalized and partial eta-squared effect
    sizes to nice.anova (this also included adding
    observed arguments to, ez.glm, nice.anova)        

o   added new return arguments to and ez.glm
    (nice, lm, data).

o   changed default return value of and ez.glm
    to "nice" which now returns a nice ANOVA table

o   mixed has method = "PB" for obtaining parametric
    bootstrapped p-values (e.g., for GLMMs)
o   added alternative argument to compare.2.vectors.

o (and ez.glm) now give a warning if 
    observations are missing on within-subject factors.
    (As before, cases with missing values are simply
    excluded from the analysis)

o   had to disable saving of the previous contrasts and
    resetting those after detaching afex due to 
    CRAN policies (no assignment in global environment).


o   Bug when running mixed() inside a function and
    handing the data.frame over as an argument fixed
    (thanks, again, to Florent Duyme). See

o   nice.anova did not work with a model with only one 
    within-subjects factor.

                ** afex VERSION 0.3-x **
                Changes in afex Version 0.3-x
                Released August/September 2012

Significant User Visible Changes and New Features

o   added function compare.2.vectors().

o   Name of function univariate() changed to univ(), as
    a function with this name is part of package multcomp.
    This may leed to problems. Thanks to Florent Duyme for
    spotting this.

o   added return argument to and ez.glm().

o   added rather dubious Type 2 tests to mixed().

o checks if each id is only present in one
    cell of the between subjects design.


o now uses when calling lm() to avoid local
    variables in the call to lm() which could led to problems when
    working with the lm model.

o   mixed() now passes ... (further arguments to lmer) correctly.
    (Now mixed uses and eval on the call instead of 
    invoking lmer directly.)

o   corrected bug that was introduced by pbkrtest v3.2 when 
    invoking the print method to an object of class mixed 
    (again thanks to Florent Duyme for spotting this).

o   removed bug when factor levels of within-subject factors
    were non-legal names (solution uses make.names).

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.20-2 by Henrik Singmann, 3 months ago,

Report a bug at

Browse source code at

Authors: Henrik Singmann [aut, cre] (<>), Ben Bolker [aut], Jake Westfall [aut], Frederik Aust [aut] (<>), Søren Højsgaard [ctb], John Fox [ctb], Michael A. Lawrence [ctb], Ulf Mertens [ctb], Jonathon Love [ctb], Russell Lenth [ctb], Rune Haubo Bojesen Christensen [ctb]

Documentation:   PDF Manual  

GPL (>= 2) license

Imports stringr, coin, pbkrtest, lmerTest, car, reshape2, stats, methods

Depends on lme4, emmeans

Suggests xtable, parallel, plyr, optimx, nloptr, knitr, rmarkdown, R.rsp, lattice, latticeExtra, multcomp, testthat, mlmRev, dplyr, tidyr, dfoptim, Matrix, psych, ggplot2, MEMSS, effects, carData

Imported by fullfact, jmv, r2glmm.

Suggested by apa, standardize.

See at CRAN