Skip to contents

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.

Usage

contr_code_poly(fct, levels = NULL, colnames = NULL)

Arguments

fct

the factor to contrast code (or a vector)

levels

the levels of the factor in order

colnames

optional list of column names for the added contrasts

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.38        0.0409 156.      0        
#> 2 time^1       4.15        0.100   41.4     2.91e-177
#> 3 time^2       6.17        0.100   61.6     5.45e-260
#> 4 time^3      -0.0449      0.100   -0.448   6.55e-  1
#> 5 time^4       0.00516     0.100    0.0515  9.59e-  1
#> 6 time^5      -0.000218    0.100   -0.00217 9.98e-  1