1

I want to conduct a simple two sample t-test in R to compare marginal effects that are generated by ggpredict (or ggeffect).

Both ggpredict and ggeffect provide nice outputs: (1) table (pred prob / std error / CIs) and (2) plot. However, it does not provide p-values for assessing statistical significance of the marginal effects (i.e., is the difference between the two predicted probabilities difference from zero?). Further, since I’m working with Interaction Effects, I'm also interested in a two sample t-tests for the First Differences (between two marginal effects) and the Second Differences.

Is there an easy way to run the relevant t tests with ggpredict/ggeffect output? Other options?

Attaching: . reprex code with fictitious data . To be specific: I want to test the following "1st differences":

--> .67 - .33=.34 (diff from zero?)

--> .5 - .5 = 0 (diff from zero?)

...and the following Second difference:

--> 0.0 - .34 = .34 (diff from zero?)

See also Figure 12 / Table 3 in Mize 2019 (interaction effects in nonlinear models)

Thanks Scott


  library(mlogit)
#> Loading required package: dfidx
#> 
#> Attaching package: 'dfidx'
#> The following object is masked from 'package:stats':
#> 
#>     filter
  library(sjPlot)
  library(ggeffects)
  
  # create ex. data set.  1 row per respondent (dataset shows 2 resp). Each resp answers 3 choice sets, w/ 2 alternatives in each set. 
  cedata.1 <- data.frame( id    =  c(1,1,1,1,1,1,2,2,2,2,2,2),    # respondent ID. 
                          QES    = c(1,1,2,2,3,3,1,1,2,2,3,3),   # Choice set (with 2 alternatives)    
                          Alt    = c(1,2,1,2,1,2,1,2,1,2,1,2),   # Alt 1 or Alt 2 in  choice set 
                          LOC    = c(0,0,1,1,0,1,0,1,1,0,0,1),   # attribute describing alternative. binary categorical variable
                          SIZE   = c(1,1,1,0,0,1,0,0,1,1,0,1),   # attribute describing alternative. binary categorical variable
                          Choice = c(0,1,1,0,1,0,0,1,0,1,0,1),   # if alternative is Chosen (1) or not (0)
                          gender = c(1,1,1,1,1,1,0,0,0,0,0,0)   # male or female (repeats for each indivdual) 
  )
  
  # convert dep var Choice to factor as required by sjPlot
  cedata.1$Choice <- as.factor(cedata.1$Choice)
  cedata.1$LOC <- as.factor(cedata.1$LOC)
  cedata.1$SIZE <- as.factor(cedata.1$SIZE)
  
  # estimate model. 
glm.model <- glm(Choice ~  LOC*SIZE, data=cedata.1, family = binomial(link = "logit"))

  # estimate MEs for use in IE assessment
LOC.SIZE <- ggpredict(glm.model, terms = c("LOC", "SIZE")) 
LOC.SIZE
#> 
#> # Predicted probabilities of Choice
#> # x = LOC
#> 
#> # SIZE = 0
#> 
#> x | Predicted |   SE |       95% CI
#> -----------------------------------
#> 0 |      0.33 | 1.22 | [0.04, 0.85]
#> 1 |      0.50 | 1.41 | [0.06, 0.94]
#> 
#> # SIZE = 1
#> 
#> x | Predicted |   SE |       95% CI
#> -----------------------------------
#> 0 |      0.67 | 1.22 | [0.15, 0.96]
#> 1 |      0.50 | 1.00 | [0.12, 0.88]
#> Standard errors are on the link-scale (untransformed).

  # plot 
  # plot(LOC.SIZE, connect.lines = TRUE)
  • What you are looking for is pairwise comparisons or contrasts. You may look at `contrasts()` from the emmeans package (and the related vignette). – Daniel Sep 28 '20 at 20:19
  • thx, this was helpful. But I got stuck using emmeans package, as described on this post. grateful if you could give input. https://stackoverflow.com/questions/64446605/contrast-of-contrast-with-emmeans-second-differences – scottsweden Oct 20 '20 at 13:52

0 Answers0