<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-6710487119650146215</atom:id><lastBuildDate>Wed, 15 May 2013 21:28:54 +0000</lastBuildDate><category>z-scores</category><category>bibliography</category><category>categorical regression</category><category>books</category><category>Bonferroni</category><category>polynomial regression</category><category>two-way</category><category>factor analysis</category><category>Tukey HSD</category><category>open source</category><category>unequal</category><category>reshape</category><category>interaction effect</category><category>simple main effects</category><category>EFA</category><category>ANOVA</category><category>R Project</category><category>scatterplots</category><category>cell size</category><category>multiple linear regression</category><category>dummy coding</category><category>review</category><category>one-way</category><category>centering</category><category>variables</category><category>update</category><category>sample size</category><category>R Bloggers</category><category>repeated measures</category><category>pairwise comparisons</category><category>packages</category><category>descriptive statistics</category><category>R Programming Wikibook</category><category>simple linear regression</category><category>guide</category><category>citations</category><category>scale</category><category>summary statistics</category><category>box plots</category><category>regression assumptions</category><category>tutorial</category><category>graphics</category><category>Fisher LSD</category><category>Word</category><category>APA</category><category>hierarchical linear regression</category><category>analysis of variance</category><category>Statistical Analysis with R</category><category>R Graphs Cookbook</category><category>publishing</category><category>EndNote</category><category>Google Analytics</category><category>Packt Publishing</category><category>Holm</category><category>packt</category><category>psych</category><category>standardized scores</category><category>interaction model</category><category>statistics</category><category>correlation</category><category>omnibus</category><category>data</category><category>R Tutorial Series</category><title>R Tutorial Series</title><description>&lt;p&gt;&lt;strong&gt;By &lt;a href="http://www.johnmquick.com"&gt;John M Quick&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The R Tutorial Series provides a collection of user-friendly guides to researchers, students, and others who want to learn how to use R for their statistical analyses.&lt;/p&gt;</description><link>http://rtutorialseries.blogspot.com/</link><managingEditor>noreply@blogger.com (John Quick)</managingEditor><generator>Blogger</generator><openSearch:totalResults>36</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RTutorialSeries" /><feedburner:info uri="rtutorialseries" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-4552488905530897204</guid><pubDate>Thu, 01 Mar 2012 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:42:19.082-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">standardized scores</category><category domain="http://www.blogger.com/atom/ns#">centering</category><category domain="http://www.blogger.com/atom/ns#">z-scores</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">scale</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">variables</category><title>R Tutorial Series: Centering Variables and Generating Z-Scores with the Scale() Function</title><description>Centering variables and creating z-scores are two common data analysis activities. While they are relatively simple to calculate by hand, R makes these operations extremely easy thanks to the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;scale()&lt;/span&gt; function.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_scale.csv" target="_blank"&gt;dataset (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory.&lt;br /&gt;
&lt;h3&gt;The Scale() Function&lt;/h3&gt;The &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;scale()&lt;/span&gt; function makes use of the following arguments.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;x: a numeric object&lt;/li&gt;
&lt;li&gt;center: if TRUE, the objects' column means are subtracted from the values in those columns (ignoring NAs); if FALSE, centering is not performed&lt;/li&gt;
&lt;li&gt;scale: if TRUE, the centered column values are divided by the column's standard deviation (when center is also TRUE; otherwise, the root mean square is used); if FALSE, scaling is not performed&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Centering Variables&lt;/h3&gt;Normally, to center a variable, you would subtract the mean of all data points from each individual data point. With &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;scale()&lt;/span&gt;, this can be accomplished in one simple call.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #center variable A using the scale() function&lt;/li&gt;
&lt;li&gt;&amp;gt; scale(A, center = TRUE, scale = FALSE)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;You can verify these results by making the calculation by hand, as demonstrated in the following screenshot.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-WgKo4UZvx5w/T8FKJZ53y2I/AAAAAAAAA8k/lqde6E_8heU/s1600/20120224_scale_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-WgKo4UZvx5w/T8FKJZ53y2I/AAAAAAAAA8k/lqde6E_8heU/s1600/20120224_scale_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="alignCenter"&gt;&lt;/div&gt;&lt;div class="alignCenter" style="text-align: center;"&gt;Centering a variable with the scale() function and by hand&lt;/div&gt;&lt;h3&gt;Generating Z-Scores&lt;/h3&gt;Normally, to create z-scores (standardized scores) from a variable, you would subtract the mean of all data points from each individual data point, then divide those points by the standard deviation of all points. Again, this can be accomplished in one call using &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;scale()&lt;/span&gt;.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #generate z-scores for variable A using the scale() function&lt;/li&gt;
&lt;li&gt;&amp;gt; scale(A, center = TRUE, scale = TRUE)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;Again, the following screenshot demonstrates equivalence between the function results and hand calculation.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-EVDTuoRwlAg/T8FKJzvQSRI/AAAAAAAAA88/JXKEVUKagjM/s1600/20120224_scale_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-EVDTuoRwlAg/T8FKJzvQSRI/AAAAAAAAA88/JXKEVUKagjM/s1600/20120224_scale_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="alignCenter"&gt;&lt;/div&gt;&lt;div class="alignCenter" style="text-align: center;"&gt;Generating z-scores from a variable by hand and using the scale() function&lt;/div&gt;&lt;h3&gt;Complete Scale() Example&lt;/h3&gt;To see a complete example of how &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;scale()&lt;/span&gt; can be used to center variables and generate z-scores in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_scale.txt" target="_blank"&gt;scale() example (.txt)&lt;/a&gt; file.&lt;br /&gt;
&lt;h3&gt;References&lt;/h3&gt;The official scale function manual page is available from: http://stat.ethz.ch/R-manual/R-patched/library/base/html/scale.html&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/2rk7_AY2ZQI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/2rk7_AY2ZQI/r-tutorial-series-centering-variables.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-WgKo4UZvx5w/T8FKJZ53y2I/AAAAAAAAA8k/lqde6E_8heU/s72-c/20120224_scale_1.png" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2012/03/r-tutorial-series-centering-variables.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-7990576896814528785</guid><pubDate>Fri, 06 Jan 2012 15:55:00 +0000</pubDate><atom:updated>2012-05-26T15:43:43.597-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">APA</category><category domain="http://www.blogger.com/atom/ns#">EndNote</category><category domain="http://www.blogger.com/atom/ns#">citations</category><category domain="http://www.blogger.com/atom/ns#">bibliography</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">Word</category><title>R Tutorial Series: Citing R with EndNote</title><description>Unfortunately, due to the vexing complexities of academic style guides and the limitations of associated software packages, citing a non-standard name, such as Cher, Prince, or R Development Core Team can be problematic. Thankfully, I have discovered a simple trick in Word and EndNote that allows for the accurate automatic formatting of R citations. Note that this method was developed using Word 2011 and EndNote X4 for Mac. I am unaware of the differences between operating systems and software versions, but it is anticipated that this method will work for almost anyone.&lt;br /&gt;
&lt;h3&gt;

The Intuitive, But Nonworking Way&lt;/h3&gt;
If you were going to create your R record in EndNote, you would probably enter something like what is pictured below. In the name field, it makes sense to just type in R Development Core Team.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-swKDkzeP4uU/T8FKIhL_CzI/AAAAAAAAA8M/uhaaB5dTxXQ/s1600/20120106_endnote_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="243" src="http://4.bp.blogspot.com/-swKDkzeP4uU/T8FKIhL_CzI/AAAAAAAAA8M/uhaaB5dTxXQ/s400/20120106_endnote_1.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="alignCenter"&gt;
&lt;/div&gt;
However, this is where things take an untimely turn. EndNote will try to interpret that peculiar name as a series of first, last, and middle names, which leads to inaccurate citations.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-9IgLQzbLJA0/T8FKI_BXerI/AAAAAAAAA8Q/qnoKvinVzvE/s1600/20120106_endnote_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-9IgLQzbLJA0/T8FKI_BXerI/AAAAAAAAA8Q/qnoKvinVzvE/s1600/20120106_endnote_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="alignCenter"&gt;
&lt;/div&gt;
&lt;h3&gt;

The Unintiuitive, But Working Way&lt;/h3&gt;
This is where we basically need to trick EndNote into interpreting our R citation the proper way. All we have to do is add a comma after R Development Core Team in the name field.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-BNY9theSeZI/T8FKJSd7N4I/AAAAAAAAA8c/q9eNrUnUV6A/s1600/20120106_endnote_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="240" src="http://4.bp.blogspot.com/-BNY9theSeZI/T8FKJSd7N4I/AAAAAAAAA8c/q9eNrUnUV6A/s400/20120106_endnote_3.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="alignCenter"&gt;
&lt;/div&gt;
This tells EndNote that R Core Development Team is a complete last name of an author that has no first name. Hence, EndNote uses what it has (a last name with no first name) in generating its citations.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-iIZ13yrprX8/T8FKJWuQPWI/AAAAAAAAA8g/VHTr4Ee40LI/s1600/20120106_endnote_4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-iIZ13yrprX8/T8FKJWuQPWI/AAAAAAAAA8g/VHTr4Ee40LI/s1600/20120106_endnote_4.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="alignCenter"&gt;
&lt;/div&gt;
Note: The official citation for R can be found by issuing the citation() command in the R console.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/wUym6DTaAIk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/wUym6DTaAIk/r-tutorial-series-citing-r-with-endnote.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-swKDkzeP4uU/T8FKIhL_CzI/AAAAAAAAA8M/uhaaB5dTxXQ/s72-c/20120106_endnote_1.png" height="72" width="72" /><thr:total>15</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2012/01/r-tutorial-series-citing-r-with-endnote.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-5802274663531997130</guid><pubDate>Mon, 24 Oct 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:40:45.079-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">packages</category><category domain="http://www.blogger.com/atom/ns#">psych</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">factor analysis</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">EFA</category><title>R Tutorial Series: Exploratory Factor Analysis</title><description>Exploratory factor analysis (EFA) is a common technique in the social sciences for explaining the variance between several measured variables as a smaller set of latent variables. EFA is often used to consolidate survey data by revealing the groupings (factors) that underly individual questions. This will be the context for demonstration in this tutorial.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_exploratoryFactorAnalysis.csv" target="_blank"&gt;dataset (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 300 responses on 6 items from a survey of college students' favorite subject matter. The items range in value from 1 to 5, which represent a scale from Strongly Dislike to Strongly Like. Our 6 items asked students to rate their liking of different college subject matter areas, including biology (BIO), geology (GEO), chemistry (CHEM), algebra (ALG), calculus (CALC), and statistics (STAT). This is where our tutorial ends, because all students rated all of these content areas as Strongly Dislike, thereby rendering insufficient variance for conducting EFA (just kidding).&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our datasets into R and store their contents in variables.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; data &amp;lt;- read.csv("dataset_EFA.csv")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-EywFSq_7Tsg/T8FKIWXvBII/AAAAAAAAA70/J4NyA8Xrxbk/s1600/20111024_exploratoryFactorAnalysis_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-EywFSq_7Tsg/T8FKIWXvBII/AAAAAAAAA70/J4NyA8Xrxbk/s1600/20111024_exploratoryFactorAnalysis_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="alignCenter"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="alignCenter"&gt;&lt;/div&gt;&lt;div class="alignCenter" style="text-align: center;"&gt;First 10 rows of the dataset&lt;/div&gt;&lt;h3&gt;Psych Package&lt;/h3&gt;Next, we need to install and load the &lt;em&gt;psych&lt;/em&gt; package, which I prefer to use when conducting EFA. In this tutorial, we will make use of the package's &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;fa()&lt;/span&gt; function.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #install the package&lt;/li&gt;
&lt;li&gt;&amp;gt; install.packages("psych")&lt;/li&gt;
&lt;li&gt;&amp;gt; #load the package&lt;/li&gt;
&lt;li&gt;&amp;gt; library(psych)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Number of Factors&lt;/h3&gt;For this tutorial, we will assume that the appropriate number of factors has already been determined to be 2, such as through eigenvalues, scree tests, and a priori considerations. Most often, you will want to test solutions above and below the determined amount to ensure the optimal number of factors was selected.&lt;br /&gt;
&lt;h3&gt;Factor Solution&lt;/h3&gt;To derive the factor solution, we will use the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;fa()&lt;/span&gt; function from the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;psych&lt;/span&gt; package, which receives the following primary arguments.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;r: the correlation matrix&lt;/li&gt;
&lt;li&gt;nfactors: number of factors to be extracted (default = 1)&lt;/li&gt;
&lt;li&gt;rotate: one of several matrix rotation methods, such as "varimax" or "oblimin"&lt;/li&gt;
&lt;li&gt;fm: one of several factoring methods, such as "pa" (principal axis) or "ml" (maximum likelihood)&lt;/li&gt;
&lt;/ul&gt;Note that several rotation and factoring methods are available when conducting EFA. Rotation methods can be described as &lt;em&gt;orthogonal&lt;/em&gt;, which do not allow the resulting factors to be correlated, and &lt;em&gt;oblique&lt;/em&gt;, which do allow the resulting factors to be correlated. Factoring methods can be described as &lt;em&gt;common&lt;/em&gt;, which are used when the goal is to better describe data, and &lt;em&gt;component&lt;/em&gt;, which are used when the goal is to reduce the amount of data. The &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;fa()&lt;/span&gt; function is used for common factoring. For component analysis, see &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;princomp()&lt;/span&gt;. The best methods will vary by circumstance and it is therefore recommended that you seek professional council in determining the optimal parameters for your future EFAs.&lt;br /&gt;
In this tutorial, we will use oblique rotation (&lt;span class="Apple-style-span" style="color: #cc0000;"&gt;rotate = "oblimin"&lt;/span&gt;), which recognizes that there is likely to be some correlation between students' latent subject matter preference factors in the real world. We will use principal axis factoring (&lt;span class="Apple-style-span" style="color: #cc0000;"&gt;fm = "pa"&lt;/span&gt;), because we are most interested in identifying the underlying constructs in the data.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #calculate the correlation matrix&lt;/li&gt;
&lt;li&gt;&amp;gt; corMat &amp;lt;- cor(data)&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the correlation matrix&lt;/li&gt;
&lt;li&gt;&amp;gt; corMat&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-zv86aI7Auh8/T8FKIvof-LI/AAAAAAAAA8A/DeoF7ZSgIlU/s1600/20111024_exploratoryFactorAnalysis_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-zv86aI7Auh8/T8FKIvof-LI/AAAAAAAAA8A/DeoF7ZSgIlU/s1600/20111024_exploratoryFactorAnalysis_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="alignCenter"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="alignCenter"&gt;&lt;/div&gt;&lt;div class="alignCenter" style="text-align: center;"&gt;The correlation matrix&lt;/div&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use fa() to conduct an oblique principal-axis exploratory factor analysis &lt;/li&gt;
&lt;li&gt;&amp;gt; #save the solution to an R variable &lt;/li&gt;
&lt;li&gt;&amp;gt; solution &amp;lt;- fa(r = corMat, nfactors = 2, rotate = "oblimin", fm = "pa") &lt;/li&gt;
&lt;li&gt;&amp;gt; #display the solution output &lt;/li&gt;
&lt;li&gt;&amp;gt; solution&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-R43t9T64GYQ/T8FKImdHrRI/AAAAAAAAA8I/bOKKx4PO27w/s1600/20111024_exploratoryFactorAnalysis_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-R43t9T64GYQ/T8FKImdHrRI/AAAAAAAAA8I/bOKKx4PO27w/s1600/20111024_exploratoryFactorAnalysis_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="alignCenter"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="alignCenter"&gt;&lt;/div&gt;&lt;div class="alignCenter" style="text-align: center;"&gt;Complete solution output&lt;/div&gt;&lt;br /&gt;
By looking at our factor loadings, we can begin to assess our factor solution. We can see that BIO, GEO, and CHEM all have high factor loadings around 0.8 on the first factor (PA1). Therefore, we might call this factor &lt;em&gt;Science&lt;/em&gt; and consider it representative of a student's interest in science subject matter. Similarly, ALG, CALC, and STAT load highly on the second factor (PA2), which we might call &lt;em&gt;Math&lt;/em&gt;. Note that STAT has a much lower loading on PA2 than ALG or CALC and that it has a slight loading on factor PA1. This suggests that statistics is less related to the concept of &lt;em&gt;Math&lt;/em&gt; than algebra and calculus. Just below the loadings table, we can see that each factor accounted for around 30% of the variance in responses, leading to a factor solution that accounted for 66% of the total variance in students' subject matter preference. Lastly, notice that our factors are correlated at 0.21 and recall that our choice of oblique rotation allowed for the recognition of this relationship. &lt;br /&gt;
Of course, there are many other considerations to be made in developing and assessing an EFA that will not be presented here. The intent with this tutorial was simply to demonstrate the basic execution of EFA in R. For a detailed and digestible overview of EFA, I recommend the Factor Analysis chapter of &lt;em&gt;Multivariate Data Analysis&lt;/em&gt; by Hair, Black, Babin, and Anderson.&lt;br /&gt;
&lt;h3&gt;Complete EFA Example&lt;/h3&gt;To see a complete example of how EFA data can be organized using the &lt;em&gt;psych&lt;/em&gt; package in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_exploratoryFactorAnalysis.txt" target="_blank"&gt;EFA example (.txt)&lt;/a&gt; file. For the code used in this tutorial, download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_exploratoryFactorAnalysis.R" target="_blank"&gt;EFA Example (.R)&lt;/a&gt; file.&lt;br /&gt;
&lt;h3&gt;References&lt;/h3&gt;Revelle, W. (2011). psych: Procedures for Personality and Psychological Research. http://personality-project.org/r/psych.manual.pdf&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/LLM3lnuMWzQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/LLM3lnuMWzQ/r-tutorial-series-exploratory-factor.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-EywFSq_7Tsg/T8FKIWXvBII/AAAAAAAAA70/J4NyA8Xrxbk/s72-c/20111024_exploratoryFactorAnalysis_1.png" height="72" width="72" /><thr:total>6</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/10/r-tutorial-series-exploratory-factor.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-5875265027083992903</guid><pubDate>Fri, 08 Jul 2011 14:00:00 +0000</pubDate><atom:updated>2011-07-08T07:00:09.408-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">open source</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">R Programming Wikibook</category><title>The R Programming Wikibook</title><description>&lt;p&gt;The R Programming wikibook is an open source community project that "aims to create a cross-disciplinary practical guide to the R programming language." It was launched in June 2011 and is seeking content and contributors. The full call for the R Programming wikibook can be found on &lt;a href="http://www.r-statistics.com/2011/06/calling-r-lovers-and-bloggers-to-work-together-on-the-r-programming-wikibook/" target="_blank"&gt;Tal Galili's blog&lt;/a&gt;. The R Programming wikibook itself is available at &lt;a href="http://en.wikibooks.org/wiki/R_Programming" target="_blank"&gt;http://en.wikibooks.org/wiki/R_Programming&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;I am writing to raise awareness for the R Programming wikibook and to formally offer content from the &lt;span dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type"&gt;R Tutorial Series&lt;/span&gt; by &lt;a cc="http://creativecommons.org/ns#" href="http://www.johnmquick.com/" property="cc:attributionName" rel="cc:attributionURL" target="_blank"&gt;John M. Quick&lt;/a&gt; for use under a &lt;a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/" target="_blank"&gt;Creative Commons Attribution-ShareAlike 3.0 Unported License&lt;/a&gt;. This means that articles from the R Tutorial Series may be included in and modified for the R Programming wikibook, so long as proper attribution is given and the resulting content is made available under an equivalent license. A complete list of contributing blogs can be found on the R Programming wikibook's &lt;a href="http://en.wikibooks.org/wiki/R_Programming/Sources" target="_blank"&gt;Sources page&lt;/a&gt;. I hope that the &lt;a href="http://en.wikibooks.org/wiki/R_Programming" target="_blank"&gt;R Programming wikibook&lt;/a&gt; will thrive and grow to support a large community of R users, including readers of the R Tutorial Series.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/JVjrzr_Rk5E" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/JVjrzr_Rk5E/r-programming-wikibook.html</link><author>noreply@blogger.com (John Quick)</author><thr:total>1</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/07/r-programming-wikibook.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-7200295657489296954</guid><pubDate>Mon, 28 Mar 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-26T15:26:42.709-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">data</category><category domain="http://www.blogger.com/atom/ns#">Google Analytics</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: 2011 ANOVA Article Data</title><description>Having wrapped up a recent flurry of R ANOVA articles (and exhausted my knowledge of the subject), I decided to take a look at the R Tutorial Series' Google Analytics data from the past few months. &lt;br /&gt;
Since I posted the Two-Way Omnibus ANOVA article on January 17, we have had about 150 visits per day and over 19,000 total page views. The original introduction to R posts are still the most popular ones here, although a few from the regression and ANOVA series are also represented in the most viewed.&lt;br /&gt;
I also wanted to share a funny observation about the patterns of visits to the R Tutorial Series. The following graph portrays our daily viewership over the past few months.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-FYrT1afnSwg/T8FKIIWjKBI/AAAAAAAAA7o/GQxSdWW5ems/s1600/20110325_rBlogStats.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-FYrT1afnSwg/T8FKIIWjKBI/AAAAAAAAA7o/GQxSdWW5ems/s1600/20110325_rBlogStats.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align="center"&gt;
&lt;/div&gt;
The valleys on the chart correspond to the weekends (evidently when no one wants to read about statistical computing). The initial peaks are Mondays, which happen to be when I most often make new posts. Typically, a slightly higher peak comes on Tuesday, followed by a gradual decline back into the weekend valley. Thus, our visits to the R Tutorial Series end up creating a nice little wave pattern throughout the year. The good news with these numbers is that people are reading the R Tutorial Series and (hopefully) learning to use R and apply it to their daily work, which is the blog's ultimate purpose. I also appreciate all of the comments, questions, and tips that have been posted by readers. Your feedback really helps to improve the tutorials.&lt;br /&gt;
&lt;h3&gt;
Upcoming Plans&lt;/h3&gt;
As mentioned, I have concluded my planned coverage of ANOVA in R. Thus, we have reached a sort of break period, much like the one that followed last year's spurt of regression tutorials. I do plan to keep writing R tutorials, but for the time being, they may arrive in less predictable intervals and cover a wider variety of content. As always, I welcome contact regarding guest posts, especially on statistical and R content that I have not covered or am not yet familiar with.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/xbytgM5u2mk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/xbytgM5u2mk/r-tutorial-series-2011-anova-article.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-FYrT1afnSwg/T8FKIIWjKBI/AAAAAAAAA7o/GQxSdWW5ems/s72-c/20110325_rBlogStats.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/03/r-tutorial-series-2011-anova-article.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-5974080616435214255</guid><pubDate>Mon, 14 Mar 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:38:18.440-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">packages</category><category domain="http://www.blogger.com/atom/ns#">reshape</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: Applying the Reshape Package to Organize ANOVA Data</title><description>As demonstrated in the preceding ANOVA tutorials, data organization is central to conducting ANOVA in R. In standard ANOVA, we used the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;tapply()&lt;/span&gt; function to generate a table for a single summary function. In repeated measures ANOVA, we used separate datasets for our omnibus ANOVA and follow-up comparisons. This tutorial will demonstrate how the &lt;em&gt;reshape&lt;/em&gt; package can be used to simplify the ANOVA data organization process in R.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_reshape_1.csv" target="_blank"&gt;between group&lt;/a&gt; and &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_reshape_2.csv" target="_blank"&gt;repeated measures&lt;/a&gt; datasets (.csv) used in this tutorial. Be sure to right-click and save the files to your R working directory. The between groups dataset contains a hypothetical sample of 30 cases separated into three groups (a, b, and c). The repeated measures dataset contains a hypothetical sample of 10 cases across three measurements (a, b, and c). In both cases, the values are represented on a scale that ranges from 1 to 5.&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our datasets into R and store their contents in variables.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the datasets into R variables using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataBetween &amp;lt;- read.csv("dataset_ANOVA_reshape_1.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; dataRepeated &amp;lt;- read.csv("dataset_ANOVA_reshape_2.csv")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Reshape Package&lt;/h3&gt;Next, we need to install and load the &lt;em&gt;reshape&lt;/em&gt; package. In this tutorial, we will make use of the package's &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;cast()&lt;/span&gt; and &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;melt()&lt;/span&gt; functions.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #install the package&lt;/li&gt;
&lt;li&gt;&amp;gt; install.packages("reshape")&lt;/li&gt;
&lt;li&gt;&amp;gt; #load the package&lt;/li&gt;
&lt;li&gt;&amp;gt; library(reshape)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Using cast() to Derive ANOVA Descriptives&lt;/h3&gt;The  &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;cast()&lt;/span&gt; function can be used to easily derive summary statistics for a between groups ANOVA dataset. The &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;cast()&lt;/span&gt; function receives the following primary arguments.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;data: the dataset&lt;/li&gt;
&lt;li&gt;formula: in our case, a one-sided formula indicating the grouping variable&lt;/li&gt;
&lt;li&gt;fun.aggregate: a function or vector of functions for deriving summary statistics, such as mean, var, or sd&lt;/li&gt;
&lt;/ul&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #display the raw between groups data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataBetween&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-qENWqmSzDwI/T8FKHaOMw1I/AAAAAAAAA68/ecbHNiPsAPQ/s1600/20110314_anova_reshape_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-qENWqmSzDwI/T8FKHaOMw1I/AAAAAAAAA68/ecbHNiPsAPQ/s1600/20110314_anova_reshape_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The raw between groups data&lt;/div&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #cast the between groups data using cast(data, formula, fun.aggregate) to get the group means&lt;/li&gt;
&lt;li&gt;&amp;gt; cast(dataBetween, formula = ~group, fun.aggregate = mean)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-O6sKYFRuSio/T8FKHwOpolI/AAAAAAAAA7M/NRdtMP7505E/s1600/20110314_anova_reshape_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-O6sKYFRuSio/T8FKHwOpolI/AAAAAAAAA7M/NRdtMP7505E/s1600/20110314_anova_reshape_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The casted data with means&lt;/div&gt;&lt;br /&gt;
Note that the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;fun.aggregate&lt;/span&gt; argument can also receive a vector of summary statistics functions. This will yield all of the requested descriptives via a single &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;cast()&lt;/span&gt; function.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #cast the between groups data using cast(data, formula, fun.aggregate) to get the group means, variances, and standard deviations&lt;/li&gt;
&lt;li&gt;&amp;gt; cast(dataBetween, formula = ~group, fun.aggregate = c(mean, var, sd))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-L6RrWuSiqQk/T8FKHwiecmI/AAAAAAAAA7Q/LJqgrsh69r0/s1600/20110314_anova_reshape_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="32" src="http://1.bp.blogspot.com/-L6RrWuSiqQk/T8FKHwiecmI/AAAAAAAAA7Q/LJqgrsh69r0/s400/20110314_anova_reshape_3.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The casted data with descriptives&lt;/div&gt;&lt;h3&gt;Using melt() to Prepare Repeated Measures Data for Pairwise Comparisons&lt;/h3&gt;The  &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;melt()&lt;/span&gt; function can be used to morph a repeated measures ANOVA dataset prior to conducting pairwise comparisons. The &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;melt()&lt;/span&gt; function receives the following primary arguments.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;data: the dataset&lt;/li&gt;
&lt;li&gt;id.vars: the id variable or a vector of values that can be used as ids&lt;/li&gt;
&lt;li&gt;measure.vars: a vector containing the variables to be melted&lt;/li&gt;
&lt;li&gt;variable_name: the name of the column containing the melted variables&lt;/li&gt;
&lt;/ul&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #display the repeated measures data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataRepeated&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-GUSmU7e-di8/T8FKH_g9S3I/AAAAAAAAA7U/3H3WDvTKK8A/s1600/20110314_anova_reshape_4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-GUSmU7e-di8/T8FKH_g9S3I/AAAAAAAAA7U/3H3WDvTKK8A/s1600/20110314_anova_reshape_4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The raw repeated measures data&lt;/div&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #melt the repeated measures data using melt(data, id.vars, measure.vars, variable_name) to organize it for pairwise comparisons&lt;/li&gt;
&lt;li&gt;&amp;gt; melt(dataRepeated, id.vars = "case", measure.vars = c("valueA", "valueB", "valueC"), variable_name = "abcValues")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-fd9QdF-ODz0/T8FKIGYFivI/AAAAAAAAA7s/ekBP5LQMkl4/s1600/20110314_anova_reshape_5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-fd9QdF-ODz0/T8FKIGYFivI/AAAAAAAAA7s/ekBP5LQMkl4/s1600/20110314_anova_reshape_5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The melted repeated measures data&lt;/div&gt;&lt;br /&gt;
Note that the data are now prepared to be used in the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function. See the &lt;a href="http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-one-way-anova-with.html" target="_blank"&gt;One-Way ANOVA with Pairwise Comparisons&lt;/a&gt; tutorial for details on using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function.&lt;br /&gt;
&lt;h3&gt;Complete ANOVA Reshape Example&lt;/h3&gt;To see a complete example of how ANOVA data can be organized using the &lt;em&gt;reshape&lt;/em&gt; package in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_reshape.txt" target="_blank"&gt;ANOVA reshape example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/uBwUngF18-k" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/uBwUngF18-k/r-tutorial-series-applying-reshape.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-qENWqmSzDwI/T8FKHaOMw1I/AAAAAAAAA68/ecbHNiPsAPQ/s72-c/20110314_anova_reshape_1.png" height="72" width="72" /><thr:total>4</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/03/r-tutorial-series-applying-reshape.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-7160272712432283929</guid><pubDate>Mon, 07 Mar 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:36:47.535-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">Holm</category><category domain="http://www.blogger.com/atom/ns#">Bonferroni</category><category domain="http://www.blogger.com/atom/ns#">Tukey HSD</category><category domain="http://www.blogger.com/atom/ns#">Fisher LSD</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">pairwise comparisons</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: ANOVA Pairwise Comparison Methods</title><description>When we have a statistically significant effect in ANOVA and an independent variable of more than two levels, we typically want to make follow-up comparisons. There are numerous methods for making pairwise comparisons and this tutorial will demonstrate how to execute several different techniques in R.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_comparisonMethods.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 30 participants who are divided into three stress reduction treatment groups (mental, physical, and medical). The values are represented on a scale that ranges from 1 to 5. This dataset can be conceptualized as a comparison between three stress treatment programs, one using mental methods, one using physical training, and one using medication. The values represent how effective the treatment programs were at reducing participant's stress levels, with higher numbers indicating higher effectiveness.&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataPairwiseComparisons &amp;lt;- read.csv("dataset_ANOVA_OneWayComparisons.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataPairwiseComparisons &lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-WxHzPxXIomI/T8FKGlEIFjI/AAAAAAAAA6Q/rtN9ikTjhL8/s1600/20110307_anova_comparisonMethods_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-WxHzPxXIomI/T8FKGlEIFjI/AAAAAAAAA6Q/rtN9ikTjhL8/s1600/20110307_anova_comparisonMethods_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our dataset&lt;/div&gt;&lt;h3&gt;Omnibus ANOVA&lt;/h3&gt;For the purposes of this tutorial, we will assume that the omnibus ANOVA has already been conducted and that the main effect for treatment was statistically significant. For details on this process, see the &lt;a href="http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-one-way-anova-with.html" target="_blank"&gt;One-Way ANOVA with Pairwise Comparisons&lt;/a&gt; tutorial, which uses the same dataset.&lt;br /&gt;
&lt;h3&gt;Means&lt;/h3&gt;Let's also look at the means of our treatment groups. Here, we will use the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;tapply()&lt;/span&gt; function, along with the following arguments, to generate a table of means.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;X: the data&lt;/li&gt;
&lt;li&gt;INDEX: a list() of factor variables&lt;/li&gt;
&lt;li&gt;FUN: the function to be applied&lt;/li&gt;
&lt;/ul&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use tapply(X, INDEX, FUN) to generate a table displaying each treatment group mean&lt;/li&gt;
&lt;li&gt;&amp;gt; tapply(X = dataPairwiseComparisons$StressReduction, INDEX = list(dataPairwiseComparisons$Treatment), FUN = mean)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-CxsN4gqGd-s/T8FKGizzM5I/AAAAAAAAA6M/gcI3XZwC74Y/s1600/20110307_anova_comparisonMethods_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-CxsN4gqGd-s/T8FKGizzM5I/AAAAAAAAA6M/gcI3XZwC74Y/s1600/20110307_anova_comparisonMethods_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The treatment group means&lt;/div&gt;&lt;h3&gt;Pairwise Comparisons&lt;/h3&gt;We will cover five major techniques for controlling Type I error when making pairwise comparisons. These methods are no adjustment, Bonferroni's adjustment, Holm's adjustment, Fisher's LSD, and Tukey's HSD. All of these techniques will be demonstrated on our sample dataset, although the decision as to which to use in a given situation is left up to the reader.&lt;br /&gt;
&lt;h4&gt;pairwise.t.test()&lt;/h4&gt;Our first three methods will make use of the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function, which has the following major arguments.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;x: the dependent variable&lt;/li&gt;
&lt;li&gt;g: the independent variable&lt;/li&gt;
&lt;li&gt;p.adj: the p-value adjustment method used to control for the family-wise Type I error rate across the comparisons; one of "none", "bonferroni", "holm", "hochberg", "hommel", "BH", or "BY"&lt;/li&gt;
&lt;/ul&gt;&lt;h4&gt;No Adjustment&lt;/h4&gt;Using &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;p.adj = "none"&lt;/span&gt; in the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function makes no correction for the Type I error rate across the pairwise tests. This technique can be useful for employing methods that are not already built into R functions, such as the Shaffer/Modified Shaffer, which use different alpha level divisors based on the number of levels composing the independent variable. The console results will contain no adjustment, but the researcher can manually consider the statistical significance of the p-values under his or her desired alpha level.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use pairwise.t.test(x, g, p.adj) to test the pairwise comparisons between the treatment group means&lt;/li&gt;
&lt;li&gt;&amp;gt; #no adjustment&lt;/li&gt;
&lt;li&gt;&amp;gt; pairwise.t.test(dataPairwiseComparisons$StressReduction, dataPairwiseComparisons$Treatment, p.adj = "none")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-XcyvFcdOW4o/T8FKGzPgwUI/AAAAAAAAA6c/fH5PBt4ahUQ/s1600/20110307_anova_comparisonMethods_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-XcyvFcdOW4o/T8FKGzPgwUI/AAAAAAAAA6c/fH5PBt4ahUQ/s1600/20110307_anova_comparisonMethods_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of treatment group means with no adjustment&lt;/div&gt;&lt;br /&gt;
With no adjustment, the mental-medical and physical-medical comparisons are statistically significant, whereas the mental-physical comparison is not. This suggests that both the mental and physical treatments are superior to the medical treatment, but that there is insufficient statistical support to distinguish between the mental and physical treatments.&lt;br /&gt;
&lt;h4&gt;Bonferroni Adjustment&lt;/h4&gt;The Bonferroni adjustment simply divides the Type I error rate (.05) by the number of tests (in this case, three). Hence, this method is often considered overly conservative. The Bonferroni adjustment can be made using &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;p.adj = "bonferroni"&lt;/span&gt; in the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #Bonferroni adjustment&lt;/li&gt;
&lt;li&gt;&amp;gt; pairwise.t.test(dataPairwiseComparisons$StressReduction, dataPairwiseComparisons$Treatment, p.adj = "bonferroni")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-Ctgt_WxceWU/T8FKG5wRc0I/AAAAAAAAA6k/eHsIsrIZMHY/s1600/20110307_anova_comparisonMethods_4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-Ctgt_WxceWU/T8FKG5wRc0I/AAAAAAAAA6k/eHsIsrIZMHY/s1600/20110307_anova_comparisonMethods_4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of treatment group means using Bonferroni adjustment&lt;/div&gt;&lt;br /&gt;
Using the Bonferroni adjustment, only the mental-medical comparison is statistically significant. This suggests that the mental treatment is superior to the medical treatment, but that there is insufficient statistical support to distinguish between the mental and physical treatments and the physical and medical treatments. Notice that these results are more conservative than with no adjustment.&lt;br /&gt;
&lt;h4&gt;Holm Adjustment&lt;/h4&gt;The Holm adjustment sequentially compares the lowest p-value with a Type I error rate that is reduced for each consecutive test. In our case, this means that our first p-value is tested at the .05/3 level (.017),  second at the .05/2 level (.025), and third at the .05/1 level (.05).  This method is generally considered superior to the Bonferroni adjustment and can be employed using &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;p.adj = "holm"&lt;/span&gt; in the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #Holm adjustment&lt;/li&gt;
&lt;li&gt;&amp;gt; pairwise.t.test(dataPairwiseComparisons$StressReduction, dataPairwiseComparisons$Treatment, p.adj = "holm")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-aThW1ZFASJ4/T8FKHGP-8VI/AAAAAAAAA6o/5vxVz5DqX-c/s1600/20110307_anova_comparisonMethods_5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-aThW1ZFASJ4/T8FKHGP-8VI/AAAAAAAAA6o/5vxVz5DqX-c/s1600/20110307_anova_comparisonMethods_5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of treatment group means using Holm adjustment&lt;/div&gt;&lt;br /&gt;
Using the Holm procedure, our results are practically (but not mathematically) identical to using no adjustment.&lt;br /&gt;
&lt;h4&gt;LSD Method&lt;/h4&gt;The Fisher Least Significant Difference (LSD) method essentially does not correct for the Type I error rate for multiple comparisons and is generally not recommended relative to other options. However, should the need arise to employ this method, one should seek out the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;LSD.test()&lt;/span&gt; function in the &lt;em&gt;agricolae&lt;/em&gt; package, which has the following major arguments.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;y: the dependent variable&lt;/li&gt;
&lt;li&gt;trt: the independent variable&lt;/li&gt;
&lt;li&gt;DFerror: the degrees of freedom error&lt;/li&gt;
&lt;li&gt;MSerror: the mean squared error&lt;/li&gt;
&lt;/ul&gt;Note that the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;DFerror&lt;/span&gt; and &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;MSerror&lt;/span&gt; can be found in the omnibus ANOVA table.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #load the agricolae package (install first, if necessary)&lt;/li&gt;
&lt;li&gt;&amp;gt; library(agricolae)&lt;/li&gt;
&lt;li&gt;#LSD method&lt;/li&gt;
&lt;li&gt;#use LSD.test(y, trt, DFerror, MSerror) to test the pairwise comparisons between the treatment group means&lt;/li&gt;
&lt;li&gt;&amp;gt; LSD.test(dataPairwiseComparisons$StressReduction, dataPairwiseComparisons$Treatment, 30.5, 1.13)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-8go73ribmyY/T8FKHLRNFkI/AAAAAAAAA64/WG_RNorcXFQ/s1600/20110307_anova_comparisonMethods_6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-8go73ribmyY/T8FKHLRNFkI/AAAAAAAAA64/WG_RNorcXFQ/s1600/20110307_anova_comparisonMethods_6.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of treatment group means using LSD method&lt;/div&gt;&lt;br /&gt;
Using the LSD method, our results are practically (but not mathematically) identical to using no adjustment or the Holm procedure.&lt;br /&gt;
&lt;h4&gt;HSD Method&lt;/h4&gt;The Tukey Honest Significant Difference (HSD) method controls for the Type I error rate across multiple comparisons and is generally considered an acceptable technique. This method can be executed using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;TukeyHSD(x)&lt;/span&gt; function, where &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;x&lt;/span&gt; is a linear model object created using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;aov(formula, data)&lt;/span&gt; function. Note that in this application, the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;aov(formula, data)&lt;/span&gt; function is identical to the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(formula, data)&lt;/span&gt; that we are already familiar with from &lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-simple-linear.html" target="_blank"&gt;linear regression&lt;/a&gt;.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #HSD method&lt;/li&gt;
&lt;li&gt;&amp;gt; #use TukeyHSD(x), in tandem with aov(formula, data), to test the pairwise comparisons between the treatment group means&lt;/li&gt;
&lt;li&gt;TukeyHSD(aov(StressReduction ~ Treatment, dataPairwiseComparisons))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-NbSnQBu_utw/T8FKHRmswHI/AAAAAAAAA7A/53NZVpklO0g/s1600/20110307_anova_comparisonMethods_7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-NbSnQBu_utw/T8FKHRmswHI/AAAAAAAAA7A/53NZVpklO0g/s1600/20110307_anova_comparisonMethods_7.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of treatment group means using HSD method&lt;/div&gt;&lt;br /&gt;
Using the HSD method, our results are practically (but not mathematically) identical to using the Bonferroni, Holm, or LSD methods. &lt;br /&gt;
&lt;h3&gt;Complete Pairwise Comparisons Example&lt;/h3&gt;To see a complete example of how various pairwise comparison techniques can be applied in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_comparisonMethods.txt" target="_blank"&gt;ANOVA pairwise comparisons example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/A9gRXiQp8zo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/A9gRXiQp8zo/r-tutorial-series-anova-pairwise.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-WxHzPxXIomI/T8FKGlEIFjI/AAAAAAAAA6Q/rtN9ikTjhL8/s72-c/20110307_anova_comparisonMethods_1.png" height="72" width="72" /><thr:total>5</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/03/r-tutorial-series-anova-pairwise.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-2339830260901121974</guid><pubDate>Mon, 28 Feb 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:35:06.319-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">unequal</category><category domain="http://www.blogger.com/atom/ns#">two-way</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">cell size</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">sample size</category><title>R Tutorial Series: Two-Way ANOVA with Unequal Sample Sizes</title><description>When the sample sizes within the levels of our independent variables are not equal, we have to handle our ANOVA differently than in the typical two-way case. This tutorial will demonstrate how to conduct a two-way ANOVA in R when the sample sizes within each level of the independent variables are not the same.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_twoWay_unequalSample.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 30 students who were exposed to one of two learning environments (offline or online) and one of two methods of instruction (classroom or tutor), then tested on a math assessment. Possible math scores range from 0 to 100 and indicate how well each student performed on the math assessment. Each student participated in either an offline or online learning environment and received either classroom instruction (i.e. one to many) or instruction from a personal tutor (i.e. one to one).&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayUnequalSample &amp;lt;- read.csv("dataset_ANOVA_TwoWayUnequalSample.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayUnequalSample&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-rFjx6KNhpcA/T8FKFv3GnVI/AAAAAAAAA5c/vAJ8iqcOMOU/s1600/20110228_anova_twoWay_unequalSample_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-rFjx6KNhpcA/T8FKFv3GnVI/AAAAAAAAA5c/vAJ8iqcOMOU/s1600/20110228_anova_twoWay_unequalSample_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our dataset&lt;/div&gt;&lt;h3&gt;Unequal Sample Sizes&lt;/h3&gt;In our study, 16 students participated in the online environment, whereas only 14 participated in the offline environment. Further, 20 students received classroom instruction, whereas only 10 received personal tutor instruction. As such, we should take action to compensate for the unequal sample sizes in order to retain the validity of our analysis. Generally, this comes down to examining the correlation between the factors and the causes of the unequal sample sizes en route to choosing whether to use weighted or unweighted means - a decision which can drastically impact the results of an ANOVA. This tutorial will demonstrate how to conduct ANOVA using both weighted and unweighted means. Thus, the ultimate decision as to the use of weighted or unweighted means is left up to each individual and his or her specific circumstances.&lt;br /&gt;
&lt;h3&gt;Weighted Means&lt;/h3&gt;First, let's suppose that we decided to go with weighted means, which take into account the correlation between our factors that results from having treatment groups with different sample sizes. A weighted mean is calculated by simply adding up all of the values and dividing by the total number of values. Consequently, we can easily derive the weighted means for each treatment group using our &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;subset(data, condition)&lt;/span&gt; and &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;mean(data)&lt;/span&gt; functions.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use subset(data, condition) to create subsets for each treatment group&lt;/li&gt;
&lt;li&gt;&amp;gt; #offline subset&lt;/li&gt;
&lt;li&gt;&amp;gt; offlineData &amp;lt;- subset(dataTwoWayUnequalSample, dataTwoWayUnequalSample$environment == "offline")&lt;/li&gt;
&lt;li&gt;&amp;gt; #online subset&lt;/li&gt;
&lt;li&gt;&amp;gt; onlineData &amp;lt;- subset(dataTwoWayUnequalSample, dataTwoWayUnequalSample$environment == "online")&lt;/li&gt;
&lt;li&gt;&amp;gt; #classroom subset&lt;/li&gt;
&lt;li&gt;&amp;gt; classroomData &amp;lt;- subset(dataTwoWayUnequalSample, dataTwoWayUnequalSample$instruction == "classroom")&lt;/li&gt;
&lt;li&gt;&amp;gt; #tutor subset&lt;/li&gt;
&lt;li&gt;&amp;gt; tutorData &amp;lt;- subset(dataTwoWayUnequalSample, dataTwoWayUnequalSample$instruction == "tutor")&lt;/li&gt;
&lt;li&gt;&amp;gt; #use mean(data) to calculate the weighted means for each treatment group&lt;/li&gt;
&lt;li&gt;&amp;gt; #offline weighted mean&lt;/li&gt;
&lt;li&gt;&amp;gt; mean(offlineData$math)&lt;/li&gt;
&lt;li&gt;&amp;gt; #online weighted mean&lt;/li&gt;
&lt;li&gt;&amp;gt; mean(onlineData$math)&lt;/li&gt;
&lt;li&gt;&amp;gt; #classroom weighted mean&lt;/li&gt;
&lt;li&gt;&amp;gt; mean(classroomData$math)&lt;/li&gt;
&lt;li&gt;&amp;gt; #tutor weighted mean&lt;/li&gt;
&lt;li&gt;&amp;gt; mean(tutorData$math)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-hainF6pJ8MM/T8FKF9WBGjI/AAAAAAAAA5s/4UsxV4UTlvA/s1600/20110228_anova_twoWay_unequalSample_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-hainF6pJ8MM/T8FKF9WBGjI/AAAAAAAAA5s/4UsxV4UTlvA/s1600/20110228_anova_twoWay_unequalSample_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The weighted means for the environment and instruction conditions&lt;/div&gt;&lt;h3&gt;ANOVA using Type I Sums of Squares&lt;/h3&gt;When applying weighted means, it is suggested that we use Type I sums of squares (SS) in our ANOVA. Type I happens to be the default SS used in our standard &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;anova(object)&lt;/span&gt; function, which will be used to execute our analysis. Note that in the case of two-way ANOVA, the ordering of our independent variables matters when using weighted means. Therefore, we must run our ANOVA two times, once with each independent variable taking the lead. However, the interaction effect is not affected by the ordering of the independent variables.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use anova(object) to execute the Type I SS ANOVAs&lt;/li&gt;
&lt;li&gt;&amp;gt; #environment ANOVA&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(math ~ environment * instruction, dataTwoWayUnequalSample))&lt;/li&gt;
&lt;li&gt;&amp;gt; #instruction ANOVA&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(math ~ instruction * environment, dataTwoWayUnequalSample))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-JXle4szoxwc/T8FKF_qt39I/AAAAAAAAA6I/ZkvQRg8zk20/s1600/20110228_anova_twoWay_unequalSample_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-JXle4szoxwc/T8FKF_qt39I/AAAAAAAAA6I/ZkvQRg8zk20/s1600/20110228_anova_twoWay_unequalSample_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The Type I SS ANOVA results. Note the differences in main effects based on the ordering of the independent variables.&lt;/div&gt;&lt;br /&gt;
These results indicate statistically insignificant main effects for both the environment and instruction variables, as well as the interaction between them.&lt;br /&gt;
&lt;h3&gt;Unweighted Means&lt;/h3&gt;Now let's turn to using unweighted means, which essentially ignore the correlation between the independent variables that arise from unequal sample sizes. An unweighted mean is calculated by taking the average of the individual group means. Thus, we can derive our unweighted means by summing the means of each level of our independent variables and dividing by the total number of levels. For instance, to find the unweighted mean for environment, we will add the means for our offline and online groups, then divide by two.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use mean(data) and subset(data, condition) to calculate the unweighted means for each treatment group&lt;/li&gt;
&lt;li&gt;&amp;gt; #offline unweighted mean = (classroom offline mean + tutor offline mean) / 2&lt;/li&gt;
&lt;li&gt;(mean(subset(offlineData$math, offlineData$instruction == "classroom")) + mean(subset(offlineData$math, offlineData$instruction == "tutor"))) / 2&lt;/li&gt;
&lt;li&gt;&amp;gt; #online unweighted mean = (classroom online mean + tutor online mean) / 2&lt;/li&gt;
&lt;li&gt;&amp;gt; (mean(subset(onlineData$math, onlineData$instruction == "classroom")) + mean(subset(onlineData$math, onlineData$instruction == "tutor"))) / 2&lt;/li&gt;
&lt;li&gt;&amp;gt; #classroom unweighted mean = (offline classroom mean + online classroom mean) / 2&lt;/li&gt;
&lt;li&gt;&amp;gt; (mean(subset(classroomData$math, classroomData$environment == "offline")) + mean(subset(classroomData$math, classroomData$environment == "online"))) / 2&lt;/li&gt;
&lt;li&gt;&amp;gt; #tutor unweighted mean = (offline tutor mean + online tutor mean) / 2&lt;/li&gt;
&lt;li&gt;&amp;gt; (mean(subset(tutorData$math, tutorData$environment == "offline")) + mean(subset(tutorData$math, tutorData$environment == "online"))) / 2&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-qjGjU34pu74/T8FKGWlemHI/AAAAAAAAA54/SSDMaoV0Gs8/s1600/20110228_anova_twoWay_unequalSample_4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-qjGjU34pu74/T8FKGWlemHI/AAAAAAAAA54/SSDMaoV0Gs8/s1600/20110228_anova_twoWay_unequalSample_4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The unweighted means for the environment and instruction conditions&lt;/div&gt;&lt;h3&gt;ANOVA using Type III Sums of Squares&lt;/h3&gt;When applying unweighted means, it is suggested that we use Type III sums of squares (SS) in our ANOVA. Type III SS can be set using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;type&lt;/span&gt; argument in the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova(mod, type)&lt;/span&gt; function, which is a member of the &lt;em&gt;car&lt;/em&gt; package.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #load the car package (install first, if necessary)&lt;/li&gt;
&lt;li&gt;&amp;gt; library(car)&lt;/li&gt;
&lt;li&gt;&amp;gt; #use the Anova(mod, type) function to conduct the Type III SS ANOVA&lt;/li&gt;
&lt;li&gt;&amp;gt; Anova(lm(math ~ environment * instruction, dataTwoWayUnequalSample), type = "3")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-XdfQtjUha6M/T8FKGexEyHI/AAAAAAAAA58/3390cOxn0J4/s1600/20110228_anova_twoWay_unequalSample_5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-XdfQtjUha6M/T8FKGexEyHI/AAAAAAAAA58/3390cOxn0J4/s1600/20110228_anova_twoWay_unequalSample_5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The Type III SS ANOVA results.&lt;/div&gt;&lt;br /&gt;
Once again, our ANOVA results indicate statistically insignificant main effects for both the environment and instruction variables, as well as the interaction between them. However, it is worth noting that both the means and p-values are different when using unweighted means and Type III SS compared to weighted means and Type I SS. In certain cases, this difference can be quite pronounced and lead to entirely different outcomes between the two methods. Hence, choosing the appropriate means and SS for a given analysis is a matter that should be approached with conscious consideration.&lt;br /&gt;
&lt;h3&gt;Pairwise Comparisons&lt;/h3&gt;Note that since our independent variables contain only two levels, there is no need to conduct follow-up comparisons. However, should you reach this point with a statistically significant independent variable of more than three levels, you could conduct pairwise comparisons in the same manner as demonstrated in the &lt;a href="http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-two-way-anova-with.html" target="_blank"&gt;Two-Way ANOVA with Comparisons&lt;/a&gt; tutorial.&lt;br /&gt;
&lt;h3&gt;Complete Two-Way ANOVA with Unequal Sample Sizes Example&lt;/h3&gt;To see a complete example of how two-way ANOVA with unequal sample sizes can be conducted in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_twoWay_unequalSample.txt" target="_blank"&gt;two-way ANOVA with unequal sample sizes example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/cXv3FZ8N2S4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/cXv3FZ8N2S4/r-tutorial-series-two-way-anova-with_28.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-rFjx6KNhpcA/T8FKFv3GnVI/AAAAAAAAA5c/vAJ8iqcOMOU/s72-c/20110228_anova_twoWay_unequalSample_1.png" height="72" width="72" /><thr:total>8</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/02/r-tutorial-series-two-way-anova-with_28.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-8574974866783965268</guid><pubDate>Mon, 21 Feb 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:33:30.696-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">two-way</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">repeated measures</category><title>R Tutorial Series: Two-Way Repeated Measures ANOVA</title><description>Repeated measures data require a different analysis procedure than our typical two-way ANOVA and subsequently follow a different R process. This tutorial will demonstrate how to conduct two-way repeated measures ANOVA in R using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova()&lt;/span&gt; function from the &lt;em&gt;car&lt;/em&gt; package.&lt;br /&gt;
Note that the two-way repeated measures ANOVA process can be very complex to organize and execute in R. Although it has been distilled into just a few small steps in this guide, it is recommended that you fully and precisely complete the example before experimenting with your own data. As you will see, organization of the raw data is critical to successfully conducting a two-way repeated measures ANOVA using the demonstrated technique.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_twoWay_repeatedMeasures.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; and &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_twoWay_repeatedMeasures_idata.csv" target="_blank"&gt;sample idata frame (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the files to your R working directory. This dataset contains a hypothetical sample of 30 participants whose interest in school and interest in work was measured at three different ages (10, 15, and 20). The interest values are represented on a scale that ranges from 1 to 5 and indicate how interested each participant was in a given topic at each given age.&lt;br /&gt;
&lt;h3&gt;Data Setup&lt;/h3&gt;Notice that our data are arranged differently for a repeated measures ANOVA. In a typical two-way ANOVA, we would place all of the values of our independent variable in a single column and identify their respective levels with a second column, as demonstrated in this &lt;a href="http://www.dailyi.org/blogFiles/RTutorialSeries/dataset_ANOVA_TwoWay.csv" target="_blank"&gt;sample two-way dataset&lt;/a&gt;. In a two-way repeated measures ANOVA, we instead combine each independent variable with its time interval, thus yielding columns for each pairing. Hence, rather than having one vertical column for school interest and one for work interest, with a second column for age, we have six separate columns for interest, three for school interest and three for work interest at each age level. The following graphic is intended to help demonstrate this organization method.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-JbtPXVx43jE/T8FKE7-rC0I/AAAAAAAAA48/7b5aHtJ-0gk/s1600/20110221_anova_twoWay_repeatedMeasures_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-JbtPXVx43jE/T8FKE7-rC0I/AAAAAAAAA48/7b5aHtJ-0gk/s1600/20110221_anova_twoWay_repeatedMeasures_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Treat time as if it were an independent variable. Then combine each independent variable with each level of time and arrange the columns horizontally.&lt;/div&gt;&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayRepeatedMeasures &amp;lt;- read.csv("dataset_ANOVA_TwoWayRepeatedMeasures.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; #notice the atypical column arrangement for repeated measures data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayRepeatedMeasures&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-aIoropoyPEQ/T8FKFEByeoI/AAAAAAAAA5I/CK3AohvwnHk/s1600/20110221_anova_twoWay_repeatedMeasures_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-aIoropoyPEQ/T8FKFEByeoI/AAAAAAAAA5I/CK3AohvwnHk/s1600/20110221_anova_twoWay_repeatedMeasures_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our dataset&lt;/div&gt;&lt;h3&gt;idata Frame&lt;/h3&gt;Another item that we need to import for this analysis is our idata frame. This object will be used in our &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova()&lt;/span&gt; function to define the structure of our analysis.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the idata frame into an R variable&lt;/li&gt;
&lt;li&gt;&amp;gt; idataTwoWayRepeatedMeasures &amp;lt;- read.csv("idata_ANOVA_TwoWayRepeatedMeasures.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the idata frame&lt;/li&gt;
&lt;li&gt;&amp;gt; #notice the text values and correspondence between our idata rows and the columns in our dataset&lt;/li&gt;
&lt;li&gt;&amp;gt; idataTwoWayRepeatedMeasures&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-ebN1RUVUX1I/T8FKFIjtZDI/AAAAAAAAA5M/KgOO38DmlKo/s1600/20110221_anova_twoWay_repeatedMeasures_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-ebN1RUVUX1I/T8FKFIjtZDI/AAAAAAAAA5M/KgOO38DmlKo/s1600/20110221_anova_twoWay_repeatedMeasures_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The idata frame&lt;/div&gt;&lt;br /&gt;
Note that it is critical that your idata frame take the demonstrated form for this technique to work. I experimented with several alternative, perhaps more intuitive, layouts without success. It is particularly important to notice that both columns of the idata frame contain text values (not numerical ones - hence the repeated prefixing of &lt;em&gt;Age&lt;/em&gt; to the values in every row of the &lt;em&gt;Age&lt;/em&gt; column). Additionally, if you read the rows of the idata frame horizontally, you will see that they correspond precisely to the columns of our dataset. The following graphic is intended to help demonstrate this organization method.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-E2NK7JAJYIY/T8FKFWwCG5I/AAAAAAAAA5Y/nNBFeviI59Y/s1600/20110221_anova_twoWay_repeatedMeasures_4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-E2NK7JAJYIY/T8FKFWwCG5I/AAAAAAAAA5Y/nNBFeviI59Y/s1600/20110221_anova_twoWay_repeatedMeasures_4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;Use only text values in your idata frame. Ensure that the rows of your idata frame correspond to the columns in your dataset.&lt;/div&gt;&lt;h3&gt;Linear Model&lt;/h3&gt;Prior to executing our analysis, we must follow two steps to formulate our linear model to be used in the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova()&lt;/span&gt; function.&lt;br /&gt;
&lt;h4&gt;Step 1: Bind the Columns&lt;/h4&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use cbind() to bind the columns of the original dataset&lt;/li&gt;
&lt;li&gt;&amp;gt; interestBind &amp;lt;- cbind(dataTwoWayRepeatedMeasures$schoolAge10, dataTwoWayRepeatedMeasures$schoolAge15, dataTwoWayRepeatedMeasures$schoolAge20, dataTwoWayRepeatedMeasures$workAge10, dataTwoWayRepeatedMeasures$workAge15, dataTwoWayRepeatedMeasures$workAge20)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h4&gt;Step 2: Define the Model&lt;/h4&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use lm() to generate a linear model using the bound columns from step 1&lt;/li&gt;
&lt;li&gt;&amp;gt; interestModel &amp;lt;- lm(interestBind ~ 1)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;&lt;em&gt;Anova(mod, idata, idesign)&lt;/em&gt; Function&lt;/h3&gt;Typically, researchers will choose one of several techniques for analyzing repeated measures data, such as an epsilon-correction method, like Huynh-Feldt or Greenhouse-Geisser, or a multivariate method, like Wilks' Lambda or Hotelling's Trace. Conveniently, having already prepared our data, we can employ a single &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova(mod, idata, idesign)&lt;/span&gt;  function from the &lt;em&gt;car&lt;/em&gt; package to yield all of the relevant repeated measures results. This allows us simplicity in that only a single function is required, regardless of the technique that we wish to employ. Thus, witnessing our outcomes becomes as simple as locating the desired method in the cleanly printed results.&lt;br /&gt;
Our &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova(mod, idata, idesign)&lt;/span&gt; function will be composed of three arguments. First, &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;mod&lt;/span&gt; will contain our linear model. Second, &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;idata&lt;/span&gt; will contain our data frame. Third, &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;idesign&lt;/span&gt; will contain a multiplication of the row headings from our idata frame (in other words, our independent variables), preceded by a tilde (~). Thus, our final function takes on the following form.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #load the car package (install first, if necessary)&lt;/li&gt;
&lt;li&gt;library(car)&lt;/li&gt;
&lt;li&gt;&amp;gt; #compose the Anova(mod, idata, idesign) function&lt;/li&gt;
&lt;li&gt;&amp;gt; analysis &amp;lt;- Anova(interestModel, idata = idataTwoWayRepeatedMeasures, idesign = ~Interest * Age)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Results Summary&lt;/h3&gt;Finally, we can use the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;summary(object)&lt;/span&gt; function to visualize the results of our repeated measures ANOVA.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use summary(object) to visualize the results of the repeated measures ANOVA&lt;/li&gt;
&lt;li&gt;&amp;gt; summary(analysis)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-0Gm4chKy_VY/T8FKFQPjb0I/AAAAAAAAA5o/iAWGd-_CEso/s1600/20110221_anova_twoWay_repeatedMeasures_5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-0Gm4chKy_VY/T8FKFQPjb0I/AAAAAAAAA5o/iAWGd-_CEso/s1600/20110221_anova_twoWay_repeatedMeasures_5.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Relevant segment of repeated measures ANOVA results&lt;/div&gt;&lt;br /&gt;
Supposing that we are interested in the Wilks' Lambda method, we can see that there is a statistically significant interaction effect between interest in school and interest in work across the age groups (&lt;em&gt;p&lt;/em&gt; &amp;lt; .001). This suggests that we should further examine our data at the level of simple main effects. For more information investigating on simple main effects, see the &lt;a href="http://rtutorialseries.blogspot.com/2011/02/r-tutorial-series-two-way-anova-with.html" target="_blank"&gt;Two-Way ANOVA with Interactions and Simple Main Effects&lt;/a&gt; tutorial. Of course, in this case of repeated measures ANOVA, another way to break the data down would be to run two &lt;a href="http://rtutorialseries.blogspot.com/2011/02/r-tutorial-series-one-way-repeated.html" target="_blank"&gt;one-way repeated measures ANOVAs&lt;/a&gt;, one for each of the independent variables. In either instance, &lt;a href="http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-two-way-anova-with.html" target="_blank"&gt;pairwise comparisons&lt;/a&gt; can be conducted to determine the significance of the differences between the levels of any significant effects.&lt;br /&gt;
&lt;h3&gt;Complete Two-Way Repeated Measures ANOVA Example&lt;/h3&gt;To see a complete example of how two-way repeated measures ANOVA can be conducted in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_twoWay_repeatedMeasures.txt" target="_blank"&gt;two-way repeated measures ANOVA example (.txt)&lt;/a&gt; file.&lt;br /&gt;
&lt;h3&gt;References&lt;/h3&gt;Moore, Colleen. (n.d.). 610 R9 -- Two-way Repeated-measures Anova. Retrieved January 21, 2011 from http://psych.wisc.edu/moore/Rpdf/610-R9_Within2way.pdf&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/UfCLWHf_3dM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/UfCLWHf_3dM/r-tutorial-series-two-way-repeated.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-JbtPXVx43jE/T8FKE7-rC0I/AAAAAAAAA48/7b5aHtJ-0gk/s72-c/20110221_anova_twoWay_repeatedMeasures_1.png" height="72" width="72" /><thr:total>14</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/02/r-tutorial-series-two-way-repeated.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-3265947639041470786</guid><pubDate>Thu, 17 Feb 2011 14:00:00 +0000</pubDate><atom:updated>2011-02-17T07:00:03.955-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">R Graphs Cookbook</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">review</category><title>Book Review: R Graphs Cookbook</title><description>&lt;h3&gt;Book Information&lt;/h3&gt;&lt;p&gt;Mittal, H. (2011). &lt;em&gt;R graphs cookbook&lt;/em&gt;. Birmingham, UK: Packt Publishing Ltd.&lt;/p&gt;&lt;h3&gt;Audience&lt;/h3&gt;&lt;p&gt;The book's stated audience is anyone who is familiar with the basics of R, as well as expert users who are looking for a graphical reference. However, it is my opinion that the book is better suited for advanced users who are already somewhat familiar with R graphics and are very comfortable with programming in R.&lt;/p&gt;&lt;h3&gt;Content&lt;/h3&gt;&lt;p&gt;To begin, the first chapter of &lt;em&gt;R Graphs Cookbook&lt;/em&gt; rapidly introduces all of the major graphic types covered in the book. Next, in Chapter two, readers are acquainted with various arguments and modification functions that are used throughout the book to customize and enhance visuals. Subsequently, individual chapters focus on specific topics in R graphics, such as:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;scatterplots,&lt;/li&gt;&lt;li&gt;line and time series charts,&lt;/li&gt;&lt;li&gt;bar, dot, and pie charts,&lt;/li&gt;&lt;li&gt;histograms,&lt;/li&gt;&lt;li&gt;box plots,&lt;/li&gt;&lt;li&gt;heat and contour maps,&lt;/li&gt;&lt;li&gt;geographical maps, and&lt;/li&gt;&lt;li&gt;exporting and annotating graphics.&lt;/li&gt;&lt;/ol&gt;&lt;h3&gt;Analysis&lt;/h3&gt;&lt;p&gt;I will start with some general impressions, before moving into chapter by chapter analyses.&lt;/p&gt;&lt;p&gt;First, I feel that the book needs both more and larger screenshots. Often times, recipes are without any visuals and most of the time only one is present, whereas one per major graphical modification is expected. Furthermore, the screenshots are too small. These are critical items to neglect in a book that explicitly deals with visuals. Fortunately, full-size, full-color images are provided with the downloadable code for the book.&lt;/p&gt;&lt;p&gt;Second, I feel that the topics presented in the book are glanced over with far too little explanation. This is the main reason that I feel it is not suited for those who are not already well versed in R programming. Moreover, &lt;i&gt;R Graphs Cookbook&lt;/i&gt; frequently refers the reader to help documentation or to other books on R, which can be frustrating. I personally feel that a book should be largely self-contained, at least when discussing topics within its scope.&lt;/p&gt;&lt;p&gt;Third, I believe that the book could be better organized for use as a fast reference guide and that it generally could be better structured to present information. For example, rather than  tables are a clearer way to present head to head comparisons between objects, and lists are better for describing several function arguments.&lt;/p&gt;&lt;p&gt;On the other hand, I do like the book's code formatting, which displays one argument per line. While this could confuse novice users into thinking that each argument is a separate line of executable code, most readers should find this a welcomed organization style for often lengthy graphics functions. I also enjoyed how the &lt;em&gt;see also&lt;/em&gt; sections at the end of each recipe let me know whether more recipes would build on a given topic.&lt;/p&gt;&lt;p&gt;Continuing, chapter one felt like a whirlwind of information that charged forward with a lack of purpose, organization, and explanation. Chapter two was much better, offering several nice recipes that were fast and easy to digest, with just enough information provided.&lt;/p&gt;&lt;p&gt;Chapter three takes an in-depth look at scatterplots and provides a number of useful recipes, such as how to group data, label points, generate error bars, and create graphical correlation matrices. Similarly, chapter four provides a solid collection of recipes for time series and line charts.&lt;/p&gt;&lt;p&gt;In contrast, chapters five through seven cover a disappointingly sparse amount of material related to their respective topics. Unfortunately, they do not stretch far beyond what is covered in the two graphics-focused chapters of &lt;em&gt;&lt;a href="http://link.packtpub.com/or7f1u" target="_blank"&gt;Statistical Analysis with R&lt;/a&gt;&lt;/em&gt;, which is a guide for newcomers and early beginners. From an advanced reference like &lt;em&gt;R Graphs Cookbook&lt;/em&gt;, I expected broader coverage. For instance, very few external packages are presented in this book, with the author choosing to focus on built-in graphics functions almost exclusively. An introduction to external options, such as &lt;em&gt;ggplot2&lt;/em&gt;, would be warmly welcomed.&lt;/p&gt;&lt;p&gt;Chapters eight and nine relate a few of the lesser covered topics in R, including heat, contour, and geographical maps. These chapters will likely be informative and valuable for readers interested in these graphical applications.&lt;/p&gt;&lt;p&gt;Lastly, chapter ten deals with the presentation and exportation of graphics. While I wish a deeper exploration was made, there are some useful tips in is chapter. Namely, the use of the &lt;em&gt;expression()&lt;/em&gt; function to annotate graphics is well covered.&lt;/p&gt;&lt;h3&gt;Brief Summary&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Title: &lt;em&gt;R Graphs Cookbook&lt;/em&gt;&lt;/li&gt;&lt;li&gt;Author: &lt;a href="http://www.prettygraph.com/" target="_blank"&gt;Hrishi Mittal&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Where To Find: &lt;a href="http://link.packtpub.com/T7PMbW" target="_blank"&gt;Packt Publishing&lt;/a&gt;
&lt;/li&gt;&lt;li&gt;Audience: those who are comfortable programming in R, able to mix, match, apply, and extend recipes for their own purposes, and looking to learn more about R's built-in graphical capabilities.&lt;/li&gt;&lt;li&gt;Content: a loosely associated collection of recipes for applying R's built-in graphics functions to create the most common types of charts, graphs, plots, and maps.&lt;/li&gt;&lt;li&gt;Analysis: although it could have better visuals, structure, and coverage, it is likely that almost any reader will be able to take away valuable techniques from this book&lt;/li&gt;&lt;li&gt;Arbitrary Rating: 6/10&lt;/li&gt;&lt;li&gt;Recommendation: take a look at the table of contents and count the number of recipes that would both be useful to you and that you do not already know how to accomplish to get an idea of how much you will take away from this book; also read the &lt;a href="https://www.packtpub.com/sites/default/files/3067OS-Chapter-2-beyond-the-basics-adjusting-Key-Parameters.pdf" target="_blank"&gt;free sample chapter&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Disclaimer: I received a review copy of this book&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/P6dBs19xt74" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/P6dBs19xt74/book-review-r-graphs-cookbook.html</link><author>noreply@blogger.com (John Quick)</author><thr:total>0</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/02/book-review-r-graphs-cookbook.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-729591615944645575</guid><pubDate>Mon, 14 Feb 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:30:49.523-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">one-way</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">repeated measures</category><title>R Tutorial Series: One-Way Repeated Measures ANOVA</title><description>Repeated measures data require a different analysis procedure than our typical one-way ANOVA and subsequently follow a different R process. This tutorial will demonstrate how to conduct one-way repeated measures ANOVA in R using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova(mod, idata, idesign)&lt;/span&gt; function from the &lt;em&gt;car&lt;/em&gt; package.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_oneWay_repeatedMeasures.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 30 participants whose interest in voting was measured at three different ages (10, 15, and 20). The interest values are represented on a scale that ranges from 1 to 5 and indicate how interested each participant was in voting at each given age.&lt;br /&gt;
&lt;h3&gt;Data Setup&lt;/h3&gt;Notice that our data are arranged differently for a repeated measures ANOVA. In a typical one-way ANOVA, we would place all of the values of our independent variable in a single column and identify their respective levels with a second column, as demonstrated in this &lt;a href="http://www.dailyi.org/blogFiles/RTutorialSeries/dataset_ANOVA_OneWay.csv" target="_blank"&gt;sample one-way dataset&lt;/a&gt;. In a repeated measures ANOVA, we instead treat each level of our independent variable as if it were a variable, thus placing them side by side as columns. Hence, rather than having one vertical column for voting interest, with a second column for age, we have three separate columns for voting interest, one for each age level.&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataOneWayRepeatedMeasures &amp;lt;- read.csv("dataset_ANOVA_OneWayRepeatedMeasures.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; #notice the atypical column arrangement for repeated measures data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataOneWayRepeatedMeasures&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-BL7ll9Cg-Fk/T8FKEo4_X6I/AAAAAAAAA4s/7pNnYcA4p0M/s1600/20110214_anova_oneWay_repeatedMeasures_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-BL7ll9Cg-Fk/T8FKEo4_X6I/AAAAAAAAA4s/7pNnYcA4p0M/s1600/20110214_anova_oneWay_repeatedMeasures_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our dataset&lt;/div&gt;&lt;h3&gt;Preparing the Repeated Measures Factor&lt;/h3&gt;Prior to executing our analysis, we must follow a small series of steps in order to prepare our repeated measures factor.&lt;br /&gt;
&lt;h4&gt;Step 1: Define the Levels&lt;/h4&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use c() to create a vector containing the number of levels within the repeated measures factor&lt;/li&gt;
&lt;li&gt;&amp;gt; #create a vector numbering the levels for our three voting interest age groups&lt;/li&gt;
&lt;li&gt;&amp;gt; ageLevels &amp;lt;- c(1, 2, 3)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h4&gt;Step 2: Define the Factor&lt;/h4&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use as.factor() to create a factor using the level vector from step 1&lt;/li&gt;
&lt;li&gt;&amp;gt; #convert the age levels into a factor&lt;/li&gt;
&lt;li&gt;&amp;gt; ageFactor &amp;lt;- as.factor(ageLevels)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h4&gt;Step 3: Define the Frame&lt;/h4&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use data.frame() to create a data frame using the factor from step 2&lt;/li&gt;
&lt;li&gt;&amp;gt; #convert the age factor into a data frame&lt;/li&gt;
&lt;li&gt;&amp;gt; ageFrame &amp;lt;- data.frame(ageFactor)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h4&gt;Step 4: Bind the Columns&lt;/h4&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use cbind() to bind the levels of the factor from the original dataset&lt;/li&gt;
&lt;li&gt;&amp;gt; #bind the age columns&lt;/li&gt;
&lt;li&gt;&amp;gt; ageBind &amp;lt;- cbind(dataOneWayRepeatedMeasures$Interest10, dataOneWayRepeatedMeasures$Interest15, dataOneWayRepeatedMeasures$Interest20)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h4&gt;Step 5: Define the Model&lt;/h4&gt;&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use lm() to generate a linear model using the bound factor levels from step 4&lt;/li&gt;
&lt;li&gt;&amp;gt; #generate a linear model using the bound age levels&lt;/li&gt;
&lt;li&gt;&amp;gt; ageModel &amp;lt;- lm(ageBind ~ 1)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Employing the &lt;em&gt;Anova(mod, idata, idesign)&lt;/em&gt; Function&lt;/h3&gt;Typically, researchers will choose one of several techniques for analyzing repeated measures data, such as an epsilon-correction method, like Huynh-Feldt or Greenhouse-Geisser, or a multivariate method, like Wilks' Lambda or Hotelling's Trace. Conveniently, having already prepared our data, we can employ a single &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova(mod, idata, idesign)&lt;/span&gt;  function from the &lt;em&gt;car&lt;/em&gt; package to yield all of the relevant repeated measures results. This allows us simplicity in that only a single function is required, regardless of the technique that we wish to employ. Thus, witnessing our outcomes becomes as simple as locating the desired method in the cleanly printed results.&lt;br /&gt;
Our &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;Anova(mod, idata, idesign)&lt;/span&gt; function will be composed of three arguments. First, &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;mod&lt;/span&gt; will contain our linear model from Step 5 in the preceding section. Second, &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;idata&lt;/span&gt; will contain our data frame from Step 3. Third, &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;idesign&lt;/span&gt; will contain our factor from Step 2, preceded by a tilde (~). Thus, our final function takes on the following form.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #load the car package (install first, if necessary)&lt;/li&gt;
&lt;li&gt;library(car)&lt;/li&gt;
&lt;li&gt;&amp;gt; #compose the Anova(mod, idata, idesign) function&lt;/li&gt;
&lt;li&gt;&amp;gt; analysis &amp;lt;- Anova(ageModel, idata = ageFrame, idesign = ~ageFactor)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Visualizing the Results&lt;/h3&gt;Finally, we can use the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;summary(object)&lt;/span&gt; function to visualize the results of our repeated measures ANOVA.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use summary(object) to visualize the results of the repeated measures ANOVA&lt;/li&gt;
&lt;li&gt;&amp;gt; summary(analysis)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-nIpdUa9T_5I/T8FKEi-q9BI/AAAAAAAAA4w/Y5GKIuJfCKE/s1600/20110214_anova_oneWay_repeatedMeasures_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-nIpdUa9T_5I/T8FKEi-q9BI/AAAAAAAAA4w/Y5GKIuJfCKE/s1600/20110214_anova_oneWay_repeatedMeasures_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Relevant segment of repeated measures ANOVA results&lt;/div&gt;&lt;br /&gt;
Supposing that we are interested in the Wilks' Lambda method, we can see that the differences in the means for voting interest at ages 10, 15, and 20 are statistically significant (&lt;em&gt;p&lt;/em&gt; &amp;lt; .001).&lt;br /&gt;
&lt;h3&gt;Pairwise Comparisons&lt;/h3&gt;Note that we could conduct follow-up comparisons on our age factor to determine which age level means are significantly different from one another. For this purpose, it is recommended that the data be rearranged into the standard ANOVA format that we have used throughout our other tutorials. Subsequently, we could conduct pairwise comparisons in the same manner as demonstrated in the &lt;a href="http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-one-way-anova-with.html" target="_blank"&gt;One-Way ANOVA with Comparisons&lt;/a&gt; tutorial.&lt;br /&gt;
&lt;h3&gt;Complete One-Way Repeated Measures ANOVA Example&lt;/h3&gt;To see a complete example of how one-way repeated measures ANOVA can be conducted in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_oneWay_repeatedMeasures.txt" target="_blank"&gt;one-way repeated measures ANOVA example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/mcD7wZHho0s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/mcD7wZHho0s/r-tutorial-series-one-way-repeated.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-BL7ll9Cg-Fk/T8FKEo4_X6I/AAAAAAAAA4s/7pNnYcA4p0M/s72-c/20110214_anova_oneWay_repeatedMeasures_1.png" height="72" width="72" /><thr:total>24</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/02/r-tutorial-series-one-way-repeated.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-2593087957678846202</guid><pubDate>Mon, 07 Feb 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:28:38.173-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">two-way</category><category domain="http://www.blogger.com/atom/ns#">simple main effects</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">interaction effect</category><title>R Tutorial Series: Two-Way ANOVA with Interactions and Simple Main Effects</title><description>When an interaction is present in a two-way ANOVA, we typically choose to ignore the main effects and elect to investigate the simple main effects when making pairwise comparisons. This tutorial will demonstrate how to conduct pairwise comparisons when an interaction is present in a two-way ANOVA.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_twoWay_interactions.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 60 participants who are divided into three stress reduction treatment groups (mental, physical, and medical) and two gender groups (male and female). The stress reduction values are represented on a scale that ranges from 1 to 5. This dataset can be conceptualized as a comparison between three stress treatment programs, one using mental methods, one using physical training, and one using medication across genders. The values represent how effective the treatment programs were at reducing participant's stress levels, with higher numbers indicating higher effectiveness.&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayInteraction &amp;lt;- read.csv("dataset_ANOVA_TwoWayInteraction.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayInteraction&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-IUa_Lg7wsLA/T8FKEAvX6LI/AAAAAAAAA4c/sCUthAx8OmI/s1600/20110207_anova_twoWay_interactions_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-IUa_Lg7wsLA/T8FKEAvX6LI/AAAAAAAAA4c/sCUthAx8OmI/s1600/20110207_anova_twoWay_interactions_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our dataset.&lt;/div&gt;&lt;h3&gt;Omnibus Test&lt;/h3&gt;Let's run a general omnibus test to assess the main effects and interactions present in the dataset.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use anova(object) to test the omnibus hypothesis&lt;/li&gt;
&lt;li&gt;&amp;gt; #Are main effects or interaction effects present in the independent variables?&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(StressReduction ~ Treatment * Gender, dataTwoWayInteraction))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-6fs4WyRC0qk/T8FKEOTZ60I/AAAAAAAAA4Y/diMHkJg9tf8/s1600/20110207_anova_twoWay_interactions_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-6fs4WyRC0qk/T8FKEOTZ60I/AAAAAAAAA4Y/diMHkJg9tf8/s1600/20110207_anova_twoWay_interactions_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The omnibus ANOVA test&lt;/div&gt;&lt;h3&gt;Divide the Data&lt;/h3&gt;The significant omnibus interaction suggests that we should ignore the main effects and instead investigate the simple main effects for our independent variables. To do so, we need to divide our dataset along each level of our treatment variable. We can create subsets of our dataset using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;subset(data, condition)&lt;/span&gt; function, where &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;data&lt;/span&gt; is the original dataset and &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;condition&lt;/span&gt; contains the parameters defining the subset.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use subset(data, condition) to divide the original dataset&lt;/li&gt;
&lt;li&gt;&amp;gt; #medical subset&lt;/li&gt;
&lt;li&gt;&amp;gt; dataMedical &amp;lt;- subset(dataTwoWayInteraction, Treatment == "medical")&lt;/li&gt;
&lt;li&gt;&amp;gt; #mental subset&lt;/li&gt;
&lt;li&gt;&amp;gt; dataMental &amp;lt;- subset(dataTwoWayInteraction, Treatment == "mental")&lt;/li&gt;
&lt;li&gt;&amp;gt; #physical subset&lt;/li&gt;
&lt;li&gt;&amp;gt; dataPhysical &amp;lt;- subset(dataTwoWayInteraction, Treatment == "physical")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Group ANOVAs&lt;/h3&gt;With datasets representing each of our treatment groups, we can now run an ANOVA for each that investigates the impact of gender. You may notice that this is effectively running three one-way ANOVAs with gender being the independent variable. Therefore, we should control for Type I error by dividing our typical .05 alpha level by three (.017).&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #run ANOVA on the treatment subsets to investigate the impacts of gender within each&lt;/li&gt;
&lt;li&gt;&amp;gt; #medical&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(StressReduction ~ Gender, dataMedical))&lt;/li&gt;
&lt;li&gt;&amp;gt; #mental&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(StressReduction ~ Gender, dataMental))&lt;/li&gt;
&lt;li&gt;&amp;gt; #physical&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(StressReduction ~ Gender, dataPhysical))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-5ZO2VqyvQNE/T8FKEVWoAlI/AAAAAAAAA4o/Bg7YnFwP4kU/s1600/20110207_anova_twoWay_interactions_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-5ZO2VqyvQNE/T8FKEVWoAlI/AAAAAAAAA4o/Bg7YnFwP4kU/s1600/20110207_anova_twoWay_interactions_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The gender within treatment group ANOVA tests&lt;/div&gt;&lt;br /&gt;
At an alpha level of .017, the gender effect within the mental (&lt;em&gt;p&lt;/em&gt; = .014) and physical (&lt;em&gt;p&lt;/em&gt; &amp;lt; .001) groups was statistically significant. In the mental condition, the means are 3 for males and 4 for females. In the physical condition, the means are 4 for males and 2 for females. These results suggest that the mental treatment is more effective in reducing stress for females than males, while the physical treatment is more effective for males than females. Further, there is insufficient statistical support for a gender difference in the medical treatment.&lt;br /&gt;
&lt;h3&gt;Pairwise Comparisons&lt;/h3&gt;Note that since our gender variable contains only two levels, there is no need to conduct follow-up comparisons. However, should you reach this point with an independent variable of more than three levels, you could conduct pairwise comparisons in the same manner as demonstrated in the &lt;a href="http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-two-way-anova-with.html" target="_blank"&gt;Two-Way ANOVA with Comparisons&lt;/a&gt; tutorial. In this case, remember to carry through your reduced Type I error rate from the preceding ANOVA tests.&lt;br /&gt;
&lt;h3&gt;Complete Two-Way ANOVA with Interactions Example&lt;/h3&gt;To see a complete example of how two-way ANOVA simple main effects can be explored in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_twoWay_interactions.txt" target="_blank"&gt;two-way ANOVA interaction example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/4mFG0DBmpz8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/4mFG0DBmpz8/r-tutorial-series-two-way-anova-with.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-IUa_Lg7wsLA/T8FKEAvX6LI/AAAAAAAAA4c/sCUthAx8OmI/s72-c/20110207_anova_twoWay_interactions_1.png" height="72" width="72" /><thr:total>7</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/02/r-tutorial-series-two-way-anova-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-6158900307091057984</guid><pubDate>Wed, 02 Feb 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-26T15:13:46.130-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">Statistical Analysis with R</category><category domain="http://www.blogger.com/atom/ns#">R Graphs Cookbook</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">review</category><title>Statistical Analysis with R Book Reviews</title><description>Reviews of my &lt;a href="http://link.packtpub.com/or7f1u" target="_blank"&gt;Statistical Analysis with R&lt;/a&gt; book have started to emerge online and I am writing today to share them with potential readers and recommenders.&lt;br /&gt;
&lt;h3&gt;
Reviews&lt;/h3&gt;
The following is a list of online reviews for &lt;em&gt;Statistical Analysis with R&lt;/em&gt;. If you have written a review of the book and would like it to be featured in this post, please &lt;a href="http://www.emailmeform.com/builder/form/612615" target="_blank"&gt;contact me&lt;/a&gt;.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://slashdot.org/submission/1452180/Book-Review-Statistical-Analysis-with-R" target="_blank"&gt;A review from Slashdot.org&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.stubbornmule.net/2011/01/a-gentle-introduction-to-r/" target="_blank"&gt;A review from the Stubborn Mule blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://nortalktoowise.com/?p=848" target="_blank"&gt;A review from the Nor Talk Too Wise blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.rtwilson.com/review-statistical-analysis-with-r-beginners-guide-by-john-m-quick/" target="_blank"&gt;A review from Robin Wilson's blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://lemire.me/blog/archives/2011/01/19/book-review-statistical-analysis-with-r/" target="_blank"&gt;A review from Daniel Lemire's blog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
In summarizing the reviews, a few points are very clear about &lt;em&gt;Statistical Analysis with R&lt;/em&gt;.&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;It is for beginners: The book was written for people who have little to no experience with R, statistical software, and programming. It makes no assumptions of prior experience along these lines and starts right from the beginning. If you are new to R and want to learn how to apply it to your work, then this book is for you. If you are already an intermediate or experienced user, perhaps you might recommend it to people you know who are just becoming familiar with R.&lt;/li&gt;
&lt;li&gt;It is a learning tool, not a reference: The book is structured with the intent that it is experienced as a holistic learning experience. The chapters build on one another and progressively delve deeper into R. It is not a dictionary-style reference that one might pull out, flip to an entry, and get a brief answer on a single item. Again, this has implications for the audience. Beginners are more likely to enjoy this approach, whereas experienced users may be interested in more of a reference-style book.&lt;/li&gt;
&lt;li&gt;It has a story: Woven into the book's learning structure is a storyline based on the Three Kingdoms period of ancient Chinese history. For many, this will be a motivating and engaging way to learn. For others, the story may not inspire the same level of interest. If you would like to get a taste of the story, and the book in general, it is recommended that you read the &lt;a href="https://www.packtpub.com/sites/default/files/2084-chapter-8-briefing-the-emperor.pdf" target="_blank"&gt;free sample chapter&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-bKGPx8X7sYA/T8FTtmB2rTI/AAAAAAAABIQ/JC9GhirToag/s1600/000000_statisticalAnalysisWithR_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-bKGPx8X7sYA/T8FTtmB2rTI/AAAAAAAABIQ/JC9GhirToag/s1600/000000_statisticalAnalysisWithR_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div align="center"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="center"&gt;
&lt;/div&gt;
&lt;h3&gt;
New Release: R Graph Cookbook&lt;/h3&gt;
Packt Publishing recently released a second book on R, the &lt;a href="http://link.packtpub.com/T7PMbW" target="_blank"&gt;R Graph Cookbook&lt;/a&gt; by Hrishi Mittal. This reference-style guide covers an array of R graphical applications and is geared towards users who are already familiar with the basics of R. A &lt;a href="https://www.packtpub.com/sites/default/files/3067OS-Chapter-2-beyond-the-basics-adjusting-Key-Parameters.pdf" target="_blank"&gt;sample chapter&lt;/a&gt; is available. I will be reviewing this book in the near future and posting my thoughts here on the R Tutorial Series blog. [Update: &lt;a href="http://rtutorialseries.blogspot.com/2011/02/book-review-r-graphs-cookbook.html"&gt;read my review of R Graph Cookbook&lt;/a&gt;]&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/yk82bhVkC9A" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/yk82bhVkC9A/r-tutorial-series-statistical-analysis.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-bKGPx8X7sYA/T8FTtmB2rTI/AAAAAAAABIQ/JC9GhirToag/s72-c/000000_statisticalAnalysisWithR_2.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/02/r-tutorial-series-statistical-analysis.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-5187844404717526239</guid><pubDate>Mon, 31 Jan 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:25:38.860-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">two-way</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">pairwise comparisons</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: Two-Way ANOVA with Pairwise Comparisons</title><description>By extending our one-way ANOVA procedure, we can test the pairwise comparisons between the levels of several independent variables. This tutorial will demonstrate how to conduct pairwise comparisons in a two-way ANOVA.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_twoWay_comparisons.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 27 participants who are divided into three stress reduction treatment groups (mental, physical, and medical) and three age groups (young, mid, and old). The stress reduction values are represented on a scale that ranges from 0 to 10. This dataset can be conceptualized as a comparison between three stress treatment programs, one using mental methods, one using physical training, and one using medication, across three age groups. The stress reduction values represent how effective the treatment programs were at reducing participant's stress levels, with higher numbers indicating higher effectiveness. Note that the numbers in this dataset are not very realistic and are simply used to make this example possible.&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayComparisons &amp;lt;- read.csv("dataset_ANOVA_TwoWayComparisons.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWayComparisons&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-XGEPx6pJoFs/T8FKDcsZT7I/AAAAAAAAA3w/YBokRUW199o/s1600/20110131_anova_twoWay_comparisons_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-XGEPx6pJoFs/T8FKDcsZT7I/AAAAAAAAA3w/YBokRUW199o/s1600/20110131_anova_twoWay_comparisons_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our dataset.&lt;/div&gt;&lt;h3&gt;Omnibus Test&lt;/h3&gt;Let's run a general omnibus test to assess the main effects and interactions present in the dataset.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use anova(object) to test the omnibus hypothesis&lt;/li&gt;
&lt;li&gt;&amp;gt; #Are main effects or interaction effects present in the independent variables?&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(StressReduction ~ Treatment * Age, dataTwoWayComparisons))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-kgTwWW4oPHY/T8FKDehFnrI/AAAAAAAAA38/XfCaEY_kdZM/s1600/20110131_anova_twoWay_comparisons_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-kgTwWW4oPHY/T8FKDehFnrI/AAAAAAAAA38/XfCaEY_kdZM/s1600/20110131_anova_twoWay_comparisons_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The omnibus ANOVA test&lt;/div&gt;&lt;h3&gt;Pairwise Comparisons&lt;/h3&gt;Since the omnibus test was significant for both variables and no interaction effect was present, we can proceed to testing the main effect pairwise comparisons. To accomplish this, we will apply our &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function to each of our independent variables. For more details on the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function, see the &lt;a href="http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-one-way-anova-with.html" target="_blank"&gt;One-Way ANOVA with Pairwise Comparisons&lt;/a&gt; tutorial.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use pairwise.t.test(x, g, p.adj) to test the pairwise comparisons between the treatment group means&lt;/li&gt;
&lt;li&gt;&amp;gt; #What significant differences are present amongst the treatment means?&lt;/li&gt;
&lt;li&gt;&amp;gt; pairwise.t.test(dataTwoWayComparisons$StressReduction, dataTwoWayComparisons$Treatment, p.adj = "none")&lt;/li&gt;
&lt;li&gt;&amp;gt; #use pairwise.t.test(x, g, p.adj) to test the pairwise comparisons between the age group means&lt;/li&gt;
&lt;li&gt;&amp;gt; #What significant differences are present amongst the age group means?&lt;/li&gt;
&lt;li&gt;&amp;gt; pairwise.t.test(dataTwoWayComparisons$StressReduction, dataTwoWayComparisons$Age, p.adj = "none")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-SXChcKcHhnE/T8FKDgrPkUI/AAAAAAAAA4A/hpO7zPoCJZA/s1600/20110131_anova_twoWay_comparisons_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-SXChcKcHhnE/T8FKDgrPkUI/AAAAAAAAA4A/hpO7zPoCJZA/s1600/20110131_anova_twoWay_comparisons_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of treatment group means&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-JVTzxJDY3_c/T8FKDogFnhI/AAAAAAAAA4M/h_GUfh4iy50/s1600/20110131_anova_twoWay_comparisons_4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-JVTzxJDY3_c/T8FKDogFnhI/AAAAAAAAA4M/h_GUfh4iy50/s1600/20110131_anova_twoWay_comparisons_4.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of age group means&lt;/div&gt;&lt;br /&gt;
Note that the desired p-adjustment method will vary by researcher, study, etc. Here, we will assume an alpha level of .05 for all tests, effectively making no adjustment for the family-wise Type I error rate.&lt;br /&gt;
These results indicate that there are are no statistically significant pairwise differences between the treatment groups and that all of the comparisons between age groups are statistically significant. The age group means are 8 for young, 5 for mid, and 2 for old. Consequently, we are inclined to conclude that, regardless of treatment, young patients are going to be most responsive, followed by middle aged patients, followed by older ones. However, there is insufficient support to differentiate between the effectiveness of the treatment methods themselves.&lt;br /&gt;
&lt;h3&gt;Complete Two-Way ANOVA with Pairwise Comparisons Example&lt;/h3&gt;To see a complete example of how two-way ANOVA pairwise comparisons can be conducted in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_twoWay_comparisons.txt" target="_blank"&gt;two-way ANOVA comparisons example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/3OFmHX3fOLE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/3OFmHX3fOLE/r-tutorial-series-two-way-anova-with.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-XGEPx6pJoFs/T8FKDcsZT7I/AAAAAAAAA3w/YBokRUW199o/s72-c/20110131_anova_twoWay_comparisons_1.png" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-two-way-anova-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-1933794817062516625</guid><pubDate>Mon, 24 Jan 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:24:12.873-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">one-way</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">pairwise comparisons</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: One-Way ANOVA with Pairwise Comparisons</title><description>When we have more than two groups in a one-way ANOVA, we typically want to statistically assess the differences between each group. Whereas a &lt;a href="http://rtutorialseries.blogspot.com/2010/10/r-tutorial-series-one-way-omnibus-anova.html" target="_blank"&gt;one-way omnibus ANOVA&lt;/a&gt; assesses whether a significant difference exists at all amongst the groups, pairwise comparisons can be used to determine which group differences are statistically significant.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_oneWay_comparisons.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 30 participants who are divided into three stress reduction treatment groups (mental, physical, and medical). The values are represented on a scale that ranges from 1 to 5. This dataset can be conceptualized as a comparison between three stress treatment programs, one using mental methods, one using physical training, and one using medication. The values represent how effective the treatment programs were at reducing participant's stress levels, with higher numbers indicating higher effectiveness.&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataOneWayComparisons &amp;lt;- read.csv("dataset_ANOVA_OneWayComparisons.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataOneWayComparisons&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-gac8Ch-1jNo/T8FKC740KGI/AAAAAAAAA3s/DW5p1p0UAo0/s1600/20110124_anova_oneWay_comparisons_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-gac8Ch-1jNo/T8FKC740KGI/AAAAAAAAA3s/DW5p1p0UAo0/s1600/20110124_anova_oneWay_comparisons_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our dataset.&lt;/div&gt;&lt;h3&gt;Omnibus Test&lt;/h3&gt;One way to begin an ANOVA is to run a general omnibus test. The advantage to starting here is that if the omnibus test comes up insignificant, you can stop your analysis and deem all pairwise comparisons insignificant. If the omnibus test is significant, you should continue with pairwise comparisons.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use anova(object) to test the omnibus hypothesis&lt;/li&gt;
&lt;li&gt;&amp;gt; #Is there a significant difference amongst the treatment means?&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(StressReduction ~ Treatment, dataOneWayComparisons))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-4nKYwSPu8HA/T8FKCzNIO_I/AAAAAAAAA3c/F0b3OVR_YZg/s1600/20110124_anova_oneWay_comparisons_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-4nKYwSPu8HA/T8FKCzNIO_I/AAAAAAAAA3c/F0b3OVR_YZg/s1600/20110124_anova_oneWay_comparisons_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;The omnibus ANOVA test&lt;/div&gt;&lt;h3&gt;Pairwise Comparisons&lt;/h3&gt;Since the omnibus test was significant, we are safe to continue with our pairwise comparisons. To make pairwise comparisons between the treatment groups, we will use the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function, which has the following major arguments. &lt;br /&gt;
&lt;ul&gt;&lt;li&gt;x: the dependent variable&lt;/li&gt;
&lt;li&gt;g: the independent variable&lt;/li&gt;
&lt;li&gt;p.adj: the p-value adjustment method used to control for the family-wise Type I error rate across the comparisons; one of "none", "bonferroni", "holm", "hochberg", "hommel", "BH", or "BY"&lt;/li&gt;
&lt;/ul&gt;The &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;pairwise.t.test()&lt;/span&gt; function can be implemented as follows.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use pairwise.t.test(x, g, p.adj) to test the pairwise comparisons between the treatment group means&lt;/li&gt;
&lt;li&gt;&amp;gt; #What significant differences are present amongst the treatment means?&lt;/li&gt;
&lt;li&gt;&amp;gt; pairwise.t.test(dataOneWayComparisons$StressReduction, dataOneWayComparisons$Treatment, p.adj = "none")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-6DBSb9sXZ1I/T8FKDFZsrAI/AAAAAAAAA3k/ueRwzSsRv4U/s1600/20110124_anova_oneWay_comparisons_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-6DBSb9sXZ1I/T8FKDFZsrAI/AAAAAAAAA3k/ueRwzSsRv4U/s1600/20110124_anova_oneWay_comparisons_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Pairwise comparisons of treatment group means&lt;/div&gt;&lt;br /&gt;
Note that the desired p-adjustment method will vary by researcher, study, etc. Here, we will assume an alpha level of .05 for all tests, effectively making no adjustment for the family-wise Type I error rate.&lt;br /&gt;
These results indicate that there is a statistically significant difference between the mental and medical (&lt;em&gt;p&lt;/em&gt; = .004) and physical and medical (&lt;em&gt;p&lt;/em&gt; = 0.045), but not the mental and physical (&lt;em&gt;p&lt;/em&gt; = 0.302) treatments. The treatment means are 3.5 for mental, 3 for physical, and 2 for medical. Subsequently, we are inclined to conclude based on this study that the mental and physical treatments lead to greater stress reduction than the medical method, but that there is insufficient statistical support to determine that either the mental or physical treatment method is superior.&lt;br /&gt;
&lt;h3&gt;Complete One-Way ANOVA with Pairwise Comparisons Example&lt;/h3&gt;To see a complete example of how one-way ANOVA pairwise comparisons can be conducted in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_oneWay_comparisons.txt" target="_blank"&gt;one-way ANOVA comparisons example (.txt)&lt;/a&gt; file.&lt;br /&gt;
&lt;h3&gt;References&lt;/h3&gt;R Documentation (n.d.). Adjust P-values for Multiple Comparisons. Retrieved January 16, 2011 from http://stat.ethz.ch/R-manual/R-devel/library/stats/html/p.adjust.html&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/BEs5KoabVm0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/BEs5KoabVm0/r-tutorial-series-one-way-anova-with.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-gac8Ch-1jNo/T8FKC740KGI/AAAAAAAAA3s/DW5p1p0UAo0/s72-c/20110124_anova_oneWay_comparisons_1.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-one-way-anova-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-6403865339076805282</guid><pubDate>Mon, 17 Jan 2011 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:21:56.038-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">two-way</category><category domain="http://www.blogger.com/atom/ns#">analysis of variance</category><category domain="http://www.blogger.com/atom/ns#">omnibus</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: Two-Way Omnibus ANOVA</title><description>As with the &lt;a href="http://rtutorialseries.blogspot.com/2010/10/r-tutorial-series-one-way-omnibus-anova.html" target="_blank"&gt;one-way&lt;/a&gt; case, testing the omnibus hypothesis via two-way ANOVA is simple process in R. This tutorial will explore how R can be used to perform a two-way ANOVA to test the difference between two (or more) group means.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_twoWay_omnibus.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 60 participants who are divided by gender (male and female) and treatment group (control and treatment). The values represent a scale that ranges from 1 to 5. For instance, this dataset could be conceptualized as a comparison between two professional training programs, where the control group participated the company's longstanding program and the treatment group participated in an experimental program. The values could represent the attitudes of employees towards the training programs on a scale from 1 (poor) to 5 (excellent).&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the two-way ANOVA dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWay &amp;lt;- read.csv("dataset_ANOVA_TwoWay.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataTwoWay&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-S1rI6GbjR-k/T8FKClt_j_I/AAAAAAAAA3M/JKM4pnXT2lA/s1600/20110117_anova_twoWay_omnibus_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-S1rI6GbjR-k/T8FKClt_j_I/AAAAAAAAA3M/JKM4pnXT2lA/s1600/20110117_anova_twoWay_omnibus_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our two-way ANOVA dataset.&lt;/div&gt;&lt;h3&gt;Two-Way ANOVA&lt;/h3&gt;Now that our data are ready, we can conduct a two-way omnibus ANOVA test using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;anova(object)&lt;/span&gt; function. Note that the only step necessary to add a second independent variable into our ANOVA model is to incorporate it into our &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(model, dataset) function&lt;/span&gt; using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;*&lt;/span&gt; operator. Whereas our one-way model was &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(Values ~ Group)&lt;/span&gt;, our two-way model becomes &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(Values ~ Group * Gender)&lt;/span&gt;. As you can see from the results below, adding a second independent variable in this manner also gives us information about the interaction between our variables.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use anova(object) to test the omnibus hypothesis in two-way ANOVA&lt;/li&gt;
&lt;li&gt;&amp;gt; #Are the differences between the group means for treatment and gender statistically significant?&lt;/li&gt;
&lt;li&gt;&amp;gt; #Is there a statistically significant interaction between treatment and gender?&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(Values ~ Group * Gender, dataTwoWay))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-tyJ_0NLsmOE/T8FKCpJE5_I/AAAAAAAAA3Q/jNx8y87Ak-M/s1600/20110117_anova_twoWay_omnibus_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-tyJ_0NLsmOE/T8FKCpJE5_I/AAAAAAAAA3Q/jNx8y87Ak-M/s1600/20110117_anova_twoWay_omnibus_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Our two-way ANOVA table.&lt;/div&gt;&lt;br /&gt;
The output of our ANOVA test indicates that the difference between our treatment group means is statistically significant (&lt;em&gt;p&lt;/em&gt; &amp;lt; .001) and that the difference between genders is not (&lt;em&gt;p&lt;/em&gt; = .585). However, in light of the statistically significant interaction between treatment group and gender (&lt;em&gt;p&lt;/em&gt; = .032), we would generally elect to forgo these main effects. Subsequently, a series of follow-up procedures could be carried out to examine the simple main effects for treatment group and gender.&lt;br /&gt;
&lt;h3&gt;Two-Way Multiple Group ANOVA&lt;/h3&gt;Conducting a two-way omnibus ANOVA with multiple groups is identical to the demonstrated two-group test. The only difference is that the values in your dataset would be associated with more than two groups. Subsequently, the omnibus hypothesis would test for mean differences across all of the groups. The &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;anova(object)&lt;/span&gt; function and its contained &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(formula, data)&lt;/span&gt; function would remain the same.&lt;br /&gt;
&lt;h3&gt;Complete Two-Way Omnibus ANOVA Example&lt;/h3&gt;To see a complete example of how a two-way omnibus ANOVA can be conducted in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_twoWay_omnibus.txt" target="_blank"&gt;two-way ANOVA example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/1xJ77SbSQx8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/1xJ77SbSQx8/r-tutorial-series-two-way-omnibus-anova.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-S1rI6GbjR-k/T8FKClt_j_I/AAAAAAAAA3M/JKM4pnXT2lA/s72-c/20110117_anova_twoWay_omnibus_1.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2011/01/r-tutorial-series-two-way-omnibus-anova.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-113246361364847354</guid><pubDate>Mon, 08 Nov 2010 14:00:00 +0000</pubDate><atom:updated>2012-05-26T15:56:08.456-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">guide</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">Packt Publishing</category><title>R Beginner's Guide Book Update: Statistical Analysis with R Released</title><description>In the final days of October, my beginner's guide to R was released. The book's official title is &lt;a href="http://link.packtpub.com/or7f1u" target="_blank"&gt;&lt;strong&gt;Statistical Analysis with R&lt;/strong&gt;&lt;/a&gt; and it can be found on the Packt Publishing website.&lt;br /&gt;
The primary focus of &lt;em&gt;Statistical Analysis with R&lt;/em&gt; is helping new users become accustomed to R and empowering them to apply R to suit their own needs. No prior experience with R, statistical software packages, or programming is necessary to learn from this book. It is written for a broad audience and should be well received by businesspeople, IT professionals, researchers, and students alike. &lt;em&gt;Statistical Analysis with R&lt;/em&gt; takes readers on a journey from their first installation and launch of R, to analyzing and assessing data, to communicating and visualizing results. This guide is an excellent way to rapidly become an experienced R user and learn the skills that you need to apply R to your work.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-qc4gZnjuFzw/T8Ffa08SEgI/AAAAAAAABIg/uRBL7vlrxso/s1600/000000_statisticalAnalysisWithR_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-qc4gZnjuFzw/T8Ffa08SEgI/AAAAAAAABIg/uRBL7vlrxso/s1600/000000_statisticalAnalysisWithR_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h3&gt;

Samples&lt;/h3&gt;
A sample chapter from &lt;em&gt;Statistical Analysis with R&lt;/em&gt; is available from the Packt website. This chapter, the book's eight, introduces the graphical capabilities of R, such as generating, customizing, and exporting various plots, charts, and graphs. You can download the &lt;a href="https://www.packtpub.com/sites/default/files/2084-chapter-8-briefing-the-emperor.pdf" target="_blank"&gt;sample chapter&lt;/a&gt; and its accompanying &lt;a href="https://www.packtpub.com/support?nid=6478" target="_blank"&gt;R files&lt;/a&gt; for free. If you like this chapter and are interested in learning more about R's graphical capabilities, you should know that chapter 9 demonstrates in depth how you can build and customize your own R visualizations.&lt;br /&gt;
The publisher has also posted a few brief samples from the book, which can be accessed via the following links. These samples are taken from chapters 7 and 8 of &lt;em&gt;Statistical Analysis with R&lt;/em&gt;. Respectively, they cover the common process behind all R analyses and introduce the graphical capabilities of R.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.packtpub.com/article/organizing-clarifying-communicating-r-data-analyses" target="_blank"&gt;Organizing and Communicating R Analyses&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.packtpub.com/article/customizing-graphics-creating-bar-chart-scatterplot-r" target="_blank"&gt;R Graphics Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.packtpub.com/article/graphical-capabilities-of-r" target="_blank"&gt;R Graphics Part 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;

Feedback&lt;/h3&gt;
If you decide to read &lt;em&gt;Statistical Analysis with R&lt;/em&gt;, please feel free to provide me with your feedback. It would be great to know what you learned from the book, how future guides could be improved, and your overall experience with R.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/GoRNSxe4EUU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/GoRNSxe4EUU/r-beginners-guide-book-update.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-qc4gZnjuFzw/T8Ffa08SEgI/AAAAAAAABIg/uRBL7vlrxso/s72-c/000000_statisticalAnalysisWithR_1.png" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/11/r-beginners-guide-book-update.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-1048569794875239790</guid><pubDate>Mon, 11 Oct 2010 21:01:00 +0000</pubDate><atom:updated>2012-05-28T22:19:30.308-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">one-way</category><category domain="http://www.blogger.com/atom/ns#">analysis of variance</category><category domain="http://www.blogger.com/atom/ns#">omnibus</category><category domain="http://www.blogger.com/atom/ns#">ANOVA</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: One-Way Omnibus ANOVA</title><description>Testing the omnibus hypothesis via one-way ANOVA is simple process in R. This tutorial will explore how R can be used to perform a one-way ANOVA to test the difference between two (or more) group means.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_anova_oneWay_omnibus.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 60 participants, who are divided into two groups (control and treatment) of 30. The values represent a scale that ranges from 1 to 5. For instance, this dataset could be conceptualized as a comparison between two professional training programs, where the control group participated the company's longstanding program and the treatment group participated in an experimental program. The values could represent the attitudes of employees towards the training programs on a scale from 1 (poor) to 5 (excellent).&lt;br /&gt;
&lt;h3&gt;Beginning Steps&lt;/h3&gt;To begin, we need to read our dataset into R and store its contents in a variable.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #read the one-way ANOVA dataset into an R variable using the read.csv(file) function&lt;/li&gt;
&lt;li&gt;&amp;gt; dataOneWay &amp;lt;- read.csv("dataset_ANOVA_OneWay.csv")&lt;/li&gt;
&lt;li&gt;&amp;gt; #display the data&lt;/li&gt;
&lt;li&gt;&amp;gt; dataOneWay&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-D0OF2-ruDbY/T8FKCWCMi7I/AAAAAAAAA20/k1zXPwRKes8/s1600/20101011_anova_oneWay_omnibus_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-D0OF2-ruDbY/T8FKCWCMi7I/AAAAAAAAA20/k1zXPwRKes8/s1600/20101011_anova_oneWay_omnibus_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;The first ten rows of our one-way ANOVA dataset.&lt;/div&gt;&lt;h3&gt;One-Way ANOVA&lt;/h3&gt;Now that our data are ready, we can conduct a one-way omnibus ANOVA test using the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;anova(object)&lt;/span&gt; function.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use anova(object) to test the omnibus hypothesis in one-way ANOVA&lt;/li&gt;
&lt;li&gt;&amp;gt; #is the difference between the group means statistically significant?&lt;/li&gt;
&lt;li&gt;&amp;gt; anova(lm(Values ~ Group, dataOneWay))&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-MIDkFLREMd8/T8FKCrH627I/AAAAAAAAA3A/jQ182dAZAck/s1600/20101011_anova_oneWay_omnibus_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-MIDkFLREMd8/T8FKCrH627I/AAAAAAAAA3A/jQ182dAZAck/s1600/20101011_anova_oneWay_omnibus_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;div align="center"&gt;Our one-way ANOVA table.&lt;/div&gt;&lt;br /&gt;
The output of our ANOVA test indicates that the difference between our group means is statistically significant (&lt;em&gt;p&lt;/em&gt; &amp;lt; .001). Conceptually, this suggests that employee attitudes towards the experimental training program were significantly higher than their attitudes towards the preexisting program.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;Note that the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;object&lt;/span&gt; argument in our &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;anova(object)&lt;/span&gt; function contained a linear model generated by the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(formula, data)&lt;/span&gt; function. This is the same type of model that is used when conducting linear regression in R. A more detailed explanation of the &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(formula, data)&lt;/span&gt; function and examples of its use are available in my &lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-simple-linear.html" target="_blank"&gt;Simple Linear Regression&lt;/a&gt; article.&lt;/blockquote&gt;&lt;h3&gt;One-Way Multiple Group ANOVA&lt;/h3&gt;Conducting a one-way omnibus ANOVA with multiple groups is identical to the demonstrated two-group test. The only difference is that the values in your dataset would be associated with more than two groups. Subsequently, the omnibus hypothesis would test for mean differences across all of the groups. The &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;anova(object)&lt;/span&gt; function and its contained &lt;span class="Apple-style-span" style="color: #cc0000;"&gt;lm(formula, data)&lt;/span&gt; function would remain the same.&lt;br /&gt;
&lt;h3&gt;Complete One-Way Omnibus ANOVA Example&lt;/h3&gt;To see a complete example of how a one-way omnibus ANOVA can be conducted in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_anova_oneWay_omnibus.txt" target="_blank"&gt;one-way ANOVA example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/US3kr0tJJp4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/US3kr0tJJp4/r-tutorial-series-one-way-omnibus-anova.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-D0OF2-ruDbY/T8FKCWCMi7I/AAAAAAAAA20/k1zXPwRKes8/s72-c/20101011_anova_oneWay_omnibus_1.png" height="72" width="72" /><thr:total>4</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/10/r-tutorial-series-one-way-omnibus-anova.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-7053711926821223922</guid><pubDate>Fri, 01 Oct 2010 14:00:00 +0000</pubDate><atom:updated>2010-11-13T09:28:33.404-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">update</category><category domain="http://www.blogger.com/atom/ns#">guide</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">publishing</category><category domain="http://www.blogger.com/atom/ns#">Packt Publishing</category><title>R Beginner's Guide Book Update 10/1/2010</title><description>&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; &lt;a href="http://rtutorialseries.blogspot.com/2010/11/r-beginners-guide-book-update.html"&gt;&lt;em&gt;Statistical Analysis with R&lt;/em&gt;&lt;/a&gt; is now available!&lt;/p&gt;&lt;hr/&gt;&lt;p&gt;I recently submitted the final drafts of all chapters of my R Beginner's Guide book, which is to be published through Packt. The official publishing timeline is set to December 2010, although the book may release ahead of schedule if all continues to go well. Below is an updated list of the major topics covered in the R Beginner's Guide.&lt;/p&gt;&lt;p&gt;Over the course of this book, you will acquire the knowledge and skills necessary to:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Conduct organized data analyses in R &lt;/li&gt;&lt;li&gt;Communicate data analyses conducted in R&lt;/li&gt;&lt;li&gt;Generate, customize, and export detailed charts, plots, and graphs&lt;/li&gt;&lt;li&gt;Build your own custom data visualizations &lt;/li&gt;&lt;li&gt;Program in the R language &lt;/li&gt;&lt;li&gt;Create your own custom functions &lt;/li&gt;&lt;li&gt;Extend the functionality of R via external packages &lt;/li&gt;&lt;li&gt;Manage the R workspace and console&lt;/li&gt;&lt;li&gt;Import external data into R&lt;/li&gt;&lt;li&gt;Manipulate data using variables&lt;/li&gt;&lt;li&gt;Execute a wide array of multi-argument and variable-argument functions&lt;/li&gt;&lt;li&gt;Develop and employ predictive regression models&lt;/li&gt;&lt;li&gt;Assess the practical and statistical significance of predictions &lt;/li&gt;&lt;li&gt;Understand R, its benefits, and how to use it to maximize the impact of your data analyses&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/TgXtmZB7MxA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/TgXtmZB7MxA/r-beginners-guide-book-update-1012010.html</link><author>noreply@blogger.com (John Quick)</author><thr:total>0</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/10/r-beginners-guide-book-update-1012010.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-7055142157745742503</guid><pubDate>Sun, 19 Sep 2010 18:05:00 +0000</pubDate><atom:updated>2012-05-28T22:16:04.868-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">scatterplots</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">graphics</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: Labeling Data Points on a Plot</title><description>There are times that labeling a plot's data points can be very useful, such as when conveying information in certain visuals or looking for patterns in our data. Fortunately, labeling the individual data points on a plot is a relatively simple process in R. In this tutorial, we will use the &lt;em&gt;Calibrate&lt;/em&gt; package's &lt;em&gt;textxy&lt;/em&gt; function to label the points on a scatterplot.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_plot_labelingPoints.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains information used to estimate undergraduate enrollment at the University of New Mexico (Office of Institutional Research, 1990). Note that this tutorial assumes that this data has already been read into R and saved into a variable named &lt;em&gt;enrollmentData&lt;/em&gt;.&lt;br /&gt;
&lt;h3&gt;Plot&lt;/h3&gt;To begin, we need to create a scatterplot using the &lt;em&gt;plot(x,y)&lt;/em&gt; function. With our example data, we will plot the year on the x axis and the unemployment rate on the y axis.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #generate a plot using the plot(x,y) function&lt;/li&gt;
&lt;li&gt;&amp;gt; #plot year on the x axis and unemployment rate on the y axis&lt;/li&gt;
&lt;li&gt; &amp;gt; plot(enrollmentData$YEAR, enrollmentData$UNEM)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-OFFVGMcECa4/T8FKB8ZUhqI/AAAAAAAAA2s/h66FJpV-MKU/s1600/20100919_plot_labelingPoints_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="373" src="http://2.bp.blogspot.com/-OFFVGMcECa4/T8FKB8ZUhqI/AAAAAAAAA2s/h66FJpV-MKU/s400/20100919_plot_labelingPoints_1.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;For a more detailed description of plotting data in R, see the article on &lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-scatterplots.html" target="_blank"&gt;scatterplots&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;Textxy&lt;/h3&gt;Within the &lt;em&gt;calibrate&lt;/em&gt; package, the &lt;em&gt;textxy()&lt;/em&gt; function can be used to label a plot's data points. The &lt;em&gt;textxy()&lt;/em&gt; function accepts the following arugments ("Label points in a plot," n.d.).&lt;br /&gt;
&lt;ul&gt;Required
&lt;li&gt;x: the x values of the plot's points&lt;/li&gt;
&lt;li&gt;y: the y values of the plot's points&lt;/li&gt;
&lt;li&gt;labs: the labels to be associated with the plot's points&lt;/li&gt;
&lt;ul&gt;&lt;/ul&gt;Optional
&lt;li&gt;cx: used to resize the label font&lt;/li&gt;
&lt;li&gt;dcol: used to set the label color; defaults to black&lt;/li&gt;
&lt;li&gt;m: sets the origin of the plot; defaults to (0,0)&lt;/li&gt;
&lt;/ul&gt;Here, we will use &lt;em&gt;textxy()&lt;/em&gt; to add labels for the enrollment at the University of New Mexico to each of our plot's data points.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #if necessary, install the calibrate package&lt;/li&gt;
&lt;li&gt;&amp;gt; #install.packages("calibrate")&lt;/li&gt;
&lt;li&gt;&amp;gt; #load the calibrate package&lt;/li&gt;
&lt;li&gt;&amp;gt; library(calibrate)&lt;/li&gt;
&lt;li&gt;&amp;gt; #use the textxy() function to add labels to the preexisting plot's points&lt;/li&gt;
&lt;li&gt;&amp;gt; #add labels for the total enrollment&lt;/li&gt;
&lt;li&gt;&amp;gt; textxy(enrollmentData$YEAR, enrollmentData$UNEM, enrollmentData$ROLL)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-f3KWEKhmX-0/T8FKB-6TymI/AAAAAAAAA24/XZq1L-MSAaI/s1600/20100919_plot_labelingPoints_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="370" src="http://4.bp.blogspot.com/-f3KWEKhmX-0/T8FKB-6TymI/AAAAAAAAA24/XZq1L-MSAaI/s400/20100919_plot_labelingPoints_2.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;In this case, adding labels to our data points helps us to better assess the relationships in our dataset.&lt;br /&gt;
&lt;h3&gt;Complete Data Point Labeling Example&lt;/h3&gt;To see a complete example of how a plot's data points can be labeled in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_plot_labelingPoints.txt" target="_blank"&gt;Data Point Labeling (.txt)&lt;/a&gt; file.&lt;br /&gt;
&lt;h3&gt;References&lt;/h3&gt;Label points in a plot. (n.d.). Retrieved September 19, 2010 from http://rss.acs.unt.edu/Rdoc/library/calibrate/html/textxy.html&lt;br /&gt;
Office of Institutional Research (1990). Enrollment Forecast [Data File]. Retrieved November 22, 2009 from http://lib.stat.cmu.edu/DASL/Datafiles/enrolldat.html&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/QjNHdDrfhwM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/QjNHdDrfhwM/r-tutorial-series-labeling-data-points.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-OFFVGMcECa4/T8FKB8ZUhqI/AAAAAAAAA2s/h66FJpV-MKU/s72-c/20100919_plot_labelingPoints_1.png" height="72" width="72" /><thr:total>21</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/09/r-tutorial-series-labeling-data-points.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-1283198749198598037</guid><pubDate>Mon, 19 Jul 2010 17:46:00 +0000</pubDate><atom:updated>2010-11-13T09:28:48.020-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">update</category><category domain="http://www.blogger.com/atom/ns#">guide</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">publishing</category><category domain="http://www.blogger.com/atom/ns#">Packt Publishing</category><title>R Beginner's Guide Book Update 7/19/2010</title><description>&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; &lt;a href="http://rtutorialseries.blogspot.com/2010/11/r-beginners-guide-book-update.html"&gt;&lt;em&gt;Statistical Analysis with R&lt;/em&gt;&lt;/a&gt; is now available!&lt;/p&gt;&lt;hr/&gt;&lt;p&gt;I am excited to announce that I have submitted the entire first draft of my R Beginner's Guide book, which is to be published through Packt. The tenth and final chapter was submitted a full month ahead of schedule. The printed book could become available in as little as three to four months.&lt;/p&gt;&lt;p&gt;Below is a list of the major topics covered in the R Beginner's Guide.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Understanding what R is, its benefits, and why to use it&lt;/li&gt;&lt;li&gt;Downloading, installing, and running R&lt;/li&gt;&lt;li&gt;Dissecting the anatomy of R&lt;/li&gt;&lt;li&gt;Programming in R&lt;/li&gt;&lt;li&gt;Handling external data&lt;/li&gt;&lt;li&gt;Using variables&lt;/li&gt;&lt;li&gt;Managing the R workspace and console&lt;/li&gt;&lt;li&gt;Using multi-argument and variable-argument functions&lt;/li&gt;&lt;li&gt;Creating predictive data models&lt;/li&gt;&lt;li&gt;Assessing practical vs. statistical significance&lt;/li&gt;&lt;li&gt;Regression modeling&lt;/li&gt;&lt;li&gt;Creating custom functions&lt;/li&gt;&lt;li&gt;Assessing the viability of predictions&lt;/li&gt;&lt;li&gt;Organizing and communicating data analyses&lt;/li&gt;&lt;li&gt;Generating, customizing, and exporting graphics&lt;/li&gt;&lt;li&gt;Building custom visualizations&lt;/li&gt;&lt;li&gt;Extending R via packages&lt;/li&gt;&lt;li&gt;Taking advantage of electronic learning resources&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/g5OUOIRXqT0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/g5OUOIRXqT0/r-beginners-guide-book-update-7192010.html</link><author>noreply@blogger.com (John Quick)</author><thr:total>3</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/07/r-beginners-guide-book-update-7192010.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-7775596228241952551</guid><pubDate>Thu, 29 Apr 2010 00:44:00 +0000</pubDate><atom:updated>2010-11-13T09:29:14.487-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">packt</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">update</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Beginner's Guide Book Update 4/28/2010</title><description>&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; &lt;a href="http://rtutorialseries.blogspot.com/2010/11/r-beginners-guide-book-update.html"&gt;&lt;em&gt;Statistical Analysis with R&lt;/em&gt;&lt;/a&gt; is now available!&lt;/p&gt;&lt;hr/&gt;&lt;p&gt;I am writing to update you on the progress of my R Beginner's Guide book, which is to be published through Packt. I have really gotten to work over the past couple months and have recently completed the first draft of the first half of the book. Right now, I am operating a few weeks ahead of our planned schedule, which calls for the first draft of all ten chapters by mid-August.&lt;/p&gt;&lt;p&gt;To give you an idea of its content, the book focuses on most of the topics covered in this blog as well as many more, such as data visualization, custom functions, and online resources. The topics are covered in great depth and numerous opportunities for practice and exploration are offered. The book's theme centers around the Three Kingdoms period of ancient China. The reader takes on the role of the lead strategist for the Shu kingdom at a pivotal point in history. Throughout the book, the reader uses R to help devise a course of action for the Shu forces.&lt;/p&gt;&lt;p&gt;I will continue to make steady progress on this book over the summer months. I am also excited to be able to share more R tutorials and knowledge in the near future through this project.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/Xj3-w3tJENU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/Xj3-w3tJENU/r-beginners-guide-book-update-4282010.html</link><author>noreply@blogger.com (John Quick)</author><thr:total>0</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/04/r-beginners-guide-book-update-4282010.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-224259975705340065</guid><pubDate>Mon, 15 Mar 2010 14:00:00 +0000</pubDate><atom:updated>2012-05-26T13:10:21.023-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">R Bloggers</category><category domain="http://www.blogger.com/atom/ns#">books</category><category domain="http://www.blogger.com/atom/ns#">update</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><title>R Tutorial Series: R Beginner's Guide and R Bloggers Updates</title><description>&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;1/1/2011 Update:&lt;/strong&gt; Tal Galili wrote an article that revisits &lt;a href="http://www.r-statistics.com/2011/01/r-bloggers-in-2010-top-14-r-posts-site-statistics-and-invitation-for-sponsors/" target="_blank"&gt;the first year of R-Bloggers&lt;/a&gt; and this post was listed as one of the top 14. Therefore, I decided to make a small update to each section. I start by describing the initial series of tutorials that I wrote. A few more have been added since and even more planned in the upcoming year. As always, an up to date listing of my articles can be found on the &lt;a href="http://rtutorialseries.blogspot.com/" target="_blank"&gt;R Tutorial Series blog&lt;/a&gt;. New posts will also continue to be offered through the &lt;a href="http://www.r-bloggers.com/" target="_blank"&gt;R Bloggers&lt;/a&gt; network.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;Since October 2009, I have written 13 articles [many more now, of course] for the R Tutorial Series blog. The first two introduce new users to R. The remaining 11 cover a wide range of topics related to multiple regression and correlation. This collection of tutorials represents my most recent training in statistics. Thus, for the time being, I will not be contributing new articles as frequently as I have over the past few months. However, I will undoubtedly encounter future projects that require new statistical methods and partake in more statistics courses, both of which will provide additional tutorial material. Below is a categorized list of the articles currently offered in the R Tutorial Series.&lt;/p&gt;&lt;p&gt;Introduction to R&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/10/r-tutorial-series-introduction-to-r_11.html" target="_blank"&gt;Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/10/r-tutorial-series-introduction-to-r.html" target="_blank"&gt;Part 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Descriptive Statistics&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-summary-and.html" target="_blank"&gt;Summary and Descriptive Statistics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Data Visualization&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-scatterplots.html" target="_blank"&gt;Scatterplots&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Correlation&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-zero-order.html" target="_blank"&gt;Zero-Order Correlations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Regression&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-simple-linear.html" target="_blank"&gt;Simple Linear Regression&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/12/r-tutorial-series-multiple-linear.html" target="_blank"&gt;Multiple Linear Regression&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2009/12/r-tutorial-series-graphic-analysis-of.html" target="_blank"&gt;Regression Assumptions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2010/01/r-tutorial-series-regression-with.html" target="_blank"&gt;Regression with Interaction Variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2010/02/r-tutorial-series-regression-with.html" target="_blank"&gt;Regression with Categorical Variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2010/02/r-tutorial-series-basic-polynomial.html" target="_blank"&gt;Polynomial Regression&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://rtutorialseries.blogspot.com/2010/01/r-tutorial-series-basic-hierarchical.html" target="_blank"&gt;Hierarchical Linear Regression&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I also have two additional R-related items to update you on. The first is the R Bloggers website and the second is my R Beginner's Guide.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;1/1/2011 Update:&lt;/strong&gt; I originally reported that 50 blogs composed the &lt;a href="http://www.r-bloggers.com/" target="_blank"&gt;R Bloggers&lt;/a&gt; network. Now that number has risen to over 140. I hope that R Bloggers continues to thrive and contribute to the R community.&lt;/p&gt;&lt;hr /&gt;&lt;h3&gt;R Tutorial Series on R Bloggers&lt;/h3&gt;&lt;p&gt;&lt;a href="http://www.r-bloggers.com/" target="_blank"&gt;R Bloggers&lt;/a&gt; (http://www.r-bloggers.com) is a website that aggregates over 50 different blogs that focus on R. It is an excellent resource for keeping up to date on the many uses of R and for learning about the wide range of work being conducted in R. I recommend using R Bloggers for these purposes. The R Tutorial Series was invited to participate in the R Bloggers collection and is now available to R Bloggers' readers.&lt;/p&gt;&lt;h3&gt;R Beginner's Guide&lt;/h3&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;11/1/2010 Update:&lt;/strong&gt; &lt;a href="http://rtutorialseries.blogspot.com/2010/11/r-beginners-guide-book-update.html" target="_blank"&gt;&lt;em&gt;Statistical Analysis with R&lt;/em&gt;&lt;/a&gt; is now available!&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;Lastly, I want to let you know that I am working on a beginner's guide for R. It is primarily focused towards introducing R to information technology, business, and data analyst professionals. The book will be offered through &lt;a href="http://link.packtpub.com/or7f1u" target="_blank"&gt;PACKT Publishing&lt;/a&gt; (http://www.packtpub.com) and should be available within the next year. If you have enjoyed the R Tutorial Series, then you may be interested in looking for the guide once it is completed. In the meantime, keep reading the R Tutorial Series and R Bloggers and I will keep you updated on the book's major milestones.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/59iGaXEXk9c" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/59iGaXEXk9c/r-tutorial-series-r-beginners-guide-and.html</link><author>noreply@blogger.com (John Quick)</author><thr:total>2</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/03/r-tutorial-series-r-beginners-guide-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-9121762178098403960</guid><pubDate>Mon, 08 Feb 2010 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:13:05.075-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">hierarchical linear regression</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">polynomial regression</category><category domain="http://www.blogger.com/atom/ns#">multiple linear regression</category><title>R Tutorial Series: Basic Polynomial Regression</title><description>Often times, a scatterplot reveals a pattern that seems not so linear. Polynomial regression can be used to explore a predictor at different levels of curvilinearity. This tutorial will demonstrate how polynomial regression can be used in a hierarchical fashion to best represent a dataset in R.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_multipleRegression_polynomial.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. Note that all code samples in this tutorial assume that this data has already been read into an R variable and has been attached. This dataset contains hypothetical student data that uses practice exam scores to predict final exam scores.&lt;br /&gt;
&lt;h3&gt;Scatterplot&lt;/h3&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-0tmqX1l4j58/T8FKBjbvTjI/AAAAAAAAA2c/tMd5lcBSIxc/s1600/20100208_multipleRegression_polynomial_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-0tmqX1l4j58/T8FKBjbvTjI/AAAAAAAAA2c/tMd5lcBSIxc/s1600/20100208_multipleRegression_polynomial_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="center"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align="center"&gt;&lt;/div&gt;The preceding &lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-scatterplots.html" target="_blank"&gt;scatterplot&lt;/a&gt; demonstrates that these data may not be linear. Notably, no one scored lower than 50 on the practice exam and at approximately the  85 and above practice mark, final exam scores taper off. These suggest that the data is curvilinear. Furthermore, since exam scores range between 0 to 100, it is not possible to observe nor appropriate to predict that an individual with a 150 practice score would have a certain final exam score.&lt;br /&gt;
&lt;h3&gt;Creating The Higher Order Variables&lt;/h3&gt;A two step process, identical to the one used to create &lt;a href="http://rtutorialseries.blogspot.com/2010/01/r-tutorial-series-regression-with.html" target="_blank"&gt;interaction variables&lt;/a&gt;, can be followed to create higher order variables in R. First, the variables must be centered to mitigate multicollinearity. Second, the predictor must be multiplied by itself a certain number of times to create each higher order variable. In this tutorial, we will explore the a linear, quadratic, and cubic model. Therefore, the predictor will need to be squared to create the quadratic model and cubed to create the cubic model.&lt;br /&gt;
&lt;h4&gt;Step 1: Centering&lt;/h4&gt;To center a variable, simply subtract its mean from each data point and save the result into a new R variable, as demonstrated below.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #center the independent variable&lt;/li&gt;
&lt;li&gt;&amp;gt; FinalC &amp;lt;- Final - mean(Final)&lt;/li&gt;
&lt;li&gt;&amp;gt; #center the predictor&lt;/li&gt;
&lt;li&gt;&amp;gt; PracticeC &amp;lt;- Practice - mean(Practice)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h4&gt;Step 2: Multiplication&lt;/h4&gt;Once the input variable has been centered, the higher order terms can be created. Since a higher order variable is formed by the product of a predictor with itself, we can simply multiply our centered term from step one and save the result into a new R variable, as demonstrated below.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #create the quadratic variable&lt;/li&gt;
&lt;li&gt;&amp;gt; PracticeC2 &amp;lt;- PracticeC * PracticeC&lt;/li&gt;
&lt;li&gt;&amp;gt; #create the cubic variable&lt;/li&gt;
&lt;li&gt;&amp;gt; PracticeC3 &amp;lt;- PracticeC * PracticeC * PracticeC&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Creating The Models&lt;/h3&gt;Now we have all of the pieces necessary to assemble our linear and curvilinear models.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #create the models using lm(FORMULA, DATAVAR)&lt;/li&gt;
&lt;li&gt;&amp;gt; #linear model&lt;/li&gt;
&lt;li&gt;&amp;gt; linearModel &amp;lt;- lm(FinalC ~ PracticeC, datavar)&lt;/li&gt;
&lt;li&gt;&amp;gt; #quadratic model&lt;/li&gt;
&lt;li&gt;&amp;gt; quadraticModel &amp;lt;- lm(FinalC ~ PracticeC + PracticeC2, datavar)&lt;/li&gt;
&lt;li&gt;&amp;gt; #cubic model&lt;/li&gt;
&lt;li&gt;&amp;gt; cubicModel &amp;lt;- lm(FinalC ~ PracticeC + PracticeC2 + PracticeC3, datavar)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;&lt;h3&gt;Evaluating The Models&lt;/h3&gt;As is the case in other forms of regression, it can be helpful to summarize and compare our potential models using the summary(MODEL) and anova(MODEL1, MODEL2,… MODELi) functions.&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&amp;gt; #display summary information about the models&lt;/li&gt;
&lt;li&gt;&amp;gt; summary(linearModel)&lt;/li&gt;
&lt;li&gt;&amp;gt; summary(quadraticModel)&lt;/li&gt;
&lt;li&gt;&amp;gt; summary(cubicModel)&lt;/li&gt;
&lt;li&gt;#compare the models using ANOVA&lt;/li&gt;
&lt;li&gt;anova(linearModel, quadraticModel, cubicModel)&lt;/li&gt;
&lt;/ol&gt;The model summaries and ANOVA comparison chart are displayed below.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-11Vz-2J0dGE/T8FKBjPbXGI/AAAAAAAAA2o/AxjCGPvTjuw/s1600/20100208_multipleRegression_polynomial_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-11Vz-2J0dGE/T8FKBjPbXGI/AAAAAAAAA2o/AxjCGPvTjuw/s1600/20100208_multipleRegression_polynomial_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align="center"&gt;&lt;/div&gt;At this point we can compare the models. In this case, the quadratic and cubic terms are not statistically significant themselves nor are their models statistically significant beyond the linear model. However, in a real research study, there would be other practical considerations to make before deciding on a final model.&lt;br /&gt;
&lt;h3&gt;More On Interactions, Polynomials, and HLR&lt;/h3&gt;Certainly, much more can be done with these topics than I have covered in my tutorials. What I have provided is a basic discussion with guided examples. The regression topics covered in these tutorials can be mixed and matched to create exceedingly complex models. For example, multiple interactions and higher order variables could be contained in a single model. The good news is that more complex models can be created using the same techniques covered here. The basic principles remain the same.&lt;br /&gt;
&lt;h3&gt;Complete Polynomial Regression Example&lt;/h3&gt;To see a complete example of how polynomial regression models can be created in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_multipleRegression_polynomial.txt" target="_blank"&gt;polynomial regression example (.txt)&lt;/a&gt; file.&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/zhQ3mwvkjuM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/zhQ3mwvkjuM/r-tutorial-series-basic-polynomial.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-0tmqX1l4j58/T8FKBjbvTjI/AAAAAAAAA2c/tMd5lcBSIxc/s72-c/20100208_multipleRegression_polynomial_1.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/02/r-tutorial-series-basic-polynomial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6710487119650146215.post-8942017737289520969</guid><pubDate>Mon, 01 Feb 2010 14:00:00 +0000</pubDate><atom:updated>2012-05-28T22:11:15.211-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">statistics</category><category domain="http://www.blogger.com/atom/ns#">categorical regression</category><category domain="http://www.blogger.com/atom/ns#">dummy coding</category><category domain="http://www.blogger.com/atom/ns#">tutorial</category><category domain="http://www.blogger.com/atom/ns#">R Tutorial Series</category><category domain="http://www.blogger.com/atom/ns#">R Project</category><category domain="http://www.blogger.com/atom/ns#">multiple linear regression</category><category domain="http://www.blogger.com/atom/ns#">box plots</category><title>R Tutorial Series: Regression With Categorical Variables</title><description>Categorical predictors can be incorporated into regression analysis, provided that they are properly prepared and interpreted. This tutorial will explore how categorical variables can be handled in R.&lt;br /&gt;
&lt;h3&gt;Tutorial Files&lt;/h3&gt;Before we begin, you may want to download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_multipleRegression_categorical.csv" target="_blank"&gt;sample data (.csv)&lt;/a&gt; used in this tutorial. Be sure to right-click and save the file to your R working directory. Note that all code samples in this tutorial assume that this data has already been read into an R variable and has been attached. This dataset contains variables for the following information related to NFL quarterback and team salaries in 1991.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;TEAM: Name of team&lt;/li&gt;
&lt;li&gt;QB: Starting quarterback salary in thousands of dollars&lt;/li&gt;
&lt;li&gt;TOTAL: team salary in thousands of dollars&lt;/li&gt;
&lt;li&gt;CONF: conference (NFC or AFC)&lt;/li&gt;
&lt;/ul&gt;In this dataset, the CONF variable is categorical. It can take on one of two values, either NFC or AFC. Suppose for the purposes of this tutorial that our research question is "how well do quarterback salary and conference predict total team salary?" The model that we use to answer this question will need to incorporate the categorical predictor for conference.&lt;br /&gt;
&lt;h3&gt;Dummy Coding&lt;/h3&gt;To be able to perform regression with a categorical variable, it must first be coded. Here, I will use the as.numeric(VAR) function, where VAR is the categorical variable, to dummy code the CONF predictor. As a result, CONF will represent NFC as 1 and AFC as 0. The sample code below demonstrates this process.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #represent a categorical variable numerically using as.numeric(VAR)&lt;/li&gt;
&lt;li&gt;&amp;gt; #dummy code the CONF variable into NFC = 1 and AFC = 0&lt;/li&gt;
&lt;li&gt;&amp;gt; dCONF &amp;lt;- as.numeric(CONF) - 1&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;Note that the -1 that comes after the as.numeric(CONF) function causes the variables to read 1 and 0 rather than 2 and 1, which is the default behavior.&lt;br /&gt;
&lt;h3&gt;Interpretation&lt;/h3&gt;&lt;h4&gt;Visual&lt;/h4&gt;One useful way to visualize the relationship between a categorical and continuous variable is through a box plot. When dealing with categorical variables, R automatically creates such a graph via the plot() function (see &lt;a href="http://rtutorialseries.blogspot.com/2009/11/r-tutorial-series-scatterplots.html" target="_blank"&gt;Scatterplots&lt;/a&gt;). The CONF variable is graphically compared to TOTAL in the following sample code.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #use the plot() function to create a box plot&lt;/li&gt;
&lt;li&gt;&amp;gt; #what does the relationship between conference and team salary look like?&lt;/li&gt;
&lt;li&gt;&amp;gt; plot(CONF, TOTAL, main="Team Salary by Conference", xlab="Conference", ylab="Salary ($1,000s)")&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;The resulting box plot is show below.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-pTE9mZUYges/T8FKA76zdwI/AAAAAAAAA10/FbizemwTN78/s1600/20100201_multipleRegression_categorical_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-pTE9mZUYges/T8FKA76zdwI/AAAAAAAAA10/FbizemwTN78/s1600/20100201_multipleRegression_categorical_1.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align="center"&gt;&lt;/div&gt;From a box plot, we can derive many useful insights, such as the minimum, maximum, and median values. Our box plot of total team salary on conference suggests that, compared to AFC teams, NFC teams have slightly higher salaries on average and the range of these salaries is larger.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-bMIOnf9SUOU/T8FKBA082iI/AAAAAAAAA2Q/mEvPXr4NpaI/s1600/20100201_multipleRegression_categorical_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-bMIOnf9SUOU/T8FKBA082iI/AAAAAAAAA2Q/mEvPXr4NpaI/s1600/20100201_multipleRegression_categorical_2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align="center"&gt;&lt;/div&gt;&lt;h4&gt;Routine Analysis&lt;/h4&gt;Once a categorical variable has been quantified, it can be used in routine analyses, such as descriptive statistics and correlations. The following code depicts a few examples.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #what are the mean and standard deviation of conference?&lt;/li&gt;
&lt;li&gt;&amp;gt; mean(dCONF)&lt;/li&gt;
&lt;li&gt;&amp;gt; [1] 0.5&lt;/li&gt;
&lt;li&gt;&amp;gt; sd(dCONF)&lt;/li&gt;
&lt;li&gt;&amp;gt; [1] 0.5091751&lt;/li&gt;
&lt;li&gt;&amp;gt; #this makes sense… there are an even number of teams in both conferences and they are coded as either 0 or 1!&lt;/li&gt;
&lt;li&gt;&amp;gt; #what is the correlation between total team salary and conference?&lt;/li&gt;
&lt;li&gt;&amp;gt; cor(dCONF, TOTAL)&lt;/li&gt;
&lt;li&gt;&amp;gt; [1]0.007019319&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;The correlation between total team salary and conference indicates that there is little to no linear relationship between the variables.&lt;br /&gt;
&lt;h4&gt;Linear Regression&lt;/h4&gt;Let's return to our original question of how well quarterback salary and conference predict team salary. With the categorical predictor quantified, we can create a regression model for this relationship, as demonstrated below.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #create a linear model using lm(FORMULA, DATAVAR)&lt;/li&gt;
&lt;li&gt;&amp;gt; #predict team salary using quarterback salary and conference&lt;/li&gt;
&lt;li&gt;linearModel &amp;lt;- lm(TOTAL ~ QB + dCONF, datavar)&lt;/li&gt;
&lt;li&gt;#generate model summary&lt;/li&gt;
&lt;li&gt;summary(linearModel)&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;The model summary is pictured below.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-mGf3yJlNHZU/T8FKBSU7KYI/AAAAAAAAA2E/Dlq2tW_pT8Q/s1600/20100201_multipleRegression_categorical_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-mGf3yJlNHZU/T8FKBSU7KYI/AAAAAAAAA2E/Dlq2tW_pT8Q/s1600/20100201_multipleRegression_categorical_3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div align="center"&gt;&lt;/div&gt;Considering both the counterintuitive and statistically insignificant results of this model, our analysis of the conference variable would likely end or change directions at this point. However, there is one more interpretation method that is worth mentioning for future reference.&lt;br /&gt;
&lt;h4&gt;Split Model&lt;/h4&gt;With a dummy coded predictor, a regression model can be split into two halves by substituting in the possible values for the categorical variable. For example, we can think of our model as a regression of total salary on quarterback salary for two states of the world - teams in the AFC and teams in the NFC. These derivative models are covered in the following sample code.&lt;br /&gt;
&lt;blockquote class="codeBlock"&gt;&lt;ol&gt;&lt;li&gt;&amp;gt; #input the categorical values to split the linear model into two representations&lt;/li&gt;
&lt;li&gt;&amp;gt; #the original model: TOTAL = 19099 + 2.5 * QB - 103 * dCONF&lt;/li&gt;
&lt;li&gt;&amp;gt; #substitute 0 for dCONF to derive the AFC model: TOTAL = 19099 + 2.5 * QB&lt;/li&gt;
&lt;li&gt;&amp;gt; #substitute 1 for dCONF to derive the NFC model: TOTAL = 18996 + 2.5 * QB&lt;/li&gt;
&lt;li&gt;#what is the predicted salary for a team with a quarterback salary of $2,000,000 in the AFC and NFC conferences?&lt;/li&gt;
&lt;li&gt;#AFC prediction&lt;/li&gt;
&lt;li&gt;19099 + 2.5 * 2000&lt;/li&gt;
&lt;li&gt;[1] 24099&lt;/li&gt;
&lt;li&gt;#NFC prediction&lt;/li&gt;
&lt;li&gt;18996 + 2.5 * 2000&lt;/li&gt;
&lt;li&gt;[1] 23996&lt;/li&gt;
&lt;/ol&gt;&lt;/blockquote&gt;Based only on what we have modeled, we can further infer that conference was not a significant predictor of total team salaries in the NFL in 1991. The difference between the team salaries based on conference is less than one-half of one percent on average! Of course, only using quarterback salary and conference to predict an NFL team's overall salary is neglecting quite a few potentially significant predictors. Nonetheless, split model interpretation is a useful way to break down the perspectives captured by a categorical regression model.&lt;br /&gt;
&lt;h3&gt;More On Categorical Predictors&lt;/h3&gt;Certainly, much more can be done with categorical variables than the basic dummy coding that was demonstrated here. Individuals whose work requires a deeper inspection into the procedures of categorical regression are encouraged to seek additional resources (and to consider writing a guest tutorial for this series).&lt;br /&gt;
&lt;h3&gt;Complete Categorical Regression Example&lt;/h3&gt;To see a complete example of how a categorical regression model can be created in R, please download the &lt;a href="http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/example_multipleRegression_categorical.txt" target="_blank"&gt;categorical regression example (.txt)&lt;/a&gt; file.&lt;br /&gt;
&lt;h3&gt;References&lt;/h3&gt;The Associated Press. (1991). Q-back and team salaries [Data File]. Retrieved December 14, 2009 from http://lib.stat.cmu.edu/DASL/Datafiles/qbacksalarydat.html&lt;img src="http://feeds.feedburner.com/~r/RTutorialSeries/~4/ACaZRs_OEYI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/RTutorialSeries/~3/ACaZRs_OEYI/r-tutorial-series-regression-with.html</link><author>noreply@blogger.com (John Quick)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-pTE9mZUYges/T8FKA76zdwI/AAAAAAAAA10/FbizemwTN78/s72-c/20100201_multipleRegression_categorical_1.png" height="72" width="72" /><thr:total>17</thr:total><feedburner:origLink>http://rtutorialseries.blogspot.com/2010/02/r-tutorial-series-regression-with.html</feedburner:origLink></item></channel></rss>
