Learn R Programming

contrastable

This package provides utilities to set different common contrast coding schemes for use with regression models. Detailed usage is available in the contrasts vignette with vignette('contrasts', 'contrastable').

Installation

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)

Citation

To cite contrastable in publications, please use

Sostarics, T. (2024). contrastable: Contrast Coding Utilities in R. R package version 1.0.1.

A BibTeX entry for LaTeX users is

@Manual{,
author = {Thomas Sostarics},
title = {{contrastable}: Contrast Coding Utilities in {R}},
year = {2024},
note = {R package version 1.0.1},
url = {https://github.com/tsostarics/contrastable},
doi = {10.5281/zenodo.11869427},
}

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.

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)
#> Converting to factors: cyl carb vs
#> Expect contr.treatment or contr.poly for unset factors: gear
#> $cyl
#>   4    8   
#> 4  2/3 -1/3
#> 6 -1/3 -1/3
#> 8 -1/3  2/3
#> 
#> $carb
#>   <2   <3   <4   <6   <8  
#> 1 -1/2 -1/3 -1/4 -1/5 -1/6
#> 2  1/2 -1/3 -1/4 -1/5 -1/6
#> 3    0  2/3 -1/4 -1/5 -1/6
#> 4    0    0  3/4 -1/5 -1/6
#> 6    0    0    0  4/5 -1/6
#> 8    0    0    0    0  5/6
#> 
#> $vs
#>   0
#> 0 1
#> 1 0

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()
factornlevel_namesschemereferenceintercept
cyl34, 6, 8contrastable::scaled_sum_code6grand mean
carb61, 2, 3,….contrastable::helmert_codeNAgrand mean
vs20, 1contrastable::treatment_code1mean(1)
gear33, 4, 5stats::contr.polyNAgrand mean

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)
#> $cyl
#>   4    8   
#> 4  2/3 -1/3
#> 6 -1/3 -1/3
#> 8 -1/3  2/3
#> 
#> $carb
#>   <2   <3   <4   <6   <8  
#> 1 -1/2 -1/3 -1/4 -1/5 -1/6
#> 2  1/2 -1/3 -1/4 -1/5 -1/6
#> 3    0  2/3 -1/4 -1/5 -1/6
#> 4    0    0  3/4 -1/5 -1/6
#> 6    0    0    0  4/5 -1/6
#> 8    0    0    0    0  5/6
#> 
#> $vs
#>   0
#> 0 1
#> 1 0

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)
#> $b
#>              .L         .Q            .C         ^4
#> 1 -6.324555e-01  0.5345225 -3.162278e-01  0.1195229
#> 2 -3.162278e-01 -0.2672612  6.324555e-01 -0.4780914
#> 3 -3.510833e-17 -0.5345225  1.755417e-16  0.7171372
#> 4  3.162278e-01 -0.2672612 -6.324555e-01 -0.4780914
#> 5  6.324555e-01  0.5345225  3.162278e-01  0.1195229
#> 
#> $a
#>     <2
#> 1 -0.5
#> 2  0.5
#> 
#> $c
#>     <2         <3    <4   <5
#> 1 -0.5 -0.3333333 -0.25 -0.2
#> 2  0.5 -0.3333333 -0.25 -0.2
#> 3  0.0  0.6666667 -0.25 -0.2
#> 4  0.0  0.0000000  0.75 -0.2
#> 5  0.0  0.0000000  0.00  0.8
#> 
#> $d
#>     2  3  4  5  6  7  8  9 10
#> 1  -1 -1 -1 -1 -1 -1 -1 -1 -1
#> 2   1  0  0  0  0  0  0  0  0
#> 3   0  1  0  0  0  0  0  0  0
#> 4   0  0  1  0  0  0  0  0  0
#> 5   0  0  0  1  0  0  0  0  0
#> 6   0  0  0  0  1  0  0  0  0
#> 7   0  0  0  0  0  1  0  0  0
#> 8   0  0  0  0  0  0  1  0  0
#> 9   0  0  0  0  0  0  0  1  0
#> 10  0  0  0  0  0  0  0  0  1
#> 
#> $e
#>    12 13 14 15 16 17 18 19 20
#> 11 -1 -1 -1 -1 -1 -1 -1 -1 -1
#> 12  1  0  0  0  0  0  0  0  0
#> 13  0  1  0  0  0  0  0  0  0
#> 14  0  0  1  0  0  0  0  0  0
#> 15  0  0  0  1  0  0  0  0  0
#> 16  0  0  0  0  1  0  0  0  0
#> 17  0  0  0  0  0  1  0  0  0
#> 18  0  0  0  0  0  0  1  0  0
#> 19  0  0  0  0  0  0  0  1  0
#> 20  0  0  0  0  0  0  0  0  1

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').

Copy Link

Version

Install

install.packages('contrastable')

Monthly Downloads

167

Version

1.0.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Thomas Sostarics

Last Published

October 1st, 2024

Functions in contrastable (1.0.2)

.is_polynomial_scheme

Check if a scheme is a polynomial scheme
.make_placeholder_glimpse

Make glimpse for 1-level factors
find_same_col

Find column with same non-positive values
enlist_contrasts

List of contrast matrices
is_centered

Check for orthogonality
is.unordered

Check for unordered factor
is_orthogonal

Check for orthogonality
.get_from_params

Retrieve value from param list
.get_reference_level

Get reference level of contrast matrix
.msg_if_ordered_reset

Alert user when setting ordered factors
.switch_reference_if_needed

Check if reference switching is necessary
orth_polynomial_code

Orthogonal Polynomial code
.split_if_language

Split contrast function using parens
.msg_if_remaining_factors

Alert user if there are more factors
forward_difference_code

Forward difference code
.warn_if_nondefault

Warn user if nondefault contrasts are set
glimpse_contrasts

Glimpse contrasts in dataframe
.make_parameters

Make parameters for contrast code call
.process_code_by

Process code_by
.process_contrasts

Pass arguments to contrast code
.warn_if_onelevel

Warn if one level factor
use_contrasts.AsIs

AsIs method for use_contrasts
sum_code

Sum code
use_contrasts

Contrast code factors
treatment_code

Treatment code
.is_reserved_operator

Check if node is a contrast-manipulation operator
.msg_if_coerced_to_factors

Alert user when factor coercion happens
use_contrasts.name

Symbol method for use_contrasts
.switch_reference_level

Switch reference level in contrast matrix
use_contrasts.default

Default method for use_contrasts
use_contrasts.function

Function method for use_contrasts
raw_polynomial_code

Raw Polynomial code
.set_intercept

Set intercept for contrast matrix
.process_factor_col

Process factor column
reverse_helmert_code

Reverse Helmert code
use_contrasts.hypr

Use a hypr object for contrasts
scaled_sum_code

Scaled sum coding
set_contrasts

Set contrasts to dataframe
.set_param

Process and set parameter
.reset_comparison_labels

Reset comparison labels of matrix to defaults
use_contrasts.matrix

Matrix method for use_contrasts
helmert_code

Helmert code
.warn_if_mismatched_contrasts

Diagnose glimpse issues and send warnings
interpret_intercept

Interpret intercept from contrasts
.expand_formulas

Expand contrast formulas
.cols_where

Get columns where
.get_dimnames

Get dimnames of contrasts from factor
.get_reference_levels

Get reference levels from a (possibly set) list of contrasts
cumulative_split_code

Cumulative split contrasts
.add_namespace

Lookup namespace of contrast scheme function
decompose_contrasts

Decompose contrasts into new columns
.convert_to_factors

Convert non factors to factors
.bundle_params

Extract parameters from dots
as.unordered

Convert to unordered factor
.get_reserved_operator

Check for reserved operators
.get_scheme_labels

Get contrast scheme labels for glimpse table
.glimpse_default_factors

Glimpse default factors
.is_valid_contrmat

Check if valid contrast
backward_difference_code

Backward difference code
.plus_to_c

Handle + in formula LHS
.postprocess_matrix

Postprocess contrast matrices