<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/two-way-anova-in-r-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/two-way-anova-in-r-exercises/" data-counturl="https://www.r-bloggers.com/two-way-anova-in-r-exercises/" data-count="horizontal" data-text="Two Way ANOVA in R Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/10/17/two-way-anova-in-r-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/11/box-plot-2-way.png?x61433"><img sizes="(max-width: 300px) 100vw, 300px" alt="box-plot-2-way" class="wp-image-2288" src="https://i1.wp.com/r-exercises.com/wp-content/uploads/2016/11/box-plot-2-way-300x214.png?resize=300%2C214" data-recalc-dims="1"></a></p>
<p>One way analysis of variance helps us understand the relationship between one continuous dependent variable and one categorical independent variable. When we have one continuous dependent variable and more than one independent categorical variable we cannot use one way ANOVA. When we have two independent categorical variable we need to use two way ANOVA. When we have more than two categorical independent variables we need to use N way ANOVA.</p>
<p>In two way ANOVA there are three hypotheses of interest as listed below</p>
<ol>
<li>H<sub>0</sub>: There is an effect of the first factor on the dependent continuous variable (main effect)</li>
<li>H<sub>0</sub>: There is an effect of the second factor variable on the dependent continuous variable (main effect)</li>
<li>H<sub>0</sub>: There is a combined effect of the first and second factor variable on the continuous dependent variable (interaction)</li>
</ol>
<p>The above hypotheses can be extended from two factor variables to N factor variables.</p>
<p>For results of two way ANOVA to be valid there are several assumptions that need to be satisfied. They are listed below.</p>
<ol>
<li>Observations must be independent within and across groups</li>
<li>Observations are approximately normally distributed.</li>
<li>There is equal variance in the observations</li>
<li>We should not have any outliers especially when our design is unbalanced</li>
<li>The errors are independent</li>
</ol>
<p> </p>
<p>When the normality and equal variance assumptions are violated you need to transform your data.</p>
<p>In this exercise we will use data on a moth experiment which is available here <a target="_blank" href="http://college.cengage.com/mathematics/brase/understandable_statistics/7e/students/datasets/twan/frames/twan03.html">here</a>. The data is not well formatted in that link so use this csv file <a target="_blank" href="http://r-exercises.com/wp-content/uploads/2016/10/moth-trap-experiment.csv">moth-trap-experiment</a>.</p>
<p>The dependent variable is the number of moths in a trap. The independent variables are location and type of lure. There were four locations (top, middle, lower and ground). There were three types of lure (scent, sugar and chemical).</p>
<p>Solutions to these exercises are found <a target="_blank" href="http://r-exercises.com/2016/10/17/two-way-anova-in-r-solutions/">here</a></p>
<p>Exercise 1</p>
<p>Read in the data and inspect its structure</p>
<p>Exercise 2</p>
<p>Create summary statistics for location</p>
<p>Exercise 3</p>
<p>Create summary statistics for type of lure</p>
<p>Exercise 4</p>
<p>Create boxplots for each category</p>
<p>Exercise 5</p>
<p>Check for normality</p>
<p>Exercise 6</p>
<p>Check for equality of variance</p>
<p>Exercise 7</p>
<p>Take a log transformation of our data</p>
<p>Exercise 8</p>
<p>Perform a power analysis</p>
<p>Exercise 9</p>
<p>Perform anova</p>
<p>Exercise 10</p>
<p>Check homogeneity of variance</p>
<p> </p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/two-way-anova-in-r-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/two-way-anova-in-r-exercises/" data-counturl="https://www.r-bloggers.com/two-way-anova-in-r-exercises/" data-count="vertical" data-text="Two Way ANOVA in R Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<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-exercises.com/2016/10/17/two-way-anova-in-r-exercises/"> R-exercises</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/vMZ9rau4Xig"></a></div>