Skip to contents

This is a wrapper function for FME$new(...)$compute(). It computes forward marginal effects (FMEs) for a specified change in feature values.

Usage

fme(
  model,
  data,
  features,
  ep.method = "none",
  compute.nlm = FALSE,
  nlm.intervals = 1
)

Arguments

model

The (trained) model, with the ability to predict on new data. This must be a train.formula (tidymodels), Learner (mlr3), train (caret), lm or glm object.

data

The data used for computing FMEs, must be data.frame or data.table.

features

A named list with the feature name(s) and step size(s). The list names should correspond to the names of the feature variables affected by the step. The list must exclusively contain either numeric or categorical features, but not a combination of both. Numeric features must have a number as step size, categorical features the name of the reference category.

ep.method

String specifying the method used for extrapolation detection. One of "none" or "envelope". Defaults to "none".

compute.nlm

Compute NLMs for FMEs for numerical steps. Defaults to FALSE.

nlm.intervals

Number of intervals for computing NLMs. Results in longer computing time but more accurate approximation of NLMs. Defaults to 1.

Value

ForwardsMarginalEffect object with the following fields:

  • ame average marginal effect (AME).

  • anlm average non-linearity measure (NLM).

  • extrapolation.ids observations that have been identified as extrapolation points and not included in the analysis.

  • data.step, a data.table of the feature matrix after the step has been applied.

  • results, a data.table of the individual FMEs (and NLMs, if applicable) for all observations that are not extrapolation points.

Details

If one or more numeric features are passed to the features argument, FMEs are computed as $$FME_{x, h_{S}} = f(x + h_{S}, x_{-S}) - f(x)$$ where \(h_{S}\) is the step size vector and \(x_{-S}\) the other features. If one or more categorical features are passed to features, $$FME_{x, c_{J}} = f(c_{J}, x_{-J}) - f(x)$$ where \(c_{J}\) is the set of selected reference categories in features and \(x_{-J}\) the other features.

References

Scholbeck, C.A., Casalicchio, G., Molnar, C. et al. Marginal effects for non-linear prediction functions. Data Min Knowl Disc (2024). https://doi.org/10.1007/s10618-023-00993-x

Examples

# Train a model:

library(mlr3verse)
library(ranger)
data(bikes, package = "fmeffects")
forest = lrn("regr.ranger")$train(as_task_regr(x = bikes, target = "count"))

# Compute FMEs for a numerical feature:
effects = fme(model = forest, data = bikes, features = list("temp" = 1), ep.method = "envelope")

# Analyze results:
summary(effects)
#> 
#> Forward Marginal Effects Object
#> 
#> Step type:
#>   numerical
#> 
#> Features & step lengths:
#>   temp, 1
#> 
#> Extrapolation point detection:
#>   envelope, EPs: 6 of 727 obs. (1 %)
#> 
#> Average Marginal Effect (AME):
#>   2.3416
plot(effects)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'


# Extract results:
effects$results
#> Key: <obs.id>
#>      obs.id        fme
#>       <int>      <num>
#>   1:      1  2.9106222
#>   2:      2  2.4063463
#>   3:      3  4.8788924
#>   4:      4 -0.6349359
#>   5:      5  1.5575833
#>  ---                  
#> 717:    723 36.2026597
#> 718:    724  8.6551796
#> 719:    725  4.4907142
#> 720:    726  2.6597105
#> 721:    727 11.1546918

# Compute the AME for a categorial feature:
fme(model = forest, data = bikes, features = list("weather" = "rain"))$ame
#> [1] -55.56764