<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/repeated-measures-anova-in-r-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/repeated-measures-anova-in-r-exercises/" data-counturl="https://www.r-bloggers.com/repeated-measures-anova-in-r-exercises/" data-count="horizontal" data-text="Repeated measures 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/11/29/repeated-measures-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" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/12/repeated-measure.png?x61433"><img sizes="(max-width: 200px) 100vw, 200px" alt="repeated-measure" class="wp-image-2816" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2016/12/repeated-measure-300x214.png?resize=200%2C144" data-recalc-dims="1"></a></p>
<p>One way, two way and n way ANOVA are used to test difference in means when we have one, two and n factor variables. A key assumption when performing these ANOVAs is that the measurements are independent. When we have repeated measures this assumption is violated, so we have to use repeated measures ANOVA. Repeated measures designs occur often in longitudinal studies where we are interested in understanding change over time. For example a medical researcher would be interested in assessing the level of depression before and after a surgery procedure. Repeated measures designs are not limited to longitudinal studies, they can also be used when you have an important variable you would like to repeat measures. For example in a fitness experiment you can repeat your measures at different intensity levels. Repeated measures ANOVA can be considered an extension of the paired t test.</p>
<p>Before diving deeper into repeated measures ANOVA you need to understand terminology used. A <strong><em>subject</em> </strong>is a member of the sample under consideration. In our medical study introduced earlier an individual patient is a subject. The <strong><em>within-subjects factor</em></strong> is the variable that identifies how the dependent variable has been repeatedly measured. In our medical study we would measure depression 4 weeks before surgery, 4 weeks after surgery and 8 weeks after surgery. The different conditions when repeated measurements are made are referred to as trials. A <strong><em>between-subjects factor</em></strong> identifies independent groups in the study. For example if we had two different procedures this would be the between subjects factor. These conditions are referred to as groups. Repeated measures analysis requires balance in between-subjects factor. For example subjects in each of surgery procedures need to be equal.</p>
<p>With a repeated measures design we are able to test the following hypotheses.</p>
<ol>
<li>There is no within-subjects main effect</li>
<li>There is no between-subjects main effect</li>
<li>There is no between subjects interaction effect</li>
<li>There is no within subject by between subject interaction effect</li>
</ol>
<p>There are two assumptions that need to be satisfied when using repeated measures.</p>
<ol>
<li>The dependent variable is normally distributed in each level of the within-subjects factor. Repeated measures analysis is robust to violations of normality with a large sample size which is considered at least 30 subjects. However the accuracy of p values is questionable when the distribution is heavily skewed or thick tailed.</li>
<li>The variance across the within subject factor is equal. This is the sphericity assumption. Repeated measures analysis is not robust to this assumption so when there is a violation power decreases and a corresponding increase in probability of a type II error occurs. A Mauchly’s test assesses the null hypothesis variance is equal. The sphericity assumption is only relevant when there are more than 2 levels of the within subjects factor.</li>
</ol>
<p>When the sphericity assumption is violated we make corrections by adjusting the degrees of freedom. Corrections available are Greenhouse-Geisser, Huynh-Feldt and Lower bound. To make a decision on appropriate correction we use a Greenhouse-Geisser estimate of sphericity (ξ). When ξ &lt; 0.75 or we do not know anything about sphericity the Greenhouse-Geisser is the appropriate correction. When ξ > 0.75 Huynh-Feldt is the appropriate correction.</p>
<p>For this exercise we will use data on pulse rate <a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2016/11/exer-1.csv">exer</a>. People were randomized to two diets, three exercise types and pulse was measured at three different time points. For this data time points is the within-subjects factor. The between-subjects factors are diet and exercise type</p>
<p>The solutions to the exercises below can be found <a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/11/29/repeated-measures-anova-in-r-solutions/">here</a></p>
<p>Exercise 1</p>
<p>Load the data and inspect its structure</p>
<p>Exercise 2</p>
<p>Check for missing values</p>
<p>Exercise 3</p>
<p>Check for balance in between-subjects factor</p>
<p>Exercise 4</p>
<p>Generate descriptive statistics for the sex variable which is a between subjects factor</p>
<p>Exercise 5</p>
<p>Generate descriptive statistics for the treatment level variable which is a between subjects factor</p>
<p>Exercise 6</p>
<p>Generate descriptive statistics for the weeks variable which is the within subjects factor</p>
<p>Exercise 7</p>
<p>Use histograms to assess distribution across within subjects factor.</p>
<p>Exercise 8</p>
<p>Perform a repeated measures analysis with only the within subjects factor</p>
<p>Exercise 9</p>
<p>Perform a repeated measures analysis with the within subjects factor and one between subjects factor</p>
<p>Exercise 10</p>
<p>Perform a repeated measures analysis with the within subjects factor and two between subjects factors</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/repeated-measures-anova-in-r-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/repeated-measures-anova-in-r-exercises/" data-counturl="https://www.r-bloggers.com/repeated-measures-anova-in-r-exercises/" data-count="vertical" data-text="Repeated measures 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/11/29/repeated-measures-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/MCDMTKNlqtQ"></a></div>