contr.weighted: Contrast matrix for weighted effect coding

contr.weightedR Documentation

Contrast matrix for weighted effect coding

Description

Weighted contrast coding for linear models.

Usage

contr.weighted(x, base)

Arguments

x

factor for which a contrast matrix should be made.

base

factor level used as basis for contrast coding. Default is the (first) level with maximum frequency.

Details

Different from the contrasts made throught the stats package functions this contrast requires a full factor vector as input rather than its respective levels as weights are computed from the frequencies of the factor levels.

Value

A matrix with n rows and n-1 values.

Note

contr.weighted cannot be used directly as a replacement for other contrasts by name, but must be used via contrasts matrix computations.

Author(s)

Kristian Hovde Liland

References

Nieuwenhuis, R.; Grotenhuis, M.; Pelzer, B. Weighted Effect Coding for Observational Data with wec. R. J. 2017, 9, 477–485.

See Also

lm

Examples

balanced   <- factor(c(rep("A", 3), rep("B", 3), rep("C", 3)))
unbalanced <- factor(c(rep("A", 3), rep("B", 3), rep("C", 2)))
# Weighted coding when applied to balanced data
contr.weighted(balanced)
# Weighted coding when applied to unbalanced data (default base level)
contr.weighted(unbalanced)
# Weighted coding when applied to unbalanced data (base level = "C")
contr.weighted(unbalanced, "C")

mixlm documentation built on Aug. 8, 2023, 5:08 p.m.