Learn R Programming

insight (version 0.16.0)

get_datagrid: Create a reference grid

Description

Create a reference matrix, useful for visualisation, with evenly spread and combined values.

Usage

get_datagrid(x, ...)

# S3 method for data.frame get_datagrid( x, at = "all", factors = "reference", numerics = "mean", preserve_range = FALSE, reference = x, ... )

# S3 method for numeric get_datagrid(x, length = 10, range = "range", ...)

# S3 method for factor get_datagrid(x, ...)

# S3 method for default get_datagrid( x, at = "all", factors = "reference", numerics = "mean", preserve_range = TRUE, reference = x, include_smooth = TRUE, include_random = FALSE, include_response = FALSE, data = NULL, ... )

Arguments

x

An object from which to construct the reference grid.

...

Arguments passed to or from other methods (for instance, length or range to control the spread of numeric variables.).

at

Can be "all" or list of characters indicating columns of interest. Can also contain assignments (as named list, e.g. at = list(c(Sepal.Length = c(2, 4), Species = "setosa")), or as string, e.g. at = "Sepal.Length = 2" or at = c("Sepal.Length = 2", "Species = 'setosa'") - note the usage of single and double quotes to assign strings within strings). The remaining variables will be fixed.

factors

Type of summary for factors. Can be "reference" (set at the reference level), "mode" (set at the most common level) or "all" to keep all levels.

numerics

Type of summary for numeric values. Can be "all" (will duplicate the grid for all unique values), any function ("mean", "median", ...) or a value (e.g., numerics = 0).

preserve_range

In the case of combinations between numeric variables and factors, setting preserve_range = TRUE will drop the observations where the value of the numeric variable is originally not present in the range of its factor level. This leads to an unbalanced grid. Also, if you want the minimum and the maximum to closely match the actual ranges, you should increase the length argument.

reference

The reference vector from which to compute the mean and SD.

length

Length of numeric "at" variables.

range

Can be one of "range", "iqr", "ci", "hdi" or "eti". If "range" (default), will use the minimum and maximun of the original vector as end-points. If any other interval, will spread within the range (the default CI width is 95% but this can be changed by setting something else, e.g., ci = 0.90). See IQR() and bayestestR::ci().

include_smooth

If x is a model object, decide whether smooth terms should be included in the data grid or not.

include_random

If x is a mixed model object, decide whether random effect terms should be included in the data grid or not. If include_random is FALSE, but x is a mixed model with random effects, these will still be included in the returned grid, but set to their "population level" value (e.g., NA for glmmTMB or 0 for merMod). This ensures that common predict() methods work properly, as these usually need data with all variables in the model included.

include_response

If x is a model object, decide whether the response variable should be included in the data grid or not.

data

Optional, the data frame that was used to fit the model. Usually, the data is retrieved via get_data().

Value

Reference grid data frame.

Examples

Run this code
# NOT RUN {
# Add one row to change the "mode" of Species
data <- rbind(iris, iris[149, ], make.row.names = FALSE)

# Single variable is of interest; all others are "fixed"
get_datagrid(data, at = "Sepal.Length")
get_datagrid(data, at = "Sepal.Length", length = 3)
get_datagrid(data, at = "Sepal.Length", range = "ci", ci = 0.90)
get_datagrid(data, at = "Sepal.Length", factors = "mode")

# Multiple variables are of interest, creating a combination
get_datagrid(data, at = c("Sepal.Length", "Species"), length = 3)
get_datagrid(data, at = c(1, 3), length = 3)
get_datagrid(data, at = c("Sepal.Length", "Species"), preserve_range = TRUE)
get_datagrid(data, at = c("Sepal.Length", "Species"), numerics = 0)
get_datagrid(data, at = c("Sepal.Length = 3", "Species"))
get_datagrid(data, at = c("Sepal.Length = c(3, 1)", "Species = 'setosa'"))

# with list-style at-argument
get_datagrid(data, at = list(Sepal.Length = c(1, 3), Species = "setosa"))
# }

Run the code above in your browser using DataLab