[R-sig-ME] Coding comparisons and interactions with the R package emmeans
Chen, Gang (NIH/NIMH) [E]
g@ngchen @end|ng |rom m@||@n|h@gov
Fri Oct 18 23:33:12 CEST 2024
I am starting to learn how to use the R package emmeans and would appreciate help with some specific applications.
For illustration, let me use a dataset from the afex package. The variable phase is a factor with three levels: "fup", "post", and "pre". The variable age is continuous. Below is a model fit using lme4:
library(afex); library(lme4); library(phia)
data(obk.long, package = "afex")
options(contrasts = c("contr.sum", "contr.poly"))
fm <- lmer(value ~ phase * age + (1|id), data = obk.long)
I would like to replicate the following seven comparisons and interactions using emmeans:
1. Contrast between the first two levels ("fup" vs. "post") of the factor phase
In phia, this is done with:
testInteractions(fm, custom = list(phase = c(-1, 1, 0)), adjustment = "none")
1. Contrast between the first two levels of phase, with age fixed at 5.5
In phia:
testInteractions(fm, custom = list(phase = c(-1, 1, 0)), covariates = c(age = 5.5), adjustment = "none")
1. Slope of age (averaged across all levels of phase)
In phia:
testInteractions(fm, pairwise = NULL, slope = "age", adjustment = "none")
1. Slope difference in age between the first two levels ("fup" vs. "post") of phase
In phia:
testInteractions(fm, custom = list(phase = c(-1, 1, 0)), slope = "age", adjustment = "none")
1. Composite contrast among the three levels of phase (e.g., "fup" - "pre" and "post" - "pre"). By "composite contrasts," I mean a null hypothesis that involves multiple effects simultaneously. In this case, the null hypothesis for the three factor levels is 𝐻0: fup - pre = 0 and post - pre = 0.
In phia:
testFactors(fm, levels = testFactors(fm, levels = list(phase = rbind(diag(2), c(-1,-1)))))
1. Composite slope comparisons among the three levels of phase (e.g., slope differences for "fup" vs. "pre" and "post" vs. "pre")
In phia:
testFactors(fm, levels = testFactors(fm, levels = list(phase = rbind(diag(2), c(-1,-1)))), slope = "age")
1. Composite contrasts among the three levels of phase at a fixed age (e.g., "fup" - "pre" and "post" - "pre" at age = 5.5)
In phia:
testFactors(fm, levels = testFactors(fm, levels = list(phase = rbind(diag(2), c(-1,-1)))), covariates = c(age = 5.5))
My question is: how can I code these seven effects/interactions using the emmeans package?
Thanks,
Gang Chen
[[alternative HTML version deleted]]
More information about the R-sig-mixed-models
mailing list