Polynomial coding sets the grand mean as the intercept. Each contrast tests a trend (linear, quadratic, cubic, etc.). This is only suitable for ordered factors.

contr_code_poly(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(within = list(time = 1:6), 
                 mu = 1:6 + (1:6-3.5)^2, 
                 long = TRUE, plot = FALSE)
                 
df$time <- contr_code_poly(df$time)
lm(y ~ time, df) %>% broom::tidy()
#> # A tibble: 6 × 5
#>   term        estimate std.error statistic   p.value
#>   <chr>          <dbl>     <dbl>     <dbl>     <dbl>
#> 1 (Intercept)   6.36      0.0404   158.    0        
#> 2 time^1        4.13      0.0989    41.8   7.85e-179
#> 3 time^2        6.21      0.0989    62.8   1.64e-264
#> 4 time^3        0.122     0.0989     1.23  2.18e-  1
#> 5 time^4        0.0571    0.0989     0.578 5.64e-  1
#> 6 time^5       -0.0155    0.0989    -0.157 8.75e-  1