| AverageMarginalEffects {fmeffects} | R Documentation |
R6 Class computing Average Marginal Effects (AME) based on Forward Marginal Effects (FME) for a model
Description
The AME is a simple mean FME and computed w.r.t. a feature variable and a model.
Public fields
predictorPredictorobjectfeaturesvector of features for which AMEs should be computed
ep.methodstring specifying extrapolation detection method
resultsdata.table with AMEs computed
computedlogical specifying if compute() has been run
Methods
Public methods
Method new()
Create a new AME object.
Usage
AverageMarginalEffects$new(model, data, features = NULL, ep.method = "none")
Arguments
modelThe (trained) model, with the ability to predict on new data. This must be a
train.formula(tidymodels),Learner(mlr3),train(caret),lmorglmobject.dataThe data used for computing AMEs, must be data.frame or data.table.
featuresIf 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.methodString 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
deepWhether to make a deep clone.
Examples
## ------------------------------------------------
## Method `AverageMarginalEffects$new`
## ------------------------------------------------
# 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:
## Not run:
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)
## End(Not run)
## ------------------------------------------------
## Method `AverageMarginalEffects$compute`
## ------------------------------------------------
# Compute results:
## Not run:
overview$compute()
## End(Not run)