Learn R Programming

linear.tools (version 1.3.0)

get_contrast: get contrast of categorical variables in a model

Description

get contrast of categorical variables in a model

Usage

get_contrast(model, data = NULL, PRINT = TRUE, return_method = FALSE, delete.minus.var = TRUE)

Arguments

model
a model, either lm or glm.
data
dataframe, to provide new data to evaluate the model. If NULL (default), then we use the default data in the model.
PRINT
a boolean, whether to print messages. Default is TRUE.
return_method
a boolean, whether to return the method of contrast, rather than the contrast itself. Default is FALSE.
delete.minus.var
a boolean. whether to delete x2 in y ~ x1 - x2. Default is TRUE.

Value

contrasts of the categorical vars in the model, or the contrast method if return_method is TRUE.

Details

When R put categorical vars in the linear model, R will transform them into set of 'contrast' using certain contrast encoding schedule. See example code and the reference link below for details.

References

http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm

Examples

Run this code

get_contrast(lm(price ~ carat + I(carat^2) + cut:carat +
                  color,ggplot2::diamonds))
get_contrast(lm(price ~ carat + I(carat^2) + cut:carat +
                  color,ggplot2::diamonds),return_method = TRUE)

# dirty formulas: all categorical vars are with minus sign
# no categorical vars, thus no contast
get_contrast(lm(price ~ carat + I(carat^2) ,ggplot2::diamonds))

model_dirty = lm(price ~ carat + I(carat^2) - cut:carat - color,
                 ggplot2::diamonds)
get_contrast(model = model_dirty )

diamond_lm3 = lm(price~ I(cut) + depth,ggplot2::diamonds) # a GLM
get_contrast(model = diamond_lm3 )

Run the code above in your browser using DataLab