get contrast of categorical variables in a model

Share:

Description

get contrast of categorical variables in a model

Usage

1
2
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.

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.

Value

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

References

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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 )

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.