Skip to contents

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, 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".

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.

Examples

glm(
  am ~ mpg + factor(cyl),
  data = mtcars,
  family = binomial,
  contrasts = list(`factor(cyl)` = contr.sum)
) |>
  model_list_terms_levels()
#> # A tibble: 3 × 11
#>   variable    contrasts_type term     level level_rank reference reference_level
#>   <chr>       <chr>          <chr>    <chr>      <int> <lgl>     <chr>          
#> 1 factor(cyl) sum            factor(… 4              1 FALSE     8              
#> 2 factor(cyl) sum            factor(… 6              2 FALSE     8              
#> 3 factor(cyl) sum            factor(… 8              3 TRUE      8              
#> # ℹ 4 more variables: var_label <chr>, var_nlevels <int>, dichotomous <lgl>,
#> #   label <glue>

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()
#> # A tibble: 4 × 11
#>   variable contrasts_type term      level  level_rank reference reference_level
#>   <chr>    <chr>          <chr>     <chr>       <int> <lgl>     <chr>          
#> 1 Age      sum            Age1      Adult           1 FALSE     Child          
#> 2 Age      sum            Age2      Child           2 TRUE      Child          
#> 3 Sex      treatment      SexFemale Female          1 TRUE      Female         
#> 4 Sex      treatment      SexMale   Male            2 FALSE     Female         
#> # ℹ 4 more variables: var_label <chr>, var_nlevels <int>, dichotomous <lgl>,
#> #   label <glue>
mod |> model_list_terms_levels("{level} vs {reference_level}")
#> # A tibble: 4 × 11
#>   variable contrasts_type term      level  level_rank reference reference_level
#>   <chr>    <chr>          <chr>     <chr>       <int> <lgl>     <chr>          
#> 1 Age      sum            Age1      Adult           1 FALSE     Child          
#> 2 Age      sum            Age2      Child           2 TRUE      Child          
#> 3 Sex      treatment      SexFemale Female          1 TRUE      Female         
#> 4 Sex      treatment      SexMale   Male            2 FALSE     Female         
#> # ℹ 4 more variables: var_label <chr>, var_nlevels <int>, dichotomous <lgl>,
#> #   label <glue>
mod |> model_list_terms_levels("{variable} [{level} - {reference_level}]")
#> # A tibble: 4 × 11
#>   variable contrasts_type term      level  level_rank reference reference_level
#>   <chr>    <chr>          <chr>     <chr>       <int> <lgl>     <chr>          
#> 1 Age      sum            Age1      Adult           1 FALSE     Child          
#> 2 Age      sum            Age2      Child           2 TRUE      Child          
#> 3 Sex      treatment      SexFemale Female          1 TRUE      Female         
#> 4 Sex      treatment      SexMale   Male            2 FALSE     Female         
#> # ℹ 4 more variables: var_label <chr>, var_nlevels <int>, dichotomous <lgl>,
#> #   label <glue>
mod |> model_list_terms_levels(
  "{ifelse(reference, level, paste(level, '-', reference_level))}"
)
#> # A tibble: 4 × 11
#>   variable contrasts_type term      level  level_rank reference reference_level
#>   <chr>    <chr>          <chr>     <chr>       <int> <lgl>     <chr>          
#> 1 Age      sum            Age1      Adult           1 FALSE     Child          
#> 2 Age      sum            Age2      Child           2 TRUE      Child          
#> 3 Sex      treatment      SexFemale Female          1 TRUE      Female         
#> 4 Sex      treatment      SexMale   Male            2 FALSE     Female         
#> # ℹ 4 more variables: var_label <chr>, var_nlevels <int>, dichotomous <lgl>,
#> #   label <glue>