<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/raccoon-ch-2-4-3-way-anova/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.quantide.com/raccoon-ch-2-4-3-way-anova/"> R blog | Quantide - R training &amp; consulting</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="3-way Anova with R" class="wp-image-3516" src="https://i0.wp.com/www.quantide.com/wp-content/uploads/2017/02/3-way-Anova-with-R-e1485961827467.png?resize=456%2C255" data-recalc-dims="1"></p>
<p>Download the 3-way Anova cheat sheet in full resolution: <a target="_blank" rel="nofollow" href="http://www.quantide.com/wp-content/uploads/2017/02/Three-way-Anova-with-R.pdf">3-way Anova with R cheat sheet</a></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>Unbalanced Anova with one obs dropped</li>
<li>Fixed effects Nested Anova</li>
</ul>
<p><em>This post is the fourth section of the chapter, about 3-way 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>
<p> </p>
<h2>Example: Braking distances (3-way ANOVA)</h2>
<h3>Data description</h3>
<p>Distance data contain measurements of braking distances on the same car equipped with several configurations of:</p>
<ul>
<li><code>Tire</code>: Factor with 3 levels <code>GT</code>, <code>LS</code>, <code>MX</code></li>
<li><code>Tread</code>: Factor with 2 levels <code>1.5</code>, <code>10</code></li>
<li><code>ABS</code>: Factor with levels <code>disabled</code>,<code>enabled</code></li>
</ul>
<div>
<p>For each combination of levels of the above three factors, 2 measurements of brake distance have been registered.</p>
<p>The objective of the experiment is to find which factor(s) influence the brake distance, and in which direction.</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>
<h3>Descriptives</h3>
<p>Let us first get a graphical insight of the data. We want to graphically explore how the average braking distance changes given specific levels of the factors considered. Thus we first plot the univariate effects.</p>
<pre>plot.design(Distance ~ ., data = distance)</pre>
<p>
<img sizes="(max-width: 450px) 100vw, 450px" alt="anova17" class="wp-image-2903" src="https://i1.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova17-300x300.png?resize=450%2C450" data-recalc-dims="1"></p>
<p>Plot of univariate factors effects on Distance response variable</p>
<p>The mean of the braking distance seems to change mainly with <code>Tire</code> type and <code>ABS</code> levels, while different levels of <code>Tread</code> seem to not influence the mean braking distance.</p>
<p>It may be interesting however to see whether different combinations of the levels of the factors differently affect the average braking distance. We could for example see whether a specific type of tire, combined with a specific level of tread affect the braking distance. Thus, let us plot the two-way interactions.</p>
<pre>op &lt;- par(mfrow = c(3, 1))
with(distance, {
  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="anova18" class="wp-image-2904" src="https://i0.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova18-300x300.png?resize=450%2C450" data-recalc-dims="1"></p>
<p>Plots of two-way interaction effects of factors on Distance response variable</p>
<p>The braking distance seems to decrease when ABS is enabled as compared to ABS disabled independently of the tire type (no interaction). However, there may be an interaction between <code>Tread</code> and <code>ABS</code>, that means that ABS might affect the braking distance differently based on whether it is combined with a specific type of tire.</p>
<p>So far we have commented on plots based on descriptive statistics only. In order to say whether the differences between the several means plotted are significant or not, we need to insert these factors in a model and run tests on each factor/interaction included in the model.</p>
<h3>Inference and models of 3-way Anova</h3>
<p>Let’s build a full model, that is the model with the three factors considered, the two-way interactions between all factors and the three-way interaction:</p>
<pre>fm &lt;- aov(Distance ~ ABS * Tire * Tread, data = distance)</pre>
<p>
Notice that this is equivalent to writing:</p>
<pre>fm &lt;- aov(Distance ~ ABS + Tire + Tread + ABS:Tire + ABS:Tread + Tire:Tread
         + ABS:Tire:Tread, data = distance)
summary(fm)</pre>

<pre>##                Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS             1  92.62   92.62  41.038 3.37e-05 ***
## Tire            2  42.64   21.32   9.446  0.00344 ** 
## Tread           1   1.14    1.14   0.505  0.49108    
## ABS:Tire        2   9.60    4.80   2.127  0.16187    
## ABS:Tread       1   5.13    5.13   2.273  0.15748    
## Tire:Tread      2   2.08    1.04   0.462  0.64107    
## ABS:Tire:Tread  2   4.45    2.22   0.986  0.40149    
## Residuals      12  27.08    2.26                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
Only main effects <code>ABS</code> and <code>Tire</code> seem to influence the mean of the braking distance (similarly to what we had guessed by looking at the plots!). Interactions seem not significant, meaning that those differences we had noticed in the two-way interactions plots were too small to be statistically significant. We can now start to drop the interactions from the three-way interaction.</p>
<p>In general we are interested in finding the most provident model which better explains the variability in the data. Once the full model has been fitted, we start by dropping the highest-level interactions because we “move” within the hierarchical models paradigm.</p>
<p>To obtain the model without three-way interaction, we can use <code>update()</code>:</p>
<pre>fm &lt;- update(fm, . ~ . -ABS:Tire:Tread)
summary(fm)</pre>

<pre>##             Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS          1  92.62   92.62  41.123 1.62e-05 ***
## Tire         2  42.64   21.32   9.465  0.00251 ** 
## Tread        1   1.14    1.14   0.506  0.48873    
## ABS:Tire     2   9.60    4.80   2.132  0.15552    
## ABS:Tread    1   5.13    5.13   2.278  0.15345    
## Tire:Tread   2   2.08    1.04   0.462  0.63902    
## Residuals   14  31.53    2.25                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
Since two ways interactions seem to not affect the mean braking distance, we could now try to remove all two-way interactions, always using <code>update()</code> function:</p>
<pre>fm1 &lt;- update(fm, .~ABS+Tire+Tread)
summary(fm1)</pre>

<pre>##             Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS          1  92.62   92.62  36.397 8.37e-06 ***
## Tire         2  42.64   21.32   8.377  0.00246 ** 
## Tread        1   1.14    1.14   0.447  0.51157    
## Residuals   19  48.35    2.54                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p>
The result is not surprising, as it is very similar to that found with the previous models. It is better to check if the three removed effects together are still not significant using <code>anova()</code>:</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     14 31.533                           
## 2     19 48.351 -5   -16.818 1.4933 0.2539</pre>
<p>
The combined effect of the three-way interactions is not significant, thus we can continue on with the model with the main effects only.<br>
The tables of effects from model follow below</p>
<pre>model.tables(fm1,type=&quot;effects&quot;)</pre>

<pre>## Tables of effects
## 
##  ABS 
## ABS
## disabled  enabled 
##   1.9645  -1.9645 
## 
##  Tire 
## Tire
##      GT      LS      MX 
## -1.8846  0.9755  0.9091 
## 
##  Tread 
## Tread
##      1.5       10 
##  0.21783 -0.21783</pre>
<p>
And now the table of means from model</p>
<pre>model.tables(fm1,type=&quot;means&quot;)</pre>

<pre>## Tables of means
## Grand mean
##          
## 24.67633 
## 
##  ABS 
## ABS
## disabled  enabled 
##   26.641   22.712 
## 
##  Tire 
## Tire
##     GT     LS     MX 
## 22.792 25.652 25.585 
## 
##  Tread 
## Tread
##    1.5     10 
## 24.894 24.459</pre>
<p>
Finally, we remove the <code>Tread</code> effect to obtain the final model with significant effects only:</p>
<pre>fm &lt;- aov(Distance ~ ABS + Tire, data = distance)
summary(fm)</pre>

<pre>##             Df Sum Sq Mean Sq F value   Pr(>F)    
## ABS          1  92.62   92.62  37.431 5.59e-06 ***
## Tire         2  42.64   21.32   8.615    0.002 ** 
## Residuals   20  49.49    2.47                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</pre>
<p><h3>Residual analysis of 3-way Anova</h3>

<pre>op &lt;-  par(mfrow = c(2, 2))
plot(fm)
par(op)</pre>
<p>
<img sizes="(max-width: 450px) 100vw, 450px" alt="anova19" class="wp-image-2905" src="https://i2.wp.com/www.quantide.com/wp-content/uploads/2016/09/Anova19-300x300.png?resize=450%2C450" data-recalc-dims="1"></p>
<p>Compound residual plot for final brake distance model</p>
<p>Since the leverages are constant (as is typically the case in a balanced <code>aov</code> situation) the 4th plot draws factor level combinations instead of the leverages for the x-axis. (Notice that the factor levels are ordered by mean fitted value)</p>
<p>Distance means for each <code>ABS</code> and <code>Tire</code> category follow:</p>
<pre>distance %>% group_by(ABS, Tire) %>% summarise(mean(Distance))</pre>

<pre>## Source: local data frame [6 x 3]
## Groups: ABS [?]
## 
##        ABS   Tire `mean(Distance)`
##     &lt;fctr> &lt;fctr>            &lt;dbl>
## 1 disabled     GT         24.98647
## 2 disabled     LS         28.24991
## 3 disabled     MX         26.68618
## 4  enabled     GT         20.59702
## 5  enabled     LS         23.05378
## 6  enabled     MX         24.48464</pre>
<p>
<span>While the table of grand means from model can be found as follows:</span></p>
<pre>model.tables(fm, type=&quot;means&quot;)</pre>

<pre>## Tables of means
## Grand mean
##          
## 24.67633 
## 
##  ABS 
## ABS
## disabled  enabled 
##   26.641   22.712 
## 
##  Tire 
## Tire
##     GT     LS     MX 
## 22.792 25.652 25.585</pre>
<p>
Last we can get some predicted values from ANOVA model:</p>
<pre>df_pred &lt;- data.frame(ABS=c(&quot;enabled&quot;,&quot;disabled&quot;),Tire=c(&quot;GT&quot;,&quot;LS&quot;))
predict(object=fm,newdata=df_pred)</pre>

<pre>##        1        2 
## 20.82722 27.61636</pre>
<p>
<span>Question: predicted values are different from the above calculated sampling averages. Why?</span></p>
<p> </p>
<h2></h2>
<p>The post <a target="_blank" rel="nofollow" href="http://www.quantide.com/raccoon-ch-2-4-3-way-anova/">Raccoon | Ch 2.4 – 3-way Anova</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.quantide.com/">Quantide – R training &amp; consulting</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.quantide.com/raccoon-ch-2-4-3-way-anova/"> R blog | Quantide - R training &amp; consulting</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/3Tm8y_fK2dM"></a></div>