Skip to content
Back to formatted view

Raw Message

Message-ID: <MW4PR09MB932930A6587D2152FA120573ED402@MW4PR09MB9329.namprd09.prod.outlook.com>
Date: 2024-10-18T21:33:12Z
From: Chen, Gang (NIH/NIMH) [E]
Subject: Coding comparisons and interactions with the R package emmeans
In-Reply-To: <DM6PR04MB44740C72BA14660A2720798EF1402@DM6PR04MB4474.namprd04.prod.outlook.com>

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]]