Running examples in ‘marginaleffects-Ex.R’ failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: comparisons
> ### Title: Contrasts Between Adjusted Predictions
> ### Aliases: comparisons
> 
> ### ** Examples
> 
> 
> library(marginaleffects)
> library(magrittr)
> 
> # Linear model
> tmp <- mtcars
> tmp$am <- as.logical(tmp$am)
> mod <- lm(mpg ~ am + factor(cyl), tmp)
> comparisons(mod, variables = list(cyl = "reference")) %>% tidy()
Warning: The `cyl` variable is treated as a categorical (factor) variable, but
  the original data is of class numeric. It is safer and faster to convert
  such variables to factor before fitting the model and calling
  `marginaleffects` functions.
  
  This warning appears once per session.
      type term contrast   estimate std.error statistic      p.value   conf.low
1 response  cyl    6 - 4  -6.156118  1.535723 -4.008612 6.107658e-05  -9.166079
2 response  cyl    8 - 4 -10.067560  1.452082 -6.933187 4.114626e-12 -12.913589
  conf.high
1 -3.146156
2 -7.221530
> comparisons(mod, variables = list(cyl = "sequential")) %>% tidy()
      type term contrast  estimate std.error statistic      p.value  conf.low
1 response  cyl    6 - 4 -6.156118  1.535723 -4.008612 6.107658e-05 -9.166079
2 response  cyl    8 - 6 -3.911442  1.470254 -2.660385 7.805144e-03 -6.793087
  conf.high
1 -3.146156
2 -1.029797
> comparisons(mod, variables = list(cyl = "pairwise")) %>% tidy()
      type term contrast   estimate std.error statistic      p.value   conf.low
1 response  cyl    6 - 4  -6.156118  1.535723 -4.008612 6.107658e-05  -9.166079
2 response  cyl    8 - 4 -10.067560  1.452082 -6.933187 4.114626e-12 -12.913589
3 response  cyl    8 - 6  -3.911442  1.470254 -2.660385 7.805144e-03  -6.793087
  conf.high
1 -3.146156
2 -7.221530
3 -1.029797
> 
> # GLM with different scale types
> mod <- glm(am ~ factor(gear), data = mtcars)
> comparisons(mod, type = "response") %>% tidy()
      type term contrast  estimate std.error statistic      p.value  conf.low
1 response gear    4 - 3 0.6666667 0.1174440  5.676462 1.375090e-08 0.4364806
2 response gear    5 - 3 1.0000000 0.1565921  6.386020 1.702589e-10 0.6930852
  conf.high
1 0.8968528
2 1.3069148
> comparisons(mod, type = "link") %>% tidy()
  type term contrast  estimate std.error statistic      p.value  conf.low
1 link gear    4 - 3 0.6666667 0.1174440  5.676462 1.375090e-08 0.4364806
2 link gear    5 - 3 1.0000000 0.1565921  6.386020 1.702589e-10 0.6930852
  conf.high
1 0.8968528
2 1.3069148
> 
> # Contrasts at the mean
> comparisons(mod, newdata = "mean")
  rowid     type term contrast comparison std.error statistic      p.value
1     1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
2     1 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
   conf.low conf.high     predicted predicted_hi  predicted_lo      am gear
1 0.4364806 0.8968528 -2.355139e-16    0.6666667 -2.355139e-16 0.40625    3
2 0.6930852 1.3069148 -2.355139e-16    1.0000000 -2.355139e-16 0.40625    3
> 
> # Contrasts between marginal means
> comparisons(mod, newdata = "marginalmeans")
      type term contrast comparison std.error statistic      p.value  conf.low
1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08 0.4364806
2 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10 0.6930852
  conf.high predicted predicted_hi  predicted_lo
1 0.8968528 0.5555556    0.6666667 -2.355139e-16
2 1.3069148 0.5555556    1.0000000 -2.355139e-16
> 
> # Contrasts at user-specified values
> comparisons(mod, newdata = datagrid(am = 0, gear = tmp$gear))
  rowid     type term contrast comparison std.error statistic      p.value
1     1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
2     2 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
3     3 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
4     1 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
5     2 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
6     3 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
   conf.low conf.high     predicted predicted_hi  predicted_lo am gear
1 0.4364806 0.8968528  6.666667e-01    0.6666667 -2.355139e-16  0    4
2 0.4364806 0.8968528 -2.355139e-16    0.6666667 -2.355139e-16  0    3
3 0.4364806 0.8968528  1.000000e+00    0.6666667 -2.355139e-16  0    5
4 0.6930852 1.3069148  6.666667e-01    1.0000000 -2.355139e-16  0    4
5 0.6930852 1.3069148 -2.355139e-16    1.0000000 -2.355139e-16  0    3
6 0.6930852 1.3069148  1.000000e+00    1.0000000 -2.355139e-16  0    5
> comparisons(mod, newdata = datagrid(am = unique, gear = max))
  rowid     type term contrast comparison std.error statistic      p.value
1     1 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
2     2 response gear    4 - 3  0.6666667 0.1174440  5.676462 1.375090e-08
3     1 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
4     2 response gear    5 - 3  1.0000000 0.1565921  6.386020 1.702589e-10
   conf.low conf.high predicted predicted_hi  predicted_lo am gear
1 0.4364806 0.8968528         1    0.6666667 -2.355139e-16  1    5
2 0.4364806 0.8968528         1    0.6666667 -2.355139e-16  0    5
3 0.6930852 1.3069148         1    1.0000000 -2.355139e-16  1    5
4 0.6930852 1.3069148         1    1.0000000 -2.355139e-16  0    5
> 
> m <- lm(mpg ~ hp + drat + factor(cyl) + factor(am), data = mtcars)
> comparisons(m, variables = "hp", newdata = datagrid(FUN_factor = unique, FUN_numeric = median))
  rowid     type term contrast  comparison  std.error statistic     p.value
1     1 response   hp       +1 -0.04522926 0.01489036 -3.037487 0.002385599
2     2 response   hp       +1 -0.04522926 0.01489036 -3.037487 0.002385599
3     3 response   hp       +1 -0.04522926 0.01489036 -3.037487 0.002385599
4     4 response   hp       +1 -0.04522926 0.01489036 -3.037487 0.002385599
5     5 response   hp       +1 -0.04522926 0.01489036 -3.037487 0.002385599
6     6 response   hp       +1 -0.04522926 0.01489036 -3.037487 0.002385599
     conf.low  conf.high predicted predicted_hi predicted_lo  mpg  hp  drat cyl
1 -0.07441382 -0.0160447  21.95333     21.93072     21.97595 19.2 123 3.695   6
2 -0.07441382 -0.0160447  18.18910     18.16648     18.21171 19.2 123 3.695   6
3 -0.07441382 -0.0160447  25.54890     25.52628     25.57151 19.2 123 3.695   4
4 -0.07441382 -0.0160447  21.78467     21.76205     21.80728 19.2 123 3.695   4
5 -0.07441382 -0.0160447  22.61705     22.59444     22.63967 19.2 123 3.695   8
6 -0.07441382 -0.0160447  18.85282     18.83020     18.87543 19.2 123 3.695   8
  am    eps
1  1 0.0283
2  0 0.0283
3  1 0.0283
4  0 0.0283
5  1 0.0283
6  0 0.0283
> 
> 
> # Numeric contrasts
> mod <- lm(mpg ~ hp, data = mtcars)
> comparisons(mod, variables = list(hp = 1)) %>% tidy()
      type term contrast    estimate std.error statistic      p.value
1 response   hp       +1 -0.06822828 0.0101193 -6.742389 1.558037e-11
     conf.low   conf.high
1 -0.08806175 -0.04839481
> comparisons(mod, variables = list(hp = 5)) %>% tidy()
      type term contrast   estimate  std.error statistic      p.value
1 response   hp       +5 -0.3411414 0.05059652 -6.742389 1.558038e-11
    conf.low conf.high
1 -0.4403087 -0.241974
> comparisons(mod, variables = list(hp = c(90, 100))) %>% tidy()
      type term contrast   estimate std.error statistic      p.value   conf.low
1 response   hp 100 - 90 -0.6822828  0.101193 -6.742389 1.558038e-11 -0.8806175
   conf.high
1 -0.4839481
> comparisons(mod, variables = list(hp = "iqr")) %>% tidy()
      type term contrast  estimate std.error statistic      p.value  conf.low
1 response   hp  Q3 - Q1 -5.697061 0.8449619 -6.742389 1.558038e-11 -7.353156
  conf.high
1 -4.040966
> comparisons(mod, variables = list(hp = "sd")) %>% tidy()
      type term                contrast  estimate std.error statistic
1 response   hp (x + sd/2) - (x - sd/2) -4.677926 0.6938085 -6.742389
       p.value  conf.low conf.high
1 1.558038e-11 -6.037766 -3.318087
> comparisons(mod, variables = list(hp = "minmax")) %>% tidy()
      type term  contrast estimate std.error statistic      p.value  conf.low
1 response   hp Max - Min -19.3086  2.863763 -6.742389 1.558038e-11 -24.92147
  conf.high
1 -13.69573
> 
> # using a function to specify a custom difference in one regressor
> dat <- mtcars
> dat$new_hp <- 49 * (dat$hp - min(dat$hp)) / (max(dat$hp) - min(dat$hp)) + 1
> modlog <- lm(mpg ~ log(new_hp) + factor(cyl), data = dat)
> fdiff <- \(x) data.frame(x, x + 10)
> comparisons(modlog, variables = list(new_hp = fdiff)) %>% summary()
    Term Contrast Effect Std. Error z value  Pr(>|z|)  2.5 %  97.5 %
1 new_hp   custom -1.974     0.7105  -2.778 0.0054696 -3.366 -0.5812

Model type:  lm 
Prediction type:  response 
> 
> # Adjusted Risk Ratio: see the contrasts vignette
> mod <- glm(vs ~ mpg, data = mtcars, family = binomial)
> cmp <- comparisons(mod, transform_pre = "lnratioavg")
> summary(cmp, transform_avg = exp)
  Term Contrast Effect   Pr(>|z|) 2.5 % 97.5 %
1  mpg mean(+1)  1.135 2.3808e-10 1.091   1.18

Model type:  glm 
Prediction type:  response 
Average-transformation:  exp 
> 
> # Adjusted Risk Ratio: Manual specification of the `transform_pre`
> cmp <- comparisons(mod, transform_pre = function(hi, lo) log(mean(hi) / mean(lo)))
> summary(cmp, transform_avg = exp)
  Term Contrast Effect   Pr(>|z|) 2.5 % 97.5 %
1  mpg       +1  1.135 2.3808e-10 1.091   1.18

Model type:  glm 
Prediction type:  response 
Pre-transformation:  function(hi, lo) log(mean(hi)/mean(lo)) 
Average-transformation:  exp 
> # cross contrasts
> mod <- lm(mpg ~ factor(cyl) * factor(gear) + hp, data = mtcars)
> cmp <- comparisons(mod, variables = c("cyl", "gear"), cross = TRUE)
Warning: Model matrix is rank deficient. Some variance-covariance parameters are
  missing.
Error in `[[<-.data.frame`(`*tmp*`, "type", value = c(expectation = "response")) : 
  replacement has 1 row, data has 0
Calls: comparisons -> [[<- -> [[<-.data.frame
Execution halted