Learn R Programming

broom.helpers (version 1.17.0)

model_list_terms_levels: List levels of categorical terms

Description

Only for categorical variables with treatment, SAS, sum or successive differences contrasts (cf. MASS::contr.sdif()), and categorical variables with no contrast.

Usage

model_list_terms_levels(
  model,
  label_pattern = "{level}",
  variable_labels = NULL,
  sdif_term_level = c("diff", "ratio")
)

# S3 method for default model_list_terms_levels( model, label_pattern = "{level}", variable_labels = NULL, sdif_term_level = c("diff", "ratio") )

Value

A tibble with ten columns:

  • variable: variable

  • contrasts_type: type of contrasts ("sum" or "treatment")

  • term: term name

  • level: term level

  • level_rank: rank of the level

  • reference: logical indicating which term is the reference level

  • reference_level: level of the reference term

  • var_label: variable label obtained with model_list_variables()

  • var_nlevels: number of levels in this variable

  • dichotomous: logical indicating if the variable is dichotomous

  • label: term label (by default equal to term level) The first nine columns can be used in label_pattern.

Arguments

model

(a model object, e.g. glm)
A model object.

label_pattern

(glue pattern)
A glue pattern for term labels (see examples).

variable_labels

(list or string)
An optional named list or named vector of custom variable labels passed to model_list_variables()

sdif_term_level

(string)
For successive differences contrasts, how should term levels be named? "diff" for "B - A" (default), "ratio" for "B / A".

See Also

Other model_helpers: model_compute_terms_contributions(), model_get_assign(), model_get_coefficients_type(), model_get_contrasts(), model_get_model(), model_get_model_frame(), model_get_model_matrix(), model_get_n(), model_get_nlevels(), model_get_offset(), model_get_pairwise_contrasts(), model_get_response(), model_get_response_variable(), model_get_terms(), model_get_weights(), model_get_xlevels(), model_identify_variables(), model_list_contrasts(), model_list_higher_order_variables(), model_list_variables()

Examples

Run this code
glm(
  am ~ mpg + factor(cyl),
  data = mtcars,
  family = binomial,
  contrasts = list(`factor(cyl)` = contr.sum)
) |>
  model_list_terms_levels()

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

mod <- glm(
  Survived ~ Class + Age + Sex,
  data = df, weights = df$n, family = binomial,
  contrasts = list(Age = contr.sum, Class = "contr.helmert")
)
mod |> model_list_terms_levels()
mod |> model_list_terms_levels("{level} vs {reference_level}")
mod |> model_list_terms_levels("{variable} [{level} - {reference_level}]")
mod |> model_list_terms_levels(
  "{ifelse(reference, level, paste(level, '-', reference_level))}"
)

Run the code above in your browser using DataLab