Skip to content

Commit

Permalink
Inconsistency in the use of parenthesis between parameters() and `c…
Browse files Browse the repository at this point in the history
…ompare_parameters()` (#1029)

* Inconsistency in the use of parenthesis between `parameters()` and `compare_parameters()`
Fixes #1028

* fix

* fix

* update snapshots

* update test

* fix printing issue

* submit to PR
  • Loading branch information
strengejacke authored Oct 11, 2024
1 parent 33f8987 commit 594cb97
Show file tree
Hide file tree
Showing 26 changed files with 335 additions and 251 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.22.2.18
Version: 0.22.2.19
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

* Improved printing for `simulate_parameters()` for models from packages *mclogit*.

* `print()` for `compare_parameters()` now also puts factor levels into square
brackets, like the `print()` method for `model_parameters()`.

## Bug fixes

* Arguments like `digits` etc. were ignored in `model_parameters() for objects
Expand Down
2 changes: 1 addition & 1 deletion R/1_model_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
#' [`print_md()`][print_md.parameters_model]. \cr \cr **For developers**, if
#' speed performance is an issue, you can use the (undocumented) `pretty_names`
#' argument, e.g. `model_parameters(..., pretty_names = FALSE)`. This will
#' skip the formatting of the coefficient names and make `model_parameters()`
#' skip the formatting of the coefficient names and makes `model_parameters()`
#' faster.
#'
#' @section Standardization of model coefficients:
Expand Down
9 changes: 2 additions & 7 deletions R/compare_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#' @name compare_parameters
#'
#' @description Compute and extract model parameters of multiple regression
#' models. See [model_parameters()] for further details.
#' models. See [`model_parameters()`] for further details.
#'
#' @param ... One or more regression model objects, or objects returned by
#' `model_parameters()`. Regression models may be of different model
#' types. Model objects may be passed comma separated, or as a list.
#' If model objects are passed with names or the list has named elements,
#' these names will be used as column names.
#' @param component Model component for which parameters should be shown. See
#' documentation for related model class in [model_parameters()].
#' documentation for related model class in [`model_parameters()`].
#' @param column_names Character vector with strings that should be used as
#' column headers. Must be of same length as number of models in `...`.
#' @param ci_method Method for computing degrees of freedom for p-values
Expand Down Expand Up @@ -303,11 +303,6 @@ compare_models <- compare_parameters
}
}
}

if (!is.null(x$Parameter)) {
x$Parameter <- gsub("]", ")", gsub("[", "(", x$Parameter, fixed = TRUE), fixed = TRUE)
}

x
}

Expand Down
2 changes: 1 addition & 1 deletion R/print.compare_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ print.compare_parameters <- function(x,
zap_small = FALSE,
groups = NULL,
column_width = NULL,
ci_brackets = c("(", ")"),
ci_brackets = c("[", "]"),
select = NULL,
...) {
# save original input
Expand Down
35 changes: 21 additions & 14 deletions R/print.parameters_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,33 @@
#' printed. There are three options for this argument:
#'
#' 1. Selecting columns by name or index
#' \cr
#'
#' `select` can be a character vector (or numeric index) of column names that
#' should be printed. There are two pre-defined options for selecting columns:
#' `select = "minimal"` prints coefficients, confidence intervals and p-values,
#' while `select = "short"` prints coefficients, standard errors and p-values.
#' should be printed, where columns are extracted from the data frame returned
#' by `model_parameters()` and related functions.
#'
#' There are two pre-defined options for selecting columns:
#' `select = "minimal"` prints coefficients, confidence intervals and
#' p-values, while `select = "short"` prints coefficients, standard errors and
#' p-values.
#'
#' 2. A string expression with layout pattern
#' \cr
#' `select` is a string with "tokens" enclosed in braces. These tokens will
#' be replaced by their associated columns, where the selected columns will
#' be collapsed into one column. However, it is possible to create multiple
#' columns as well. Following tokens are replaced by the related coefficients
#' or statistics: `{estimate}`, `{se}`, `{ci}` (or `{ci_low}` and `{ci_high}`),
#' `{p}` and `{stars}`. The token `{ci}` will be replaced by `{ci_low}, {ci_high}`.
#' Furthermore, a `|` separates values into new cells/columns. If
#' `format = "html"`, a `<br>` inserts a line break inside a cell. See
#'
#' `select` is a string with "tokens" enclosed in braces. These tokens will be
#' replaced by their associated columns, where the selected columns will be
#' collapsed into one column. Following tokens are replaced by the related
#' coefficients or statistics: `{estimate}`, `{se}`, `{ci}` (or `{ci_low}` and
#' `{ci_high}`), `{p}` and `{stars}`. The token `{ci}` will be replaced by
#' `{ci_low}, {ci_high}`. Example: `select = "{estimate}{stars} ({ci})"`
#'
#' It is possible to create multiple columns as well. A `|` separates values
#' into new cells/columns. Example: `select = "{estimate} ({ci})|{p}"`.
#'
#' If `format = "html"`, a `<br>` inserts a line break inside a cell. See
#' 'Examples'.
#'
#' 3. A string indicating a pre-defined layout
#' \cr
#'
#' `select` can be one of the following string values, to create one of the
#' following pre-defined column layouts:
#'
Expand Down
6 changes: 6 additions & 0 deletions R/print_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ print_html.compare_parameters <- function(x,
groups = groups
)

# replace brackets by parenthesis
if (!is.null(ci_brackets) && "Parameter" %in% colnames(formatted_table)) {
formatted_table$Parameter <- gsub("[", ci_brackets[1], formatted_table$Parameter, fixed = TRUE)
formatted_table$Parameter <- gsub("]", ci_brackets[2], formatted_table$Parameter, fixed = TRUE)
}

out <- insight::export_table(
formatted_table,
format = "html",
Expand Down
12 changes: 11 additions & 1 deletion R/print_md.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ print_md.compare_parameters <- function(x,
engine = engine
)

# replace brackets by parenthesis
if (!is.null(ci_brackets) && "Parameter" %in% colnames(formatted_table)) {
formatted_table$Parameter <- gsub("[", ci_brackets[1], formatted_table$Parameter, fixed = TRUE)
formatted_table$Parameter <- gsub("]", ci_brackets[2], formatted_table$Parameter, fixed = TRUE)
}

if (identical(engine, "tt")) {
# retrieve output format - print_md() may be called from print_html()
dots <- list(...)
Expand Down Expand Up @@ -285,7 +291,11 @@ print_md.parameters_pca <- print_md.parameters_efa
# Equivalence test ----------------------------

#' @export
print_md.equivalence_test_lm <- function(x, digits = 2, ci_brackets = c("(", ")"), zap_small = FALSE, ...) {
print_md.equivalence_test_lm <- function(x,
digits = 2,
ci_brackets = c("(", ")"),
zap_small = FALSE,
...) {
rule <- attributes(x)$rule
rope <- attributes(x)$rope

Expand Down
13 changes: 13 additions & 0 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,19 @@
}
# create a pretty level for the reference category
pretty_level <- paste0(fn_clean, " [", sub(fn, "", reference_level, fixed = TRUE), "]")
pretty_level <- gsub("_", " ", pretty_level, fixed = TRUE)
# special handling for "cut()"
pattern_cut_right <- "(.*)\\((.*),(.*)\\]\\]$"
pattern_cut_left <- "(.*)\\[(.*),(.*)\\)\\]$"
if (all(grepl(pattern_cut_right, pretty_level))) {
lower_bounds <- gsub(pattern_cut_right, "\\2", pretty_level)
upper_bounds <- gsub(pattern_cut_right, "\\3", pretty_level)
pretty_level <- gsub(pattern_cut_right, paste0("\\1>", as.numeric(lower_bounds), "-", upper_bounds, "]"), pretty_level)
} else if (all(grepl(pattern_cut_left, pretty_level))) {
lower_bounds <- gsub(pattern_cut_left, "\\2", pretty_level)
upper_bounds <- gsub(pattern_cut_left, "\\3", pretty_level)
pretty_level <- gsub(pattern_cut_left, paste0("\\1", as.numeric(lower_bounds), "-<", upper_bounds, "]"), pretty_level)
}
# insert new pretty level at the correct position in "pretty_names"
pretty_names <- .insert_element_at(
pretty_names,
Expand Down
8 changes: 3 additions & 5 deletions man/bootstrap_parameters.Rd

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

69 changes: 40 additions & 29 deletions man/compare_parameters.Rd

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

69 changes: 40 additions & 29 deletions man/display.parameters_model.Rd

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

16 changes: 7 additions & 9 deletions man/model_parameters.BFBayesFactor.Rd

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

Loading

0 comments on commit 594cb97

Please sign in to comment.