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

visualisation_matrix(
  x,
  target = "all",
  length = 10,
  factors = "reference",
  numerics = "mean",
  preserve_range = FALSE,
  standardize = FALSE,
  standardize_robust = FALSE,
  reference = x,
  na.rm = TRUE,
  ...
)

Arguments

x

An object from which to construct the reference grid.

target

Can be "all" or list of characters indicating columns of interest. Can also contain assignments (e.g., target = "Sepal.Length = 2" or target = 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.

length

Length of numeric target variables.

factors

Type of summary for factors. Can be "combination" (include all unique values), "reference" (set at the reference level) or "mode" (set at the most common level).

numerics

Type of summary for numeric values. Can be "combination" (include 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 removes observations where the value of the numeric variable is originally not present in the range of its factor level.

standardize

The numeric target value is spread as deviations from the mean, with the central value being the mean (or the median if standardize_robust is TRUE). For instance, if x is a vector of mean 1 and SD 2.5, and a standardized grid is required of length 3, the result will be c(Mean-1*SD, Mean, Mean+1*SD), i.e., c(-1.5, 1, 3.5). Each value represents deviations (in terms of SD or MAD) from the central value. This needs the length argument to be an even integer, so that the central value represent the mean.

standardize_robust

Standardization based on median and MAD (a robust equivalent of the SD).

reference

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

na.rm

Remove NaNs.

...

Arguments passed to or from other methods.

Value

Reference grid data frame.

Examples

library(modelbased)

visualisation_matrix(iris, target = "Sepal.Length")
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1           4.3    3.053147     3.804196     1.21958  setosa
#> 2           4.7    3.053147     3.804196     1.21958  setosa
#> 3           5.1    3.053147     3.804196     1.21958  setosa
#> 4           5.5    3.053147     3.804196     1.21958  setosa
#> 5           5.9    3.053147     3.804196     1.21958  setosa
#> 6           6.3    3.053147     3.804196     1.21958  setosa
#> 7           6.7    3.053147     3.804196     1.21958  setosa
#> 8           7.1    3.053147     3.804196     1.21958  setosa
#> 9           7.5    3.053147     3.804196     1.21958  setosa
#> 10          7.9    3.053147     3.804196     1.21958  setosa
visualisation_matrix(iris, target = "Sepal.Length", factors = "combinations")
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 1           4.3    3.053147     3.804196     1.21958     setosa
#> 2           4.7    3.053147     3.804196     1.21958     setosa
#> 3           5.1    3.053147     3.804196     1.21958     setosa
#> 4           5.5    3.053147     3.804196     1.21958     setosa
#> 5           5.9    3.053147     3.804196     1.21958     setosa
#> 6           6.3    3.053147     3.804196     1.21958     setosa
#> 7           6.7    3.053147     3.804196     1.21958     setosa
#> 8           7.1    3.053147     3.804196     1.21958     setosa
#> 9           7.5    3.053147     3.804196     1.21958     setosa
#> 10          7.9    3.053147     3.804196     1.21958     setosa
#> 11          4.3    3.053147     3.804196     1.21958 versicolor
#> 12          4.7    3.053147     3.804196     1.21958 versicolor
#> 13          5.1    3.053147     3.804196     1.21958 versicolor
#> 14          5.5    3.053147     3.804196     1.21958 versicolor
#> 15          5.9    3.053147     3.804196     1.21958 versicolor
#> 16          6.3    3.053147     3.804196     1.21958 versicolor
#> 17          6.7    3.053147     3.804196     1.21958 versicolor
#> 18          7.1    3.053147     3.804196     1.21958 versicolor
#> 19          7.5    3.053147     3.804196     1.21958 versicolor
#> 20          7.9    3.053147     3.804196     1.21958 versicolor
#> 21          4.3    3.053147     3.804196     1.21958  virginica
#> 22          4.7    3.053147     3.804196     1.21958  virginica
#> 23          5.1    3.053147     3.804196     1.21958  virginica
#> 24          5.5    3.053147     3.804196     1.21958  virginica
#> 25          5.9    3.053147     3.804196     1.21958  virginica
#> 26          6.3    3.053147     3.804196     1.21958  virginica
#> 27          6.7    3.053147     3.804196     1.21958  virginica
#> 28          7.1    3.053147     3.804196     1.21958  virginica
#> 29          7.5    3.053147     3.804196     1.21958  virginica
#> 30          7.9    3.053147     3.804196     1.21958  virginica
visualisation_matrix(iris, target = c("Sepal.Length", "Species"), length = 3)
#>   Sepal.Length    Species Sepal.Width Petal.Length Petal.Width
#> 1          4.3     setosa    3.053147     3.804196     1.21958
#> 2          6.1     setosa    3.053147     3.804196     1.21958
#> 3          7.9     setosa    3.053147     3.804196     1.21958
#> 4          4.3 versicolor    3.053147     3.804196     1.21958
#> 5          6.1 versicolor    3.053147     3.804196     1.21958
#> 6          7.9 versicolor    3.053147     3.804196     1.21958
#> 7          4.3  virginica    3.053147     3.804196     1.21958
#> 8          6.1  virginica    3.053147     3.804196     1.21958
#> 9          7.9  virginica    3.053147     3.804196     1.21958
visualisation_matrix(iris, target = c("Sepal.Length", "Species"), numerics = 0)
#>    Sepal.Length    Species Sepal.Width Petal.Length Petal.Width
#> 1           4.3     setosa           0            0           0
#> 2           4.7     setosa           0            0           0
#> 3           5.1     setosa           0            0           0
#> 4           5.5     setosa           0            0           0
#> 5           5.9     setosa           0            0           0
#> 6           6.3     setosa           0            0           0
#> 7           6.7     setosa           0            0           0
#> 8           7.1     setosa           0            0           0
#> 9           7.5     setosa           0            0           0
#> 10          7.9     setosa           0            0           0
#> 11          4.3 versicolor           0            0           0
#> 12          4.7 versicolor           0            0           0
#> 13          5.1 versicolor           0            0           0
#> 14          5.5 versicolor           0            0           0
#> 15          5.9 versicolor           0            0           0
#> 16          6.3 versicolor           0            0           0
#> 17          6.7 versicolor           0            0           0
#> 18          7.1 versicolor           0            0           0
#> 19          7.5 versicolor           0            0           0
#> 20          7.9 versicolor           0            0           0
#> 21          4.3  virginica           0            0           0
#> 22          4.7  virginica           0            0           0
#> 23          5.1  virginica           0            0           0
#> 24          5.5  virginica           0            0           0
#> 25          5.9  virginica           0            0           0
#> 26          6.3  virginica           0            0           0
#> 27          6.7  virginica           0            0           0
#> 28          7.1  virginica           0            0           0
#> 29          7.5  virginica           0            0           0
#> 30          7.9  virginica           0            0           0
visualisation_matrix(iris, target = c("Sepal.Length = 3", "Species"))
#>   Sepal.Length    Species Sepal.Width Petal.Length Petal.Width
#> 1            3     setosa    3.053147     3.804196     1.21958
#> 2            3 versicolor    3.053147     3.804196     1.21958
#> 3            3  virginica    3.053147     3.804196     1.21958
visualisation_matrix(iris, target = c("Sepal.Length = c(3, 1)", "Species = 'setosa'"))
#>   Sepal.Length Species Sepal.Width Petal.Length Petal.Width
#> 1            3  setosa    3.053147     3.804196     1.21958
#> 2            1  setosa    3.053147     3.804196     1.21958
visualisation_matrix(iris, target = "Sepal.Length", standardize = TRUE, length = 3)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1     5.015267    3.053147     3.804196     1.21958  setosa
#> 2     5.843333    3.053147     3.804196     1.21958  setosa
#> 3     6.671399    3.053147     3.804196     1.21958  setosa