# Datagrids of variables and dataframes =====================================
if (require("bayestestR", quietly = TRUE) & require("datawizard", quietly = TRUE)) {
# Single variable is of interest; all others are "fixed" ------------------
# Factors
get_datagrid(iris, at = "Species") # Returns all the levels
get_datagrid(iris, at = "Species = c('setosa', 'versicolor')") # Specify an expression
# Numeric variables
get_datagrid(iris, at = "Sepal.Length") # default spread length = 10
get_datagrid(iris, at = "Sepal.Length", length = 3) # change length
get_datagrid(iris[2:150, ],
at = "Sepal.Length",
factors = "mode", numerics = "median"
) # change non-targets fixing
get_datagrid(iris, at = "Sepal.Length", range = "ci", ci = 0.90) # change min/max of target
get_datagrid(iris, at = "Sepal.Length = [0, 1]") # Manually change min/max
get_datagrid(iris, at = "Sepal.Length = [sd]") # -1 SD, mean and +1 SD
get_datagrid(iris, at = "Sepal.Length = [quartiles]") # quartiles
# Numeric and categorical variables, generating a grid for plots
# default spread length = 10
get_datagrid(iris, at = c("Sepal.Length", "Species"), range = "grid")
# default spread length = 3 (-1 SD, mean and +1 SD)
get_datagrid(iris, at = c("Species", "Sepal.Length"), range = "grid")
# Standardization and unstandardization
data <- get_datagrid(iris, at = "Sepal.Length", range = "sd", length = 3)
data$Sepal.Length # It is a named vector (extract names with `names(out$Sepal.Length)`)
datawizard::standardize(data, select = "Sepal.Length")
data <- get_datagrid(iris, at = "Sepal.Length = c(-2, 0, 2)") # Manually specify values
data
datawizard::unstandardize(data, select = "Sepal.Length")
# Multiple variables are of interest, creating a combination --------------
get_datagrid(iris, at = c("Sepal.Length", "Species"), length = 3)
get_datagrid(iris, at = c("Sepal.Length", "Petal.Length"), length = c(3, 2))
get_datagrid(iris, at = c(1, 3), length = 3)
get_datagrid(iris, at = c("Sepal.Length", "Species"), preserve_range = TRUE)
get_datagrid(iris, at = c("Sepal.Length", "Species"), numerics = 0)
get_datagrid(iris, at = c("Sepal.Length = 3", "Species"))
get_datagrid(iris, at = c("Sepal.Length = c(3, 1)", "Species = 'setosa'"))
# With list-style at-argument
get_datagrid(iris, at = list(Sepal.Length = c(1, 3), Species = "setosa"))
}
# With models ===============================================================
# Fit a linear regression
model <- lm(Sepal.Length ~ Sepal.Width * Petal.Length, data = iris)
# Get datagrid of predictors
data <- get_datagrid(model, length = c(20, 3), range = c("range", "sd"))
# same as: get_datagrid(model, range = "grid", length = 20)
# Add predictions
data$Sepal.Length <- get_predicted(model, data = data)
# Visualize relationships (each color is at -1 SD, Mean, and + 1 SD of Petal.Length)
plot(data$Sepal.Width, data$Sepal.Length,
col = data$Petal.Length,
main = "Relationship at -1 SD, Mean, and + 1 SD of Petal.Length"
)
Run the code above in your browser using DataLab