Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tidy_add_reference_rows

Add references rows for categorical variables


Description

For categorical variables with a treatment contrast (stats::contr.treatment()), a SAS contrast (stats::contr.SAS()) or a sum contrast (stats::contr.sum()), add a reference row.

Usage

tidy_add_reference_rows(
  x,
  no_reference_row = NULL,
  model = tidy_get_model(x),
  quiet = FALSE
)

Arguments

x

a tidy tibble

no_reference_row

a vector indicating the name of variables for those no reference row should be added. Accepts tidyselect syntax. Default is NULL. See also all_categorical() and all_dichotomous()

model

the corresponding model, if not attached to x

quiet

logical argument whether broom.helpers should not return a message when requested output cannot be generated. Default is FALSE

Details

The added reference_row column will be equal to:

  • TRUE for a reference row;

  • FALSE for a normal row of a variable with a reference row;

  • NA for variables without a reference row.

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

tidy_add_reference_rows() will not populate the label of the reference term. It is therefore better to apply tidy_add_term_labels() after tidy_add_reference_rows() rather than before. Similarly, it is better to apply tidy_add_reference_rows() before tidy_add_n().

See Also

Examples

df <- Titanic %>%
  dplyr::as_tibble() %>%
  dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))

res <- df %>%
  glm(
    Survived ~ Class + Age + Sex,
    data = ., weights = .$n, family = binomial,
    contrasts = list(Age = contr.sum, Class = "contr.SAS")
  ) %>%
  tidy_and_attach()
res %>% tidy_add_reference_rows()
res %>% tidy_add_reference_rows(no_reference_row = all_dichotomous())
res %>% tidy_add_reference_rows(no_reference_row = "Class")

if (requireNamespace("gtsummary")) {
  glm(
    response ~ stage + grade * trt,
    gtsummary::trial,
    family = binomial,
    contrasts = list(
      stage = contr.treatment(4, base = 3),
      grade = contr.treatment(3, base = 2),
      trt = contr.treatment(2, base = 2)
    )
  ) %>%
    tidy_and_attach() %>%
    tidy_add_reference_rows()
}

broom.helpers

Helpers for Model Coefficients Tibbles

v1.3.0
GPL-3
Authors
Joseph Larmarange [aut, cre] (<https://orcid.org/0000-0001-7097-700X>), Daniel D. Sjoberg [aut] (<https://orcid.org/0000-0003-0862-2018>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.