Description
Professional Contrast Coding for OLS Models.
Description
Automates sum coding (also known as effect coding) for Ordinary Least Squares (OLS) regression models. This approach is specifically designed to handle seasonal time series and categorical variables by comparing each group to the grand mean, rather than a single baseline category. This ensures that the intercept represents the unweighted grand mean of the dependent variable. For a comprehensive overview of contrast coding systems, see the UCLA Advanced Research Computing documentation (2021) <https://stats.oarc.ucla.edu/r/library/r-library-contrast-coding-systems-for-categorical-variables/>.
README.md
peticontrast
The peticontrast package provides a simple way to apply sum coding (effect coding) to factors in OLS models.
Sum Coding vs. Treatment Coding
In R, the default is Treatment Coding (contr.treatment), where each level is compared to a reference category (baseline). The intercept represents the mean of that baseline category.
Sum Coding (implemented here as apply_peticontrast) compares each level to the grand mean (trend) of all categories. In this case:
- The intercept represents the overall average.
- The coefficients represent the deviation of each category from that average.
This is particularly useful for seasonal data where you want to see how each period (e.g., Quarter) differs from the yearly trend.
Installation
# Internal use only
Usage
library(peticontrast)
df <- data.frame(Month = factor(c("Jan", "Feb", "Mar", "Apr")))
df <- apply_peticontrast(df, "Month")
summary(lm(Value ~ Month, data = df))