Difference coding sets the grand mean as the intercept. Each contrast compares one level with the previous level.

contr_code_difference(fct, levels = NULL)

Arguments

fct

the factor to contrast code (or a vector)

levels

the levels of the factor in order

Value

the factor with contrasts set

Examples

df <- sim_design(between = list(pet = c("cat", "dog", "ferret")), 
                 mu = c(2, 4, 9), empirical = TRUE, plot = FALSE)
                 
df$pet <- contr_code_difference(df$pet)
lm(y ~ pet, df) %>% broom::tidy()
#> # A tibble: 3 × 5
#>   term           estimate std.error statistic   p.value
#>   <chr>             <dbl>     <dbl>     <dbl>     <dbl>
#> 1 (Intercept)        5       0.0577      86.6 8.45e-213
#> 2 pet.dog-cat        2.00    0.141       14.1 4.53e- 35
#> 3 pet.ferret-dog     5.0     0.141       35.4 1.89e-108