Skip to contents

The AME is a simple mean FME and computed w.r.t. a feature variable and a model.

Public fields

predictor

Predictor object

features

vector of features for which AMEs should be computed

ep.method

string specifying extrapolation detection method

results

data.table with AMEs computed

computed

logical specifying if compute() has been run

Methods


Method new()

Create a new AME object.

Usage

AverageMarginalEffects$new(model, data, features = NULL, ep.method = "none")

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 AMEs, must be data.frame or data.table.

features

If not NULL, a named list of the names of the feature variables for which AMEs should be computed, together with the desired step sizes. For numeric features, the step size must be a single number. For categorial features, the step size must be a character vector of category names that is a subset of the levels of the factor variable.

ep.method

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

Returns

A new AME object.

Examples

# Train a model:

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

# Compute AMEs for all features:
\dontrun{
overview = AverageMarginalEffects$new(
  model = forest,
  data = bikes)$compute()
summary(overview)

# Compute AMEs for a subset of features with non-default step.sizes:
overview = AverageMarginalEffects$new(model = forest,
                                      data = bikes,
                                      features = list(humidity = 0.1,
                                                   weather = c("clear", "rain")))$compute()
summary(overview)
}


Method compute()

Computes results, i.e., AMEs including the SD of FMEs, for an AME object.

Usage

AverageMarginalEffects$compute()

Returns

An AME object with results.

Examples

# Compute results:
\dontrun{
overview$compute()
}


Method clone()

The objects of this class are cloneable with this method.

Usage

AverageMarginalEffects$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `AverageMarginalEffects$new`
## ------------------------------------------------

# Train a model:

library(mlr3verse)
#> Loading required package: mlr3
library(ranger)
set.seed(123)
data(bikes, package = "fmeffects")
task = as_task_regr(x = bikes, id = "bikes", target = "count")
forest = lrn("regr.ranger")$train(task)

# Compute AMEs for all features:
if (FALSE) { # \dontrun{
overview = AverageMarginalEffects$new(
  model = forest,
  data = bikes)$compute()
summary(overview)

# Compute AMEs for a subset of features with non-default step.sizes:
overview = AverageMarginalEffects$new(model = forest,
                                      data = bikes,
                                      features = list(humidity = 0.1,
                                                   weather = c("clear", "rain")))$compute()
summary(overview)
} # }

## ------------------------------------------------
## Method `AverageMarginalEffects$compute`
## ------------------------------------------------

# Compute results:
if (FALSE) { # \dontrun{
overview$compute()
} # }