Add a contrast to a data frame
Usage
add_contrast(
data,
col,
contrast = c("anova", "sum", "treatment", "helmert", "poly", "difference"),
levels = NULL,
...,
add_cols = TRUE,
colnames = NULL
)
Arguments
- data
the data frame
- col
the column to recode
- contrast
the contrast to recode to
- levels
the levels of the factor in order
- ...
arguments to pass to the contrast function (base or omit)
- add_cols
whether to just add the contrast to the existing column or also to create new explicit columns in the dataset (default)
- colnames
optional list of column names for the added contrasts
Examples
df <- sim_design(between = list(time = 1:6), plot = FALSE) %>%
add_contrast("time", "poly")
# test all polynomial contrasts
lm(y ~ time, df) %>% broom::tidy()
#> # A tibble: 6 × 5
#> term estimate std.error statistic p.value
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 0.0193 0.0416 0.465 0.642
#> 2 time^1 -0.152 0.102 -1.49 0.137
#> 3 time^2 -0.0521 0.102 -0.511 0.609
#> 4 time^3 0.0412 0.102 0.405 0.686
#> 5 time^4 0.0985 0.102 0.968 0.334
#> 6 time^5 0.222 0.102 2.18 0.0298
# test only the linear and quadratic contrasts
lm(y ~ `time^1` + `time^2`, df) %>% broom::tidy()
#> # A tibble: 3 × 5
#> term estimate std.error statistic p.value
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 0.0193 0.0417 0.464 0.643
#> 2 `time^1` -0.152 0.102 -1.49 0.138
#> 3 `time^2` -0.0521 0.102 -0.510 0.610