Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

afex R Package: aov_ez ANOVA Guide for Factorial Designs

How to install and use the afex R package for factorial ANOVA: aov_ez, aov_car and aov_4 equivalents, output and troubleshooting.

Written and maintained by CASRAI Editorial Board

Last updated

The afex R package (Analysis of Factorial Experiments) runs ANOVA and mixed models for factorial designs from long-format data, and sets sensible defaults for you: Type 3 sums of squares, sum-to-zero contrasts and sphericity corrections for within-subject factors. This page shows the install, the three equivalent ways to specify the same model (aov_ez, aov_car, aov_4) and how to read the output. It is for researchers analysing experimental data in R. (“AFEX” is also the name of unrelated commercial organisations; this page covers only the R package.)

What the afex package is used for in research

According to its CRAN listing (read 2026-11-08), afex provides functions for ANOVA and mixed models for factorial designs, with Type 3 sums of squares by default, plus ggplot2-based plotting. Version 1.5-1 was published on CRAN on 2025-12-14 under GPL (≥ 2). The maintainer is Henrik Singmann; the authors listed are Singmann, Bolker, Westfall, Aust and Ben-Shachar. It requires R ≥ 3.5.0 and lme4 ≥ 1.1-8, and imports packages including pbkrtest, lmerTest, car and reshape2. emmeans, ggplot2, glmmTMB and brms appear under Suggests.

Use it when you have a between-subjects, within-subjects or mixed factorial design and want an ANOVA table with sensible defaults, without hand-setting contrasts. For general mixed-model background see linear mixed-effects models in R.

Install or access (official source only)

afex is distributed on CRAN, the official source: cran.r-project.org/web/packages/afex. The project website is afex.singmann.science and the development repository is singmann/afex on GitHub. From R:

install.packages("afex")library(afex)

Step-by-step: a mixed-design ANOVA

The package vignette (read 2026-11-08) uses the built-in sk2011.1 data: 640 observations of 9 variables from 40 participants, with instruction as a between-subjects factor and inference (four levels) and plausibility as within-subject factors.

  1. Put the data in long format: one row per observation, with a participant identifier column, the dependent variable and one column per factor.
  2. Fit the model with aov_ez, passing the id, the dependent variable and factor names as character strings:
    data(sk2011.1)a1 <- aov_ez("id", "response", sk2011.1,             between = "instruction",             within = c("inference", "plausibility"))
  3. Print the result by typing a1, or use nice(a1) for a table suitable for knitr::kable, or anova(a1) for an anova data frame.
  4. Follow up significant effects with emmeans:
    library(emmeans)m1 <- emmeans(a1, ~ inference)pairs(m1)emmeans(a1, "inference", by = "instruction")

    The vignette notes that pairs() uses Tukey adjustment by default. For other adjustments see Bonferroni correction in R.

  5. Plot:
    afex_plot(a1, x = "inference", trace = "instruction",          panel = "plausibility", error = "within")

The three equivalent ways to specify the same model

Function How you specify the model Call for the example above
aov_ez Character strings for id, dependent variable and factors aov_ez("id", "response", sk2011.1, between = "instruction", within = c("inference", "plausibility"))
aov_car Formula with an Error() term aov_car(response ~ instruction + Error(id/inference*plausibility), sk2011.1)
aov_4 lme4-style formula with the id after a bar aov_4(response ~ instruction + (inference*plausibility|id), sk2011.1)

The package documentation states that the same model results from all three; choose whichever syntax you find easiest to read and reproduce.

Output and how to read it

Printing an afex_aov object gives a Type 3 ANOVA table with the columns Effect, df, MSE, F, ges and p.value, together with a sphericity correction. Per the vignette defaults:

  • ges is generalized eta squared, the effect size reported by default.
  • Sphericity: the Greenhouse-Geisser correction is applied to within-subject factors with more than two levels, so the df shown for those effects are corrected.
  • Type 3 sums of squares and automatically set sum-to-zero contrasts (contr.sum) are used.
  • emmeans on an afex object uses the multivariate model by default.

If you plan sample sizes for this kind of design, see G*Power; for ANOVA assumptions see assumptions of two-way ANOVA.

Troubleshooting

A warning about multiple observations per cell

afex expects one observation per participant per condition cell. Per the vignette, when there are several, they are averaged (fun_aggregate = mean) and a warning is shown. If you did not intend that, check your data for duplicate rows or a missing factor column.

Errors about variable names or data shape

The data must be in long format, and for aov_ez the id, dependent variable and factors are passed as character strings naming columns (for example aov_ez("id", "log_rt", fhch, between = "task", within = c("stimulus", "length")) in the package examples).

Free alternatives

Package What it offers Source (read 2026-11-08)
ez Easy analysis and visualization of factorial experiments: within-subjects, between-subjects and mixed designs; version 4.5-0, published 2026-02-16, GPL-2 | GPL-3 CRAN: ez
lme4 Mixed-effects models; afex requires it (lme4 ≥ 1.1-8) CRAN: afex
emmeans Estimated marginal means and post-hoc contrasts; listed under afex Suggests CRAN: afex

For the SAS equivalent of a factorial ANOVA workflow, see ANOVA in SAS.

FAQ

What is afex?

An R package, maintained by Henrik Singmann, for ANOVA and mixed models in factorial designs with Type 3 sums of squares by default (CRAN, version 1.5-1, 2025-12-14).

How do I install afex?

Run install.packages("afex") in R, then library(afex); CRAN is the official distribution.

What is the difference between aov_ez, aov_car and aov_4?

Only the syntax: character strings, an Error() formula, or an lme4-style formula. All three fit the same model.

Which effect size does afex report?

Generalized eta squared (ges) by default.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about afex R Package: aov_ez ANOVA Guide for Factorial Designs

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →