<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rense Nieuwenhuis &#187; Statistics</title>
	<atom:link href="http://www.rensenieuwenhuis.nl/tag/statistics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rensenieuwenhuis.nl</link>
	<description>&#34;The extra-ordinary lies within the curve of normality&#34;</description>
	<lastBuildDate>Thu, 12 Mar 2026 14:58:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>Statistical Tools &#8211; Te Grotenhuis and Van der Weegen (2009)</title>
		<link>http://www.rensenieuwenhuis.nl/statistical-tools-te-grotenhuis-and-van-der-weegen-2009/</link>
		<comments>http://www.rensenieuwenhuis.nl/statistical-tools-te-grotenhuis-and-van-der-weegen-2009/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 10:00:29 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Book]]></category>
		<category><![CDATA[statistical tools]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[te grotenhuis]]></category>
		<category><![CDATA[van der weegen]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=1096</guid>
		<description><![CDATA[How does one teach statistics? Is it more important to start with mathematical thoroughness, or to help students to gain a conceptual understanding first? Few give a comprehensive introduction to statistics for those without the ...]]></description>
				<content:encoded><![CDATA[<p>How does one teach statistics? Is it more important to start with mathematical thoroughness, or to help students to gain a conceptual understanding first? Few give a comprehensive introduction to statistics for those without the otherwise indispensable mathematical background. Manfred te Grotenhuis and Theo van der Weegen recently published an introductory book on statistics, explaining statistical concepts using words and graphs, rather than formulas.</p>
<p><i>Less than a year ago, I wrote these exact words. I then discussed the publication of a Dutch book on statistics, to which I provided minor assistance. Now, I repeat these words to introduce the Enligsh translation of this conceptual introduction to statistics, called <a href="http://www.vangorcum.nl/EN_toonBoek.asp?PublID=4503">Statistical Tools</a>. Again, I contributed to this publication, this time by providing a first, rough, translation from Dutch to English. Let me repeat below what I wrote before on this blog, for of course this still holds relevance for the translation to English:</i></p>
<p>With the focus on practical application rather than statistical theory, the first chapter starts explaining the goal of inferential statistics, meanwhile introducing the concepts of measurement and variables. Considerable attention is paid to the importance of high quality data to perform your analyses on. The second chapter <span id="more-1096"></span>deals with descriptive statistics, both in a numerical and a graphical way. Here, also the concepts of a distribution and of correlation are introduced. The third and final chapter discusses the testing of hypotheses, using techniques as the cross-table, tests for means and proportions, various forms of correlation, and finally multiple regression.</p>
<p>Clearly, the setup of this book is what one might expect from an introduction to statistics. However, I think this book has a unique approach by its strong focus on the conceptual level, rather than the (mathematical) statistical theory. Nevertheless, it does not shy away from relatively complex subjects such as the multiple regression. Even on the conceptual level, it pays a lot of attention to the assumptions required for the various analyses discussed. The practical approach of this book is enhanced even further, because all examples come from â€˜real lifeâ€™ research. On the accompanying website SPSS data files and syntax files are made available, so that every example from the book can be repeated by the reader.</p>
<p>Aimed at the novice statistics student, this book offers a comprehensible and conceptual approach at statistics. It will surely help students of statistics to grasp what theyâ€™re actually doing when pushing SPSSâ€™s buttons or trying to interpret published figures. In that sense, I think that for many statistics student, this book successfully reaches is goal of transforming statistics form an abstract undertaking to an actually useful and applicable tool.</p>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;<br />
<i>This post is part of my &#8216;Reading List&#8217;. In this series I jot down some thoughts about the books I read and enjoyed. Some posts my give a somewhat balanced overview of a book, others will just focus on some aspects that, for whatever reason, caught my attention. Never are these posts meant as an evaluation or even review of the book. I just like to share some impressions. </p>
<p>An <a href="http://www.rensenieuwenhuis.nl/reading-list/">overview of my Reading List</a> is available, which contains both a list of the books that I wrote about, and another list of books I&#8217;m planning to read.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/statistical-tools-te-grotenhuis-and-van-der-weegen-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Triumph of Numbers &#8211; Cohen (2005)</title>
		<link>http://www.rensenieuwenhuis.nl/the-triumph-of-numbers-cohen-2005/</link>
		<comments>http://www.rensenieuwenhuis.nl/the-triumph-of-numbers-cohen-2005/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 10:00:54 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Book]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[Cohen]]></category>
		<category><![CDATA[Nightingale]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[nursing]]></category>
		<category><![CDATA[Quetelet]]></category>
		<category><![CDATA[sociology]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=1092</guid>
		<description><![CDATA[My new job involves working with numbers. A lot. So, I started reading about using numbers, and I very much enjoyed &#8216;The Triumph of Numbers&#8217; by I.B. Cohen (2005). This book gives an historical account ...]]></description>
				<content:encoded><![CDATA[<p>My <a href="http://www.rensenieuwenhuis.nl/finished-thesis-new-job/">new</a> job involves working with numbers. A lot. So, I started reading about using numbers, and I very much enjoyed &#8216;The Triumph of Numbers&#8217; by I.B. Cohen (2005). This book gives an historical account not only of how numbers were used in different times, but also of &#8216;how counting shaped modern life&#8217;. </p>
<p>The books starts out by illustrating the power of numbers. Just by using very simple calculations, Cohen quickly arrives at the conclusion that the building of the ancient pyramids involved placing one giant block of stone in the structure, <i>every two minutes</i>. Since the weight of such stones is enormous, this required quite advanced techniques to achieve. Knowing the vast size of such an operation, this helps us to gain an understanding in how the Egyptians may have done it, and the level of technology available to them.</p>
<p>For long, people have been fascinated by numbers. Cohen&#8217;s description of the history of using numbers therefore starts with numerology. The reader is treated with lovely exercises is numerology: it is quite amazing how we can prove about anything, simply by reordering numbers that somehow correspond to letters. If only there was an empirical basis for such magic.</p>
<p>Off to more serious applications of numbers (by today&#8217;s standards), Cohen locates the proper start of using numbers in Hutcheson&#8217;s Moral Arithmetic. Hutcheson used formulae (and which are based on numbers) to make his claims about morality. Here, numbers were only used to illustrate a claim, but not much later people started to relate such numbers to observable phenomena. An example of this Benjamin Franklin, who used his mathematical genius to find arguments based on numbers for his political claims regarding the safety of  inoculation against smallpox. He used numbers to show it was safe to have your children inoculated.<br />
<span id="more-1092"></span><br />
Many more examples are given of how claims were backed up with (increasingly advanced) numbers, and calculations based on these numbers. For instance, Alexandre Louis&#8217;s statistics showed the ineffectiveness of blood-letting in treating patients. Laplace used probability theory to suggest improvements to both the British and the French judicial system. Guerry was struck by the regularities he found in his tables on crime. Quetelet, referred to as the &#8216;powerhouse of the statistical movement&#8217;, introduced the &#8216;average man&#8217;. He explicitly started using statistics to gain an understanding of society. Quetelet is seen as the founder of statistically based sociology.</p>
<h4>What did I learn</h4>
<p>I think that the central claim of the book is that statistics became interesting when society became more complex. Especially in warfare, knowing how many troops one has, and can expect in the coming years, provides key insights in military strength. Unsurprisingly, the results of early censuses were highly confidential, not to give the opponents the benefit of the information. From a sociological perspective, this insight allows the rise of the use of statistics to be understood from an evolutionary perspective: the fundaments of societies change, and so does the way people think, <i>as a result of that</i>.  </p>
<h4>Florence Nightingale: the lady with the numbers</h4>
<p>I was especially intrigued by Cohen spending a complete chapter on Florence Nightingale. As early readers of this blog may know, I used to be in the nursing profession myself, and was inspired by how she and her ideas was a strong force behind the movement towards a professional nursing practice. Of course, Nightingale has saved many lives during the Crimean Wars, &#8216;simply&#8217; by improving sanitary and hygienic conditions in the war hospitals. Later, she improved these conditions in other hospitals, saving many more. </p>
<p>What I didn&#8217;t knwo about Nightingale, is that she greatly admired Quetelet. Cohen acclaims her not by inventing new statistics, but by using them appropriately in a time when such use was not common at all. By recording causes of death, Nightingale found that many soldiers died from infections, rather than war wounds. Also, using these records, she was able to show the results of the sanitary and hygienic changes she made. In that, she was a very early proponent of evidence based medicine. </p>
<h4>Conclusion: Abrupt Ending</h4>
<p>Unfortunately, the book seems to come to an abrupt ending after Florence Nightingale&#8217;s interest in statistics is described. As a result of this, only the distant history of statistics is dealt with in this book, whereas the introduction seems to hint at more recent developments in the use of numbers as well. Since the book was published after his death in 2003, I suspect that Cohen has been unable to finish his work. Despite this abrupt ending, however, I think the book is a very nice introduction to the history of using numbers, and provides an insightful overview of many of those in history who have contributed to the modern applications of statistics.</p>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;<br />
<i>This post is part of my &#8216;Reading List&#8217;. In this series I jot down some thoughts about the books I read and enjoyed. Some posts my give a somewhat balanced overview of a book, others will just focus on some aspects that, for whatever reason, caught my attention. Never are these posts meant as an evaluation or even review of the book. I just like to share some impressions. </p>
<p>An <a href="http://www.rensenieuwenhuis.nl/reading-list/">overview of my Reading List</a> is available, which contains both a list of the books that I wrote about, and another list of books I&#8217;m planning to read.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/the-triumph-of-numbers-cohen-2005/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Curving Normality Blog Carnival #1</title>
		<link>http://www.rensenieuwenhuis.nl/curving-normality-blog-carnival-1/</link>
		<comments>http://www.rensenieuwenhuis.nl/curving-normality-blog-carnival-1/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 10:00:11 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[blog carnival]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[papers]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[terrorism]]></category>
		<category><![CDATA[world economy]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=848</guid>
		<description><![CDATA[Today, I am happy to present to you the first edition of the <i>Curving Normality blog carnival</a>. It is all about the quantitative social sciences, and aims at bringing together high quality blog posts about our lovely profession. With just a few weeks of preparation, I am very pleased with the number of submissions, and especially glad with their quality. Apparently, the quantitative social scientists are quite well represented in the blogosphere!]]></description>
				<content:encoded><![CDATA[<p>Today, I am happy to present to you the first edition of the <i>Curving Normality blog carnival</i>. It is all about the quantitative social sciences, and aims at bringing together high quality blog posts about our lovely profession. With just a few weeks of preparation, I am very pleased with the number of submissions, and especially glad with their quality. Apparently, the quantitative social scientists are quite well represented in the blogosphere!<br />
<span id="more-848"></span><br />
The first article was submitted <i>really</i> quickly by <a href="http://www.scicha.org/blog/">Inti Suarez</a>. In his series on the applicability of (social) science articles for political practice, he  <a href="http://www.scicha.org/blog/?p=46">investigates the worth of an article on Terrorism and the world economy</a>. After sharing some of his own personal experiences in politics with having difficulties to properly define the concept of `terrorism&#8217;, he praises the article to be confined to a single issue. To come short: &#8220;<i>The claim of this paper is straightforward: if a country is threaten by terrorism, it will attract less investments.</i>&#8221; Does this have practical relevance? <i>&#8220;What is painful to realize is that this conclusion might reinforce the terrorist agenda, instead of weaken it.&#8221;</i></p>
<p>Secondly, statistics aficionado Stijn Ruiter writes on his blog <a href="http://stijnr.socsci.ru.nl/blog/">&#8216;Your Sixth Degree&#8217;</a> about the advanced use of statistics. In his <a href="http://stijnr.socsci.ru.nl/blog/?p=244">post on the presidential elections and the so-called Bradley-effect</a>, he does however show that without asking the right question, advanced statstics does bring you nowhere. The election of Barack Obama denies this Bradley-effect, which <i>&#8220;basically refers to the idea that a black American would not get elected because in the election booth voters would decide against what they said in the polls.&#8221;</i> However, research should perhaps have a more detailed starting point: <i>&#8220;The Bradley effect hypothesis is rather general, and as it is generally described (as above), it does not really specify who the voters are and what characteristics they (should) have. It only specifies whom to choose from, a black candidate or a white candidate. But there are two sides to the voting equation, namely voters and candidates. [&#8230;] So, the question becomes who votes for whom.&#8221;</i> ((Also see <a href="http://www.iq.harvard.edu/blog/sss/archives/2008/10/dan_hopkins_on.shtml">Gary King&#8217;s note</a> on a paper investgating the (decline) of the Bradley effect.))</p>
<p>Such a detailed perspective was also taken up in an article on the <a href="http://www.rensenieuwenhuis.nl/archive/immigrant-children-from-poor-countries-academically-outperform-those-from-developed-countries/">educational achievement of migrants&#8217; children</a>, which I described myself a while ago. <i>&#8220;The authors of the article â€” recently published in American Sociological Review â€” were able to take into account influences from both (characteristics of) country of origin, country of destination, and the migrant community in the country of origin.&#8221;</i> Doing so, has led to some interesting findings, which would remain unclear if not this level of detail was maintained. <i>&#8220;Counter-intuitively, immigrant children from countries with lower levels of economic development have better scholastic performance than comparable children who emigrate from countries with higher levels of economic development.&#8221;</i></p>
<p>Also focused on <a href="http://jostamon.blogspot.com/2008/07/educated-mothers-and-attainment-among.html">educational attainment of migrants&#8217; children</a>, in relation with integration in the host society, <a href="http://jostamon.blogspot.com/">FÃ«anor on &#8216;Just a Mon&#8217;</a> discusses a &#8216;natural experiment&#8217;. This natural experiment entails that after Indonesian independence thousands Moluccans were allowed to settle in various Dutch municipalities. The socio-economic backgrounds of these people were rather similar, which allowed the the researchers to compare their children on educational achievement, and cross-tabulate this with measures of integration. They found that <i>&#8220;children from Moluccan fathers and native mothers have a higher educational attainment than children from ethnic homogeneous Moluccan couples or children from a Moluccan mother and a native father.&#8221;</i></p>
<p>Finally, a `natural experiment&#8217; is nice, but what about the holy grail of scientific rigourness: a real experiment? Often difficult to achieve in the social sciences, but it has been done. <a href="http://scienceblogs.com/notrocketscience/">Ed Yong on &#8216;Not exactly Rocket Science&#8217;</a> discusses an <a href="http://scienceblogs.com/notrocketscience/2008/11/the_spread_of_disorder_can_graffiti_promote_littering_and_th.php">experimental test of the &#8216;broken windows theory&#8217;</a>, <i>&#8220;which suggests that signs of petty crimes, like broken windows, serve as a trigger for yet more criminal behaviour&#8221;</i>. The science-published article describes how simple experiments were conducted, such as measuring &#8216;littering&#8217; when a wall was severely tainted by graffiti, or when it was completely painted over. A very interesting article, and Ed Yong gives a thorough summary. <i>&#8220;All in all, the suite of experiments, all in a realistic setting, provide powerful evidence that the Broken Windows Theory is valid and all of Keiser&#8217;s results were statistically significant&#8221;</i></p>
<p>That&#8217;s it for today. No more entries for this first edition of the Curving Normality blog carnival. I would like to thank all those having submitted their entries. It was very nice to read all your blogs and to tie it all together in this editorial. The next edition will be published on the first day of 2009, so please submit your next article in the comments below as soon as it&#8217;s ready!</p>
<p><!--adsense--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/curving-normality-blog-carnival-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Newsflash: Lucia de B. gets re-trial!</title>
		<link>http://www.rensenieuwenhuis.nl/newsflash-lucia-de-b-gets-re-trial/</link>
		<comments>http://www.rensenieuwenhuis.nl/newsflash-lucia-de-b-gets-re-trial/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 10:00:37 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[Science]]></category>
		<category><![CDATA[Lucia de B.]]></category>
		<category><![CDATA[lying with statistics]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=668</guid>
		<description><![CDATA[Dutch nurse Lucia de B., convicted to a life sentence for the murder on 7 infants during her shifts, is now entitled to a re-trial. Why do I write about it here? Because one of the grounds she was convicted on was a statistical argument. A statistical argument that has been thoroughly contested by prominent statisticians, arguing that according to the court's line of reasoning, <a href="http://www.math.leidenuniv.nl/~gill/hetero2.pdf">one out of every nine nurses</a> would go to jail!

I have written before about this statistical argument, but did so in Dutch. For those interested, I'll give you a short recap, and a nice movie.]]></description>
				<content:encoded><![CDATA[<p>Dutch nurse Lucia de B., convicted to a life sentence for the murder on 7 infants during her shifts, is now entitled to a re-trial. Why do I write about it here? Because one of the grounds she was convicted on was a statistical argument. A statistical argument that has been thoroughly contested by prominent statisticians, arguing that according to the court&#8217;s line of reasoning, <a href="http://www.math.leidenuniv.nl/~gill/hetero2.pdf">one out of every nine nurses</a> would go to jail!</p>
<p>I <a href="http://www.rensenieuwenhuis.nl/archive/zaak-lucia-de-b-wordt-herzien/">have</a> <a href="http://www.rensenieuwenhuis.nl/archive/lucia-de-b-deel-2/">written</a> <a href="http://www.rensenieuwenhuis.nl/archive/hoe-groot-is-de-kans-dat-lucia-de-b-onschuldig-vastzit/">before</a> about this statistical argument, but did so in Dutch. For those interested, I&#8217;ll give you a short recap, and a nice movie.<br />
<span id="more-668"></span></p>
<p>Lucia de B. has been convicted for murder on seven children on numerous grounds. Most of these have been contested or already been refuted. Ton Derksen, a Dutch philosopher of science, even wrote a book to discuss many of the court&#8217;s considerations. One of the main arguments has been, that an statistically highly improbable number of children died during her shifts. There are many arguments against this statement. For instance, after she was related to one unusual death, investigators specifically sought for other unusual deaths during her shifts. Clearly, this increases the numerator of the abovementioned chance. Later, it was discovered that the &#8216;unusual&#8217; deaths actually didn&#8217;t need to be unusual, for the &#8216;unusual&#8217; substance in the infants&#8217; blood had been mixed up with a similarly named, but completely different, substance that is found in infants blood very often.</p>
<p>Nevertheless, one of the courts&#8217; main considerations was that the chance that so many infants would die during or shortly after the shifts of Lucia de B. was so low, that she had to be guilty. Apparently, the court reasoned that the probability of these events (deaths during her shift) was so low, that other explanations would be highly improbable. </p>
<p>Clearly, something goes horribly wrong here. In the movie below a similar case is addressed by Peter Donnelly in a very accessible way. In the case that is addressed in the movie, a woman was convicted for the murder of her two children. These two children, independently, had died from sudden infant death syndrome. Sudden infant death syndrome is rather rare (and a tragedy for the family). As a matter of fact, it is so rare, that the chance that it happens to two babies of the same mother is so extremely small (according to the judge: extremely small chance times another extremely small chance), that this mother was sent to jail. </p>
<p>I&#8217;m not going to completely summarise Peter Donnelly&#8217;s arguments, but what it comes down to, is that we should interpret the court&#8217;s decision as a &#8216;test&#8217;. And we know of statistical tests that two errors can be made: we can erroneously conclude that an event is highly improbably, while it in fact is not. Or, we can erroneously conclude that something is not highly improbable, while in fact it is.</p>
<p>What this has to do with the case of the mother who lost two of her babies to infant death syndrome, and correspondingly to the case of Lucia de B., is made clear in the movie below. The basic argument, which relates to the case of Lucia de B., is that although some events are rather rare, if enough possibilities for the event to occur are present (lots of mothers have two babies, many nurses work with infants who die), the odds of the event to occur <i>in the whole population</i> isn&#8217;t that small at all. Watch and see how Peter Donnelly explains this eloquently:</p>
<p><object height="353" width="425"><param name="movie" value="http://www.youtube.com/v/kLmzxmRcUTo"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/kLmzxmRcUTo" type="application/x-shockwave-flash" wmode="transparent" height="353" width="425"></embed></object></p>
<p>And by the way: for the statisticians amongst us: the prominent statisticians <a href="<a href="http://www.math.leidenuniv.nl/~gill/hetero2.pdf">&#8220;>I mentioned before</a> basically argue that the assumption of homoscedasticity has not been met, which makes matters even worse!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/newsflash-lucia-de-b-gets-re-trial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R-Sessions 11: Tables</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-11-tables/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-11-tables/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 10:00:27 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[Academic Software]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[tables]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=425</guid>
		<description><![CDATA[The one most often used function in the analysis of statistical data is the creation of tables. This edition of the R-Sessions describes the use of several functions to do some nifty cross-tabulations. And more.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img src="http://i0.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" " title="R-Sessions" data-recalc-dims="1" /></a></p>
<p><!--adsense--><br />
The one most often used function in the analysis of statistical data is the creation of tables. This edition of the R-Sessions describes the use of several functions to do some nifty cross-tabulations. And more.</p>
<h2>TAPPLY</h2>
<p>The function TAPPLY can be used to perform calculations on table-marginals. Different functions can be used, such as MEAN, SUM, VAR, SD, LENGTH (for frequency-tables). For example: <span id="more-425"></span></p>
<blockquote><p> x &lt;- c(0,1,2,3,4,5,6,7,8,9)<br />
y &lt;- c(1,1,1,1,1,1,2,2,2,2)<br />
tapply(x,y,mean)<br />
tapply(x,y,sum)<br />
tapply(x,y,var)<br />
tapply(x,y,length)</p></blockquote>
<pre>
&gt; x &lt;- c(0,1,2,3,4,5,6,7,8,9)
&gt; y &lt;- c(1,1,1,1,1,1,2,2,2,2)
&gt; tapply(x,y,mean)
  1     2
2.5   7.5
&gt; tapply(x,y,sum)
 1  2
15 30
&gt; tapply(x,y,var)
       1        2
3.500000 1.666667
&gt; tapply(x,y,length)
1 2
6 4
&gt;</pre>
<h2>FTABLE</h2>
<p>More elaborate frequency tables can be created with the FTABLE-function. For example:</p>
<blockquote><p> x &lt;- c(0,1,2,3,4,5,6,7,8,9)<br />
y &lt;- c(1,1,1,1,1,1,2,2,2,2)<br />
z &lt;- c(1,1,1,2,2,2,2,2,1,1)<br />
ftable(x,y,z)</p></blockquote>
<pre>
&gt; x &lt;- c(0,1,2,3,4,5,6,7,8,9)
&gt; y &lt;- c(1,1,1,1,1,1,2,2,2,2)
&gt; z &lt;- c(1,1,1,2,2,2,2,2,1,1)
&gt; ftable(x,y,z)
    z 1 2
x y
0 1   1 0
  2   0 0
1 1   1 0
  2   0 0
2 1   1 0
  2   0 0
3 1   0 1
  2   0 0
4 1   0 1
  2   0 0
5 1   0 1
  2   0 0
6 1   0 0
  2   0 1
7 1   0 0
  2   0 1
8 1   0 0
  2   1 0
9 1   0 0
  2   1 0</pre>
<p><i><br />
&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;</p>
<ul>
<li><b><a href="http://www.rensenieuwenhuis.nl/R-forum/">Discuss this article and pose additional questions in the R-Sessions Forum</a></b></li>
<li><b><a href="http://www.rensenieuwenhuis.nl/r-project/manual/basics/tables/">Find the original article embedded in the manual.</a></b></li>
</ul>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;<br />
<a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/">R-Sessions</a> is a collection of manual chapters for R-Project, which are maintained on <a href="www.rensenieuwenhuis.nl">Curving Normality</a>. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.<br />
&#8212;&#8212;&#8211; &#8212;&#8211; &#8212; &#8212; &#8211; &#8211;<br />
</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-11-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R-Sessions 10: Conditionals</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-10-conditionals/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-10-conditionals/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 10:00:21 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[Academic Software]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=423</guid>
		<description><![CDATA[Conditionals, or logicals, are used to check vectors of data against conditions. In practice, this is used to select subsets of data or to recode values. In this edition of the R-Sessions, some of the fundamentals of conditionals are described.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img src="http://i2.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" " title="R-Sessions" data-recalc-dims="1" /></a></p>
<p><!--adsense--> Conditionals, or logicals, are used to check vectors of data against conditions. In practice, this is used to select subsets of data or to recode values. Here, only some of the fundamentals of conditionals are described.</p>
<h2>Basics</h2>
<p>The general form of conditionals are two values, or two sets of values, and the condition to test against. Examples of such tests are &#8216;is larger than&#8217;, &#8216;equals&#8217;, and &#8216;is larger than&#8217;. In the example below the values &#8216;3&#8217; and &#8216;4&#8217; are tested using these three tests.</p>
<blockquote><p> 3 &gt; 4 3 == 4 3 &lt; 4</p></blockquote>
<p><span id="more-423"></span></p>
<pre>
&gt; 3 &gt; 4
[1] FALSE
&gt; 3 == 4
[1] FALSE
&gt; 3 &lt; 4
[1] TRUE</pre>
<h2>Numerical returns</h2>
<p>The output shown directly above makes clear that R-Project returns the values &#8216;TRUE&#8217; and &#8216;FALSE&#8217; to conditional tests. The results here are pretty straightforward: 3 is not larger than 4, therefore R returns FALSE. If you don&#8217;t desire TRUE or FALSE as response, but a numeric output, use the as.numeric() command which transforms the values to numerics, in this case &#8216;0&#8217; or &#8216;1&#8217;. This is shown below.</p>
<blockquote><p> as.numeric(3 &gt; 4) as.numeric(3 &lt; 4)</p></blockquote>
<pre>
&gt; as.numeric(3 &gt; 4)
[1] 0
&gt; as.numeric(3 &lt; 4)
[1] 1</pre>
<h2>Conditionals on vectors</h2>
<p>As on most functionality of R-project, vectors (or multiple values) can be used alongside single values, as is the case on conditionals. These can be used not only against single values, but against variables containing multiple values as well. This will result in a succession of tests, one for each value in the variable. The output is a vector of values, &#8216;TRUE&#8217; or &#8216;FALSE&#8217;.The examples below show two things: the subsequent values 1 to 10 are tested against the condition &#8216;is smaller than or equals 5&#8242;. It is shown as well that when these values are assigned to a variable (here: &#8216;x&#8217;), this variable can be tested against the same condition, giving exactly the same results.</p>
<blockquote><p> 1:10 1:10 &lt;= 5 x &lt;- 1:10 x &lt;= 5</p></blockquote>
<pre>
&gt; 1:10
 [1]  1  2  3  4  5  6  7  8  9 10
&gt; 1:10 &lt;= 5
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
&gt; x &lt;- 1:10
&gt; x == 5
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE</pre>
<h2>Conditionals and multiple tests</h2>
<p>More tests can be gathered into one conditional expression. For instance, building on the example above, the first row of the next example tests the values of variable &#8216;x&#8217; against being smaller than or equal to 4, <em>or</em> being larger than or equal to &#8216;6&#8217;. This results in &#8216;TRUE&#8217; for all the values, except for 5. Since the &#8216;|&#8217;-operator is used, only one of the set conditions need to be true. The second row of this example below tests the same values against two conditions as well, namely &#8216;equal to or larger than 4&#8242; and &#8216;equal to or smaller than 6&#8242;. since this time the &#8216;&amp;&#8217;-operator is used, both conditionals need to be true.</p>
<blockquote><p> x &lt;= 4 | x &gt;= 6 x &gt;= 4 &amp; x &lt;= 6</p></blockquote>
<pre>
&gt; x &lt;= 4 | x &gt;= 6
 [1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
&gt; x &gt;= 4 &amp; x &lt;= 6
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE</pre>
<h2>Conditionals on character values</h2>
<p>In the example below, a string variable &#8216;gender&#8217; is constructed, containing the values &#8216;male&#8217; and &#8216;female&#8217;.  This is shown in the first two rows of the example below.</p>
<blockquote><p> gender &lt;- c(&#8220;male&#8221;,&#8221;female&#8221;,&#8221;female&#8221;,&#8221;male&#8221;,&#8221;male&#8221;,&#8221;male&#8221;,&#8221;female&#8221;) gender == &#8220;male&#8221;</p></blockquote>
<pre>
&gt; gender &lt;- c("male","female","female","male","male","male","female")
&gt; gender == "male"
[1]  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE</pre>
<h2>Additional functions</h2>
<p>The last examples demonstrate two other functions using conditionals, using the same &#8216;gender&#8217; variable as above. The first is an additional way to get a numerical output of the same test as in the row above. The iselse() command has three arguments: the first is a conditional, the second is the desired output if the conditional is TRUE, the third is the output in case the result of the test is &#8216;FALSE&#8217;. The second example shows a way to obtain a list of which values match the condition tested against. In the output above, the second, third and last values are &#8216;female&#8217;. Using which() and the condition &#8220;== &#8216;male&#8217; &#8221; (equals &#8216;male&#8217;) returns the indices of the values in variable &#8216;gender&#8217; that equal &#8216;male&#8217;.</p>
<blockquote><p> ifelse(gender==&#8221;male&#8221;,0,1) which(gender==&#8221;male&#8221;)</p></blockquote>
<pre>
&gt; ifelse(gender=="male",0,1)
[1] 0 1 1 0 0 0 1
&gt; which(gender=="male")
[1] 1 4 5 6</pre>
<h2>Conditionals on missing values</h2>
<p>Missings values (&#8216;NA&#8217;) form a special case in many ways, such as when using conditionals. Normal conditionals cannot be used to find the missing values in a range of values, as is shown below.</p>
<blockquote><p> x &lt;- c(4,3,6,NA,4,3,NA) x == NA which(x == NA)  is.na(x) which(is.na(x))</p></blockquote>
<p>The last two rows of the syntax above show what can be done. The is.na() command tests whether a value or a vector of values is missing. It returns a vector of logicals (&#8216;TRUE&#8217; or &#8216;FALSE&#8217;), that indicates missing values with a &#8216;TRUE&#8217;. Nesting this command in the which() command described earlier enables us to find which of the values are missing. In this case, the fourth and the seventh values are missing.</p>
<pre>
&gt; x &lt;- c(4,3,6,NA,4,3,NA)
&gt; x == NA
[1] NA NA NA NA NA NA NA
&gt; which(x == NA)
integer(0)
&gt; is.na(x)
[1] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
&gt; which(is.na(x))
[1] 4 7</pre>
<p><i><br />
&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;</p>
<ul>
<li><b><a href="http://www.rensenieuwenhuis.nl/r-forum/topic/r-sessions-10-conditionals">Discuss this article and pose additional questions in the R-Sessions Forum</a></b></li>
<li><b><a href="http://www.rensenieuwenhuis.nl/r-project/manual/basics/conditionals/">Find the original article embedded in the manual.</a></b></li>
</ul>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;<br />
<a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/">R-Sessions</a> is a collection of manual chapters for R-Project, which are maintained on <a href="www.rensenieuwenhuis.nl">Curving Normality</a>. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.<br />
&#8212;&#8212;&#8211; &#8212;&#8211; &#8212; &#8212; &#8211; &#8211;<br />
</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-10-conditionals/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>useR! 2008: Harrell already wrote it &#8230;</title>
		<link>http://www.rensenieuwenhuis.nl/user-2008-harrell-already-wrote-it/</link>
		<comments>http://www.rensenieuwenhuis.nl/user-2008-harrell-already-wrote-it/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 15:33:27 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[Frank E. Harrell]]></category>
		<category><![CDATA[multiple imputation]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[useR! 2008]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=440</guid>
		<description><![CDATA[Unfortunately, Frank E. Harrell Jr. already wrote the book that I would have loved to (be able to) write, probably somewhere at the end of my career. If at all. Fortunately, I can learn a lot very much faster now. I'm talking about a book on statistics that also contains a perspective and opinion on the application statistics. Oh, and Harrell also demonstrates his main arguments in R-Project. And now he is telling me that his philosophy on applied statistics is also condensed in an R-package (the design package). 

]]></description>
				<content:encoded><![CDATA[<p><!--adsense--><br />
Unfortunately, Frank E. Harrell Jr. already wrote the book that I would have loved to (be able to) write, probably somewhere at the end of my career. If at all. Fortunately, I can learn a lot very much faster now. I&#8217;m talking about a book on statistics that also contains a perspective and opinion on the application statistics. Harrell called his book &#8220;Regression Modeling Strategies&#8221;. Oh, and he also demonstrates his main arguments in R-Project. And now he is telling me that his philosophy on applied statistics is also condensed in an R-package (the design package). </p>
<p>An eye-opener to me was his description of non-statisticians being afraid of continuous variables. <span id="more-440"></span> Indeed, when I doubt the linearity of a continuous variable and I can&#8217;t find a way to fix it, I tend to categorize or even dichotomize it. I feel that this is not very uncommon to do so, but now that I have heard Harrell&#8217;s criticism on this strategy, I hope to never do that again and will give some serious thought on his suggestion of using &#8216;spline functions&#8217;. He argued that we tend to dichotomize a not-completely-linear variable, because we do not believe that it is linear in reality. But, given the finding of near linearity in our data, do we believe reality to be dichotomized? Probably our actions will bring our model farther away form correspondence with reality, than would our inaction. <i>&#8220;Nature is not that kind. There is no reason to expect linearity.&#8221;</i></p>
<p>This does, again, raise the question on how theory and statistical model relate to each other. Is there a need to analyze data with models that are richer in detail than the actual theory that we are testing? If  so, does this subsequently mean that our theories are not fit (enough) to be tested with the models that we are forced to estimate?</p>
<p>It is too easy to find interpretations of parameters that basically don&#8217;t have any meaning at all. Harrell told a story of how he had found a hugely significant interaction parameter. He went to the cardiologist he worked with, who soon thought of an interpretation. Then, Harrell found that he had made a mistake, and had to correct the sign of the parameter. Shockingly, the cardiologist immediately had a new, and completely different, interpretation. </p>
<p>Several other topics were addressed, such as the treatment of missing values. It is all to easy to add a category &#8216;missing&#8217; to our analyses, but this does mess up our number of degrees of freedom. He analyzed how a horrid technique as variable / model selection came about, by arguing that computer were able to perform the technique, before they could run the simulations to properly test the technique. Harrell explained that the purpose of data imputation is not to recover missing data, but to retain data that was not not missing to begin with on the other variables.</p>
<p>The last half hour of the presentation was spend on analyzing some magnificent data with survival rates of passengers of the Titanic. <i>&#8220;What did &#8216;women and children first&#8217; really mean?&#8221;</i> Applying much of what he already discussed, he was not only able to show how age, sex, and social social class affected chances of survival, but moreover how they interacted. For instance, younger people indeed had relatively high chances of survival, but lower class young males (> 20 years) had almost no chance of survival.</p>
<p>A lot of other topics were covered, only some of which I hinted at here. All were stated with confidence and from a clear perspective. I don&#8217;t really know what to think of the implications for the relationship between theory and empirical analysis, but Harrell sure has given some input to my thought on that. However, what I learned on the most fundamental level, I think, is that I can&#8217;t wait for the conference bookstore to open tomorrow.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/user-2008-harrell-already-wrote-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>useR! 2008: Bates excels on mixed models</title>
		<link>http://www.rensenieuwenhuis.nl/user-2008-bates-excels-on-mixed-models/</link>
		<comments>http://www.rensenieuwenhuis.nl/user-2008-bates-excels-on-mixed-models/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 12:11:42 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[Douglas Bates]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[useR!]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=436</guid>
		<description><![CDATA[Douglas Bates excelled during my first tutorial session of the useR! 2008 conference. He gave a three hours talk on mixed models, in which he was able to give an overview on theory and basic specification of these kind of models in R-Project, and to address highly advanced and avant-garde issues as well. I'm impressed. During the brake he was so kind as to answer a question regarding mixed models, that had nothing much to do with what he addressed during his talk. We even ended up having a short but nice talk about dutch politics. ]]></description>
				<content:encoded><![CDATA[<p><!--adsense--><br />
Douglas Bates excelled during my first tutorial session of the useR! 2008 conference. He gave a three hours talk on mixed models, in which he was able to give an overview on theory and basic specification of these kind of models in R-Project, and to address highly advanced and avant-garde issues as well. I&#8217;m impressed. During the brake he was so kind as to answer a question regarding mixed models, that had nothing much to do with what he addressed during his talk. We even ended up having a short but nice talk about dutch politics. </p>
<p>During what was basically his introduction, he gave a nice guideline regarding a discussion that we have been having at our own university. It is the discussion on what instances we can apply mixed models to grouped data, and in what cases we can&#8217;t. <span id="more-436"></span> Although he basically didn&#8217;t add anything that was new to me, his statements gave a lot of clarity to my thoughts on the subject. His basic argument was that we can estimate a grouping factor as mixed effects only, if it is reasonable that they come from a collection of these factors. So, for instance, the distinction between male and female would not be a good mixed effect, because should we repeat the &#8216;experiment&#8217;, we would automatically end up with the same values (male and female) on our grouping factor. A good example would be the class that a school child is in, for when we repeat the experiment with a new sample, we would end up with students in different classes. More interestingly, though, was his acknowledgment that there are simply grey areas. These are found on two extremes of the same dimension. When a small number of grouping factors are present, we end up with problems estimating the model. On the other hand, if we have (almost) all existing factors (i.e. all American states in a survey research project), then we wouldn&#8217;t end up with different grouping factors (states) when the project would be repeated. I find the fact that these extremes are defined as a grey area is rather clarifying and more informative than simply taking one of the extreme positions (&#8216;always estimate mixed models&#8217; or `mixed models are completely flawed in such cases&#8217;.</p>
<p>Following this introduction, a wide array of issues were addressed. Longitudinal models with time as a co-variate, interactions on the level of the grouping factor, theory of generalized models, an example of these generalized linear models, and finally some attention was paid to non-linear mixed models. </p>
<p>What I found especially interesting, though, was the explanation of how item response models can be represented by using a generalized linear mixed models. Item response models are based on theory that basically states that the responses people give to a stimulus (i.e. survey questions), are both due to characteristics of the stimulus, and due to characteristics of the respondent. We thus need a method for disentangling both influences. Douglas Bates demonstrated a method of doing so by applying mixed models. For long, computer was not capable of properly estimating such models. Now, it has become possible to approach the analysis of such models, by interpreting the responses to the items to be nested within individuals. Both item characteristics and person characteristics can then be added to this basic model. </p>
<p>To sum up, I found this session to be extremely fascinating. It gave a very good overview on mixed models, I learned some new thing, and I saw things that I did not understand. At all. That&#8217;s the risk that lies in getting a statistics course given by a mathematician. But, since we have the slides and books, these sections of the course will still function as pointers of what topics to study in the future. </p>
<p>Being in such an interdisciplinary setting as the useR! conference does that to you: you see topics and methods used in a completely different context that what you&#8217;re used to. From that you can easily gain a more general understanding of the techniques you work with within the safe confines of your own discipline. Very enriching and inspiring, and I think the applause was well deserved.</p>
<p>More to come this afternoon, when I will attend a session by Frank E. Harrell Jr. on regression modeling strategies. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/user-2008-bates-excels-on-mixed-models/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R-Sessions 09: Data Manipulation</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-09-data-manipulation/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-09-data-manipulation/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 10:00:39 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[Academic Software]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[data-manipulation]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=421</guid>
		<description><![CDATA[Today's edition of R-Sessions deals with the manipulation of data that is stored R-Project. Building upon the previous R-Session, attention is paid to recoding of data, ordering, and finally the merging of several sets of data.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img src="http://i1.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" " title="R-Sessions" data-recalc-dims="1" /></a><br />
<!--adsense--></p>
<p>Today&#8217;s edition of R-Sessions deals with the manipulation of data that is stored R-Project. Building upon the previous R-Session, attention is paid to recoding of data, ordering, and finally the merging of several sets of data.</p>
<p><span id="more-421"></span></p>
<h2>Recoding</h2>
<p>The most direct way to recode data in R-Project is using a combination of both indexing and <a href="http://www.rensenieuwenhuis.nl/r-project/manual/basics/conditionals/">conditionals as described elsewhere</a>. To exemplify this, a simply data.frame will be created below, containing variables indicating gender and monthly income in thousands of euros.</p>
<blockquote><p> gender &lt;- c(&#8220;male&#8221;, &#8220;female&#8221;, &#8220;female&#8221;, &#8220;male&#8221;, &#8220;male&#8221;, &#8220;male&#8221;, &#8220;female&#8221;)<br />
income &lt;- c(54, 34, 556, 57, 88, 856, 23)<br />
data &lt;- data.frame(gender, income)<br />
data</p></blockquote>
<pre>
&gt; gender &lt;- c("male", "female", "female", "male", "male", "male", "female")
&gt; income &lt;- c(54, 34, 556, 57, 88, 856, 23)
&gt; data &lt;- data.frame(gender, income)
&gt; data
  gender income
1   male     54
2 female     34
3 female    556
4   male     57
5   male     88
6   male    856
7 female     23</pre>
<p>Some of the values on the income variable seem exceptionally high. Let&#8217;s say we want to remove the two values on income higher than 500. In order to do so, we use the which() command, that reveals which of the values is greater than 500. Next, the result of this is used for indexing the data$income variable. Finally, the indicator for missing values, &#8216;NA&#8217; is assigned to the that selected values of the &#8216;income&#8217; variables. Obviously, we would normally only use the third line. The first two are shown here, to make clear exactly what is happening.</p>
<blockquote><p> which(data$income &gt; 500)<br />
data$income[data$income &gt; 500]<br />
data$income[data$income &gt; 500] &lt;- NA<br />
data</p></blockquote>
<pre>
&gt; which(data$income &gt; 500)
[1] 3 6
&gt; data$income[data$income &gt; 500]
[1] 556 856
&gt; data$income[data$income &gt; 500] &lt;- NA
&gt; data
  gender income
1   male     54
2 female     34
3 female     NA
4   male     57
5   male     88
6   male     NA
7 female     23</pre>
<p>Sometimes, it is desirable to replace missing values by the mean on the respective variables. That is what we are going to do here. Note, that in general practice it is not very sensible to impute two missing values using only five valid values. Nevertheless, we will proceed here.<br />
The first row of the example below shows that it is not automatically possible to calculate the mean of a variable that contains missing values. Since R-Project cannot compute a valid value, NA is returned. This is not what we want. Therefore, we instruct R-Project to remove missing values by adding na.rm=TRUE to the mean() command. Now, the right value is returned. When the same selection-techniques as above are used, an error will occur. Therefore, we need the is.na() command, that returns a vector of logicals (&#8216;TRUE&#8217; and &#8216;FALSE&#8217; ). Using is.na(), we can use the which() command to select the desired values on the income variable. To these, the calculated mean is assigned.</p>
<blockquote><p> mean(data$income)<br />
mean(data$income, na.rm=TRUE)<br />
data$income[which(is.na(data$income))] &lt;- mean(data$income, na.rm=TRUE)<br />
data</p></blockquote>
<pre>
&gt; mean(data$income)
[1] NA
&gt; mean(data$income, na.rm=TRUE)
[1] 51.2
&gt; data$income[which(is.na(data$income))] &lt;- mean(data$income, na.rm=TRUE)
&gt; data
  gender income
1   male   54.0
2 female   34.0
3 female   51.2
4   male   57.0
5   male   88.0
6   male   51.2
7 female   23.0</pre>
<h2>ORDER</h2>
<p>It is easy to sort a data-frame using the command order. Combined with indexing functions, it works as follows:</p>
<pre>
x &lt;- c(1,3,5,4,2)
y &lt;- c('a','b','c','d','e')
df &lt;- data.frame(x,y)

df
  x y
1 1 a
2 3 b
3 5 c
4 4 d
5 2 e

df[order(df$x),]
  x y
1 1 a
5 2 e
2 3 b
4 4 d
3 5 c</pre>
<h2>MERGE</h2>
<p>Merge puts multiple data.frames together, based on an identifier-variable which is unique or a combination of variables.</p>
<pre>
x &lt;- c(1,2,5,4,3)
y &lt;- c(1,2,3,4,5)
z &lt;- c('a','b','c','d','e')

df1 &lt;- data.frame(x,y)
df2 &lt;- data.frame(x,z)
df3 &lt;- merge(df1,df2,by=c("x"))

 df3
  x y z
1 1 1 a
2 2 2 b
3 3 5 e
4 4 4 d
5 5 3 c</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-09-data-manipulation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R-Sessions 08: Getting Data into R</title>
		<link>http://www.rensenieuwenhuis.nl/r-sessions-08-getting-data-into-r/</link>
		<comments>http://www.rensenieuwenhuis.nl/r-sessions-08-getting-data-into-r/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 10:00:13 +0000</pubDate>
		<dc:creator><![CDATA[Rense Nieuwenhuis]]></dc:creator>
				<category><![CDATA[R-Project]]></category>
		<category><![CDATA[R-Sessions]]></category>
		<category><![CDATA[Academic Software]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.rensenieuwenhuis.nl/?p=419</guid>
		<description><![CDATA[R-Project provides various ways to enter data. The most basic method is entering is manually, but this tends to get very tedious. An often more useful way is using the read.table command. It has some variants, as will be shown below. Another way of getting data into R is using the clipboard. The back-draw thereof is the loss of some control over the process. Finally, it will be described how data from SPSS can be read in directly.

Only basic ways of entering data into R are shown here. Much more is possible as other functions offer almost unlimited control. Here the emphasis will be on day-to-day usage.]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/"><img src="http://i0.wp.com/www.rensenieuwenhuis.nl/wp-content/uploads/2008/07/r-sessions.jpg?w=470" " title="R-Sessions" data-recalc-dims="1" /></a></p>
<p><!--adsense--></p>
<h2>Introduction</h2>
<p>Various ways are provided to enter data into R. The most basic method is entering is manually, but this tends to get very tedious. An often more useful way is using the read.table command. It has some variants, as will be shown below. Another way of getting data into R is using the clipboard. The back-draw thereof is the loss of some control over the process. Finally, it will be described how data from SPSS can be read in directly.</p>
<p>Only basic ways of entering data into R are shown here. Much more is possible as other functions offer almost unlimited control. Here the emphasis will be on day-to-day usage.</p>
<h2>Reading data from a file</h2>
<p>The most general of data-files are basically plain text-files that store the data. Rows generally represent the cases ( / respondents), although the top-row often will state the variable labels. The values these variables can take are written in columns, separated by some kind of indicator, often spaces, commas or tabs. Another variant is that there is no separating character. In that case all variables belonging to a single case are written in succession. Each variable then needs to have a specific number of character places defined, to be able to distinguish between variables. Variable labels are often left out on these type of files.<br />
<span id="more-419"></span></p>
<p>R is able to read all of the above-mentioned filetypes with the read.table() command, or its derivatives read.csv() and read.delim(). The exception to this are fixed-width files. These are loaded using the read.fwf() command, that uses different parameters. The derivatives of read.table() are basically the same command, but have different defaults. Because their use is so much convenience, these will be used here.</p>
<h2>Comma / Tab separated files</h2>
<p>As said, the most generic way of reading data is the read.table() command. When given only the filename as parameter, it treats a space as the separating character (so, beware on using spaces in variable labels) and assumes that there are no variable names on the first row of the data. The decimal sign is a &#8220;.&#8221;. This would lead to the first row of the syntax below, which assigns the contents of a datafile &#8220;filename&#8221; to the object data, which becomes a data.frame.</p>
<p>The read.csv() and the read.delim() commands are basically the same, but they have a different set of standard values to the parameters. Read.csv() is used for comma-separated files (such as, for instance, Microsoft Excell can export to). The syntax for read.csv() is very simple, as shown below. The read.table()-command can be used for the exact same purpose, by altering the parameters. The header=TRUE &#8211; parameter means that the first row of the file is now regarded as containing the variable names. The sep &#8211; parameter now indicates the comma &#8220;,&#8221; as the separating character.  fill=TRUE tells the function that if a row contains less columns than there are variables defined by the header row, the missing variables are still assigned to the data frame that results from this function. Those variables for these cases will have the value &#8216;NA&#8217; (missing). By dec=&#8221;.&#8221; the character used for decimal points is set to a point (to not interfere with the separating comma). In contrast with the read.table() function. the comment.char is disabled (set to nothing). Normally, if the comment.char is found in the data, no more data is read from the row that is was found on (after the sign, of course). In read.csv() this is disabled by default.</p>
<p>The last two rows of the syntax below shows the read.delim() command and the parameters needed to create the same functionality from read.table. The read.delim() function is used to read tab-delimited data. So, the sep-parameter is now set to &#8220;\t&#8221; by default. \t means tab. The other parameters are identical to those that read.csv() defaults to.</p>
<blockquote><p> data &lt;- read.table(&#8220;filename&#8221;)</p>
<p>data &lt;- read.csv(&#8220;filename&#8221;)<br />
data &lt;- read.table(&#8220;filename&#8221;, header = TRUE, sep = &#8220;,&#8221;, dec=&#8221;.&#8221;, fill = TRUE, comment.char=&#8221;&#8221;)</p>
<p>data &lt;- read.delim(&#8220;filename&#8221;)<br />
data &lt;- read.table(&#8220;filename&#8221;, header = TRUE, sep = &#8220;\t&#8221;, dec=&#8221;.&#8221;, fill = TRUE, comment.char=&#8221;&#8221;)</p></blockquote>
<h2>Variable labels</h2>
<p>Data that is read into a data.frame can be given variable names. For instance, if the above commands were used to read a data-file containing three variables, variable names can be assigned in several ways. Two ways will be described here: assigning them after the data is read or assigning them using the read.table() command.</p>
<blockquote><p>names(data) &lt;- c(&#8220;Age&#8221;,&#8221;Income&#8221;,&#8221;Gender&#8221;)<br />
data &lt;- read.table(&#8220;filename&#8221;, colnames=c(&#8220;Age&#8221;,&#8221;Income&#8221;,&#8221;Gender&#8221;))</p></blockquote>
<p>In the syntax above, the names() command is used to assign names to the columns of the data.frame (representing the variables). The names are given as strings (hence the apostrophes) and gathered using the c() command.</p>
<h2>Fixed width files</h2>
<p>When reading files in the &#8216;fixed width&#8217; format, we cannot rely on a single character that indicates the separations between variables. Instead, the read.fwf() function has a parameter by which we tell the function where to end a variable and start the next one. Just as with read.table(), a data.frame is returned. Variable labels are treated the same way as the previous mentioned</p>
<blockquote><p>data &lt;- read.fwf(&#8220;filename&#8221;, widths = c(2,5,1), colnames=c(&#8220;Age&#8221;, &#8220;Income&#8221;, &#8220;Gender&#8221;))<br />
data &lt;- read.fwf(&#8220;filename&#8221;, widths = c(-5,2,5,-2, 1), colnames=c(&#8220;Age&#8221;, &#8220;Income&#8221;, &#8220;Gender&#8221;))</p></blockquote>
<h2>Reading data from the clipboard</h2>
<blockquote><p>data &lt;- read.table(pipe(&#8220;pbpaste&#8221;))<br />
data &lt;- read.table(&#8220;clipboard&#8221;)</p></blockquote>
<p>read.table is used for read comma seperated files. read.delim is used for reading tab delimited files. read.table(pipe(&#8220;pbpaste&#8221;)) is used for reading data from the clipboard on mac. read.table(&#8220;clipboard&#8221;) is used for reading data from the clipboard on Windows. Instead of read.table(pipe(&#8220;pbpaste&#8221;)) you can use read.delim(pipe(&#8220;pbpaste&#8221;)) as well.</p>
<h2>Reading data from other statistical packages {foreign}</h2>
<blockquote><p>library(foreign)<br />
data &lt;-read.spss(&#8220;filename&#8221;)</p></blockquote>
<p>require(foreign) loads the foreign package, which contains the read.spss() function, which can read data as written by the SPSS software.</p>
<p><i></p>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;</p>
<ul>
<li><b><a href="http://www.rensenieuwenhuis.nl/r-forum/topic/r-sessions-08-getting-data-into-r">Discuss this article and pose additional questions in the R-Sessions Forum</a></b></li>
<li><b><a href="http://www.rensenieuwenhuis.nl/r-project/manual/basics/getting-data-into-r-2/">Find the original article embedded in the manual.</a></b></li>
</ul>
<p>&#8211; &#8211; &#8212; &#8212; &#8212;&#8211; &#8212;&#8212;&#8211;<br />
<a href="http://www.rensenieuwenhuis.nl/archive/category/r-project/r-sessions/">R-Sessions</a> is a collection of manual chapters for R-Project, which are maintained on <a href="www.rensenieuwenhuis.nl">Curving Normality</a>. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.<br />
&#8212;&#8212;&#8211; &#8212;&#8211; &#8212; &#8212; &#8211; &#8211;<br />
</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rensenieuwenhuis.nl/r-sessions-08-getting-data-into-r/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
