title: lsmeans contrast for interaction term
body: <p>I'm attempting use <code>lsmeans</code> and its contrast for an F-test on an interaction. Basically, I'd like to replicate what Stata does with its <code>contrast</code> command. I'd like to do this for two reasons: </p>
  
  <ol>
  <li><p>within a regression model that has an interaction between factor variables; </p></li>
  <li><p>within an ANOVA to help decompose a three-way interaction.</p></li>
  </ol>
  
  <p>For this question, I'll ask about the three-way interaction.  </p>
  
  <pre><code>library(haven)
  threeway &lt;- read_spss("https://stats.idre.ucla.edu/wp-content/uploads/2016/02/threeway.sav")
  threeway$ID &lt;- row.names(threeway)
  
  library(afex)
  three_fit &lt;- aov_ez("ID", "y", data = threeway, between = c("a", "b", "c"))
  three_fit
  
  &gt; three_fit
  Anova Table (Type 3 tests)
  
  Response: y
    Effect    df  MSE          F ges p.value
  1      a 1, 12 1.33 112.50 *** .90  &lt;.0001
  2      b 1, 12 1.33       0.50 .04     .49
  3      c 2, 12 1.33  47.84 *** .89  &lt;.0001
  4    a:b 1, 12 1.33 120.13 *** .91  &lt;.0001
  5    a:c 2, 12 1.33     6.84 * .53     .01
  6    b:c 2, 12 1.33    8.47 ** .59    .005
  7  a:b:c 2, 12 1.33    6.97 ** .54    .010
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1
  </code></pre>
  
  <p>The three-way interaction is significant. Now, from the <a href="https://stats.idre.ucla.edu/stata/faq/how-can-i-understand-a-three-way-interaction-in-anova-stata-12/" rel="nofollow noreferrer">UCLA page on Stata</a>, Stata can use the code:</p>
  
  <pre><code>contrast b#c@a
  </code></pre>
  
  <p>This will produce an F-test for the b*c interaction at the levels of a. </p>
  
  <p>I'm trying to do the same thing with <code>lsmeans</code> in R. But, I just can't get it. Here's what I've tried:</p>
  
  <pre><code>library(lsmeans)
  lsm &lt;- lsmeans(three_fit, c("b", "c"), by="a")
  test(contrast(lsm, "consec"), joint=TRUE)
  </code></pre>
  
  <p>That gets me an F-test, but it is not correct (or at least it is not the one I want). Any help in replicating the Stata results would be appreciated. I'd really like to stay within <code>lsmeans</code> to do this, but if something else works, I'll take it.</p>