Skip to contents

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

Usage

contr_code_sum(fct, levels = NULL, omit = length(levels), colnames = NULL)

Arguments

fct

the factor to contrast code (or a vector)

levels

the levels of the factor in order

omit

the level to omit (defaults to the last level)

colnames

optional list of column names for the added contrasts

Value

the factor with contrasts set

Examples

df <- sim_design(between = list(pet = c("cat", "dog", "bird", "ferret")), 
                 mu = c(2, 4, 9, 13), empirical = TRUE, plot = FALSE)

df$pet <- contr_code_sum(df$pet)
lm(y ~ pet, df) %>% broom::tidy()
#> # A tibble: 4 × 5
#>   term               estimate std.error statistic   p.value
#>   <chr>                 <dbl>     <dbl>     <dbl>     <dbl>
#> 1 (Intercept)               7    0.05       140   0        
#> 2 pet.cat-intercept        -5    0.0866     -57.7 6.13e-195
#> 3 pet.dog-intercept        -3    0.0866     -34.6 6.42e-122
#> 4 pet.bird-intercept        2    0.0866      23.1 2.33e- 75

df$pet <- contr_code_sum(df$pet, omit = "cat")
lm(y ~ pet, df) %>% broom::tidy()
#> # A tibble: 4 × 5
#>   term                 estimate std.error statistic   p.value
#>   <chr>                   <dbl>     <dbl>     <dbl>     <dbl>
#> 1 (Intercept)              7       0.05       140   0        
#> 2 pet.dog-intercept       -3.00    0.0866     -34.6 6.42e-122
#> 3 pet.bird-intercept       2       0.0866      23.1 2.33e- 75
#> 4 pet.ferret-intercept     6       0.0866      69.3 1.82e-223

df$pet <- contr_code_sum(df$pet, omit = 1)
lm(y ~ pet, df) %>% broom::tidy()
#> # A tibble: 4 × 5
#>   term                 estimate std.error statistic   p.value
#>   <chr>                   <dbl>     <dbl>     <dbl>     <dbl>
#> 1 (Intercept)              7       0.05       140   0        
#> 2 pet.dog-intercept       -3.00    0.0866     -34.6 6.42e-122
#> 3 pet.bird-intercept       2       0.0866      23.1 2.33e- 75
#> 4 pet.ferret-intercept     6       0.0866      69.3 1.82e-223