STAT 516 Lec 07

Unbalanced two-way factorial design

Karl Gregory

2026-10-26

Mice example

Change in mRNA expression of esr1 in wild-type vs gen-modified mice on low- and high-fat diets.

Data from Trey Hope in Dr. Enos’ lab in the USC School of Medicine.

Does the diet affect the response? The genetic modification? Is the effect of the diet the same in the wild-type and the gen-modified mice?

Mice

link <- url("https://people.stat.sc.edu/gregorkb/data/ADIPOER.csv")
adipoer <- read.csv(link)
adipoer
   Response  Gene Diet
1       493    WT  LFD
2       172    WT  LFD
3       617    WT  LFD
4       534    WT  LFD
5       500    WT  LFD
6       127    WT  LFD
7      1224    WT  LFD
8       143    WT  LFD
9       852    WT  LFD
10      134    WT  LFD
11      548    WT  LFD
12      555 ADIPO  LFD
13      302 ADIPO  LFD
14      434 ADIPO  LFD
15       90 ADIPO  LFD
16       72 ADIPO  LFD
17      489 ADIPO  LFD
18      453 ADIPO  LFD
19      552 ADIPO  LFD
20      636 ADIPO  LFD
21      331 ADIPO  LFD
22      170 ADIPO  LFD
23      738 ADIPO  LFD
24      596    WT  HFD
25      418    WT  HFD
26      642    WT  HFD
27      744    WT  HFD
28     1351    WT  HFD
29     1180    WT  HFD
30      938    WT  HFD
31      670    WT  HFD
32     1319    WT  HFD
33     1007    WT  HFD
34      589    WT  HFD
35      482    WT  HFD
36      785    WT  HFD
37     1060    WT  HFD
38      435    WT  HFD
39      536    WT  HFD
40     1153 ADIPO  HFD
41      890 ADIPO  HFD
42      539 ADIPO  HFD
43      652 ADIPO  HFD
44      585 ADIPO  HFD
45      784 ADIPO  HFD
46      534 ADIPO  HFD
47      895 ADIPO  HFD
48      579 ADIPO  HFD
49      551 ADIPO  HFD
50      800 ADIPO  HFD
51     1026 ADIPO  HFD
52      308 ADIPO  HFD
53     1415 ADIPO  HFD
54      342 ADIPO  HFD
55      198 ADIPO  HFD

Mice

boxplot(Response ~ Diet + Gene, data = adipoer)

Mice

stripchart(Response ~ Diet + Gene, data = adipoer, vertical = T,
           method = "jitter", xlab = "Diet.Gene")

Mice

grp_means <- aggregate(Response ~ Diet + Gene, data = adipoer, mean)
grp_means
  Diet  Gene Response
1  HFD ADIPO      703
2  LFD ADIPO      402
3  HFD    WT      797
4  LFD    WT      486


grp_counts <- aggregate(Response ~ Diet + Gene, data = adipoer, length)
grp_counts
  Diet  Gene Response
1  HFD ADIPO       16
2  LFD ADIPO       12
3  HFD    WT       16
4  LFD    WT       11

Two-way treatment effects model

The two-way treatment effects model assumes \[ Y_{ijk} = \mu + \tau_i + \gamma_j + (\tau\gamma)_{ij} + \varepsilon_{ijk} \] for \(i = 1,\dots,a\), \(j = 1,\dots,b\), and \(k = 1,\dots,n_{ij}\), where

  • \(Y_{ijk}\) is the response for EU \(k\) under level \(i\) of A and level \(j\) of B.
  • \(\mu\) represents a baseline or overall mean.
  • The \(\tau_i\) are the main effects for Factor A.
  • The \(\gamma_j\) are the main effects for Factor B.
  • The \((\tau\gamma)_{ij}\) are the interaction effects between A and B.
  • The \(\varepsilon_{ijk}\) are \(\mathcal{N}(0,\sigma^2)\) error terms.

Now suppose \(n_{ij}\) are not all the same, so the design is unbalanced.

Mice

In the mice example \(a = b = 2\). We want parameter estimates \(\hat \mu\), \(\hat \tau_i\), \(\hat \gamma_j\), and \(\hat{(\tau\gamma)}_{ij}\) which satisfy

\[\begin{align*} \hat \mu + \hat \tau_1 + \hat \gamma_1 + \hat{(\tau\gamma)}_{11} &= \bar Y_{11.}\\ \hat \mu + \hat \tau_1 + \hat \gamma_2 + \hat{(\tau\gamma)}_{12} &= \bar Y_{12.}\\ \hat \mu + \hat \tau_2 + \hat \gamma_1 + \hat{(\tau\gamma)}_{21} &= \bar Y_{21.}\\ \hat \mu + \hat \tau_2 + \hat \gamma_2 + \hat{(\tau\gamma)}_{22} &= \bar Y_{22.} \end{align*}\]

We have \(ab = 4\) equations and \(a + b + ab + 1 = 9\) unknowns.

Impose (as in balanced case) a unique solution by setting \[ \hat \tau_1 = 0, \quad \hat \gamma_1 = 0, \quad \hat{(\tau\gamma)_{11}} = \hat{(\tau\gamma)_{12}} = \hat{(\tau\gamma)}_{21} = 0. \]

Mice

Then we obtain the system of equations:

\[\begin{align*} \hat \mu &= \bar Y_{11.}\\ \hat \mu + \hat \gamma_2 &= \bar Y_{12.}\\ \hat \mu + \hat \tau_2 &= \bar Y_{21.}\\ \hat \mu + \hat \tau_2 + \hat \gamma_2 + \hat{(\tau\gamma)}_{22} &= \bar Y_{22.} \end{align*}\]

We can solve this uniquely to obtain:

\[\begin{align*} \hat \mu &= \bar Y_{11.}\\ \hat \gamma_2 &= \bar Y_{12.} - \bar Y_{11.}\\ \hat \tau_2 &= \bar Y_{21.} - \bar Y_{11.}\\ \hat{(\tau\gamma)}_{22} &= \bar Y_{22.} - \bar Y_{11.} - (\bar Y_{12.} - \bar Y_{11.}) - (\bar Y_{21.} - \bar Y_{11.}) \end{align*}\]

Mice

Construct the treatment group means from the parameter estimates:

lm_out <- lm(Response ~ Diet + Gene + Diet:Gene, data = adipoer)
lm_out

Call:
lm(formula = Response ~ Diet + Gene + Diet:Gene, data = adipoer)

Coefficients:
   (Intercept)         DietLFD          GeneWT  DietLFD:GeneWT  
        703.12         -301.38           93.81           -9.74  


grp_means
  Diet  Gene Response
1  HFD ADIPO      703
2  LFD ADIPO      402
3  HFD    WT      797
4  LFD    WT      486

Estimating the error term variance

An unbiased estimator of the error term variance \(\sigma^2\) is given by

\[ \hat \sigma^2 = \frac{1}{N - ab} \sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^{n_{ij}}(Y_{ijk} - \bar Y_{ij.})^2, \] where \(N = \sum_{i=1}^a\sum_{j=1}^b n_{ij}\).

Mice

The square root of the variance estimate is printed in this output:

summary(lm_out)

Call:
lm(formula = Response ~ Diet + Gene + Diet:Gene, data = adipoer)

Residuals:
   Min     1Q Median     3Q    Max 
-505.6 -220.1  -11.9  170.3  738.2 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
(Intercept)      703.12      75.52    9.31  1.4e-12 ***
DietLFD         -301.38     115.35   -2.61    0.012 *  
GeneWT            93.81     106.80    0.88    0.384    
DietLFD:GeneWT    -9.74     165.24   -0.06    0.953    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 302 on 51 degrees of freedom
Multiple R-squared:  0.229, Adjusted R-squared:  0.184 
F-statistic: 5.05 on 3 and 51 DF,  p-value: 0.00388

Sums of squares in the unbalanced design

Sum of squares Symbol Formula
Total \(\operatorname{SS}_{\operatorname{Tot}}\) \(\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^{n_{ij}}(Y_{ijk} - \bar Y_{...})^2\)
Treatment \(\operatorname{SS}_{\operatorname{Trt}}\) \(\sum_{i=1}^a \sum_{j=1}^b n_{ij}(\bar Y_{ij.} - \bar Y_{...})^2\)
Error \(\operatorname{SS}_{\operatorname{Error}}\) \(\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^{n_{ij}}(Y_{ijk} - \bar Y_{ij.})^2\)

We have the decomposition \(\operatorname{SS}_{\operatorname{Tot}}= \operatorname{SS}_{\operatorname{Trt}}+ \operatorname{SS}_{\operatorname{Error}}\).

The Analysis of Variance (ANOVA) table

Obtain the MS values by dividing the SS values by the Df values.

Source Df SS MS F value
Treatments \(ab-1\) \(\operatorname{SS}_{\operatorname{Trt}}\) \(\operatorname{MS}_{\operatorname{Trt}}\) \(F_{\operatorname{stat}}= \operatorname{MS}_{\operatorname{Trt}}/ \operatorname{MS}_{\operatorname{Error}}\)
Error \(N - ab\) \(\operatorname{SS}_{\operatorname{Error}}\) \(\operatorname{MS}_{\operatorname{Error}}\)
Total \(N-1\) \(\operatorname{SS}_{\operatorname{Tot}}\)

Reject \(H_0\): \(\mu_{ij}\) all the same if \(F_{\operatorname{stat}}> F_{ab-1,N-ab,\alpha}\).

In the unbalanced case we have \(N = \sum_{i=1}^a\sum_{j=1}^b n_{ij}\) in place of \(abn\).

Note that \(\hat \sigma^2 = \operatorname{MS}_{\operatorname{Error}}\).

Main effect sums of squares under unbalancedness

  • In the balanced case, we decomposed \(\operatorname{SS}_{\operatorname{Trt}}\) as \(\operatorname{SS}_{\operatorname{A}}+ \operatorname{SS}_{\operatorname{B}}+ \operatorname{SS}_{\operatorname{AB}}\).

  • No such “clean” decomposition of \(\operatorname{SS}_{\operatorname{Trt}}\) under unbalancedness.

  • Specifically, it is not obvious how to define \(\operatorname{SS}_{\operatorname{A}}\) and \(\operatorname{SS}_{\operatorname{B}}\).

  • Three versions of \(\operatorname{SS}_{\operatorname{A}}\) and \(\operatorname{SS}_{\operatorname{B}}\) measure main effect importance:

    1. Sequentially
    2. After other main effect in the absence of interaction
    3. After other main effect in the presence of interaction

    These are called Type I, Type II, and Type III sums of squares.

Type I/II/III SS for main effects

Each is a full/reduced model difference in SS.

To define them, we introduce the notation: Let

  • \(\operatorname{SS}_{\operatorname{Error}}(A)\) denote \(\operatorname{SS}_{\operatorname{Error}}\) from model with only A main effects.
  • \(\operatorname{SS}_{\operatorname{Error}}(B)\) denote \(\operatorname{SS}_{\operatorname{Error}}\) from model with only B main effects.
  • \(\operatorname{SS}_{\operatorname{Error}}(A,B)\) denote \(\operatorname{SS}_{\operatorname{Error}}\) from model with A,B main effects .
  • \(\operatorname{SS}_{\operatorname{Error}}(A,B,AB)\) denote \(\operatorname{SS}_{\operatorname{Error}}\) from full model.

Note that \(\operatorname{SS}_{\operatorname{Tot}}\) is \(\operatorname{SS}_{\operatorname{Error}}\) from the model with no effects.

Type I SS

Type I SS values are meaningless for unbalanced designs!

The Type I sums of squares \(\operatorname{SS}_{\operatorname{A}}^{\operatorname{I}}\) and \(\operatorname{SS}_{\operatorname{B}}^{\operatorname{I}}\) are defined as follows:

  • If \(A\) enters first and then \(B\), then \[\begin{align*} \operatorname{SS}_{\operatorname{A}}^{\operatorname{I}} &= \operatorname{SS}_{\operatorname{Tot}}- \operatorname{SS}_{\operatorname{Error}}(A) \\ \operatorname{SS}_{\operatorname{B}}^{\operatorname{I}} &= \operatorname{SS}_{\operatorname{Error}}(A) - \operatorname{SS}_{\operatorname{Error}}(A,B) \end{align*}\]

  • If \(B\) enters first and then \(A\), then \[\begin{align*} \operatorname{SS}_{\operatorname{B}}^{\operatorname{I}} &= \operatorname{SS}_{\operatorname{Tot}}- \operatorname{SS}_{\operatorname{Error}}(B) \\ \operatorname{SS}_{\operatorname{A}}^{\operatorname{I}} &= \operatorname{SS}_{\operatorname{Error}}(B) - \operatorname{SS}_{\operatorname{Error}}(A,B) \end{align*}\]

When the design is balanced the order of entrance does not matter.

Mice

Obtain the Type I SS when Gene is added and then Diet.

Fit model with no effects:

lm_none <- lm(Response ~ 1, data = adipoer)
SST <- sum(lm_none$residuals^2)

Fit model with just Gene:

lm_Gene <- lm(Response ~ Gene, data = adipoer)
SSE_Gene <- sum(lm_Gene$residuals^2)
SS_Gene <- SST - SSE_Gene # take difference in SSE

The total SS is reduced by \(1.273\times 10^{5}\).

Fit model with Gene and Diet:

lm_Gene_Diet <- lm(Response ~ Gene + Diet, data = adipoer)
SSE_Gene_Diet <- sum(lm_Gene_Diet$residuals^2)
SS_Diet <- SSE_Gene - SSE_Gene_Diet # take difference in SSE

The error SS is reduced by \(1.253\times 10^{6}\).

Mice

Obtain the Type I SS when Diet is added and then Gene.

Fit model with no effects:

lm_none <- lm(Response ~ 1, data = adipoer)
SST <- sum(lm_none$residuals^2)

Fit model with just Diet:

lm_Diet <- lm(Response ~ Diet, data = adipoer)
SSE_Diet <- sum(lm_Diet$residuals^2)
SS_Diet <- SST - SSE_Diet # take difference in SSE

The total SS is reduced by \(1.27\times 10^{6}\).

Fit model with Diet and Gene:

lm_Gene_Diet <- lm(Response ~ Gene + Diet, data = adipoer)
SSE_Gene_Diet <- sum(lm_Gene_Diet$residuals^2)
SS_Gene <- SSE_Diet - SSE_Gene_Diet # take difference in SSE

The error SS is reduced by \(1.107\times 10^{5}\).

Mice

Obtain the Type I SS on the mice data using anova() function.

anova(lm(Response ~ Gene + Diet + Gene:Diet, data = adipoer))
Analysis of Variance Table

Response: Response
          Df  Sum Sq Mean Sq F value  Pr(>F)    
Gene       1  127271  127271    1.39 0.24306    
Diet       1 1253492 1253492   13.74 0.00052 ***
Gene:Diet  1     317     317    0.00 0.95325    
Residuals 51 4653342   91242                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


anova(lm(Response ~ Diet + Gene + Diet:Gene, data = adipoer))
Analysis of Variance Table

Response: Response
          Df  Sum Sq Mean Sq F value  Pr(>F)    
Diet       1 1270105 1270105   13.92 0.00048 ***
Gene       1  110659  110659    1.21 0.27595    
Diet:Gene  1     317     317    0.00 0.95325    
Residuals 51 4653342   91242                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Type II SS

  • Measures contribution of each main effect in absence of interaction.

  • The Type II sums of squares \(\operatorname{SS}_{\operatorname{A}}^{\operatorname{II}}\) and \(\operatorname{SS}_{\operatorname{B}}^{\operatorname{II}}\) are given by \[\begin{align*} \operatorname{SS}_{\operatorname{A}}^{\operatorname{II}} &= \operatorname{SS}_{\operatorname{Error}}(B) - \operatorname{SS}_{\operatorname{Error}}(A,B)\\ \operatorname{SS}_{\operatorname{B}}^{\operatorname{II}} &= \operatorname{SS}_{\operatorname{Error}}(A) - \operatorname{SS}_{\operatorname{Error}}(A,B) \end{align*}\]

Mice

Obtain the Type II SS on the mice data.

Fit a one-way model for each factor:

lm_Diet <- lm(Response ~ Diet, data = adipoer)
SSE_Diet <- sum(lm_Diet$residuals^2)
lm_Gene <- lm(Response ~ Gene, data = adipoer)
SSE_Gene <- sum(lm_Gene$residuals^2)


Fit a model with both main effects (but no interaction):

lm_Diet_Gene <- lm(Response ~ Diet + Gene, data = adipoer)
SSE_Diet_Gene <- sum(lm_Diet_Gene$residuals^2)
SS_Diet <- SSE_Gene - SSE_Diet_Gene # take difference in SSE
SS_Gene <- SSE_Diet - SSE_Diet_Gene # take difference in SSE

Including Diet after Gene reduces error SS by \(1.253\times 10^{6}\).

Including Gene after Diet reduces error SS by \(1.107\times 10^{5}\).

Mice

Obtain Type II SS with Anova() function from R package car.

library(car)
Anova(lm(Response ~ Diet + Gene + Diet:Gene, data = adipoer),type = "II")
Anova Table (Type II tests)

Response: Response
           Sum Sq Df F value  Pr(>F)    
Diet      1253492  1   13.74 0.00052 ***
Gene       110659  1    1.21 0.27595    
Diet:Gene     317  1    0.00 0.95325    
Residuals 4653342 51                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Type III SS

  • In the unbalanced case we should use the so-called Type III SS.

  • Measures contribution of each main effect in presence of interaction.

  • The Type III sums of squares \(\operatorname{SS}_{\operatorname{A}}^{\operatorname{III}}\) and \(\operatorname{SS}_{\operatorname{B}}^{\operatorname{III}}\) are given by \[\begin{align*} \operatorname{SS}_{\operatorname{A}}^{\operatorname{III}} &= \operatorname{SS}_{\operatorname{Error}}(B,AB) - \operatorname{SS}_{\operatorname{Error}}(A,B,AB)\\ \operatorname{SS}_{\operatorname{B}}^{\operatorname{III}} &= \operatorname{SS}_{\operatorname{Error}}(A,AB) - \operatorname{SS}_{\operatorname{Error}}(A,B,AB) \end{align*}\]

Type III SS (cont)

  • Type III SS are tedious to obtain in the unbalanced case; they are like full-reduced model differences in the error sum of squares, where the reduced model is the model without the main effect in question.

  • The anova() or aov() functions in R give sequential SS, which are not meaningful in the unbalanced case; however, the Type III SS are equal to the sequential (i.e. Type I) SS when \(n_{ij} = n ~\forall ij\).

  • Under unbalancedness \(\operatorname{SS}_{\operatorname{Trt}}\neq \operatorname{SS}_{\operatorname{A}}^{\operatorname{III}} + \operatorname{SS}_{\operatorname{B}}^{\operatorname{III}} + \operatorname{SS}_{\operatorname{AB}}\).

Type III SS for unbalanced designs

ANOVA table with Type III SS and MS for A and B:

Source Df SS MS F value
A \(a-1\) \(\operatorname{SS}_{\operatorname{A}}^{\operatorname{III}}\) \(\operatorname{MS}_{\operatorname{A}}^{\operatorname{III}}\) \(F_{\operatorname{A}} = \operatorname{MS}_{\operatorname{A}}^{\operatorname{III}} / \operatorname{MS}_{\operatorname{Error}}\)
B \(b-1\) \(\operatorname{SS}_{\operatorname{B}}^{\operatorname{III}}\) \(\operatorname{MS}_{\operatorname{B}}^{\operatorname{III}}\) \(F_{\operatorname{B}} = \operatorname{MS}_{\operatorname{B}}^{\operatorname{III}}/\operatorname{MS}_{\operatorname{Error}}\)
AB \((a-1)(b-1)\) \(\operatorname{SS}_{\operatorname{AB}}\) \(\operatorname{MS}_{\operatorname{AB}}\) \(F_{\operatorname{AB}} = \operatorname{MS}_{\operatorname{AB}}/ \operatorname{MS}_{\operatorname{Error}}\)
Error \(N-ab\) \(\operatorname{SS}_{\operatorname{Error}}\) \(\operatorname{MS}_{\operatorname{Error}}\)
Total \(N-1\) \(\operatorname{SS}_{\operatorname{Tot}}\)
  1. Reject \(H_0\): \(\bar \mu_{1.} = \dots = \bar \mu_{a.}\) if \(F_{\operatorname{A}} > F_{a-1,N-ab,\alpha}\).
  2. Reject \(H_0\): \(\bar \mu_{.1} = \dots = \bar \mu_{.b}\) if \(F_{\operatorname{B}} > F_{b-1,N-ab,\alpha}\).
  3. Reject \(H_0\): \(\mu_{ij} = \bar \mu_{i.} + \bar \mu_{.j} - \bar\mu_{..} \quad \forall ij \quad\) if \(F_{\operatorname{AB}} > F_{(a-1)(b-1),N-ab,\alpha}\).

Mice

Obtain the Type III SS on the mice data.

Fit model with both main effects:

lm_full <- lm(Response ~ Diet + Gene + Diet:Gene, 
              contrasts = list(Gene = contr.sum,Diet = contr.sum),
              data = adipoer) # must specify constraint type!
SSE_full <- sum(lm_full$residuals^2)

Drop main effect of Gene from the model:

drop1(lm_full,~Gene,test="F",data = adipoer)
Single term deletions

Model:
Response ~ Diet + Gene + Diet:Gene
       Df Sum of Sq     RSS AIC F value Pr(>F)
<none>              4653342 632               
Gene    1    105749 4759091 631    1.16   0.29

Drop main effect of Diet from the model:

drop1(lm_full,~Diet,test="F",data = adipoer)
Single term deletions

Model:
Response ~ Diet + Gene + Diet:Gene
       Df Sum of Sq     RSS AIC F value  Pr(>F)    
<none>              4653342 632                    
Diet    1   1253698 5907040 643    13.7 0.00052 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Mice

Obtain Type III SS with Anova() from the R package car.

Important to specify all the options exactly!

library(car)
Anova(lm(Response ~ Gene + Diet + Gene:Diet,
         contrasts = list(Gene = contr.sum, Diet = contr.sum),# specify constraint
         data = adipoer),
      type = "III")
Anova Table (Type III tests)

Response: Response
              Sum Sq Df F value  Pr(>F)    
(Intercept) 19050576  1  208.79 < 2e-16 ***
Gene          105749  1    1.16 0.28674    
Diet         1253698  1   13.74 0.00052 ***
Gene:Diet        317  1    0.00 0.95325    
Residuals    4653342 51                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Since interaction is not significant, we can focus on main effects

Mice

interaction.plot(adipoer$Diet, adipoer$Gene, adipoer$Response)

Estimates of cell and marginal means in unbalanced case

The estimators of the cell and marginal means are given by

  • \(\hat \mu_{ij} = \bar Y_{ij.}\), \(i = 1,\dots,a\), \(j = 1,\dots,b\).
  • \(\hat{\bar \mu}_{i.} = \frac{1}{b}\sum_{j=1}^b \hat \mu_{ij}\), \(i = 1,\dots,a\).
  • \(\hat{\bar \mu}_{.j} = \frac{1}{a}\sum_{i=1}^a \hat \mu_{ij}\), \(j = 1,\dots,b\).

We estimate \(\hat{\bar \mu}_{i.}\) with \(\bar Y_{i..}\) (and \(\hat{\bar \mu}_{.j}\) with \(\bar Y_{.j.}\)) only when \(n_{ij} =n~\forall ij\).

Exercise: Write \(\hat{\bar \mu}_{i.}\) in terms of \(Y_{ijk}\) and find \(\operatorname{Var}\hat{\bar \mu}_{i.}\).

Some CI formulas (without familywise adjustment)

Under unbalancedness our CI formulas become more complicated.

Target \((1-\alpha)100\%\) confidence interval
\(\mu_{ij}\) \(\hat \mu_{ij} \pm t_{N-ab,\alpha/2}\hat \sigma \sqrt{\frac{1}{n_{ij}}}\)
\(\mu_{ij} - \mu_{i'j'}\) \(\hat \mu_{ij} - \hat \mu_{i'j'} \pm t_{N-ab,\alpha/2}\hat \sigma \sqrt{\frac{1}{n_{ij}} + \frac{1}{n_{i'j'}}}\)
\(\bar \mu_{i.}\) \(\hat{\bar \mu}_{i.} \pm t_{N-ab,\alpha/2}\hat \sigma\sqrt{\frac{1}{b^2}\sum_{j=1}^b\frac{1}{n_{ij}}}\)
\(\bar \mu_{.j}\) \(\hat{\bar \mu}_{.j} \pm t_{N-ab,\alpha/2}\hat \sigma\sqrt{\frac{1}{a^2}\sum_{i=1}^a\frac{1}{n_{ij}}}\)
\(\bar \mu_{i.} - \bar \mu_{i'.}\) \(\hat{\bar \mu}_{i.} - \hat{\bar \mu}_{i'.}\pm t_{N - ab,\alpha/2}\hat \sigma \sqrt{\frac{1}{b^2}\sum_{j=1}^b(\frac{1}{n_{ij}} +\frac{1}{n_{i'j}})}\)
\(\bar \mu_{.j} - \bar \mu_{.j'}\) \(\hat{\bar \mu}_{.j} - \hat{\bar \mu}_{.j'} \pm t_{N - ab,\alpha/2}\hat \sigma \sqrt{\frac{1}{a^2}\sum_{i=1}^a(\frac{1}{n_{ij}} +\frac{1}{n_{i'j}})}\)

In the above \(\hat \sigma = \sqrt{\operatorname{MS}_{\operatorname{Error}}}\).

Mice

The interaction appears to be negligible, so we can focus on main effects.

Build \(95\%\) CIs for the differences in marginal means for

  • Diet: \(\hat{\bar \mu}_{1.} - \hat{\bar \mu}_{2.} \pm t_{55-4,0.05/2} \hat \sigma \sqrt{\frac{1}{2^2}\sum_{j=1}^2(\frac{1}{n_{1j}} + \frac{1}{n_{2j}})}\)
  • Gene: \(\hat{\bar \mu}_{.1} - \hat{\bar \mu}_{.2} \pm t_{55-4,0.05/2} \hat \sigma \sqrt{\frac{1}{2^2}\sum_{i=1}^2(\frac{1}{n_{i1}} + \frac{1}{n_{i2}})}\),

where \(\hat \sigma = \sqrt{\operatorname{MS}_{\operatorname{Error}}}\).

Mice

mu_hat <- grp_means$Response
nn <- grp_counts$Response
mu11_hat <- mu_hat[1] ; n11 <- nn[1] # HFD x ADIPO
mu21_hat <- mu_hat[2] ; n21 <- nn[2] # LFD x ADIPO
mu12_hat <- mu_hat[3] ; n12 <- nn[3] # HFD x WT
mu22_hat <- mu_hat[4] ; n22 <- nn[4] # LFD x WT

mu1.hat <- (mu11_hat + mu12_hat)/2 # HFD mean
mu2.hat <- (mu21_hat + mu22_hat)/2 # LFD mean
mu.1hat <- (mu11_hat + mu21_hat)/2 # ADIPO mean
mu.2hat <- (mu12_hat + mu22_hat)/2 # WT mean

N <- sum(nn)
a <- 2
b <- 2
MSE <- sum(lm_out$residuals^2)/(N - a*b) 
alpha <- 0.05

me <- qt(1-alpha/2,N-a*b)*sqrt(MSE)*sqrt(1/2^2*(1/n11 + 1/n12 + 1/n11 + 1/n22))

ci_diet <- c(mu1.hat - mu2.hat - me, mu1.hat - mu2.hat + me)
ci_gene <- c(mu.1hat - mu.2hat - me, mu.1hat - mu.2hat + me)

We obtain the intervals:

  • Diet: \([146.27,466.24]\)
  • Gene: \([-248.93,71.04]\)

Tensile strength data (unbalanced) from Kuehl (2000)

y <- c(106,108,93,101,98,56,107,110,116,63,60,40,41,44)
agg <- as.factor(c("b","b","b","b","b","b","s","s","s","s","s","s","s","s"))
comp <- as.factor(c("r","r","l","l","l","vl","r","r","r","l","l","vl","vl","vl"))
tensile <- data.frame(y = y, agg = agg, comp = comp)

Tensile strength (unbalanced)

Compute group means:

aggregate(y ~ agg + comp,data = tensile, mean)
  agg comp     y
1   b    l  97.3
2   s    l  61.5
3   b    r 107.0
4   s    r 111.0
5   b   vl  56.0
6   s   vl  41.7

Tensile strength (unbalanced)

Again sequential SS values depend on the order we put variables in:

anova(lm(y ~ agg + comp + agg:comp, data = tensile))
Analysis of Variance Table

Response: y
          Df Sum Sq Mean Sq F value  Pr(>F)    
agg        1   1518    1518   135.2 2.7e-06 ***
comp       2   8402    4201   374.1 1.3e-08 ***
agg:comp   2    953     477    42.5 5.5e-05 ***
Residuals  8     90      11                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


anova(lm(y ~ comp + agg + agg:comp, data = tensile))
Analysis of Variance Table

Response: y
          Df Sum Sq Mean Sq F value  Pr(>F)    
comp       2   9159    4580   407.8 8.9e-09 ***
agg        1    761     761    67.7 3.6e-05 ***
comp:agg   2    953     477    42.5 5.5e-05 ***
Residuals  8     90      11                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Tensile strength (unbalanced)

Obtain Type III SS with Anova() from the R package car.

library(car)
Anova(lm(y ~ agg + comp + agg:comp, data = tensile,
         contrasts = list(agg = contr.sum, comp = contr.sum)),
      type = "III")
Anova Table (Type III tests)

Response: y
            Sum Sq Df F value  Pr(>F)    
(Intercept)  75050  1  6683.5 5.6e-13 ***
agg            710  1    63.3 4.6e-05 ***
comp          6806  2   303.1 2.9e-08 ***
agg:comp       953  2    42.5 5.5e-05 ***
Residuals       90  8                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Now we could make comparisons of means, noting the interaction.

References

Kuehl, R. O. 2000. Design of Experiments: Statistical Principles of Research Design and Analysis. Duxbury/Thomson Learning.