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.
- Put the data in long format: one row per observation, with a participant identifier column, the dependent variable and one column per factor.
- 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")) - Print the result by typing
a1, or usenice(a1)for a table suitable forknitr::kable, oranova(a1)for an anova data frame. - 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. - 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
- CRAN, afex package page, cran.r-project.org/web/packages/afex, read 2026-11-08.
- CRAN, afex vignette “ANOVA example”, afex_anova_example, read 2026-11-08.
- CRAN, ez package page, cran.r-project.org/web/packages/ez, read 2026-11-08.
- afex project website, afex.singmann.science.







