Anova coding (also called deviation or simple coding) sets the grand mean as the intercept. Each contrast compares one level with the reference level (base).
contr_code_anova(fct, levels = NULL, base = 1)
the factor to contrast code (or a vector)
the levels of the factor in order
the index of the level to use as baseline
the factor with contrasts set
df <- sim_design(between = list(pet = c("cat", "dog")),
mu = c(10, 20), plot = FALSE)
df$pet <- contr_code_anova(df$pet)
lm(y ~ pet, df) %>% broom::tidy()
#> # A tibble: 2 × 5
#> term estimate std.error statistic p.value
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 15.0 0.0663 226. 9.05e-241
#> 2 pet.dog-cat 10.0 0.133 75.6 4.76e-148
df <- sim_design(between = list(pet = c("cat", "dog", "ferret")),
mu = c(2, 4, 9), empirical = TRUE, plot = FALSE)
df$pet <- contr_code_anova(df$pet, base = 1)
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.00 0.0577 86.6 8.45e-213
#> 2 pet.dog-cat 2.00 0.141 14.1 4.53e- 35
#> 3 pet.ferret-cat 7.00 0.141 49.5 1.67e-145
df$pet <- contr_code_anova(df$pet, base = 2)
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 0.0577 86.6 8.45e-213
#> 2 pet.cat-dog -2.00 0.141 -14.1 4.53e- 35
#> 3 pet.ferret-dog 5 0.141 35.4 1.89e-108
df$pet <- contr_code_anova(df$pet, base = "ferret")
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 0.0577 86.6 8.45e-213
#> 2 pet.cat-ferret -7.00 0.141 -49.5 1.67e-145
#> 3 pet.dog-ferret -5.00 0.141 -35.4 1.89e-108