knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%", message = FALSE, warning = FALSE )
This package provides utilities to set different common contrast coding schemes for use with regression models. Please see tsostarics.github.io/contrastable/ for the package website containing documentation, get started guide, and related vignettes.
You can install from CRAN with:
install.packages("contrastable")
You can install the development version from GitHub with:
# install.packages("devtools") devtools::install_github("tsostarics/contrastable", build_vignettes = TRUE)
To cite contrastable
in publications, please use
Sostarics, T. (2024). contrastable: Contrast Coding Utilities in R. R package version r packageVersion("contrastable")
. https://CRAN.R-project.org/package=contrastable. doi:10.32614/CRAN.package.contrastable
A BibTeX entry for LaTeX users is
@Manual{contrastable, author = {Thomas Sostarics}, title = {{contrastable}: Contrast Coding Utilities in {R}}, year = {2024}, note = {R package version `r packageVersion("contrastable")`}, url = {https://CRAN.R-project.org/package=contrastable}, doi = {10.32614/CRAN.package.contrastable}, }
See the Citation Examples section in the contrasts vignette for suggestions and examples of how to cite this package in a paper.
Here is a simple example showing how to set particular factors to a specific contrast scheme.
library(contrastable) my_data <- mtcars my_data$gear <- ordered(my_data$gear) # Set as ordered factor in dataframe
set_contrasts
can be used to set the contrasts onto the dataframe itself,
which is needed when a modeling function lacks a contrasts
argument.
# Specify the contrast schemes we want, factor conversion done automatically # Set reference level with + and intercept with * my_data <- set_contrasts(my_data, cyl ~ scaled_sum_code + 6, carb ~ helmert_code, vs ~ treatment_code + 1, print_contrasts = TRUE)
We can use glimpse_contrasts
to get information about
the factors and diagnostics about the scheme we have set.
# Create a reusable list to use with multiple functions contrast_schemes <- list( cyl ~ scaled_sum_code + 6, carb ~ helmert_code, vs ~ treatment_code + 1 ) # Get information about our contrasts, even those we didn't explicitly set # (gear is ordered, and so uses contr.poly by default) glimpse_contrasts(my_data, contrast_schemes, add_namespace = TRUE, show_all_factors = TRUE) |> knitr::kable()
enlist_contrasts
can be used to generate a named list of contrasts
that can be used in the contrasts
argument of various modeling functions.
# Get a list of the contrasts we've explicitly set enlist_contrasts(mtcars, contrast_schemes)
lapply(enlist_contrasts(mtcars, contrast_schemes), MASS::fractions)
You can also set multiple contrasts at once using {tidyselect}
functionality.
# Create a new dataframe with a bunch of factors my_data2 <- data.frame(a = gl(2,10), b = gl(5,2, ordered = TRUE), c = gl(5,2), d = 1:10, e = 11:20) enlist_contrasts(my_data2, where(is.ordered) ~ polynomial_code, where(is.unordered) ~ helmert_code, d + e ~ sum_code)
The functions in this package aim to be helpful when potential mistakes are
made and transparent when things happen behind the scenes (e.g., automatic
factor coercion).
You can check out descriptions of various messages and warnings in the
warnings
vignette with vignette('warnings', 'contrastable')
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.