factorial.contrasts | R Documentation |
Helper function to construct the default contrast coefficients for various coding schemes.
Note that R has a partial matching feature which allows you to specify shortened versions of arguments, such as coding
instead of coding.scheme
.
Validated using lm()
and aov()
functions.
factorial.contrasts(factor.levels = c(3, 2),
coding.scheme = rep("deviation", length(factor.levels)),
base = factor.levels,
intercept = FALSE,
verbose = TRUE)
factor.levels |
Integer. Number of levels or groups in each factor. For example, for two factors each having two levels or groups use e.g. c(2, 2), for three factors each having two levels or groups use e.g. c(2, 2, 2) |
coding.scheme |
Character vector. Coding scheme for each factor. "sum" for deviation or effect coding, "treatment" for dummy coding, "helmert" for Helmert type of coding, and "poly" for polynomial coding. Each factor can have their own coding scheme. If a single character value is provided, it will be copied to other factors |
base |
Integer vector. Specifies which group is considered the baseline group. Ignored for coding schemes other than "treatment" |
intercept |
Logical. |
verbose |
Logical. |
factor.levels |
Number of levels (or groups) in each factor |
factor.data |
Unique combination of factor levels |
model.matrix |
Model (design) matrix based on unique combination of factor levels |
contrast.matrix |
Contrast matrix |
###################################################
############### dummy coding scheme ###############
####################################################
# one factor w/ 3 levels
contrast.object <- factorial.contrasts(factor.levels = 3,
coding = "treatment")
# model matrix
contrast.object$model.matrix
# contrast matrix
contrast.object$contrast.matrix
#######################################################
############### deviation coding scheme ##############
#######################################################
# especially useful for factorial designs
# two factors w/ 2 and 3 levels, respectively
contrast.object <- factorial.contrasts(factor.levels = c(2, 3),
coding = "sum")
# model matrix
contrast.object$model.matrix
# contrast matrix
contrast.object$contrast.matrix
######################################################
############### Helmert coding scheme ###############
######################################################
# one factor w/ 3 levels
contrast.object <- factorial.contrasts(factor.levels = 3,
coding = "helmert")
# model matrix
contrast.object$model.matrix
# contrast matrix
contrast.object$contrast.matrix
#########################################################
############### polynomial coding scheme ###############
#########################################################
# one factor w/ 3 levels
contrast.object <- factorial.contrasts(factor.levels = 3,
coding = "poly")
# model matrix
contrast.object$model.matrix
# contrast matrix
contrast.object$contrast.matrix
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.