Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 23, 2024
1 parent e47a0fd commit 2858654
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 73 deletions.
39 changes: 22 additions & 17 deletions R/visualisation_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' function. Same arguments apply.
#'
#' @inheritParams insight::get_datagrid
#' @param target Deprecated name. Please use `at` instead.
#' @param target,at Deprecated name. Please use `by` instead.
#'
#' @return Reference grid data frame.
#'
Expand All @@ -15,24 +15,24 @@
#' data <- rbind(iris, iris[149, ], make.row.names = FALSE)
#'
#' # Single variable is of interest; all others are "fixed"
#' visualisation_matrix(data, at = "Sepal.Length")
#' visualisation_matrix(data, at = "Sepal.Length", length = 3)
#' visualisation_matrix(data, at = "Sepal.Length", range = "ci", ci = 0.90)
#' visualisation_matrix(data, at = "Sepal.Length", factors = "mode")
#' visualisation_matrix(data, by = "Sepal.Length")
#' visualisation_matrix(data, by = "Sepal.Length", length = 3)
#' visualisation_matrix(data, by = "Sepal.Length", range = "ci", ci = 0.90)
#' visualisation_matrix(data, by = "Sepal.Length", factors = "mode")
#'
#' # Multiple variables are of interest, creating a combination
#' visualisation_matrix(data, at = c("Sepal.Length", "Species"), length = 3)
#' visualisation_matrix(data, at = c(1, 3), length = 3)
#' visualisation_matrix(data, at = c("Sepal.Length", "Species"), preserve_range = TRUE)
#' visualisation_matrix(data, at = c("Sepal.Length", "Species"), numerics = 0)
#' visualisation_matrix(data, at = c("Sepal.Length = 3", "Species"))
#' visualisation_matrix(data, at = c("Sepal.Length = c(3, 1)", "Species = 'setosa'"))
#' visualisation_matrix(data, by = c("Sepal.Length", "Species"), length = 3)
#' visualisation_matrix(data, by = c(1, 3), length = 3)
#' visualisation_matrix(data, by = c("Sepal.Length", "Species"), preserve_range = TRUE)
#' visualisation_matrix(data, by = c("Sepal.Length", "Species"), numerics = 0)
#' visualisation_matrix(data, by = c("Sepal.Length = 3", "Species"))
#' visualisation_matrix(data, by = c("Sepal.Length = c(3, 1)", "Species = 'setosa'"))
#'
#' # with list-style at-argument
#' visualisation_matrix(data, at = list(Sepal.Length = c(1, 3), Species = "setosa"))
#' visualisation_matrix(data, by = list(Sepal.Length = c(1, 3), Species = "setosa"))
#'
#' # Standardize
#' vizdata <- visualisation_matrix(data, at = "Sepal.Length")
#' vizdata <- visualisation_matrix(data, by = "Sepal.Length")
#' standardize(vizdata)
#' @export
visualisation_matrix <- function(x, ...) {
Expand All @@ -46,20 +46,25 @@ visualisation_matrix <- function(x, ...) {
#' @rdname visualisation_matrix
#' @export
visualisation_matrix.data.frame <- function(x,
at = "all",
by = "all",
target = NULL,
at = NULL,
factors = "reference",
numerics = "mean",
preserve_range = FALSE,
reference = x,
...) {
if (!is.null(target)) {
insight::format_warning("The 'target' argument name is deprecated in favour of 'at'. Please replace 'target' with 'at'.")
at <- target
insight::format_warning("The 'target` argument name is deprecated in favour of `by`. Please replace `target` with `by`.") # nolint
by <- target
}
if (!is.null(at)) {
insight::format_warning("The `at` argument is deprecated and will be removed in the future. Please use `by` instead.") # nolint
by <- at
}

insight::get_datagrid(x,
at = at,
by = by,
factors = factors,
numerics = numerics,
preserve_range = preserve_range,
Expand Down
32 changes: 16 additions & 16 deletions R/visualisation_recipe.estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
#' data$new_factor <- as.factor(rep(c("A", "B"), length.out = nrow(mtcars)))
#'
#' model <- lm(mpg ~ new_factor * cyl * wt, data = data)
#' x <- estimate_means(model, at = c("new_factor", "cyl"))
#' x <- estimate_means(model, by =c("new_factor", "cyl"))
#' plot(visualisation_recipe(x))
#'
#' # Modulations --------------
#' x <- estimate_means(model, at = c("new_factor", "wt"))
#' x <- estimate_means(model, by =c("new_factor", "wt"))
#' plot(visualisation_recipe(x))
#'
#' # x <- estimate_means(model, at = c("new_factor", "cyl", "wt"))
#' # x <- estimate_means(model, by =c("new_factor", "cyl", "wt"))
#' # plot(visualisation_recipe(x)) # TODO: broken
#'
#' #' # GLMs ---------------------
Expand All @@ -54,17 +54,17 @@ visualisation_recipe.estimate_means <- function(x,


# Main aesthetics -----------------
data <- as.data.frame(x)
vis_data <- as.data.frame(x)
y <- info$response
color <- NULL
alpha <- NULL

levels <- info$at[info$at %in% names(data[!sapply(data, is.numeric)])]
modulate <- info$at[info$at %in% names(data[sapply(data, is.numeric)])]
x1 <- levels[1]
if (length(levels) > 1L) {
color <- levels[2]
if (length(levels) > 2L) {
by_levels <- info$at[info$at %in% names(vis_data[!sapply(vis_data, is.numeric)])]
modulate <- info$at[info$at %in% names(vis_data[sapply(vis_data, is.numeric)])]
x1 <- by_levels[1]
if (length(by_levels) > 1L) {
color <- by_levels[2]
if (length(by_levels) > 2L) {
# TODO: add facetting (needs updating see::geom_from_list to work with facets)
insight::format_warning("Cannot deal with more than 2 levels variables for now. Other ones will be omitted.")
}
Expand Down Expand Up @@ -120,7 +120,7 @@ visualisation_recipe.estimate_means <- function(x,

# Line
layers[[paste0("l", l)]] <- .visualisation_means_line(
data,
vis_data,
x1,
y = info$coef_name[1],
color = color,
Expand All @@ -131,7 +131,7 @@ visualisation_recipe.estimate_means <- function(x,

# Pointrange
layers[[paste0("l", l)]] <- .visualisation_means_pointrange(
data,
vis_data,
x1,
y = info$coef_name[1],
color = color,
Expand All @@ -145,7 +145,7 @@ visualisation_recipe.estimate_means <- function(x,

# Out
class(layers) <- unique(c("visualisation_recipe", "see_visualisation_recipe", class(layers)))
attr(layers, "data") <- data
attr(layers, "data") <- vis_data
layers
}

Expand Down Expand Up @@ -229,16 +229,16 @@ visualisation_recipe.estimate_means <- function(x,

.visualisation_means_labs <- function(info, x1, y, labs = NULL) {
if (all(info$coef_name == "Probability")) {
title <- "Estimated Mean Probabilities"
vis_title <- "Estimated Mean Probabilities"
} else {
title <- "Estimated Means"
vis_title <- "Estimated Means"
}

out <- list(
geom = "labs",
x = x1,
y = y,
title = paste0(title, " (", format(insight::find_formula(info$model)), ")")
title = paste0(vis_title, " (", format(insight::find_formula(info$model)), ")")
)
if (!is.null(labs)) out <- utils::modifyList(out, labs) # Update with additional args
out
Expand Down
32 changes: 15 additions & 17 deletions R/visualisation_recipe.estimate_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
#' # ==============================================
#' if (require("ggplot2")) {
#' model <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
#' x <- estimate_slopes(model, trend = "Petal.Length", at = "Species")
#' x <- estimate_slopes(model, trend = "Petal.Length", by ="Species")
#'
#' layers <- visualisation_recipe(x)
#' layers
#' plot(layers)
#'
#' model <- lm(Petal.Length ~ poly(Sepal.Width, 4), data = iris)
#' x <- estimate_slopes(model, at = "Sepal.Width", length = 20)
#' x <- estimate_slopes(model, by ="Sepal.Width", length = 20)
#' plot(visualisation_recipe(x))
#'
#' model <- lm(Petal.Length ~ Species * poly(Sepal.Width, 3), data = iris)
#' x <- estimate_slopes(model, at = c("Sepal.Width", "Species"))
#' x <- estimate_slopes(model, by =c("Sepal.Width", "Species"))
#' plot(visualisation_recipe(x))
#' }
#' \donttest{
Expand All @@ -27,11 +27,11 @@
#' data$Petal.Length <- data$Petal.Length^2
#'
#' model <- mgcv::gam(Sepal.Width ~ t2(Petal.Width, Petal.Length), data = data)
#' x <- estimate_slopes(model, at = c("Petal.Width", "Petal.Length"), length = 20)
#' x <- estimate_slopes(model, by =c("Petal.Width", "Petal.Length"), length = 20)
#' plot(visualisation_recipe(x))
#'
#' model <- mgcv::gam(Sepal.Width ~ t2(Petal.Width, Petal.Length, by = Species), data = data)
#' x <- estimate_slopes(model, at = c("Petal.Width", "Petal.Length", "Species"), length = 10)
#' x <- estimate_slopes(model, by =c("Petal.Width", "Petal.Length", "Species"), length = 10)
#' plot(visualisation_recipe(x))
#' }
#' }
Expand Down Expand Up @@ -80,19 +80,17 @@ visualisation_recipe.estimate_slopes <- function(x,
if (length(facs) > 0) {
facet <- facs[1]
}
} else if (length(facs) > 0) {
color <- facs[1]
fill <- facs[1]
group_ribbon <- facs[1]
group_line <- facs[1]
alpha <- "Confidence"
} else {
if (length(facs) > 0) {
color <- facs[1]
fill <- facs[1]
group_ribbon <- facs[1]
group_line <- facs[1]
alpha <- "Confidence"
} else {
group_ribbon <- ".group"
group_line <- 1
fill <- "Confidence"
color <- NULL
}
group_ribbon <- ".group"
group_line <- 1
fill <- "Confidence"
color <- NULL
}
}

Expand Down
79 changes: 64 additions & 15 deletions man/visualisation_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/visualisation_recipe.estimate_predicted.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2858654

Please sign in to comment.