contr.weighted function

Contrast matrix for weighted effect coding

Contrast matrix for weighted effect coding

Weighted contrast coding for linear models.

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. Interactions between weighted contrasts are handled specifically using the weighting strategy while interactions between weighted contrasts and other contrasts are handled as numerical products.

Returns

A matrix with n rows and n-1 values.

References

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

Author(s)

Kristian Hovde Liland

Note

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

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")
  • Maintainer: Kristian Hovde Liland
  • License: GPL (>= 2)
  • Last published: 2025-01-14