contrast of contrast with emmeans (second differences)

975 views Asked by At

I am using emmeans to conduct a contrast of a contrast (i.e., testing for an interaction effect through 1st/2nd differences).

It involves 3 steps:

  1. estimate means using “emmeans”
  2. estimate if there is a difference in means (1st difference) using “pairs”
  3. estimate if there is a difference in the difference (2nd difference) using ????

While I can execute steps 1 and 2 (see reprex below with fictions data), i’m stuck on step 3. Tips?

(the contrast of a contrast shown in the vignette here is for alternative functional forms, which is somewhat different than what I want to test)


suppressPackageStartupMessages({
  library(emmeans)})

# create ex. data set.  1 row per respondent (dataset shows 2 resp). 
cedata.1 <- 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 <- glm(Choice ~  LOC*SIZE, data=cedata.1, family = binomial(link = "logit"))

# estimate means (i.e.,  values used to calc 1st diff). 
comp1.loc.size <- emmeans(glm.model, ~ LOC * SIZE) 

# calculate 1st diff (and p value)
pairs(comp1.loc.size, simple = "SIZE")   # gives result I want
#> LOC = 0:
#>  contrast estimate   SE  df z.ratio p.value
#>  0 - 1       -1.39 1.73 Inf -0.800  0.4235 
#> 
#> LOC = 1:
#>  contrast estimate   SE  df z.ratio p.value
#>  0 - 1        0.00 1.73 Inf  0.000  1.0000 
#> 
#> 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 = "SIZE")
#> LOC = 0:
#>  contrast estimate   SE  df z.ratio p.value
#>  0 - 1       -1.39 1.73 Inf -0.800  0.4235 
#> 
#> LOC = 1:
#>  contrast estimate   SE  df z.ratio p.value
#>  0 - 1        0.00 1.73 Inf  0.000  1.0000 
#> 
#> Results are given on the log odds ratio (not the response) scale.
2

There are 2 answers

4
Russ Lenth On BEST ANSWER

pairs(pairs(comp1.loc.size, simple = "SIZE"), by = NULL)

0
Poza On

Another solution:

# estimate means (i.e.,  values used to calc 1st diff). 
comp1.loc.size <- emmeans(glm.model, ~ LOC | SIZE) 

# second difference:
pairs(pairs(emmeans::regrid(comp1.loc.size)), by = NULL)  

PS: This solution is almost a copy of the solution here: Testing contrast of contrast (first/second difference) in outcome