Skip to content

Commit

Permalink
new function tidy_disambiguate_terms()
Browse files Browse the repository at this point in the history
fix #98
  • Loading branch information
larmarange committed Feb 16, 2021
1 parent 1adfd3b commit 51840f4
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export(tidy_add_variable_labels)
export(tidy_and_attach)
export(tidy_attach_model)
export(tidy_detach_model)
export(tidy_disambiguate_terms)
export(tidy_get_model)
export(tidy_identify_variables)
export(tidy_plus_plus)
Expand Down
5 changes: 4 additions & 1 deletion R/tidy_add_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#'
#' @param x a tidy tibble
#' @param model the corresponding model, if not attached to `x`
#' @param quiet logical argument whether broom.helpers should not return a message when `tidy_disambiguate_terms()` was already applied
#' @export
#' @family tidy_helpers
#' @examples
Expand All @@ -25,7 +26,7 @@
#' ) %>%
#' tidy_and_attach() %>%
#' tidy_add_contrasts()
tidy_add_contrasts <- function(x, model = tidy_get_model(x)) {
tidy_add_contrasts <- function(x, model = tidy_get_model(x), quiet = FALSE) {
if (is.null(model)) {
stop("'model' is not provided. You need to pass it or to use 'tidy_and_attach()'.")
}
Expand All @@ -37,6 +38,8 @@ tidy_add_contrasts <- function(x, model = tidy_get_model(x)) {
}

if (!"variable" %in% names(x)) {
if (!quiet)

x <- x %>% tidy_identify_variables()
}

Expand Down
49 changes: 49 additions & 0 deletions R/tidy_disambiguate_terms.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' Disambiguate terms
#'
#' For mixed models, the `term` column returned by `broom.mixed` may have
#' duplicated values for random-effect parameters and random-effect values.
#' In such case, the terms could be disambiguated be prefixing them with the
#' value of the `group` column. `tidy_disambiguate_terms()` will not change
#' any term if there is no `group` column in `x`. The original term value
#' is kept in a new column `original_term`.
#'
#'
#' @param x a tidy tibble
#' @param sep character, separator added between group name and term
#' @param model the corresponding model, if not attached to `x`
#' @export
#' @family tidy_helpers
#' @examples
#' if (require(lme4) & require(broom.mixed)) {
#' mod <- lme4::lmer(marker ~ stage + (1|grade) + (death|response), trial)
#' mod %>% tidy_and_attach() %>% tidy_disambiguate_terms()
#' }
tidy_disambiguate_terms <- function(x, sep = ".", model = tidy_get_model(x), quiet = FALSE) {
if (is.null(model)) {
stop("'model' is not provided. You need to pass it or to use 'tidy_and_attach()'.")
}

if ("original_term" %in% names(x)) {
if (!quiet) {
cli_alert_danger("{.code tidy_disambiguate_terms()} has already been applied. x has been returned unchanged.")
}
return(x)
}

.attributes <- .save_attributes(x)

if ("group" %in% names(x)) {
x <- x %>%
dplyr::mutate(
term = dplyr::if_else(
is.na(.data$group),
term,
paste(.data$group, .data$term, sep = sep)
),
original_term = term
)
}

x %>%
tidy_attach_model(model = model, .attributes = .attributes)
}
4 changes: 4 additions & 0 deletions R/tidy_plus_plus.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#'
#' This function will apply sequentially:
#' * [tidy_and_attach()]
#' * [tidy_disambiguate_terms()]
#' * [tidy_identify_variables()]
#' * [tidy_add_contrasts()]
#' * [tidy_add_reference_rows()]
Expand Down Expand Up @@ -38,6 +39,7 @@
#' @param add_n should the number of observations be added? \lifecycle{maturing}
#' @param intercept should the intercept(s) be included?
#' @inheritParams tidy_select_variables
#' @param disambiguate_sep separator for [`tidy_disambiguate_terms()`]
#' @param keep_model should the model be kept as an attribute of the final result?
#' @param quiet logical argument whether broom.helpers should not return a message
#' when requested output cannot be generated. Default is FALSE
Expand Down Expand Up @@ -111,6 +113,7 @@ tidy_plus_plus <- function(
add_n = TRUE,
intercept = FALSE,
include = everything(),
disambiguate_sep = ".",
keep_model = FALSE,
quiet = FALSE,
strict = FALSE,
Expand All @@ -122,6 +125,7 @@ tidy_plus_plus <- function(
exponentiate = exponentiate,
...
) %>%
tidy_disambiguate_terms(sep = disambiguate_sep, quiet = quiet) %>%
tidy_identify_variables(quiet = quiet) %>%
tidy_add_contrasts()
if (add_reference_rows) {
Expand Down
1 change: 1 addition & 0 deletions man/tidy_add_coefficients_type.Rd

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

5 changes: 4 additions & 1 deletion man/tidy_add_contrasts.Rd

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

1 change: 1 addition & 0 deletions man/tidy_add_estimate_to_reference_rows.Rd

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

1 change: 1 addition & 0 deletions man/tidy_add_header_rows.Rd

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

1 change: 1 addition & 0 deletions man/tidy_add_n.Rd

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

1 change: 1 addition & 0 deletions man/tidy_add_reference_rows.Rd

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

1 change: 1 addition & 0 deletions man/tidy_add_term_labels.Rd

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

1 change: 1 addition & 0 deletions man/tidy_add_variable_labels.Rd

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

1 change: 1 addition & 0 deletions man/tidy_attach_model.Rd

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

46 changes: 46 additions & 0 deletions man/tidy_disambiguate_terms.Rd

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

1 change: 1 addition & 0 deletions man/tidy_identify_variables.Rd

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

5 changes: 5 additions & 0 deletions man/tidy_plus_plus.Rd

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

1 change: 1 addition & 0 deletions man/tidy_remove_intercept.Rd

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

1 change: 1 addition & 0 deletions man/tidy_select_variables.Rd

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

0 comments on commit 51840f4

Please sign in to comment.