Learn R Programming

codingMatrices (version 0.1.1)

Codings: Coding matrix functions for factors in linear model formulae

Description

These functions provide an alternative to the coding functions supplied in the stats package, namely contr.treatment, contr.sum, contr.helmert and contr.poly.

Usage

code_control(n, contrasts = TRUE, sparse = FALSE)

code_control_last(n, contrasts = TRUE, sparse = FALSE)

code_diff(n, contrasts = TRUE, sparse = FALSE)

code_diff_forward(n, contrasts = TRUE, sparse = FALSE)

code_helmert(n, contrasts = TRUE, sparse = FALSE)

code_helmert_forward(n, contrasts = TRUE, sparse = FALSE)

code_deviation(n, contrasts = TRUE, sparse = FALSE)

code_deviation_first(n, contrasts = TRUE, sparse = FALSE)

code_poly(n, contrasts = TRUE, sparse = FALSE)

contr.diff(n, contrasts = TRUE, sparse = FALSE)

Arguments

n
Either a positive integer giving the number of levels or the levels attribute of a factor, supplying both the number of levels via its length and labels potentially to be used in the dimnames of the result.
contrasts
Logical: Do you want the $n \times (n-1)$ coding matrix (TRUE) or an $n \times n$ full-rank matrix, (as is sometimes needed by the fitting functions) (FALSE)?
sparse
Logical: Do you want the result to be a sparse matrix object, as generated the the Matrix package?

Value

  • A coding matrix, as requested by fitting functions using linear model formulae with factor predictors.

Details

All functions with names of the form code_xxxx return coding matrices which, in a simple model, make the intercept term the simple ("unweighted") average of the class means. This can be important in some non-standard ANOVA tables. The function contr.diff is an exception, and is offered as a natural companion to stats::contr.treatment, with which it is closely aligned. [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

See Also

The MASS function contr.sdif which is an early version of code_deviation (by the same author).

Examples

Run this code
(M <- code_control(5))
mean_contrasts(M)
(M <- stats::contr.treatment(5))
mean_contrasts(M)  ## same contrasts; different averaging vector.
mean_contrasts(stats::contr.helmert(6))  ## Interpretation obscure
mean_contrasts(code_helmert(6))          ## each mean with the average preceding
mean_contrasts(code_helmert_forward(6))  ## each mean with the averave succeeding

Run the code above in your browser using DataLab