R emmeans
emmeans is a package for calculating estimated marginal means.
Contents
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.
Can instead pass a vector of variable names, as in specs = c("foo", "bar", "baz")
- If a specified variable is continuous, one marginal mean is calculated at the mean.
- If a specified variable is a factor, a marginal mean is calculated for each level.
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.
