2
$\begingroup$

I'm trying to setup a custom contrast using emmeans but am a bit unsure on how to do so properly.

I have two factors, let's call them A and B, with three levels each.

I want to test the following contrasts:

$$ c_1 = [ 0.5A_1 \quad 0.5B_3 \quad -1(AB)_{22}]$$ $$ c_2 = [ 0.5A_3 \quad 0.5B_1 \quad -1(AB)_{22}]$$

I have the following R code so far.

# data is in dataframe df

mod <- glm(Y ~ A * B, data=df)
emm <- emmeans(mod, ~A*B)
emm

What I'm unsure of is how to set the coefficients for the interaction terms to use with the contrast() function. When I display the emm object as I did in the above code, interaction terms aren't listed. It gives me marginal means for each combination of factor levels, and not estimates for the main effects.

That makes me think I can set the contrast for $c_1$ as this:

$$ \left[ \frac{1}{6}(AB)_{11} \quad \frac{1}{6}(AB)_{12} \quad \frac{1}{6}(AB)_{13} \quad \frac{1}{6}(AB)_{13} \quad \frac{1}{6}(AB)_{23} \quad \frac{1}{6}(AB)_{33} \quad -1(AB)_{22} \right]$$

(and similarly for $c_2$)

Does this make sense? It feels clunky and I feel like I'm missing an easier solution.

Thanks.

$\endgroup$
2
  • $\begingroup$ Your notation isn't clear. By A_1 and (AB)_11, are you talking about terms in the model ? Or is A_1 the marginal mean for the 1st level of A? $\endgroup$ Commented May 7 at 2:24
  • $\begingroup$ Sorry, yes A_1 signifies the marginal mean for the 1st level of A. I had a professor who used such notation so I thought it was more common. $\endgroup$ Commented May 8 at 15:17

1 Answer 1

3
$\begingroup$

One thing you could do is to get all of the main-effect marginal means and cell means and use rbind() to stack them together in one combined set, then pick out the things you want to contrast:

all <- emmeans(mod, ~ .) |> rbind()   # the ~ . spec requires emmeans >= 1.11.0
contrast(all, list(
    con1 = c(.5,0,0,  0,0,.5,  0,0,0, 0,-1,0, 0,0,0),
    con2 = c(0,0,.5,  .5,0,0,  0,0,0, 0,-1,0, 0,0,0)))

I think I have these coefficients right, but it's best to do confint(all) so you can see exactly the order they appear in.

$\endgroup$
1
  • $\begingroup$ Another variation is to do contrast(all[c(1,3,4,6,11)], ...) so you wouldn't have to type so many zeros. Oh, and maybe add adjust = "none" if you don't want the Bonferroni adjustment $\endgroup$ Commented May 9 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.