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

View source: R/datagrid.R

datagridcfR Documentation

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


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".


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



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 object


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

See Also

Other grid: datagrid()


# Fit a model with 32 observations from the `mtcars` dataset.

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)

# 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)

marginaleffects documentation built on Nov. 24, 2022, 1:06 a.m.