diff --git a/book/articles/alternative_software.qmd b/book/articles/alternative_software.qmd
index 8fcc5bb3c..72c6beb4d 100644
--- a/book/articles/alternative_software.qmd
+++ b/book/articles/alternative_software.qmd
@@ -767,6 +767,57 @@ data.frame(bm$ContrastSummary)
avg_slopes(ls.fe, dpar = "sigma", re_formula = NA)
```
+## `fmeffects`
+
+The [`fmeffects` package](https://cran.r-project.org/package=fmeffects) is described as follows:
+
+> fmeffects: Model-Agnostic Interpretations with Forward Marginal Effects. Create local, regional, and global explanations for any machine learning model with forward marginal effects. You provide a model and data, and 'fmeffects' computes feature effects. The package is based on the theory in: C. A. Scholbeck, G. Casalicchio, C. Molnar, B. Bischl, and C. Heumann (2022)
+
+As the name says, this package is focused on "forward marginal effects" in the context of machine learning models estimated using the `mlr3` or `tidymodels` frameworks. Since version 0.16.0, `marginaleffects` also supports these machine learning frameworks, and it covers a superset of the `fmeffects` functionality. Consider a random forest model trained on the `bikes` data:
+
+```{r, message = FALSE, warning = FALSE}
+library("mlr3verse")
+library("fmeffects")
+data("bikes", package = "fmeffects")
+task <- as_task_regr(x = bikes, id = "bikes", target = "count")
+forest <- lrn("regr.ranger")$train(task)
+```
+
+Now, we use the `avg_comparisons()` function to compute *centered* marginal effects:
+
+```{r}
+avg_comparisons(forest, variables = "temp", newdata = bikes)
+```
+
+We call this quantity "centered" because it represents the average effect of a change of 1 unit in `temp` about the observed value, that is, a change from 0.5 below to 0.5 above:
+
+```{r}
+lo <- transform(bikes, temp = temp - 0.5)
+hi <- transform(bikes, temp = temp + 0.5)
+mean(predict(forest, newdata = hi) - predict(forest, newdata = lo))
+```
+
+As described in the [`comparisons()` vignette](comparisons.html), it is easy to estimate "backward", "centered" or "forward" differences by supplying an appropriate function to the `variables` argument. For example, here is how to compute "forward" marginal effects:
+
+```{r}
+avg_comparisons(
+ forest,
+ variables = list("temp" = \(x) data.frame(x, x + 1)),
+ newdata = bikes)
+```
+
+This is equivalent to the key quantity reported by the `fmeffects` package:
+
+```{r}
+fmeffects::fme(
+ model = forest,
+ data = bikes,
+ target = "count",
+ feature = "temp",
+ step.size = 1)$ame
+```
+
+
## `effects`
The [`effects` package](https://cran.r-project.org/package=effects) was created by John Fox and colleagues.
diff --git a/book/articles/machine_learning.qmd b/book/articles/machine_learning.qmd
index 2eb93647a..327697e7f 100644
--- a/book/articles/machine_learning.qmd
+++ b/book/articles/machine_learning.qmd
@@ -131,28 +131,6 @@ avg_comparisons(
newdata = bikes)
```
-## `fmeffects`: Forward vs. centered effects
-
-As the code in the `mlr3` section makes clear, the [`avg_comparisons()`](reference/comparisons.html) computes the effect of a "centered" change on the outcome. If we want to compute a "Forward Marginal Effect" instead, we can call:
-
-```{r}
-avg_comparisons(
- forest,
- variables = list("temp" = \(x) data.frame(x, x + 1)),
- newdata = bikes)
-```
-
-This is equivalent to using the `fmeffects` package:
-
-```{r}
-fmeffects::fme(
- model = forest,
- data = bikes,
- target = "count",
- feature = "temp",
- step.size = 1)$ame
-```
-
## Partial Dependence Plots
diff --git a/docs/articles/NEWS.html b/docs/articles/NEWS.html
index 69a653d05..d18f3b4c5 100644
--- a/docs/articles/NEWS.html
+++ b/docs/articles/NEWS.html
@@ -292,14 +292,14 @@
fmeffects: Model-Agnostic Interpretations with Forward Marginal Effects. Create local, regional, and global explanations for any machine learning model with forward marginal effects. You provide a model and data, and ‘fmeffects’ computes feature effects. The package is based on the theory in: C. A. Scholbeck, G. Casalicchio, C. Molnar, B. Bischl, and C. Heumann (2022)
+
+
As the name says, this package is focused on “forward marginal effects” in the context of machine learning models estimated using the mlr3 or tidymodels frameworks. Since version 0.16.0, marginaleffects also supports these machine learning frameworks, and it covers a superset of the fmeffects functionality. Consider a random forest model trained on the bikes data:
We call this quantity “centered” because it represents the average effect of a change of 1 unit in temp about the observed value, that is, a change from 0.5 below to 0.5 above:
As described in the comparisons() vignette, it is easy to estimate “backward”, “centered” or “forward” differences by supplying an appropriate function to the variables argument. For example, here is how to compute “forward” marginal effects:
contrasts =cbind("AME x"=c(-1, 1)), effects ="integrateoutRE")bm$ContrastSummary|>data.frame()
-#> M Mdn LL UL PercentROPE PercentMID CI CIType ROPE MID Label
-#> 1 0.09864399 0.09651684 0.04835076 0.1610664 NA NA 0.95 ETI <NA> <NA> AME x
+#> M Mdn LL UL PercentROPE PercentMID CI CIType ROPE MID Label
+#> 1 0.0987172 0.09704707 0.04695867 0.1609608 NA NA 0.95 ETI <NA> <NA> AME x
+1 Treatment - No Treatment 3.517374 0.4889971 2.558957 4.475791
The results are close to those that we obtained with comparisons(), but the confidence interval differs slightly because of the difference between bootstrapping and the delta method.
Population-averaged (aka “marginal”) adjusted risk difference (see this vignette) can be obtained using the avg_*() functions or using the by argument:
+
Population-averaged (aka “marginal”) adjusted risk difference (see this vignette) can be obtained using the avg_*() functions or using the by argument:
mlr3 is a machine learning framework for R. It makes it possible for users to train a wide range of models, including linear models, random forests, gradient boosting machines, and neural networks.
In this example, we use the bikes dataset supplied by the fmeffects package to train a random forest model predicting the number of bikes rented per hour. We then use marginaleffects to interpret the results of the model.
data("bikes", package ="fmeffects")task<-as_task_regr(x =bikes, id ="bikes", target ="count")forest<-lrn("regr.ranger")$train(task)
As described in other vignettes, we can use the avg_comparisons() function to compute the average change in predicted outcome that is associated with a change in each feature:
With marginaleffects::avg_comparisons(), we can also compute the average effect of a simultaneous change in multiple predictors, using the variables and cross arguments. In this example, we see what happens (on average) to the predicted outcome when the temp, season, and weather predictors all change together:
Estimate C: season C: temp C: weather
- -32.469 spring - fall +1 misty - clear
- -76.390 spring - fall +1 rain - clear
- -11.574 summer - fall +1 misty - clear
- -60.974 summer - fall +1 rain - clear
- 0.334 winter - fall +1 misty - clear
- -53.340 winter - fall +1 rain - clear
+ -37.51 spring - fall +1 misty - clear
+ -79.93 spring - fall +1 rain - clear
+ -13.97 summer - fall +1 misty - clear
+ -61.82 summer - fall +1 rain - clear
+ -2.65 winter - fall +1 misty - clear
+ -54.58 winter - fall +1 rain - clear
Columns: term, contrast_season, contrast_temp, contrast_weather, estimate
Type: response
-
-20.4fmeffects: Forward vs. centered effects
-
As the code in the mlr3 section makes clear, the avg_comparisons() computes the effect of a “centered” change on the outcome. If we want to compute a “Forward Marginal Effect” instead, we can call:
Note that marginaleffects and DALEXtra plots are not exactly identical because the randomly sampled profiles are not the same. You can try the same procedure without sampling — or equivalently with N=2930 — to see a perfect equivalence.
-
-20.6 Other Plots
+
+21.5 Other Plots
We can plot the results using the standard marginaleffects helpers. For example, to plot predictions, we can do:
As documented in ?plot_predictions, using condition="temp" is equivalent to creating an equally-spaced grid of temp values, and holding all other predictors at their means or modes. In other words, it is equivalent to:
Alternatively, we could plot “marginal” predictions, where replicate the full dataset once for every value of temp, and then average the predicted values over each value of the x-axis:
plot_predictions(forest, by ="temp", newdata =d)+geom_point(data =bikes, aes(x =temp, y =count), alpha =0.1)+geom_smooth(data =bikes, aes(x =temp, y =count), se =FALSE, color ="orange")+labs(x ="Temperature (Celsius)", y ="Predicted number of bikes rented per hour",
@@ -1010,7 +1425,7 @@
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
-
+
@@ -1455,13 +1870,13 @@
}
});
diff --git a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-10-1.png b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-10-1.png
index eaccbecf8..3436ca093 100644
Binary files a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-10-1.png and b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-10-1.png differ
diff --git a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-11-1.png b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-11-1.png
index 156e7514c..ade489592 100644
Binary files a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-11-1.png and b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-11-1.png differ
diff --git a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-12-1.png b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-12-1.png
index a9590e555..3b8469596 100644
Binary files a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-12-1.png and b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-12-1.png differ
diff --git a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-15-1.png b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-15-1.png
index 83fc61212..e41a36b7f 100644
Binary files a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-15-1.png and b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-15-1.png differ
diff --git a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-16-1.png b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-16-1.png
index 193d900d6..4daaac0ef 100644
Binary files a/docs/articles/machine_learning_files/figure-html/unnamed-chunk-16-1.png and b/docs/articles/machine_learning_files/figure-html/unnamed-chunk-16-1.png differ
diff --git a/docs/articles/marginaleffects.html b/docs/articles/marginaleffects.html
index 92f09cfcc..d4ddc781e 100644
--- a/docs/articles/marginaleffects.html
+++ b/docs/articles/marginaleffects.html
@@ -307,14 +307,14 @@