Add a contrast to a data frame
add_contrast(
data,
col,
contrast = c("anova", "sum", "treatment", "helmert", "poly", "difference"),
levels = NULL,
...,
add_cols = TRUE,
colnames = NULL
)
the data frame
the column to recode
the contrast to recode to
the levels of the factor in order
arguments to pass to the contrast function (base or omit)
whether to just add the contrast to the existing column or also to create new explicit columns in the dataset (default)
optional list of column names for the added contrasts
the data frame with the recoded column and added columns (if add_cols == TRUE)
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.0328 0.0417 -0.786 0.432
#> 2 time^1 0.0370 0.102 0.362 0.718
#> 3 time^2 -0.0534 0.102 -0.522 0.602
#> 4 time^3 0.0928 0.102 0.909 0.364
#> 5 time^4 -0.0229 0.102 -0.224 0.823
#> 6 time^5 -0.0205 0.102 -0.201 0.841
# 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.0328 0.0416 -0.787 0.431
#> 2 `time^1` 0.0370 0.102 0.363 0.717
#> 3 `time^2` -0.0534 0.102 -0.523 0.601