Call:
aov(formula = y ~ group)
Terms:
group Residuals
Sum of Squares 200.000 46.247
Deg. of Freedom 3 36
Residual standard error: 1.133419
Estimated effects may be unbalanced
Using lm
lm1 <-lm(y ~ group)anova(lm1)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
group 3 200.000 66.667 51.895 3.743e-13 ***
Residuals 36 46.247 1.285
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Using contrast matrix
fac <-factor(group)contrasts(fac) <-contr.sum(4) # effect codinglm2 <-lm(y ~ fac)anova(lm2)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
fac 3 200.000 66.667 51.895 3.743e-13 ***
Residuals 36 46.247 1.285
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Notice that we get the same sum of squares (SS). Next, we’ll compute SS by hand.
Let \(\mu_j\) be the population mean for group \(j\), and \(\bar Y_{.j}\) be the sample group mean. Let \(\mathbf{L}\) be a matrix such that each column is a contrast vector. For example,
contr.sum(4)
[,1] [,2] [,3]
1 1 0 0
2 0 1 0
3 0 0 1
4 -1 -1 -1
Note that each column sums to zero. Then, for a balanced design, the SS accounted for by \(\mathbf{L}\) is (e.g., Rencher & Schaalje, 2008)
\[
n [\mathbf{L}^\intercal \bar{\mathbf{Y}}]^\intercal [\mathbf{L}^\intercal \mathbf{L}]^{-1} [\mathbf{L}^\intercal \bar{\mathbf{Y}}],
\]
Call:
aov(formula = y2 ~ group * group2)
Terms:
group group2 group:group2 Residuals
Sum of Squares 90.00000 90.00000 30.00000 21.47982
Deg. of Freedom 3 1 3 32
Residual standard error: 0.8192951
Estimated effects may be unbalanced
Using contrast coding
fac <-factor(group)contrasts(fac) <-contr.sum(4)fac2 <-factor(group2)contrasts(fac2) <-contr.sum(2)model.matrix(~ fac * fac2) |>head()
Call:
aov(formula = y ~ group)
Terms:
group Residuals
Sum of Squares 151.10000 37.66745
Deg. of Freedom 3 36
Residual standard error: 1.022897
Estimated effects may be unbalanced
Using lm
lm1 <-lm(y ~ group)anova(lm1)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
group 3 151.100 50.367 48.137 1.107e-12 ***
Residuals 36 37.667 1.046
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Using contrast matrix
fac <-factor(group)contrasts(fac) <-contr.helmert(4)lm2 <-lm(y ~ fac)anova(lm2)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
fac 3 151.100 50.367 48.137 1.107e-12 ***
Residuals 36 37.667 1.046
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1