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.37       0.0414  154.     0        
#> 2 time^1       4.24       0.101    41.9    2.41e-179
#> 3 time^2       6.10       0.101    60.2    4.07e-255
#> 4 time^3      -0.00153    0.101    -0.0151 9.88e-  1
#> 5 time^4       0.0446     0.101     0.440  6.60e-  1
#> 6 time^5       0.0595     0.101     0.587  5.57e-  1