Learn R Programming

DMAR (version 1.0.0)

helmert_coding: Helmert-Coding Contrast Matrix for a Factor

Description

Builds the Helmert-coding contrast matrix for a factor with \(a\) levels. The \(k\)-th column contrasts the \((k + 1)\)-th level against the average of all preceding levels, giving a fully orthogonal set under equal sample sizes. The returned matrix has columns named after the contrasted level rather than the numeric column names produced by stats::contr.helmert().

Usage

helmert_coding(levels)

Value

A numeric \(a \times (a - 1)\) matrix with row names = the factor levels and column names of the form

"L2_vs_prior", "L3_vs_prior", ...

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", ...

Author

Ken Kelley [email protected]

Details

Why Helmert. Helmert contrasts are the canonical "sequential" orthogonal contrast set: under equal-\(n\), every column is orthogonal to every other column and to the intercept. They are useful when the factor has a natural ordering and the research questions are "does the \(k\)-th level differ from the average of the preceding levels?"

Equivalent to. stats::contr.helmert() but with interpretable column names.

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 Also

contr.helmert, effects_coding, is_orthogonal_set

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

Examples

Run this code
# 1. Helmert coding for a 4-level factor:
helmert_coding(c("baseline", "week1", "week2", "week3"))

# 2. Confirm orthogonality:
M <- helmert_coding(4)
is_orthogonal_set(M)

Run the code above in your browser using DataLab