title: contrast of contrast with emmeans (second differences)
body: <p>I am using emmeans to conduct a contrast of a contrast (i.e., testing for an interaction effect through 1st/2nd differences).</p>
  <p>It involves 3 steps:</p>
  <ol>
  <li>estimate means using “emmeans”</li>
  <li>estimate if there is a difference in means (1st difference) using “pairs”</li>
  <li>estimate if there is a difference in the difference  (2nd difference) using ????</li>
  </ol>
  <p>While I can execute steps 1 and 2 (see reprex below with fictions data), i’m stuck on step 3.  Tips?</p>
  <p>(the contrast of a contrast shown in the vignette <a href="https://cran.r-project.org/web/packages/emmeans/vignettes/interactions.html#contrasts" rel="nofollow noreferrer">here</a> is for alternative functional forms, which is somewhat different than what I want to test)</p>
  <pre class="lang-r prettyprint-override"><code>
  suppressPackageStartupMessages({
    library(emmeans)})
  
  # create ex. data set.  1 row per respondent (dataset shows 2 resp). 
  cedata.1 &lt;- data.frame( id    =  c(1,1,1,1,1,1,2,2,2,2,2,2),    
                          QES    = c(1,1,2,2,3,3,1,1,2,2,3,3),   # Choice set   
                          Alt    = c(1,2,1,2,1,2,1,2,1,2,1,2),   # Alt 1 or Alt 2 in  choice set 
                          Choice = c(0,1,1,0,1,0,0,1,0,1,0,1),   # Dep variable.  if  Chosen (1) or not (0)
                          LOC    = c(0,0,1,1,0,1,0,1,1,0,0,1),   # Indep variable per Choice set, binary categorical 
                          SIZE   = c(1,1,1,0,0,1,0,0,1,1,0,1),   # Indep variable per Choice set, binary categorical 
                          gender = c(1,1,1,1,1,1,0,0,0,0,0,0)    # Indep variable per indvidual, binary categorical 
  )
  
  
  # estimate model
  glm.model &lt;- glm(Choice ~  LOC*SIZE, data=cedata.1, family = binomial(link = &quot;logit&quot;))
  
  # estimate means (i.e.,  values used to calc 1st diff). 
  comp1.loc.size &lt;- emmeans(glm.model, ~ LOC * SIZE) 
  
  # calculate 1st diff (and p value)
  pairs(comp1.loc.size, simple = &quot;SIZE&quot;)   # gives result I want
  #&gt; LOC = 0:
  #&gt;  contrast estimate   SE  df z.ratio p.value
  #&gt;  0 - 1       -1.39 1.73 Inf -0.800  0.4235 
  #&gt; 
  #&gt; LOC = 1:
  #&gt;  contrast estimate   SE  df z.ratio p.value
  #&gt;  0 - 1        0.00 1.73 Inf  0.000  1.0000 
  #&gt; 
  #&gt; Results are given on the log odds ratio (not the response) scale.
  
  # calculate 2nd diff (and p value)
  # ** the following gives the relevant values for doing the 2nd diff comparison (i.e., -1.39 and 0.00)...but how to make the statistical comparison?
  pairs(comp1.loc.size, simple = &quot;SIZE&quot;)
  #&gt; LOC = 0:
  #&gt;  contrast estimate   SE  df z.ratio p.value
  #&gt;  0 - 1       -1.39 1.73 Inf -0.800  0.4235 
  #&gt; 
  #&gt; LOC = 1:
  #&gt;  contrast estimate   SE  df z.ratio p.value
  #&gt;  0 - 1        0.00 1.73 Inf  0.000  1.0000 
  #&gt; 
  #&gt; Results are given on the log odds ratio (not the response) scale.
  </code></pre>