Only for categorical variables with treatment,
SAS, sum or successive differences contrasts (cf. MASS::contr.sdif()
), and
categorical variables with no contrast.
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
orstring
)
An optional named list or named vector of custom variable labels passed tomodel_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
: variablecontrasts_type
: type of contrasts ("sum" or "treatment")term
: term namelevel
: term levellevel_rank
: rank of the levelreference
: logical indicating which term is the reference levelreference_level
: level of the reference termvar_label
: variable label obtained withmodel_list_variables()
var_nlevels
: number of levels in this variabledichotomous
: logical indicating if the variable is dichotomouslabel
: term label (by default equal to term level) The first nine columns can be used inlabel_pattern
.
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
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>