typical: Generate "typical" datasets for use in 'marginaleffects"s...

Description Usage Arguments Details Value Examples

View source: R/counterfactual_typical.R

Description

Generate "typical" datasets for use in marginaleffects's newdata argument

Usage

1
typical(..., model = NULL, newdata = NULL)

Arguments

...

named arguments with vectors of values for the typical variables to construct (see Examples below.) The typical data will include combinations of unique values from these vectors

model

Model object

newdata

data.frame (one and only one of the model and newdata arguments must be true).

Details

If typical is used in a marginaleffects or predictions call as the newdata argument, users do not need to specify the model or newdata argument. The data is extracted automatically from the model.

If users supply a model, the data used to fit that model is retrieved using the insight::get_data function.

Value

A data.frame in which each row corresponds to one combination of the named predictors supplied by the user via the ... dots. Variables which are not explicitly defined are held at their mean or mode.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# The output only has 2 rows, and all the variables except `hp` are at their
# mean or mode.
typical(newdata = mtcars, hp = c(100, 110))

# We get the same result by feeding a model instead of a data.frame
mod <- lm(mpg ~ hp, mtcars)
typical(model = mod, hp = c(100, 110))

# Use in `marginaleffects` to compute "Typical Marginal Effects"
marginaleffects(mod, newdata = typical(hp = c(100, 110)))

marginaleffects documentation built on Oct. 19, 2021, 1:09 a.m.