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.0394   162.    0        
#> 2 time^1        4.26      0.0966    44.1   1.07e-189
#> 3 time^2        6.03      0.0966    62.4   3.75e-263
#> 4 time^3       -0.109     0.0966    -1.13  2.59e-  1
#> 5 time^4        0.0494    0.0966     0.512 6.09e-  1
#> 6 time^5        0.0156    0.0966     0.162 8.72e-  1