https://github.com/tsostarics/contrastable
Contrast Coding Utilities
https://github.com/tsostarics/contrastable
contrasts modeling rstats statistics
Last synced: 4 months ago
JSON representation
Contrast Coding Utilities
- Host: GitHub
- URL: https://github.com/tsostarics/contrastable
- Owner: tsostarics
- License: other
- Created: 2021-09-05T15:01:09.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-11-25T03:19:43.000Z (7 months ago)
- Last Synced: 2025-12-09T09:33:26.958Z (7 months ago)
- Topics: contrasts, modeling, rstats, statistics
- Language: R
- Homepage: https://tsostarics.github.io/contrastable/
- Size: 2.67 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
message = FALSE,
warning = FALSE
)
```
# contrastable
[](https://CRAN.R-project.org/package=contrastable)
[](https://doi.org/10.32614/CRAN.package.contrastable)
[](https://app.codecov.io/gh/tsostarics/contrastable?branch=main)
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://github.com/tsostarics/contrastable/actions/workflows/R-CMD-check.yaml)
This package provides utilities to set different common contrast coding schemes
for use with regression models.
Please see [tsostarics.github.io/contrastable/](https://tsostarics.github.io/contrastable/) for the package website containing documentation, get started guide, and related vignettes.
## Installation
You can install from CRAN with:
```{r, eval = FALSE}
install.packages("contrastable")
```
You can install the development version from [GitHub](https://github.com/) with:
```r
# install.packages("devtools")
devtools::install_github("tsostarics/contrastable", build_vignettes = TRUE)
```
## Citation
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.
# Usage
Here is a simple example showing how to set particular factors to a specific
contrast scheme.
```{r}
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.
```{r, message = TRUE}
# 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.
```{r, message = TRUE, warning=FALSE}
# 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.
```{r, eval = FALSE, echo = TRUE}
# Get a list of the contrasts we've explicitly set
enlist_contrasts(mtcars, contrast_schemes)
```
```{r, echo = FALSE}
lapply(enlist_contrasts(mtcars, contrast_schemes), MASS::fractions)
```
You can also set multiple contrasts at once using `{tidyselect}` functionality.
```{r}
# 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')`.