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

> ### Name: comparisons
> ### Title: Comparisons Between Predictions Made With Different Regressor
> ###   Values
> ### Aliases: comparisons avg_comparisons
> 
> ### ** Examples
> 
> 
> 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 `slopes`
  functions.
  
  This warning appears once per session.

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

> 
> # 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|) 2.5 % 97.5 %
 gear    4 - 3    0.667      0.117 5.68   <0.001 0.436  0.897
 gear    5 - 3    1.000      0.157 6.39   <0.001 0.693  1.307

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

> avg_comparisons(mod, type = "link")

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

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

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

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

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

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

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

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

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

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

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

> 
> 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|)   2.5 % 97.5 % drat cyl am
   hp       +1  -0.0452     0.0149 -3.04  0.00239 -0.0744 -0.016  3.7   6  1
   hp       +1  -0.0452     0.0149 -3.04  0.00239 -0.0744 -0.016  3.7   6  0
   hp       +1  -0.0452     0.0149 -3.04  0.00239 -0.0744 -0.016  3.7   4  1
   hp       +1  -0.0452     0.0149 -3.04  0.00239 -0.0744 -0.016  3.7   4  0
   hp       +1  -0.0452     0.0149 -3.04  0.00239 -0.0744 -0.016  3.7   8  1
   hp       +1  -0.0452     0.0149 -3.04  0.00239 -0.0744 -0.016  3.7   8  0

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

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

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

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

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

> 
> # 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|) 2.5 % 97.5 %
 new_hp   custom    -1.97      0.711 -2.78  0.00547 -3.37 -0.581

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

> 
> # 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|) 2.5 % 97.5 %
  mpg mean(+1)     1.14   <0.001  1.09   1.18

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

> 
> # 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|) 2.5 % 97.5 %
  mpg       +1     1.14   <0.001  1.09   1.18

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

> # 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.

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

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

> 
> # 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|) 2.5 % 97.5 %
 gear    4 - 3    3.409      2.587  1.318   0.1876 -1.66  8.481
 gear    5 - 4    2.628      2.747  0.957   0.3387 -2.76  8.011
 hp      +10     -0.574      0.225 -2.552   0.0107 -1.02 -0.133

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

> 
> # 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|) 2.5 % 97.5 %
 wt=drat    -6.23       1.05 -5.92   <0.001 -8.29  -4.16

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

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

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

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

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

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

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

> 
> # 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|)  2.5 % 97.5 %
 custom    -6.23       1.05 -5.919   <0.001  -8.29  -4.16
 custom    -5.24       5.62 -0.931    0.352 -16.26   5.78

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

> 
> 
> # `by` argument
> mod <- lm(mpg ~ hp * am * vs, data = mtcars)
> comparisons(mod, by = TRUE)

 Term Contrast Estimate Std. Error      z Pr(>|z|)  2.5 %  97.5 %
   am    1 - 0   4.6980     1.0601  4.432   <0.001  2.620  6.7758
   hp    +1     -0.0688     0.0182 -3.780   <0.001 -0.104 -0.0331
   vs    1 - 0  -0.2943     2.3379 -0.126      0.9 -4.877  4.2879

Columns: term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high 

> 
> mod <- lm(mpg ~ hp * am * vs, data = mtcars)
> avg_comparisons(mod, variables = "hp", by = c("vs", "am"))

 Term Contrast vs am Estimate Std. Error     z Pr(>|z|)   2.5 %   97.5 %
   hp mean(+1)  0  0  -0.0422     0.0248 -1.70  0.08879 -0.0907  0.00639
   hp mean(+1)  0  1  -0.0368     0.0124 -2.97  0.00297 -0.0612 -0.01254
   hp mean(+1)  1  0  -0.0994     0.0534 -1.86  0.06289 -0.2042  0.00534
   hp mean(+1)  1  1  -0.1112     0.0463 -2.40  0.01645 -0.2020 -0.02034

Columns: term, contrast, vs, am, estimate, std.error, statistic, p.value, conf.low, conf.high, predicted, predicted_hi, predicted_lo 

> 
> library(nnet)
> mod <- multinom(factor(gear) ~ mpg + am * vs, data = mtcars, trace = FALSE)
Error in qr.default(X) : object '.F_dqrdc2' not found
Calls: multinom -> qr -> qr.default
Execution halted