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

tidy_add_estimate_to_reference_rows

Add an estimate value to references rows for categorical variables


Description

For categorical variables with a treatment contrast (stats::contr.treatment()) or a SAS contrast (stats::contr.SAS()) will add an estimate equal to 0 (or 1 if exponentiate = TRUE) to the reference row.

Usage

tidy_add_estimate_to_reference_rows(
  x,
  exponentiate = attr(x, "exponentiate"),
  model = tidy_get_model(x),
  quiet = FALSE
)

Arguments

x

a tidy tibble

exponentiate

logical indicating whether or not to exponentiate the coefficient estimates. It should be consistent with the original call to broom::tidy()

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

For categorical variables with a sum contrast (stats::contr.sum()), the estimate value of the reference row will be equal to the sum of all other coefficients multiplied by -1 (eventually exponentiated if exponentiate = TRUE), and obtained with emmeans::emmeans(). The emmeans package should therefore be installed. For sum contrasts, the model coefficient corresponds to the difference of each level with the grand mean.

For other variables, no change will be made.

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

See Also

Examples

df <- Titanic %>%
  dplyr::as_tibble() %>%
  dplyr::mutate(dplyr::across(where(is.character), factor))

df %>%
  glm(
    Survived ~ Class + Age + Sex,
    data = ., weights = .$n, family = binomial,
    contrasts = list(Age = contr.sum, Class = "contr.SAS")
  ) %>%
  tidy_and_attach(exponentiate = TRUE) %>%
  tidy_add_reference_rows() %>%
  tidy_add_estimate_to_reference_rows()

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() %>%
    tidy_add_estimate_to_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.