model_list_terms_levels: List levels of categorical terms

View source: R/model_list_terms_levels.R

model_list_terms_levelsR Documentation

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")
)

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

Arguments

model

a model object

label_pattern

a glue pattern for term labels (see examples)

variable_labels

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

sdif_term_level

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

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.

See Also

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

Examples

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 <- df %>%
  glm(
    Survived ~ Class + Age + Sex,
    data = ., weights = .$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))}"
)

broom.helpers documentation built on Aug. 7, 2023, 5:08 p.m.