<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/three-way-analysis-of-variance-simple-second-order-interaction-effects-and-simple-main-effects/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/three-way-analysis-of-variance-simple-second-order-interaction-effects-and-simple-main-effects/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this article we will show how to run a three-way analysis of variance when both the third-order interaction effect and the second-order interaction effects are statistically significant. This type of analysis can become pretty tedious, especially when our factors have many levels, so we will try to explain it here as clearly as possible. (If you want to watch me doing these analyses live, <a target="_blank" rel="nofollow" href="http://www.statistics-with-r.com/">get my free course on statistical analysis with R here</a>.)</p>
<p> First of all, let’s present the fictitious data we are going to work with. Let’s suppose that a pharmaceutical company is planning to launch a new vitamin that allegedly improves the employees’ resistance to effort. The vitamin is tested on a sample of 720 employees, divided into three groups: employees who take a placebo (the control group), employees who take the vitamin in low dose and employees who take the vitamin in high dose. Half of the employees are male, and half are female. Moreover, we have both blue collar employees and white collar employees in our sample.</p>
<p>The resistance to effort is measured on a scale whatsoever, from 1 to 30 (30 being the highest resistance). Our goal is to determine whether the effort resistance is influenced by three factors: dose of vitamin (placebo, low dose, and high dose), gender (male, female) and type of employee (blue collar, white collar). You can find the experiment data in CSV format <a target="_blank" rel="nofollow" href="http://statistics-with-r.com/datasets/vitamin3.csv">here</a>.</p>
<p><strong>Third-order interaction effect</strong></p>
<p>First of all, let’s check whether the third-order interaction effect is significant. We are going to run the analysis using the <em>aov</em> function in the <em>stats</em> package (our data frame is called <em>vitamin</em>).</p>
<pre>
aov1 &lt;- aov(effort~dose*gender*type, data=vitamin)
summary(aov1)
</pre>
<p>In the formula above the interaction effect is, of course, <em>dose<em>gender</em>type</em>. The ANOVA results can be seen below (we have only kept the line presenting the third-order interaction effect).                                 </p>
<pre>
                                  Df Sum Sq Mean Sq F value   Pr(>F)


dose:gender:type   2    187    93.4  22.367 3.81e-10
</pre>
<p>The interaction effect is statistically significant: F(2)=22.367, p&lt;0.01. In other words, we do have a third-order interaction effect.
In this situation, it is not advisable to report and interpret the second-order interaction effects (they could be misleading). Therefore, we are going to compute the <em>simple second-order interaction effects</em>.</p>
<p><strong>Simple second-order interaction effects</strong></p>
<p>The simple second-order interaction effects are the effects of each pair of factors at each level of the third factor. Specifically, we have to compute the following effects:</p>
<ol>
<li>the interaction effect of dose and type of employee, for each gender category (male and female)</li>
<li>the interaction effect of gender and type of employee, at each dose level (placebo, low and high)</li>
<li>the interaction effect of dose and gender, for each type of employee (blue collar and white collar).</li>
</ol>
<p>The total number of second-order interaction effect is given by the sum of the factor levels. In our case we have 7 effects (3+2+2). We will not analyze of all them, because this article would become too long. We will only focus on the first set of effects, leaving the others for you as an exercise.</p>
<p>So let’s investigate the interaction effect of dose and type of employee for each gender group. First we have to create two separate data frames, for male and female employees. We do that with the <em>filter</em> command in the <em>dplyr</em> package (though you can also use brackets or subsets).</p>
<pre>
vitamin_male &lt;- filter(vitamin, gender==&quot;male&quot;)
vitamin_female &lt;- filter(vitamin, gender==&quot;female&quot;)
</pre>
<p>Now we perform a two-way analysis of variance on each data frame (the factors being dose and type, of course).</p>
<p>aov1 &lt;-aov(effort~dose*type, data=vitamin_male)<br>
summary(aov1)</p>
<p>aov2 &lt;-aov(effort~dose*type, data=vitamin_female)<br>
summary(aov2)</p>
<p>The results of the analyses are shown below (we have only retained the lines with the interaction effects).</p>
<pre>
                Df Sum Sq Mean Sq F value   Pr(>F)    <br>
 dose:type     2    249   124.7   28.42 3.57e-12            <br>
                    Df Sum Sq Mean Sq F value   Pr(>F)    <br>
 dose:type     2  137.2    68.6   17.31 6.74e-08 <br>
</pre>
<p></p>
<p>We can notice that both simple second-order interaction effects are significant (p&lt;0.01). So we are dealing with a combined influence of the factors dose and type of employee in both male and female groups. In this situation, we have to examine the <em>simple main effects</em> for each factor. This is what we are going to do in the next section.</p>
<p><strong>Simple main effects</strong></p>
<p>Let’s compute the main effect for the factor dose of vitamin, which is the most important (after all, the company wants to demonstrate that the vitamin does affect the resistance to effort). You will be able to compute the other simple main effects yourself, using this as an example.</p>
<p>Now we must create four separate data frames, for each combination of the factors gender and type of employee: male – blue collar, male – white collar, female – blue collar, female – white collar.</p>
<p>vitamin_male_blue &lt;-filter(vitamin, gender==&quot;male&quot;, type==&quot;blue collar&quot;)</p>
<p>vitamin_male_white &lt;-filter(vitamin, gender==&quot;male&quot;, type==&quot;white collar&quot;)</p>
<p>vitamin_female_blue &lt;-filter(vitamin, gender==&quot;female&quot;, type==&quot;blue collar&quot;)</p>
<p>vitamin_female_white &lt;-filter(vitamin, gender==&quot;female&quot;, type==&quot;white collar&quot;)</p>
<p>Next we perform a one-way ANOVA for each data frame. Let’s do it for the first group, male – blue collar.</p>
<p>aov1 &lt;-aov(effort~dose, data=vitamin_male_blue)<br>
summary(aov1)</p>
<pre>

                 Df Sum Sq Mean Sq F value Pr(>F)    <br>
 dose          2 2943.5  1471.8   349.9 &lt;2e-16
</pre>
<p>
The simple main effect for the factor dose on this group is statistically significant (p&lt;0.01). In other words, there is a significant difference between placebo, low dose and high dose levels within the male – blue collar employees category, regarding the resistance to effort. To find out how big the differences are, we use the <em>TuckeyHSD</em> function to compute the test with the same name.</p>
<pre>TukeyHSD(aov1)


                                              diff        lwr       upr   p adj<br>
 low dose-high dose -2.528333  -3.413363 -1.643303     0<br>
 placebo-high dose  -9.558333 -10.443363 -8.673303     0<br>
 placebo-low dose   -7.030000  -7.915030 -6.144970     0

</pre>
<p>
By inspection of the table we conclude that the differences in effort resistance between the dose groups are significant (p&lt;0.01). The highest difference, in absolute values, is that between low dose and placebo levels: 9.5 points. So the employees who took a high dose present a higher resistance to effort than those who just took a placebo.</p>
<p>One more example: the simple main effects of the variable dose of vitamin on the female – blue collar group.</p>
<p>aov1 &lt;-aov(effort~dose, data=vitamin_female_blue)<br>
summary(aov1)</p>
<p>                 Df Sum Sq Mean Sq F value Pr(>F)   <br>
dose          2  399.6  199.81   45.57 &lt;2e-16</p>
<p> </p>
<pre>TukeyHSD(aov1)


                                            diff        lwr       upr     p adj<br>
 low dose-high dose  1.083333  0.1797508  1.986916 0.0141485<br>
 placebo-high dose  -2.476667 -3.3802492 -1.573084 0.0000000<br>
 placebo-low dose   -3.560000 -4.4635826 -2.656417 0.0000000
</pre>
<p> </p>
<p>The simple main effect is statistically significant, as it results from the first table. Furthermore, all the differences between dose levels are significant. The highest difference is the difference between low dose and placebo (3.5 points).</p>
<p>To learn more on data analysis in R, <a target="_blank" rel="nofollow" href="http://www.statistics-with-r.com/">check the free “Statistics with R” video course here.</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://r-posts.com/three-way-analysis-of-variance-simple-second-order-interaction-effects-and-simple-main-effects/"> R-posts.com</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/mV_p9WqnZsU"></a></div>