R emmeans

emmeans is a package for calculating estimated marginal means.


Installation

Install the emmeans package.

install.packages('emmeans')


Usage

Given a model fit by e.g., lm, try:

emmeans(mod, specs = ~ treatment)

The specs option specifies the marginal means to calculate.

If the sample should be split by some group variable before calculating marginal means, for example because the model estimated the interaction of a treatment and the group identifier, then try:

For e.g. a two-way ANOVA, it's often desirable to calculate the marginal means for one factor holding the second factor constant. Try:

mod <- lm(y ~ factor1 * factor2, data=df)

emmeans(mod, specs = ~ factor1 | factor2)
#or
emmeans(mod, specs = ~ factor1, by="factor2")

Calculate pairwise comparisons as:

pairs(emmeans(mod, specs = ~ treatment))

The adjust option determines what adjustments are applied to test statistics. If unset, by default Tukey's adjustment is used. Alternative options are "none" and "bonferroni".

pairs returns an object that can be passed to the plot function.


CategoryRicottone

R/Emmeans (last edited 2026-09-03 18:40:43 by DominicRicottone)