# Calculate contrasts from lme model across specified timepoints

I have an MMRM with the below structure:

#la_mm2 - continuous float
#arm - 2 level factor ("2mg", "SHAM")
#t_months - continuous float
#partI_bool - 2 level factor (1, 0)
#da_bool - 2 level factor (1, 0)

linr_2mg_mm2 <- lme(la_mm2 ~ arm*t_months + partI_bool*t_months + da_bool*t_months,
data = test,
method = "REML",
na.action = na.omit,
random = ~1|pt_id,
correlation = corSymm(form = ~1|pt_id),
weights = varIdent(form = ~1|t_months),
control=lmeControl(msMaxIter = 200, opt = "optim"))



I want to calculate the contrasts of the model estimates within arms over time.

I've arrived at the below to get the estimates at specified times, but I can't figure out how to difference in the contrasts.

tmp <- emmeans(linr_2mg_mm2,~arm|t_months, at=list(t_months = c(0,6,12)))

#
#Output
#

t_months =  0:
arm  emmean    SE  df lower.CL upper.CL
2mg    9.32 0.345 150     8.64     10.0
SHAM   9.84 0.303 150     9.24     10.4

t_months =  6:
arm  emmean    SE  df lower.CL upper.CL
2mg   10.32 0.355 150     9.62     11.0
SHAM  11.14 0.313 150    10.52     11.8

t_months = 12:
arm  emmean    SE  df lower.CL upper.CL
2mg   11.33 0.385 150    10.57     12.1
SHAM  12.44 0.339 150    11.77     13.1

Results are averaged over the levels of: partI_bool, da_bool
Degrees-of-freedom method: containment
Confidence level used: 0.95



The closest I've come to obtaining contrasts as desired is the below, but this is only returning the mean estimate for the "2mg" group at each of the time points:

tmp.c <- contrast(emmeans(linr_2mg_mm2, ~arm|t_months, at=list(t_months = c(0,6,12))), list(my.con = c(1,0)))

#
# Output
#

t_months =  0:
contrast estimate    SE  df t.ratio p.value
my.con       9.32 0.345 150  27.038  <.0001

t_months =  6:
contrast estimate    SE  df t.ratio p.value
my.con      10.32 0.355 150  29.061  <.0001

t_months = 12:
contrast estimate    SE  df t.ratio p.value
my.con      11.33 0.385 150  29.401  <.0001

Results are averaged over the levels of: partI_bool, da_bool
Degrees-of-freedom method: containment


I've tried the "pairwise" approach as below but this reports contrasts across arm at a specified time.

> contrast(tmp, "pairwise")
t_months =  0:
contrast   estimate    SE  df t.ratio p.value
2mg - SHAM   -0.528 0.389 150  -1.358  0.1766

t_months =  6:
contrast   estimate    SE  df t.ratio p.value
2mg - SHAM   -0.820 0.401 150  -2.046  0.0425

t_months = 12:
contrast   estimate    SE  df t.ratio p.value
2mg - SHAM   -1.112 0.434 150  -2.559  0.0115

Results are averaged over the levels of: partI_bool, da_bool
Degrees-of-freedom method: containment



Overall I'm trying to calculate:

(2mg at 12 months - 2 mg at 0 months) - (SHAM at 12 months - SHAM at 0 months)

You can do

contrast(tmp, "pairwise")


or justpairs(tmp). I suggest looking at some of the vignettes vignettes that accompany the package, for example vignette("basics", "emmeans"). There are lots of examples.

Note: The my.con "contrasts" shown in the OP are just the first of the two means, because the linear function specified is 1 times the first plus 0 times the second.

•  contrast(tmp, "pairwise") Produces the below output, which is contrasting one arm versus the other (e.g., 2mg-SHAM). What I am trying to do is calculate: (2mg at month 12 - 2mg at baseline) - (SHAM at month 12 - SHAM at baseline) So I'm looking for a way to calculate contrasts within the same arm across time. Jul 14, 2022 at 18:17
•  t_months = 0: contrast estimate SE df t.ratio p.value 2mg - SHAM -0.528 0.389 150 -1.358 0.1766 t_months = 6: contrast estimate SE df t.ratio p.value 2mg - SHAM -0.820 0.401 150 -2.046 0.0425 t_months = 12: contrast estimate SE df t.ratio p.value 2mg - SHAM -1.112 0.434 150 -2.559 0.0115 Results are averaged over the levels of: partI_bool, da_bool Degrees-of-freedom method: containment  Jul 14, 2022 at 18:18
• pairs(tmp, by = "arm") Jul 14, 2022 at 18:19
• Or switch sides of the | when creating tmp Jul 14, 2022 at 18:58