datagrid | R Documentation |
newdata
argument of the marginaleffects
or predictions
functions.Generate a data grid of "typical," "counterfactual," or user-specified values for use in the newdata
argument of the marginaleffects
or predictions
functions.
datagrid( ..., model = NULL, newdata = NULL, grid_type = "typical", FUN_character = Mode, FUN_factor = Mode, FUN_logical = Mode, FUN_numeric = function(x) mean(x, na.rm = TRUE), FUN_integer = function(x) round(mean(x, na.rm = TRUE)), FUN_other = function(x) mean(x, na.rm = TRUE) )
... |
named arguments with vectors of values or functions for user-specified variables.
|
model |
Model object |
newdata |
data.frame (one and only one of the |
grid_type |
character
|
FUN_character |
the function to be applied to character variables. |
FUN_factor |
the function to be applied to factor variables. |
FUN_logical |
the function to be applied to factor variables. |
FUN_numeric |
the function to be applied to numeric variables. |
FUN_integer |
the function to be applied to integer variables. |
FUN_other |
the function to be applied to other variable types. |
If datagrid
is used in a marginaleffects
or predictions
call as the
newdata
argument, the model is automatically inserted in the function
call, and users do not need to specify either the model
or newdata
arguments. Note that only the variables used to fit the models will be
attached to the results. If a user wants to attach other variables as well
(e.g., weights or grouping variables), they can supply a data.frame
explicitly to the newdata
argument inside datagrid()
.
If users supply a model, the data used to fit that model is retrieved using
the insight::get_data
function.
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.
Other grid:
datagridcf()
# The output only has 2 rows, and all the variables except `hp` are at their # mean or mode. datagrid(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) datagrid(model = mod, hp = c(100, 110)) # Use in `marginaleffects` to compute "Typical Marginal Effects". When used # in `marginaleffects()` or `predictions()` we do not need to specify the #`model` or `newdata` arguments. marginaleffects(mod, newdata = datagrid(hp = c(100, 110))) # datagrid accepts functions datagrid(hp = range, cyl = unique, newdata = mtcars) comparisons(mod, newdata = datagrid(hp = fivenum)) # The full dataset is duplicated with each observation given counterfactual # values of 100 and 110 for the `hp` variable. The original `mtcars` includes # 32 rows, so the resulting dataset includes 64 rows. dg <- datagrid(newdata = mtcars, hp = c(100, 110), grid_type = "counterfactual") nrow(dg) # We get the same result by feeding a model instead of a data.frame mod <- lm(mpg ~ hp, mtcars) dg <- datagrid(model = mod, hp = c(100, 110), grid_type = "counterfactual") nrow(dg)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.