Skip to content

Commit

Permalink
allow names list for "at" (see easystats/datawizard#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jan 23, 2022
1 parent b33776d commit 88492d7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
66 changes: 42 additions & 24 deletions R/visualisation_matrix.data.frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Create a reference matrix, useful for visualisation, with evenly spread and combined values. `data_matrix()` is an alternative name for `visualisation_matrix()`.
#'
#' @param x An object from which to construct the reference grid.
#' @param at,target Can be "all" or list of characters indicating columns of interest. Can also contain assignments (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. (`target` is the deprecated name of that argument).
#' @param at,target 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. (`target` is the deprecated name of that argument).
#' @param length Length of numeric "at" variables.
#' @param range Can be one of `c("range", "iqr", "ci", "hdi", "eti")`. If `"range"` (default), will use the min and max 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()].
#' @param 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.
Expand Down Expand Up @@ -36,6 +36,9 @@
#' visualisation_matrix(data, at = c("Sepal.Length = 3", "Species"))
#' visualisation_matrix(data, at = 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"))
#'
#' # Standardize
#' vizdata <- visualisation_matrix(data, at = "Sepal.Length")
#' effectsize::standardize(vizdata)
Expand All @@ -62,8 +65,9 @@ visualisation_matrix.data.frame <- function(x, at = "all", target = NULL, factor
# find numerics that were coerced to factor in-formula
numeric_factors <- colnames(x)[sapply(x, function(i) isTRUE(attributes(i)$factor))]

specs <- NULL

if (is.null(target)) {
specs <- NULL
targets <- data.frame()
} else {
# Valid target argument
Expand All @@ -75,14 +79,8 @@ visualisation_matrix.data.frame <- function(x, at = "all", target = NULL, factor
target <- names(x)[target]
}

# Deal with targets ==========================================================

# Find eventual user-defined specifications for each target
specs <- do.call(rbind, lapply(target, .visualisation_matrix_clean_target, x = x))
specs$varname <- as.character(specs$varname) # make sure it's a string not fac
specs <- specs[!duplicated(specs$varname), ] # Drop duplicates
# Deal with factor in-formula transformations ============================

# Deal with factor transformations
x[] <- lapply(x, function(i) {
if (isTRUE(attributes(i)$factor)) {
as.factor(i)
Expand All @@ -91,23 +89,43 @@ visualisation_matrix.data.frame <- function(x, at = "all", target = NULL, factor
}
})

specs$is_factor <- sapply(x[specs$varname], function(x) is.factor(x) || is.character(x))
# Create target list of factors -----------------------------------------
facs <- list()
for (fac in specs[specs$is_factor == TRUE, "varname"]) {
facs[[fac]] <- visualisation_matrix(x[[fac]], target = specs[specs$varname == fac, "expression"])
}
# Deal with targets ==========================================================

# Create target list of numerics ----------------------------------------
nums <- list()
for (num in specs[specs$is_factor == FALSE, "varname"]) {
nums[[num]] <- visualisation_matrix(x[[num]],
target = specs[specs$varname == num, "expression"],
reference = reference[[num]],
...
)
if (is.character(target)) {

# Find eventual user-defined specifications for each target
specs <- do.call(rbind, lapply(target, .visualisation_matrix_clean_target, x = x))
specs$varname <- as.character(specs$varname) # make sure it's a string not fac
specs <- specs[!duplicated(specs$varname), ] # Drop duplicates

specs$is_factor <- sapply(x[specs$varname], function(x) is.factor(x) || is.character(x))

# Create target list of factors -----------------------------------------
facs <- list()
for (fac in specs[specs$is_factor == TRUE, "varname"]) {
facs[[fac]] <- visualisation_matrix(x[[fac]],
target = specs[specs$varname == fac, "expression"])
}

# Create target list of numerics ----------------------------------------
nums <- list()
for (num in specs[specs$is_factor == FALSE, "varname"]) {
nums[[num]] <- visualisation_matrix(x[[num]],
target = specs[specs$varname == num, "expression"],
reference = reference[[num]],
...
)
}
} else if (is.list(target)) {

# we have a list as at-values
facs <- target[sapply(target, is.factor)]
nums <- target[sapply(target, is.numeric)]
}
# Assemble the two

# Assemble the two - the goal is to have two named lists, where variable
# names are the names of the list-elements: one list contains elements of
# numeric variables, the other one factors.
targets <- expand.grid(c(nums, facs))

# Preserve range ---------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion man/visualisation_matrix.Rd

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

0 comments on commit 88492d7

Please sign in to comment.