Learn R Programming

marginaleffects (version 0.8.1)

datagridcf: A "counterfactual" version of the datagrid() function.

Description

For each combination of the variable values specified, this function duplicates the entire data frame supplied to newdata, or the entire dataset used to fit model. This is a convenience shortcut to call the datagrid() function with argument grid_type="counterfactual".

Usage

datagridcf(..., model = NULL, newdata = NULL)

Arguments

...

named arguments with vectors of values or functions for user-specified variables.

  • Functions are applied to the variable in the model dataset or newdata, and must return a vector of the appropriate type.

  • Character vectors are automatically transformed to factors if necessary. +The output will include all combinations of these variables (see Examples below.)

model

Model object

newdata

data.frame (one and only one of the model and newdata arguments

See Also

Other grid: datagrid()

Examples

Run this code
# Fit a model with 32 observations from the `mtcars` dataset.
nrow(mtcars)

mod <- lm(mpg ~ hp + am, data = mtcars)

# We specify two values for the `am` variable and obtain a counterfactual
# dataset with 64 observations (32 x 2).
dat <- datagridcf(model = mod, am = 0:1)
head(dat)
nrow(dat)

# We specify 2 values for the `am` variable and 3 values for the `hp` variable
# and obtained a dataset with 192 observations (2x3x32), corresponding to the
# full original data, with each possible combination of `hp` and `am`.
dat <- datagridcf(am = 0:1, hp = c(100, 110, 120), newdata = mtcars)
head(dat)
dim(dat)

Run the code above in your browser using DataLab