sum_code {contrastable}R Documentation

Sum code

Description

Wrapper around contr.sum, but ensures that the reference level is the first level alphabetically, not the last. Returns a contrast matrix where comparisons give differences between comparison levels and the grand mean.

Usage

sum_code(n)

Arguments

n

Integer umber of factor levels to compute contrasts for.

Details

For n levels of factors, generate a matrix with n-1 comparisons where:

Example interpretation for a 4 level factor:

Note that when n = 2, the coefficient estimate is half of the difference between the two levels. But, this coincidence does not hold when the number of levels is greater than 2.

Value

A contrast matrix with dimensions n rows and (n-1) columns.

Examples

mydf <- data.frame(
  grp = gl(4,5),
  resp = c(seq(1, 5), seq(5, 9), seq(10, 14), seq(15, 19))
)

mydf <- set_contrasts(mydf, grp ~ sum_code)

lm(resp ~ grp, data = mydf)


[Package contrastable version 1.0.2 Index]