One- and two-way random effects models
2026-10-26
Strength of fish net material made from four randomly chosen machines.
Is there significant machine-to-machine variability?
Suppose
\[ Y_{ij} = \mu + A_i + \varepsilon_{ij}, \quad i = 1,\dots,a, \quad j = 1,\dots,n_{i} \] where
Until now we have studied fixed-effects models.
Assume a balanced design, i.e. \(n_1 = \dots = n_a = n\).
In the one-way random effects model we wish to
Side-by-side boxplots are a natural choice if the \(n_i\) are not too small.
Dotplots are a better when the \(n_i\) are small.
| Sum of squares | Symbol | Formula |
|---|---|---|
| Total | \(\operatorname{SS}_{\operatorname{Tot}}\) | \(\sum_{i=1}^a\sum_{j=1}^n(Y_{ij} - \bar Y_{..})^2\) |
| Treatment | \(\SStrt\) | \(\sum_{i=1}^a n(\bar Y_{i.} - \bar Y_{..})^2\) |
| Error | \(\operatorname{SS}_{\operatorname{Error}}\) | \(\sum_{i=1}^a\sum_{j=1}^n(Y_{ij} - \bar Y_{i.})^2\) |
We have the decomposition \(\operatorname{SS}_{\operatorname{Tot}}= \SStrt + \operatorname{SS}_{\operatorname{Error}}\).
Easiest way to obtain sums of squares:
We obtain \(\operatorname{SS}_{\operatorname{Tot}}= 122.95\), \(\SStrt = 87.75\), and \(\operatorname{SS}_{\operatorname{Error}}= 35.2\).
Obtain the MS values by dividing the SS values by the Df values.
| Source | Df | SS | MS | F value |
|---|---|---|---|---|
| Treatment | \(a-1\) | \(\SStrt\) | \(\MStrt\) | \(\operatorname{F}_{\operatorname{test}}= \MStrt/\MSerror\) |
| Error | \(a(n-1)\) | \(\operatorname{SS}_{\operatorname{Error}}\) | \(\MSerror\) | |
| Total | \(an-1\) | \(\operatorname{SS}_{\operatorname{Tot}}\) |
We reject \(H_0\): \(\sigma_A^2 = 0\) if \(\operatorname{F}_{\operatorname{test}}> F_{a-1,a(n-1),\alpha}\).
The corresponding p-value is \(P(F > \operatorname{F}_{\operatorname{test}})\), where \(F \sim F_{a-1,a(n-1),\alpha}\).
Discuss: It can be shown that \(\SStrt\) and \(\operatorname{SS}_{\operatorname{Error}}\) are independent and \[ \frac{\operatorname{SS}_{\operatorname{Trt}}}{n\sigma_A^2 + \sigma_\varepsilon^2} \sim \chi^2_{a-1} \quad \text{ and } \quad \frac{\operatorname{SS}_{\operatorname{Error}}}{\sigma_\varepsilon^2} \sim \chi_{a(n-1)}^2. \] Show that \(\displaystyle \operatorname{F}_{\operatorname{test}}= \frac{\MStrt}{\MSerror} \sim F_{a-1,a(n-1)}\) under \(H_0\): \(\sigma_A^2 = 0\).
We obtain the ANOVA table just as we did in the fixed-effects case.
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
machine 3 87.7 29.2 13.3 0.00013 ***
Residuals 16 35.2 2.2
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The small p-value indicates strong evidence against \(H_0\): \(\sigma^2_A = 0\).
In models with random effects, we like to track how the variance components \(\sigma_A^2\) and \(\sigma_\varepsilon^2\) affect the expected values of the mean squares.
| Source | Df | Expected mean square |
|---|---|---|
| Treatment | \(a-1\) | \(n\sigma_A^2 + \sigma_\varepsilon^2\) |
| Error | \(a(n-1)\) | \(\sigma_\varepsilon^2\) |
The method of moments (MoM) is one way to estimate the variance components.
MoM method is deprecated because it is possible to get \(\dot \sigma_A^2 < 0\).
Compute the MoM estimators \(\dot \sigma_A^2\) and \(\dot \sigma_\varepsilon^2\) for the fish net data.
We obtain \(\dot \sigma_A^2 = 5.41\) and \(\dot \sigma_\varepsilon^2 = 2.2\).
Use R package lmerTest to compute the REML estimates of \(\sigma_A^2\) and \(\sigma_\varepsilon^2\).
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: y ~ 1 + (1 | machine)
REML criterion at convergence: 79.7
Scaled residuals:
Min 1Q Median 3Q Max
-1.653 -0.688 -0.102 0.485 1.718
Random effects:
Groups Name Variance Std.Dev.
machine (Intercept) 5.41 2.33
Residual 2.20 1.48
Number of obs: 20, groups: machine, 4
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 125.55 1.21 3.00 104 2e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Obtain the predicted values of \(A_1\), \(A_2\), \(A_3\), and \(A_4\) for the fish net data.
Can use predict() on the lmer() output to obtain the \(\hat A_i\) values:
confint() function on lmer() output uses another method.Construct a \(95\%\) confidence interval for \(\mu\) in fish net example.
We obtain the interval \([121.701,129.399]\).
Default confidence intervals from confint() function.
Wald-type CI \(Y_{..} \pm z_{\alpha/2}\sqrt{\MStrt/(na)}\) on fish net data.
We obtain the interval \([123.18,127.92]\).
Compare to:
Validity of the methods in these slides depends on these assumptions:
The responses are normally distributed around the treatment means.
(Check QQ plot of residuals)
The treatment means are normally distributed.
(The number of groups \(a\) is often too small for this to be checked)
The response has the same variance in all treatment groups.
(Check residuals vs fitted values plot)
The response values in each treatment group are independent.
(No way to check; must trust experimental design)
Build fitted values from \(\hat \mu\) and the predictions \(\hat A_1,\dots,\hat A_a\) as \[ \hat Y_{ij} = \hat \mu + \hat A_i, \quad \text{ for } i =1 ,\dots, a, \quad j=1,\dots,n. \]
Our residuals then become \[ \hat \varepsilon_{ij} = Y_{ij} - \hat Y_{ij}, \quad \text{ for } i =1 ,\dots, a, \quad j=1,\dots,n. \]
These residuals are not simply the deviations of the responses around their treatment group means (as in fixed effects models)!
So \(\operatorname{SS}_{\operatorname{Error}}\) is not the sum of squared residuals here.
Use predict() on the lmer() output to obtain the \(\hat Y_{ij} = \hat \mu + \hat A_i\).
Check the residuals vs fitted values plot: Use plot() on lmer() output.
Check the normal q-q plot of the residuals:
Triglyceride levels in serum samples measured with 4 machines on 4 days.
tg <- c(142.3,144.0,148.6,146.9,142.9,147.4,133.8,133.2,
134.9,146.3,145.2,146.3,125.9,127.6,108.9,107.5,
148.6,156.5,148.6,153.1,135.5,138.9,132.1,149.7,
152.0,151.4,149.7,152.0,142.9,142.3,141.7,141.2)
days <- as.factor(c(rep(1,8),rep(2,8),rep(3,8),rep(4,8)))
machine <- as.factor(c(1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4,
1,1,2,2,3,3,4,4,1,1,2,2,3,3,4,4))Machines randomly sampled. Regard days as randomly sampled.
Assume \[ Y_{ijk} = \mu + A_i + B_j + (AB)_{ij} + \varepsilon_{ijk} \] for \(i=1,\dots,a\), \(j=1,\dots,b\), and \(n=1,\dots,n\), where
Still assume a balanced design, that is \(n_{ij} = n\) for all \(i,j\).
In the two-way random effects model we wish to
| Sum of squares | Symbol | Formula |
|---|---|---|
| Total | \(\operatorname{SS}_{\operatorname{Tot}}\) | \(\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^n(Y_{ijk} - \bar Y_{...})^2\) |
| A | \(\operatorname{SS}_{\operatorname{A}}\) | \(nb\sum_{i=1}^a (\bar Y_{i..} - \bar Y_{...})^2\) |
| B | \(\operatorname{SS}_{\operatorname{B}}\) | \(na\sum_{j=1}^b (\bar Y_{.j.} - \bar Y_{...})^2\) |
| AB | \(\operatorname{SS}_{\operatorname{AB}}\) | \(n\sum_{i=1}^a\sum_{j=1}^b (\bar Y_{ij.} - (\bar Y_{i..} + \bar Y_{.j.} - \bar Y_{...})^2\) |
| Error | \(\operatorname{SS}_{\operatorname{Error}}\) | \(\sum_{i=1}^a \sum_{j=1}^b \sum_{k=1}^n(Y_{ijk} - \bar Y_{ij.})^2\) |
We have the decomposition \(\operatorname{SS}_{\operatorname{Tot}}= \operatorname{SS}_{\operatorname{A}}+ \operatorname{SS}_{\operatorname{B}}+ \operatorname{SS}_{\operatorname{AB}}+ \operatorname{SS}_{\operatorname{Error}}\).
Obtain the MS values by dividing the SS values by the Df values.
| Source | Df | SS | MS | F value |
|---|---|---|---|---|
| A | \(a-1\) | \(\operatorname{SS}_{\operatorname{A}}\) | \(\operatorname{MS}_{\operatorname{A}}\) | \(F_{\operatorname{A}} = \operatorname{MS}_{\operatorname{A}}/ \textcolor{red}{\operatorname{MS}_{\operatorname{AB}}}\) |
| B | \(b-1\) | \(\operatorname{SS}_{\operatorname{B}}\) | \(\operatorname{MS}_{\operatorname{B}}\) | \(F_{\operatorname{B}} = \operatorname{MS}_{\operatorname{B}}/\textcolor{red}{\operatorname{MS}_{\operatorname{AB}}}\) |
| AB | \((a-1)(b-1)\) | \(\operatorname{SS}_{\operatorname{AB}}\) | \(\operatorname{MS}_{\operatorname{AB}}\) | \(F_{\operatorname{AB}} = \operatorname{MS}_{\operatorname{AB}}/ \MSerror\) |
| Error | \(ab(n-1)\) | \(\operatorname{SS}_{\operatorname{Error}}\) | \(\MSerror\) | |
| Total | \(abn-1\) | \(\operatorname{SS}_{\operatorname{Tot}}\) |
Note \(F_{\operatorname{A}}\) and \(F_{\operatorname{B}}\) are different from their fixed-effects counterparts!
We again tabulate the expected values of the mean squares:
| Source | Df | Expected mean square |
|---|---|---|
| A | \(a-1\) | \(nb\sigma_A^2 + n \sigma_{AB}^2 + \sigma^2_\varepsilon\) |
| B | \(b-1\) | \(na\sigma_B^2 + n \sigma_{AB}^2 + \sigma^2_\varepsilon\) |
| AB | \((a-1)(b-1)\) | \(n\sigma_{AB}^2 + \sigma_\varepsilon^2\) |
| Error | \(a(n-1)\) | \(\sigma_\varepsilon^2\) |
Discuss: Make F-stat by dividing by the MS having the same expectation under \(H_0\).
Obtain \(p\)-values for testing whether the variance components are zero.
The anova() function on the lm() output only gives the correct F statistic and p-value for the interaction.
Analysis of Variance Table
Response: tg
Df Sum Sq Mean Sq F value Pr(>F)
days 3 1334 445 24.86 2.9e-06 ***
machine 3 1647 549 30.68 7.2e-07 ***
days:machine 9 786 87 4.88 0.0029 **
Residuals 16 286 18
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
From the p-value for \(H_0\): \(\sigma_{AB}^2=0\), we conclude there is interaction between the day and the machine. So machine performance does not appear to be consistent across days.
For \(H_0\): \(\sigma_A^2 = 0\) we obtain a p-value of 0.0248.
For \(H_0\): \(\sigma_B^2 = 0\) we obtain a p-value of 0.0137.
There is significant machine-to-machine and day-to-day variation.
Obtain the REML estimators of \(\sigma_A^2\), \(\sigma_B^2\), \(\sigma_{AB}^2\), and \(\sigma_\varepsilon^2\).
Use summary() on the lmer() output.
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: tg ~ 1 + (1 | days) + (1 | machine) + (1 | days:machine)
REML criterion at convergence: 215
Scaled residuals:
Min 1Q Median 3Q Max
-1.8428 -0.3558 0.0348 0.2070 2.3177
Random effects:
Groups Name Variance Std.Dev.
days:machine (Intercept) 34.7 5.89
machine (Intercept) 57.7 7.60
days (Intercept) 44.7 6.68
Residual 17.9 4.23
Number of obs: 32, groups: days:machine, 16; machine, 4; days, 4
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 141.18 5.32 4.91 26.5 1.7e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
predict() on the lmer() output to obtain these fitted values.Both plots show some problems; interpret our results with caution.