set.seed(1234)
n <- 2000
gender <- rep(0:1, each = n/2)
y <- sample(0:10, n, replace = TRUE) + gender * sample(0:10, n, replace = TRUE)
x1 <- sample(0:10, n, replace = TRUE) + gender * y
x2 <- x1 + sample(0:10, n, replace = TRUE) + gender * y
y <- y + (x1 > median(x1) & x2 > median(x2) & gender == 1) * sample(0:10, n, replace = TRUE) * 2
dat <- data.frame(y = y, x1 = x1, x2 = x2, gender = gender)Treatment vs. effect contrasts
regression
statistics
contrasts
Abstract
Here is a simple example to show the differences between treatment and effect contrasts.
Example dataset
Create a random dataset with criteria y, predictors x1, x2 and gender.
yandgenderare correlatedyandx1are correlated only if gender is 1yandx2are correlated only if gender is 1x1andx2are correlatedx1andx2have an interaction effect on y only if gender is 1
Descriptives
wmisc::nice_corrmatrix(dat, type = "html", numbered_columns = FALSE)| Table Correlation matrix |
|||||||
|---|---|---|---|---|---|---|---|
| Variable | n | M | SD | y | x1 | x2 | gender |
| y | 2000 | 11.67 | 9.83 | - | |||
| x1 | 2000 | 10.09 | 6.80 | .79*** | - | ||
| x2 | 2000 | 20.30 | 12.88 | .82*** | .95*** | - | |
| gender | 2000 | 0.50 | 0.50 | .68*** | .74*** | .79*** | - |
| Note. ✞p < .10; p < .05; p < .01; p < .001. . | |||||||
Contrasts
The left part of the table is with gender as treatment contrast (0 vs. 1) and the right part with gender as effect contrast (-1 vs. 1)
# Gender has values 0 vs. 1 (treatment contrast)
fit1 <- lm(y ~ gender * x1 * x2, data = dat)
# Gender hast -1 vs. 1 (effect contrast)
dat$gender <- car::recode(dat$gender, "0 = -1; 1 = 1")
fit2 <- lm(y ~ gender * x1 * x2, data = dat)
sjPlot::tab_model(fit1, fit2, show.se = TRUE, show.ci = FALSE, col.order = c("est", "se", "std.est", "p"), digits = 4, dv.labels = c("Treatment contrast<br> for gender", "Effect contrast<br> for gender"))| Treatment contrast for gender |
Effect contrast for gender |
|||||
| Predictors | Estimates | std. Error | p | Estimates | std. Error | p |
| (Intercept) | 5.2577 | 0.6189 | <0.001 | -4.6274 | 0.6427 | <0.001 |
| gender | -19.7703 | 1.2853 | <0.001 | -9.8852 | 0.6427 | <0.001 |
| x1 | -0.0695 | 0.1358 | 0.609 | 0.6640 | 0.0851 | <0.001 |
| x2 | -0.0270 | 0.0795 | 0.734 | 0.4363 | 0.0482 | <0.001 |
| gender × x1 | 1.4671 | 0.1702 | <0.001 | 0.7335 | 0.0851 | <0.001 |
| gender × x2 | 0.9267 | 0.0963 | <0.001 | 0.4633 | 0.0482 | <0.001 |
| x1 × x2 | 0.0056 | 0.0116 | 0.629 | -0.0124 | 0.0059 | 0.036 |
| (gender × x1) × x2 | -0.0360 | 0.0118 | 0.002 | -0.0180 | 0.0059 | 0.002 |
| Observations | 2000 | 2000 | ||||
| R2 / R2 adjusted | 0.738 / 0.737 | 0.738 / 0.737 | ||||
- The
interceptin model1 (treatment contrast) is the mean ofyfor gender 0 - The
interceptin model2 (effect contrast) is the mean ofyfor all data - All predictors in model1 without a
genderterm are effects for gender 0 while the interactions with agenderterm are effects for gender 1 - All predictors in model2 without a
genderterm are effects across gender while the interactions with agenderterm are the effects ofgender(subtracted for gender 0 and added for gender 1). genderhas a significant effect onyin both modelsx1,x2andx1*x2only have a significant effect onyin model 2- All
genderinteractions are significant in both models where the effect sizes and the standard errors of model2 are half of the corresponding values in model1, sopis identical.
For those who love Anovas ;-)
fit1 |> car::Anova(type = "III") |> wmisc::nice_table()| Sum Sq | Df | F value | Pr(>F) |
|---|---|---|---|
| 1834.702440 | 1 | 72.1670347 | 3.810176e-17 |
| 6014.888596 | 1 | 236.5924110 | 1.552720e-50 |
| 6.660056 | 1 | 0.2619697 | 6.088269e-01 |
| 2.943411 | 1 | 0.1157775 | 7.336959e-01 |
| 1889.686051 | 1 | 74.3297855 | 1.324176e-17 |
| 2352.201419 | 1 | 92.5225789 | 1.930416e-21 |
| 5.937356 | 1 | 0.2335427 | 6.289624e-01 |
| 235.982758 | 1 | 9.2822550 | 2.344213e-03 |
| 50642.613735 | 1992 | NA | NA |
fit2 |> car::Anova(type = "III") |> wmisc::nice_table()| Sum Sq | Df | F value | Pr(>F) |
|---|---|---|---|
| 1318.0715 | 1 | 51.845636 | 8.472011e-13 |
| 6014.8886 | 1 | 236.592411 | 1.552720e-50 |
| 1548.5771 | 1 | 60.912449 | 9.559850e-15 |
| 2085.6571 | 1 | 82.038201 | 3.098825e-19 |
| 1889.6861 | 1 | 74.329785 | 1.324176e-17 |
| 2352.2014 | 1 | 92.522579 | 1.930416e-21 |
| 112.2512 | 1 | 4.415342 | 3.574248e-02 |
| 235.9828 | 1 | 9.282255 | 2.344213e-03 |
| 50642.6137 | 1992 | NA | NA |