Skip to contents

Will add term labels in a label column, based on:

  1. labels provided in labels argument if provided;

  2. factor levels for categorical variables coded with treatment, SAS or sum contrasts (the label could be customized with categorical_terms_pattern argument);

  3. variable labels when there is only one term per variable;

  4. term name otherwise.

Usage

tidy_add_term_labels(
  x,
  labels = NULL,
  interaction_sep = " * ",
  categorical_terms_pattern = "{level}",
  model = tidy_get_model(x),
  quiet = FALSE,
  strict = FALSE
)

Arguments

x

(data.frame)
A tidy tibble as produced by tidy_*() functions.

labels

(list or string)
An optional named list or named vector of custom term labels.

interaction_sep

(string)
Separator for interaction terms.

categorical_terms_pattern

(glue pattern)
A glue pattern for labels of categorical terms with treatment or sum contrasts (see examples and model_list_terms_levels()).

model

(a model object, e.g. glm)
The corresponding model, if not attached to x.

quiet

(logical)
Whether broom.helpers should not return a message when requested output cannot be generated. Default is FALSE.

strict

(logical)
Whether broom.helpers should return an error when requested output cannot be generated. Default is FALSE.

Details

If the variable_label column is not yet available in x, tidy_add_variable_labels() will be automatically applied. If the contrasts column is not yet available in x, tidy_add_contrasts() will be automatically applied.

It is possible to pass a custom label for any term in labels, including interaction terms.

Examples

if (FALSE) { # interactive()
df <- Titanic |>
  dplyr::as_tibble() |>
  dplyr::mutate(Survived = factor(Survived, c("No", "Yes"))) |>
  labelled::set_variable_labels(
    Class = "Passenger's class",
    Sex = "Sex"
  )

mod <-
  glm(Survived ~ Class * Age * Sex, data = df, weights = df$n, family = binomial)
mod |>
  tidy_and_attach() |>
  tidy_add_term_labels()
mod |>
  tidy_and_attach() |>
  tidy_add_term_labels(
    interaction_sep = " x ",
    categorical_terms_pattern = "{level} / {reference_level}"
  )
}