An open API service indexing awesome lists of open source software.

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

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

[![CRAN status](https://www.r-pkg.org/badges/version/contrastable)](https://CRAN.R-project.org/package=contrastable)
[![](https://img.shields.io/badge/doi-10.32614/CRAN.package.contrastable-blue.svg)](https://doi.org/10.32614/CRAN.package.contrastable)

[![Codecov test coverage](https://codecov.io/gh/tsostarics/contrastable/branch/main/graph/badge.svg?token=PW2NOWO8NE)](https://app.codecov.io/gh/tsostarics/contrastable?branch=main)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-green.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![R-CMD-check](https://github.com/tsostarics/contrastable/actions/workflows/R-CMD-check.yaml/badge.svg)](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')`.