<p>Download the Anova special cases cheat sheet in full resolution:</p>
<p><em>This article is part of Quantide’s web book “<a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-statistical-models-with-r-web-book/">Raccoon – Statistical Models with R</a>“. Raccoon is Quantide’s third web book after “<a target="_blank" rel="nofollow" href="http://www.quantide.com/rabbit-introduction-to-r/">Rabbit – Introduction to R</a>” and “<a target="_blank" rel="nofollow" href="http://www.quantide.com/ramarro-r-for-developers/">Ramarro – R for Developers</a>“. See the <a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-statistical-models-r-free-web-book/">full project here</a>.</em></p>
<p><em>The second chapter of Raccoon is focused on T-test and Anova. Through example it shows theory and R code of:</em></p>
<ul>
<li><a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-ch-2-1-1-sample-t-test-with-r/">1-sample t-test</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-ch-2-2-2-sample-t-test-and-paired-t/">2-sample t-test and Paired t</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-ch-2-3-1-way-anova/">1-way Anova</a></li>
<li>3-way Anova</li>
<li>Anova: unbalance and nested anova</li>
</ul>
<p><em>This post is the fifth section of the chapter, about unbalanced anova with one obs dropped and fixed effects nested anova.</em></p>
<p><em>Throughout the web-book we will widely use the <strong>package qdata</strong>, containing about 80 datasets. You may find it here: <a target="_blank" rel="nofollow" href="https://github.com/quantide/qdata">https://github.com/quantide/qdata</a>.</em></p>
<h2>Example: Brake distance 1 (unbalanced anova with one obs dropped)</h2>
<div>
<h3>Data description</h3>
<p>We use the same data as in 3-way anova, but here one observation (row) has been removed. The detailed data description is in 3-way anova.</p>
</div>
<div>
<h3>Data loading</h3>
</div>
<pre>data(distance)
head(distance)</pre>
<pre>## # A tibble: 6 × 4
##     Tire  Tread      ABS Distance
##   &lt;fctr> &lt;fctr>   &lt;fctr>    &lt;dbl>
## 1     GT     10  enabled 19.35573
## 2     GT    1.5 disabled 23.38069
## 3     MX    1.5  enabled 24.00778
## 4     MX     10  enabled 25.07142
## 5     LS     10 disabled 26.39833
## 6     GT     10  enabled 18.60888</pre>
<pre>str(distance)</pre>
<pre>## Classes 'tbl_df', 'tbl' and 'data.frame':    24 obs. of  4 variables:
##  $ Tire    : Factor w/ 3 levels &quot;GT&quot;,&quot;LS&quot;,&quot;MX&quot;: 1 1 3 3 2 1 2 2 2 3 ...
##  $ Tread   : Factor w/ 2 levels &quot;1.5&quot;,&quot;10&quot;: 2 1 1 2 2 2 1 1 2 2 ...
##  $ ABS     : Factor w/ 2 levels &quot;disabled&quot;,&quot;enabled&quot;: 2 1 2 2 1 2 2 1 1 1 ...
##  $ Distance: num  19.4 23.4 24 25.1 26.4 ...</pre>
<p>Let us drop one observation so that all the factor levels combinations do not contain the same number of observations. We drop the observation with the values “LS 10 enabled”</p>
<pre>distance1 &lt;- distance[-24,]</pre>
<h3>Descriptives</h3>
<p>As usual, we first plot the univariate effects:</p>
<pre>plot.design(Distance ~ ., data = distance1)</pre>
<p><p><img sizes="(max-width: 450px) 100vw, 450px" alt="anova20" class="wp-image-2906" src="https://i2.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova20-300x300.png?resize=450%2C450" data-recalc-dims="1">Univariate effects plot of unbalanced model</p>
<p>Secondly we look at the two-way interaction plot:</p>
<pre>op &lt;- par(mfrow = c(3, 1))
with(distance1, {
  interaction.plot(ABS, Tire, Distance)
  interaction.plot(ABS, Tread, Distance)
  interaction.plot(Tread, Tire, Distance)
  }
)
par(op)</pre>
<p>
<img sizes="(max-width: 450px) 100vw, 450px" alt="anova21" class="wp-image-2907" src="https://i0.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova21-300x300.png?resize=450%2C450" data-recalc-dims="1"></p>
<p>Two-way interaction effects plots of unbalanced model</p>
<p>We notice that all effects do not seem to change with respect to the previous example of 3-way anova.</p>
<h3>Inference and models</h3>
<p>In this section is where we’ll start noticing some differences with the balanced model. First let us fit the model with all interactions</p>
<pre>fm &lt;- aov(Distance ~ ABS * Tire * Tread, data = distance1)
summary(fm)</pre>

<pre>##                Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS             1  81.95   81.95  39.870 5.72e-05 ***
## Tire            2  47.95   23.98  11.665  0.00191 ** 
## Tread           1   0.19    0.19   0.092  0.76771    
## ABS:Tire        2   6.72    3.36   1.635  0.23898    
## ABS:Tread       1   3.26    3.26   1.588  0.23365    
## Tire:Tread      2   3.42    1.71   0.831  0.46107    
## ABS:Tire:Tread  2   4.99    2.50   1.215  0.33360    
## Residuals      11  22.61    2.06                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
As none of the interactions are significant, we drop them one by one, starting from the three-way interaction:</p>
<pre>fm &lt;- update(fm, . ~ . -ABS:Tire:Tread)
summary(fm)</pre>

<pre>##             Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS          1  81.95   81.95  38.594 3.16e-05 ***
## Tire         2  47.95   23.98  11.291  0.00144 ** 
## Tread        1   0.19    0.19   0.089  0.77049    
## ABS:Tire     2   6.72    3.36   1.583  0.24259    
## ABS:Tread    1   3.26    3.26   1.537  0.23691    
## Tire:Tread   2   3.42    1.71   0.805  0.46829    
## Residuals   13  27.60    2.12                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
We continue on by dropping the two way interactions:</p>
<pre>fm1 &lt;- update(fm, .~ABS+Tire+Tread)
summary(fm1)</pre>

<pre>##             Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS          1  81.95   81.95  35.972 1.13e-05 ***
## Tire         2  47.95   23.98  10.524  0.00094 ***
## Tread        1   0.19    0.19   0.083  0.77694    
## Residuals   18  41.01    2.28                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
And we then make sure that the model without the interaction is actually better than that with interactions:</p>
<pre>anova(fm, fm1)</pre>

<pre>## Analysis of Variance Table
## 
## Model 1: Distance ~ ABS + Tire + Tread + ABS:Tire + ABS:Tread + Tire:Tread
## Model 2: Distance ~ ABS + Tire + Tread
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     13 27.603                           
## 2     18 41.005 -5   -13.402 1.2624  0.337</pre>
<p>
<span>As expected, the model with interactions is not significantly better than that without interactions. </span></p>
<p>The final model is hence the same as the model in the previous example of balanced anova. However, notice that the sums of squares of the following two models (that we expect to be equal), are different:</p>
<pre>fm &lt;- aov(Distance ~ ABS + Tire, data = distance1)
summary(fm)</pre>

<pre>##             Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS          1  81.95   81.95   37.80 6.57e-06 ***
## Tire         2  47.95   23.98   11.06 0.000653 ***
## Residuals   19  41.19    2.17                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>

<pre>fminv &lt;- aov(Distance ~ Tire + ABS, data = distance1)
summary(fminv)</pre>

<pre>##             Df Sum Sq Mean Sq F value   Pr(>F)    
## Tire         2  53.09   26.55   12.24 0.000383 ***
## ABS          1  76.80   76.80   35.42 9.95e-06 ***
## Residuals   19  41.19    2.17                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
Since <code>aov()</code> performs Type I SS ANOVA (see <em>Types of Sum of Squares</em> in the Appendices) and this example uses data from unbalanced design, the previous 2 models give different results in terms of SS and respective p-values. Hence  <code>fm</code> is calculated based on SS(ABS) and SS(Tire | ABS), whereas <code>fminv</code> is built  SS(Tire) and SS(ABS | Tire).</p>
<p>In order to get Type II ANOVA SSs and p-values, one can consider SS(Tire | ABS) and SS(ABS | Tire).<br>
Otherwise, <code>drop1()</code> function can also be used.</p>
<pre>drop1(object=fm,test=&quot;F&quot;)</pre>

<pre>## Single term deletions
## 
## Model:
## Distance ~ ABS + Tire
##        Df Sum of Sq     RSS    AIC F value    Pr(>F)    
## &lt;none>               41.194 21.404                      
## ABS     1    76.802 117.996 43.609  35.424 9.949e-06 ***
## Tire    2    47.950  89.144 35.159  11.058 0.0006532 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>

<pre>drop1(object=fminv,test=&quot;F&quot;)</pre>

<pre>## Single term deletions
## 
## Model:
## Distance ~ Tire + ABS
##        Df Sum of Sq     RSS    AIC F value    Pr(>F)    
## &lt;none>               41.194 21.404                      
## Tire    2    47.950  89.144 35.159  11.058 0.0006532 ***
## ABS     1    76.802 117.996 43.609  35.424 9.949e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
In this case, the results are equal. Alternatively, the function <code>Anova()</code> of the package <code>car</code> is available. <code>Anova()</code> allows Type II and III Sum of Squares too.</p>
<p>Notice that, until now, at least six types of sum of squares have been introduced in literature.<br>
However, there are open discussions among statisticians about the use and pros/cons of different Types of SS.</p>
<h3>Residual analysis</h3>
<p>Together with the model results, one should always provide some statistics/plots on the residuals.</p>
<pre>op &lt;- par(mfrow = c(2, 2))
plot(fm)
par(op)</pre>
<p>
<img sizes="(max-width: 450px) 100vw, 450px" alt="anova22" class="wp-image-2908" src="https://i2.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova22-300x300.png?resize=450%2C450" data-recalc-dims="1"></p>
<p>Residual plots of unbalanced model</p>
<p>In this case, since the leverages are not constant (unbalanced design) 4th plot draws the leverages in x-axis.</p>
<p> </p>
<p> </p>
<h2>Example: Pin diameters (Fixed effects Nested ANOVA)</h2>
<h3>Data description</h3>
<p>The dataframe considered in this example contains data collected from five different lathes, each of them used by two different operators. The goal of the study is to evaluate if significant differences in the mean diameter of pins occur between lathes and/or operators. Notice that here we are concerned with the effect of operators, so the layout of experiment is nested. If we were concerned with shift instead of operator, the layout would have been the other way around.</p>
<h3>Data loading</h3>
<pre>data(diameters)
str(diameters)</pre>
<pre>## Classes 'tbl_df', 'tbl' and 'data.frame':    50 obs. of  3 variables:
##  $ Lathe   : Factor w/ 5 levels &quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,..: 1 1 2 2 3 3 4 4 5 5 ...
##  $ Operator: Factor w/ 2 levels &quot;D&quot;,&quot;N&quot;: 1 2 1 2 1 2 1 2 1 2 ...
##  $ Pin.Diam: num  0.125 0.124 0.118 0.116 0.123 0.122 0.126 0.126 0.118 0.125 ...</pre>
<h3></h3>
<h3>Descriptives</h3>
<p>Let us first carry out some descriptive statistics and plots in order to get a glimpse of the data. The next  few lines of code show descriptive statistics for each variable and the mean for each combination of Lathe and Operator factor levels.</p>
<pre>summary(diameters)</pre>

<pre>##  Lathe  Operator    Pin.Diam    
##  1:10   D:25     Min.   :0.114  
##  2:10   N:25     1st Qu.:0.122  
##  3:10            Median :0.125  
##  4:10            Mean   :0.124  
##  5:10            3rd Qu.:0.126  
##                  Max.   :0.130</pre>

<pre>xtabs(formula=Pin.Diam~Lathe+Operator,data=diameters)</pre>

<pre>##      Operator
## Lathe     D     N
##     1 0.631 0.634
##     2 0.603 0.605
##     3 0.623 0.618
##     4 0.636 0.634
##     5 0.615 0.603</pre>
<p>
Above statistics are not completely well-advised, since the operators working in the same part of the day (day or night) are different (nested anova) for different lathes. Let us draw a box-plot for each Late x Operator combination.</p>
<pre>ggp &lt;- ggplot(data = diameters, mapping = aes(x=Lathe, y=Pin.Diam, fill=Operator)) + 
  geom_boxplot()

print(ggp)</pre>
<p>
<img sizes="(max-width: 450px) 100vw, 450px" alt="anova23" class="wp-image-2909" src="https://i2.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova23-300x300.png?resize=450%2C450" data-recalc-dims="1"></p>
<p>Boxplot of Pin.Diam by Lathe x Operator</p>
<p>It may seem natural to perform the following (incorrect) ANOVA to analyze diameters conditional on Lathe and Shift (i.e., considering Operator levels as equivalent to different shifts of working days) as for classical factorial layout.</p>
<pre>fm1 &lt;- aov(formula = Pin.Diam~Lathe*Operator, data = diameters)
summary(fm1)</pre>

<pre>##                Df    Sum Sq   Mean Sq F value   Pr(>F)    
## Lathe           4 0.0003033 7.583e-05   8.766 3.52e-05 ***
## Operator        1 0.0000039 3.920e-06   0.453    0.505    
## Lathe:Operator  4 0.0000147 3.670e-06   0.424    0.790    
## Residuals      40 0.0003460 8.650e-06                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
The above results give however an incorrect model for the data under study. In fact the actual data structure is the following:</p>
<pre>diameters$Lathe_op &lt;- factor(diameters$Lathe:diameters$Operator)
xtabs(formula=Pin.Diam~Lathe+Lathe_op,data=diameters)</pre>

<pre>##      Lathe_op
## Lathe   1:D   1:N   2:D   2:N   3:D   3:N   4:D   4:N   5:D   5:N
##     1 0.631 0.634 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
##     2 0.000 0.000 0.603 0.605 0.000 0.000 0.000 0.000 0.000 0.000
##     3 0.000 0.000 0.000 0.000 0.623 0.618 0.000 0.000 0.000 0.000
##     4 0.000 0.000 0.000 0.000 0.000 0.000 0.636 0.634 0.000 0.000
##     5 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.615 0.603</pre>
<p>
The correct ANOVA to perform is thus the following:</p>
<pre>fm1 &lt;- aov(formula=Pin.Diam~Lathe+Lathe/Operator,data=diameters)
summary(fm1)</pre>

<pre>##                Df    Sum Sq   Mean Sq F value   Pr(>F)    
## Lathe           4 0.0003033 7.583e-05   8.766 3.52e-05 ***
## Lathe:Operator  5 0.0000186 3.720e-06   0.430    0.825    
## Residuals      40 0.0003460 8.650e-06                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
The / formula operator means that the levels of Operator factor are nested within the levels of Lathe factor. If we read the output we find that lathes seem to produce on average different products, whereas the difference between the means of the pins’ diameter conditional on the operator does not seem to be significant. Although the final results of the two anovas (the first of which is incorrect and the second one correct!) may seem similar nested anova is the one to use because there is a control over the variability given by the (fixed) effect of the operators. Nested anova is hence useful for reducing the general variability of the plan and getting more significant differences among the levels of the factors.</p>
<p>An equivalent model formulation for nested ANOVA is given by:</p>
<pre>fm1a &lt;- aov(formula=Pin.Diam~Lathe+Operator:Lathe,data=diameters)
summary(fm1a)</pre>

<pre>##                Df    Sum Sq   Mean Sq F value   Pr(>F)    
## Lathe           4 0.0003033 7.583e-05   8.766 3.52e-05 ***
## Lathe:Operator  5 0.0000186 3.720e-06   0.430    0.825    
## Residuals      40 0.0003460 8.650e-06                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
or</p>
<pre>#alternative correct model
fm1b &lt;- aov(formula=Pin.Diam~Lathe+Operator:Lathe_op,data=diameters)
summary(fm1b)</pre>

<pre>##                   Df    Sum Sq   Mean Sq F value   Pr(>F)    
## Lathe              4 0.0003033 7.583e-05   8.766 3.52e-05 ***
## Operator:Lathe_op  5 0.0000186 3.720e-06   0.430    0.825    
## Residuals         40 0.0003460 8.650e-06                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p><h3>Residuals analysis</h3>
<p>Finally, residuals may be plotted for model’s diagnostics:</p>
<pre>op &lt;-  par(mfrow = c(2, 2))
plot(fm1)
par(op)</pre>
<p>
<img sizes="(max-width: 450px) 100vw, 450px" alt="anova24" class="wp-image-2910" src="https://i2.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova24-300x300.png?resize=450%2C450" data-recalc-dims="1"></p>
<p>Residual plot of Late by Operator nested model</p>
<h2></h2>
<p>The post <a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-ch-2-5-unbalanced-anova-one-obs-dropped/">Raccoon | Ch 2.5 – Unbalanced Anova with one obs dropped</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.quantide.com/">Quantide – R training &amp; consulting</a>.</p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/CQg01POfeoQ">