set_contrasts function

Set contrasts to dataframe

Set contrasts to dataframe

Uses the same syntax as enlist_contrasts(), but returns the dataframe with the new contrasts applied. Use this when your model function doesnt have a contrasts argument and you want to avoid writing contrasts<- multiple times. See enlist_contrasts() for details about the package-specific syntax.

set_contrasts( model_data, ..., verbose = getOption("contrastable.verbose"), print_contrasts = FALSE )

Arguments

  • model_data: Data frame you intend on passing to your model
  • ...: A series of 2 sided formulas with factor name on the left hand side and desired contrast scheme on the right hand side. The reference level can be set with +, the intercept can be overwritten with *, comparison labels can be set using |, and trends for polynomial coding can be removed using -.
  • verbose: Logical, defaults to FALSE, whether messages should be printed
  • print_contrasts: Logical, default FALSE, whether to print the contrasts set for each factor. Fractions are displayed using MASS::fractions()

Returns

The model_data dataframe, but with updated contrasts.

Details

enlist_contrasts(), set_contrasts(), and glimpse_contrasts() use special syntax to set contrasts for multiple factors. The syntax consists of two-sided formulas with the desired factor column on the left hand side and the contrast specification on the right hand side. For example, varname ~ scaled_sum_code. Many contrasts support additional kinds of contrast manipulations using overloaded operators:

  • + X: Set the reference level to the level named X. Only supported for schemes that have a singular reference level such as sum_code(), scaled_sum_code(), treatment_code(), stats::contr.treatment(), stats::contr.sum(), stats::contr.SAS(). Ignored for schemes like helmert_code().
  • * X: Overwrite the intercept to the mean of the level named X
  • - A:B: For polynomial coding schemes only, drop comparisons A through B.
  • | c(...): Change the comparison labels for the contrast matrix to the character vector c(...) of length n-1. These labels will appear in the output/summary of a statistical model. Note that for brms::brm, instances of - (a minus sign) are replaced with M.

You can also specify multiple variables on the left hand side of a formula using tidyselect helpers. See examples for more information.

Typically model functions like lm will have a contrasts argument where you can set the contrasts at model run time, rather than having to manually change the contrasts on the underlying factor columns in your data. This function will return such a named list of contrast matrices to pass to these functions. Note that this function should not be used within a modeling function call, e.g., lm(y~x, data = model_data, contrasts = enlist_contrasts(model_data,x~sum_code)). Often, this will call enlist_contrasts twice, rather than just once.

For some model fitting functions, like brms::brm, there is no contrasts argument. For such cases, use set_contrasts() to set contrasts directly to the factors in a dataframe.

One good way to use enlist_contrasts() is in conjunction with MASS::fractions() to create a list of matrices that can be printed to explicitly show the entire contrast matrices you're using for your models. This can be especially helpful for supplementary materials in an academic paper.

Sometimes when using orthogonal polynomial contrasts from stats::contr.poly() people will drop higher level polynomials for parsimony. Note however that these do capture some amount of variation, so even though they're orthogonal contrasts the lower level polynomials will have their estimates changed. Moreover, you cannot reduce a contrast matrix to a matrix smaller than size n*n-1 in the dataframe you pass to a model fitting function itself, as R will try to fill in the gaps with something else. If you want to drop contrasts you'll need to use something like enlist_contrasts(df, x ~ contr.poly - 3:5) and pass this to the contrasts argument in the model fitting function.

Examples

head( set_contrasts(mtcars, carb + cyl ~ helmert_code, print_contrasts = TRUE) )

See Also

enlist_contrasts() glimpse_contrasts()

  • Maintainer: Thomas Sostarics
  • License: MIT + file LICENSE
  • Last published: 2024-10-01