Returns a matrix of contrasts.

contr.dummy(n, contrasts = TRUE)
contr.ordinal(n, contrasts = TRUE)
contr.metric(n, contrasts = TRUE)

Arguments

n

A vector containing levels of a factor, or the number of levels.

contrasts

A logical value indicating whether contrasts should be computed.

Details

contr.dummy is standard dummy-coding, contr.metric has the same effect like as.numeric (makes sense of course only for ordered variables). contr.ordinal computes contrasts for ordinal variables.

Value

A matrix with n rows and n-1 columns for contr.ordinal, a matrix with n rows and n columns for contr.dummy and a vector of length n for contr.metric.

References

Hechenbichler K. and Schliep K.P. (2004) Weighted k-Nearest-Neighbor Techniques and Ordinal Classification, Discussion Paper 399, SFB 386, Ludwig-Maximilians University Munich (https://doi.org/10.5282/ubm/epub.1769)

See also

contrasts, contr.poly and contr.sdif

Examples

contr.metric(5)
#> [,1] #> 1 -2 #> 2 -1 #> 3 0 #> 4 1 #> 5 2
contr.ordinal(5)
#> [,1] [,2] [,3] [,4] #> 1 0.5 0.5 0.5 0.5 #> 2 -0.5 0.5 0.5 0.5 #> 3 -0.5 -0.5 0.5 0.5 #> 4 -0.5 -0.5 -0.5 0.5 #> 5 -0.5 -0.5 -0.5 -0.5
contr.dummy(5)
#> 1 2 3 4 5 #> 1 1 0 0 0 0 #> 2 0 1 0 0 0 #> 3 0 0 1 0 0 #> 4 0 0 0 1 0 #> 5 0 0 0 0 1