Learn R Programming

DMAR (version 1.0.0)

effects_coding: Effects-Coding Contrast Matrix for a Factor

Description

Builds the effects-coding (also called deviation coding or sum-to-zero) contrast matrix for a factor with \(a\) levels. Each non-reference level contrasts with the grand mean (rather than with a reference category as in dummy coding). The returned matrix has rows = levels and columns named after the levels, replacing the numeric column names produced by stats::contr.sum().

Usage

effects_coding(levels, reference = NULL)

Value

A numeric \(a \times (a - 1)\) matrix with row names = the factor levels and column names = the non-reference levels. Suitable for assignment to contrasts(factor) or use in manual contrast construction.

Arguments

levels

Either an integer giving the number of levels or a character / factor vector giving the level labels. If integer, the labels default to "L1", "L2", ...

reference

Optional character name of the reference level (whose coefficients are all \(-1\)). Default is the last level.

Author

Ken Kelley [email protected]

Details

Why effects coding. Effects coding gives the regression intercept the interpretation of the grand mean (rather than the reference-category mean), and each slope coefficient becomes the deviation of that level's mean from the grand mean (rather than the difference vs the reference category). For balanced designs the effect coefficients are orthogonal to the intercept.

Equivalent to. stats::contr.sum() but with meaningful column names (the level labels), which is what is lost in the base-R implementation.

References

Cohen, J., Cohen, P., West, S. G., & Aiken, L. S. (2003). Applied multiple regression/correlation analysis for the behavioral sciences (3rd ed.). Lawrence Erlbaum. (See Chapter 8.)

Maxwell, S. E., Delaney, H. D., & Kelley, K. (2027). Designing experiments and analyzing data: A model comparison perspective (4th ed.). Routledge. (See Chapters 4, 7.)

See Also

contr.sum, helmert_coding, is_orthogonal_set

Other design utilities: design_consequences(), design_effect(), helmert_coding(), is_orthogonal_set(), orthogonal_polynomial()

Examples

Run this code
# 1. Effects coding for a 4-level factor:
effects_coding(c("low", "med", "high", "very_high"))

# 2. With "low" as the reference category:
effects_coding(c("low", "med", "high", "very_high"),
               reference = "low")

# 3. Assign to a factor for modeling:
f <- factor(c("a", "b", "c", "a", "b", "c"))
contrasts(f) <- effects_coding(levels(f))
model.matrix(~ f)

Run the code above in your browser using DataLab