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

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: comparisons
> ### Title: Comparisons Between Predictions Made With Different Regressor
> ###   Values
> ### Aliases: comparisons avg_comparisons
> 
> ### ** Examples
> 
> ## Don't show: 
> if (interactive() || isTRUE(Sys.getenv("R_DOC_BUILD") == "true")) (if (getRversion() >= "3.4") withAutoprint else force)({ # examplesIf
+ ## End(Don't show)
+ ## Don't show: 
+ }) # examplesIf
> ## End(Don't show)
> library(marginaleffects)
> 
> # Linear model
> tmp <- mtcars
> tmp$am <- as.logical(tmp$am)
> mod <- lm(mpg ~ am + factor(cyl), tmp)
> avg_comparisons(mod, variables = list(cyl = "reference"))
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 a
  `marginaleffects` function.
  
  This warning appears once per session.

 Term          Contrast Estimate Std. Error     z Pr(>|z|)    S  2.5 % 97.5 %
  cyl mean(6) - mean(4)    -6.16       1.54 -4.01   <0.001 14.0  -9.17  -3.15
  cyl mean(8) - mean(4)   -10.07       1.45 -6.93   <0.001 37.8 -12.91  -7.22

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, variables = list(cyl = "sequential"))

 Term          Contrast Estimate Std. Error     z Pr(>|z|)    S 2.5 % 97.5 %
  cyl mean(6) - mean(4)    -6.16       1.54 -4.01  < 0.001 14.0 -9.17  -3.15
  cyl mean(8) - mean(6)    -3.91       1.47 -2.66  0.00781  7.0 -6.79  -1.03

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, variables = list(cyl = "pairwise"))

 Term          Contrast Estimate Std. Error     z Pr(>|z|)    S  2.5 % 97.5 %
  cyl mean(6) - mean(4)    -6.16       1.54 -4.01  < 0.001 14.0  -9.17  -3.15
  cyl mean(8) - mean(4)   -10.07       1.45 -6.93  < 0.001 37.8 -12.91  -7.22
  cyl mean(8) - mean(6)    -3.91       1.47 -2.66  0.00781  7.0  -6.79  -1.03

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> 
> # GLM with different scale types
> mod <- glm(am ~ factor(gear), data = mtcars)
> avg_comparisons(mod, type = "response")

 Term          Contrast Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
 gear mean(4) - mean(3)    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear mean(5) - mean(3)    1.000      0.157 6.39   <0.001 32.5 0.693  1.307

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, type = "link")

 Term          Contrast Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
 gear mean(4) - mean(3)    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear mean(5) - mean(3)    1.000      0.157 6.39   <0.001 32.5 0.693  1.307

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  link 

> 
> # Contrasts at the mean
> comparisons(mod, newdata = "mean")

 Term Contrast Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 % gear
 gear    4 - 3    0.667      0.117 5.68   <0.001 26.1 0.436  0.897    3
 gear    5 - 3    1.000      0.157 6.39   <0.001 32.5 0.693  1.307    3

Columns: rowid, term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted, gear, am 
Type:  response 

> 
> # Contrasts between marginal means
> comparisons(mod, newdata = "marginalmeans")

 Term Contrast Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
 gear    4 - 3    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear    5 - 3    1.000      0.157 6.39   <0.001 32.5 0.693  1.307

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> 
> # Contrasts at user-specified values
> comparisons(mod, newdata = datagrid(am = 0, gear = tmp$gear))

 Term Contrast gear Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
 gear    4 - 3    4    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear    4 - 3    3    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear    4 - 3    5    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear    5 - 3    4    1.000      0.157 6.39   <0.001 32.5 0.693  1.307
 gear    5 - 3    3    1.000      0.157 6.39   <0.001 32.5 0.693  1.307
 gear    5 - 3    5    1.000      0.157 6.39   <0.001 32.5 0.693  1.307

Columns: rowid, term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, am, gear, predicted_lo, predicted_hi, predicted 
Type:  response 

> comparisons(mod, newdata = datagrid(am = unique, gear = max))

 Term Contrast gear Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
 gear    4 - 3    5    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear    4 - 3    5    0.667      0.117 5.68   <0.001 26.1 0.436  0.897
 gear    5 - 3    5    1.000      0.157 6.39   <0.001 32.5 0.693  1.307
 gear    5 - 3    5    1.000      0.157 6.39   <0.001 32.5 0.693  1.307

Columns: rowid, term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, am, gear, predicted_lo, predicted_hi, predicted 
Type:  response 

> 
> m <- lm(mpg ~ hp + drat + factor(cyl) + factor(am), data = mtcars)
> comparisons(m, variables = "hp", newdata = datagrid(FUN_factor = unique, FUN_numeric = median))

 Term Contrast Estimate Std. Error     z Pr(>|z|)   S   2.5 % 97.5 %  hp drat
   hp       +1  -0.0452     0.0149 -3.04  0.00239 8.7 -0.0744 -0.016 123  3.7
 cyl am
   8  0

Columns: rowid, term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted, hp, drat, cyl, am, mpg 
Type:  response 

> 
> # Numeric contrasts
> mod <- lm(mpg ~ hp, data = mtcars)
> avg_comparisons(mod, variables = list(hp = 1))

 Term Contrast Estimate Std. Error     z Pr(>|z|)    S   2.5 %  97.5 %
   hp mean(+1)  -0.0682     0.0101 -6.74   <0.001 35.9 -0.0881 -0.0484

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, variables = list(hp = 5))

 Term Contrast Estimate Std. Error     z Pr(>|z|)    S 2.5 % 97.5 %
   hp mean(+5)   -0.341     0.0506 -6.74   <0.001 35.9 -0.44 -0.242

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, variables = list(hp = c(90, 100)))

 Term             Contrast Estimate Std. Error     z Pr(>|z|)    S  2.5 %
   hp mean(100) - mean(90)   -0.682      0.101 -6.74   <0.001 35.9 -0.881
 97.5 %
 -0.484

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, variables = list(hp = "iqr"))

 Term            Contrast Estimate Std. Error     z Pr(>|z|)    S 2.5 % 97.5 %
   hp mean(Q3) - mean(Q1)     -5.7      0.845 -6.74   <0.001 35.9 -7.35  -4.04

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, variables = list(hp = "sd"))

 Term                        Contrast Estimate Std. Error     z Pr(>|z|)    S
   hp mean(x + sd/2) - mean(x - sd/2)    -4.68      0.694 -6.74   <0.001 35.9
 2.5 % 97.5 %
 -6.04  -3.32

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> avg_comparisons(mod, variables = list(hp = "minmax"))

 Term              Contrast Estimate Std. Error     z Pr(>|z|)    S 2.5 %
   hp mean(Max) - mean(Min)    -19.3       2.86 -6.74   <0.001 35.9 -24.9
 97.5 %
  -13.7

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> 
> # 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)
> avg_comparisons(modlog, variables = list(new_hp = fdiff))

   Term Contrast Estimate Std. Error     z Pr(>|z|)   S 2.5 % 97.5 %
 new_hp   custom    -1.97      0.711 -2.78  0.00547 7.5 -3.37 -0.581

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> 
> # Adjusted Risk Ratio: see the contrasts vignette
> mod <- glm(vs ~ mpg, data = mtcars, family = binomial)
> avg_comparisons(mod, comparison = "lnratioavg", transform = exp)

 Term Contrast Estimate Pr(>|z|)    S 2.5 % 97.5 %
  mpg mean(+1)     1.13   <0.001 31.9  1.09   1.17

Columns: term, contrast, estimate, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> 
> # Adjusted Risk Ratio: Manual specification of the `comparison`
> avg_comparisons(
+      mod,
+      comparison = function(hi, lo) log(mean(hi) / mean(lo)),
+      transform = exp)

 Term Contrast Estimate Pr(>|z|)    S 2.5 % 97.5 %
  mpg       +1     1.13   <0.001 31.9  1.09   1.17

Columns: term, contrast, estimate, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> # cross contrasts
> mod <- lm(mpg ~ factor(cyl) * factor(gear) + hp, data = mtcars)
> avg_comparisons(mod, variables = c("cyl", "gear"), cross = TRUE)
Warning: Model matrix is rank deficient. Some variance-covariance parameters are
  missing.

 Estimate Std. Error      z Pr(>|z|)   S 2.5 % 97.5 %            C: cyl
   -0.631       3.40 -0.185    0.853 0.2 -7.30   6.04 mean(6) - mean(4)
    2.678       4.62  0.580    0.562 0.8 -6.37  11.73 mean(6) - mean(4)
    3.348       6.43  0.521    0.602 0.7 -9.25  15.95 mean(8) - mean(4)
    5.525       5.87  0.942    0.346 1.5 -5.98  17.03 mean(8) - mean(4)
           C: gear
 mean(4) - mean(3)
 mean(5) - mean(3)
 mean(4) - mean(3)
 mean(5) - mean(3)

Columns: term, contrast_cyl, contrast_gear, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
Type:  response 

> 
> # variable-specific contrasts
> avg_comparisons(mod, variables = list(gear = "sequential", hp = 10))
Warning: Model matrix is rank deficient. Some variance-covariance parameters are
  missing.

 Term          Contrast Estimate Std. Error      z Pr(>|z|)   S 2.5 % 97.5 %
 gear mean(4) - mean(3)    3.409      2.587  1.318   0.1876 2.4 -1.66  8.481
 gear mean(5) - mean(4)    2.628      2.747  0.957   0.3387 1.6 -2.76  8.011
 hp   mean(+10)           -0.574      0.225 -2.552   0.0107 6.5 -1.02 -0.133

Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted 
Type:  response 

> 
> # hypothesis test: is the `hp` marginal effect at the mean equal to the `drat` marginal effect
> mod <- lm(mpg ~ wt + drat, data = mtcars)
> 
> comparisons(
+     mod,
+     newdata = "mean",
+     hypothesis = "wt = drat")

    Term Estimate Std. Error     z Pr(>|z|)    S 2.5 % 97.5 %
 wt=drat    -6.23       1.05 -5.92   <0.001 28.2 -8.29  -4.16

Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
Type:  response 

> 
> # same hypothesis test using row indices
> comparisons(
+     mod,
+     newdata = "mean",
+     hypothesis = "b1 - b2 = 0")

    Term Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
 b1-b2=0     6.23       1.05 5.92   <0.001 28.2  4.16   8.29

Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
Type:  response 

> 
> # same hypothesis test using numeric vector of weights
> comparisons(
+     mod,
+     newdata = "mean",
+     hypothesis = c(1, -1))

   Term Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
 custom     6.23       1.05 5.92   <0.001 28.2  4.16   8.29

Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
Type:  response 

> 
> # two custom contrasts using a matrix of weights
> lc <- matrix(c(
+     1, -1,
+     2, 3),
+     ncol = 2)
> comparisons(
+     mod,
+     newdata = "mean",
+     hypothesis = lc)

   Term Estimate Std. Error     z Pr(>|z|)    S  2.5 % 97.5 %
 custom     6.23       1.05  5.92   <0.001 28.2   4.16   8.29
 custom   -11.46       4.92 -2.33   0.0197  5.7 -21.10  -1.83

Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
Type:  response 

> 
> # Effect of a 1 group-wise standard deviation change
> # First we calculate the SD in each group of `cyl`
> # Second, we use that SD as the treatment size in the `variables` argument
> library(dplyr)
Error in library(dplyr) : there is no package called ‘dplyr’
Execution halted