Add a contrasts column corresponding to contrasts used for a categorical variable and a contrasts_type column equal to "treatment", "sum", "poly", "helmert", "other" or "no.contrast".

tidy_add_contrasts(x, model = tidy_get_model(x), quiet = FALSE)

Arguments

x

a tidy tibble

model

the corresponding model, if not attached to x

quiet

logical argument whether broom.helpers should not return a message when tidy_disambiguate_terms() was already applied

Details

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

Examples

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

df %>%
  glm(
    Survived ~ Class + Age + Sex,
    data = ., weights = .$n, family = binomial,
    contrasts = list(Age = contr.sum, Class = "contr.helmert")
  ) %>%
  tidy_and_attach() %>%
  tidy_add_contrasts()
#> # A tibble: 6 × 13
#>   term        variable   var_class var_type var_nlevels contrasts contrasts_type
#>   <chr>       <chr>      <chr>     <chr>          <int> <chr>     <chr>         
#> 1 (Intercept) (Intercep… NA        interce…          NA NA        NA            
#> 2 Class1      Class      character categor…           4 contr.he… helmert       
#> 3 Class2      Class      character categor…           4 contr.he… helmert       
#> 4 Class3      Class      character categor…           4 contr.he… helmert       
#> 5 Age1        Age        character dichoto…           2 contr.sum sum           
#> 6 SexMale     Sex        character dichoto…           2 contr.tr… treatment     
#> # ℹ 6 more variables: estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> #   p.value <dbl>, conf.low <dbl>, conf.high <dbl>