<?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>John Myles White: Die Sudelbücher</title>
	<atom:link href="http://www.johnmyleswhite.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnmyleswhite.com</link>
	<description>&#34;He who refuses to do arithmetic is doomed to talk nonsense.&#34;</description>
	<lastBuildDate>Tue, 09 Feb 2010 15:19:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>An Alternative to Occam&#8217;s Razor</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/02/09/an-alternative-to-occams-razor/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/02/09/an-alternative-to-occams-razor/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 15:19:24 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Aphorisms]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3818</guid>
		<description><![CDATA[In light of human foibles, I would suggest that this decision rule be used in lieu of Occam&#8217;s Razor: of several possible explanations for an observation, the most boring one is probably the most accurate.
]]></description>
			<content:encoded><![CDATA[<p>In light of human foibles, I would suggest that this decision rule be used in lieu of Occam&#8217;s Razor: of several possible explanations for an observation, the most boring one is probably the most accurate.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/02/09/an-alternative-to-occams-razor/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>iBad: The FSF Kool-Aid and Other Dystopian Hallucinations</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/01/29/ibad-the-fsf-kool-aid-and-other-dystopian-hallucinations/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/01/29/ibad-the-fsf-kool-aid-and-other-dystopian-hallucinations/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 15:51:23 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3816</guid>
		<description><![CDATA[The people who worry that the iPad will bring about a dystopian future for home computing keep forgetting something: for the rest of humanity, their ideal world of perfectly hackable machines is already a dystopian nightmare. It&#8217;s a world in which nothing works without spending hours setting it up, in which basic features are missing [...]]]></description>
			<content:encoded><![CDATA[<p>The people who worry that the iPad will bring about a dystopian future for home computing keep forgetting something: for the rest of humanity, <i>their ideal world of perfectly hackable machines is already a dystopian nightmare</i>. It&#8217;s a world in which nothing works without spending hours setting it up, in which basic features are missing while the manual lists thousands of irrelevant options, in which a million hardware extensions are available for their machine, but none of them help to solve a single one of their day-to-day problems. While being something of a hacker myself, I feel that the hacker&#8217;s vision of totally open computing probably should become a niche market, in much the same way that chemistry sets represent a niche market. The fact that not every person has a set of tools in his house that, by default, allows him to conduct arbitrary chemistry experiments has not substantially slowed down the progress of chemistry from what I can tell. The arrival of a world in which the most popular computers are closed to arbitrary hardware extensions and all applications are required to run within a sandbox probably won&#8217;t slow down the progress of personal computing much either.</p>
<p>Hackers of the world, your priorities are not simply different from the average user&#8217;s: they often represent a direct attack on the average user&#8217;s preferences. You keep asserting that you have the normal person&#8217;s interests in mind, but I think you&#8217;re often simply concealing your own self-interest underneath politicized rhetoric about freedom and openness.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/01/29/ibad-the-fsf-kool-aid-and-other-dystopian-hallucinations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gay Marriage: Another Data Point</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/01/16/gay-marriage-another-data-point/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/01/16/gay-marriage-another-data-point/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 16:09:36 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3813</guid>
		<description><![CDATA[Relevant to my earlier post about the relationship between direct democracy and laws prohibiting gay marriage, Pew Research just published poll data showing that a majority of Americans disapprove of same-sex marriage.
]]></description>
			<content:encoded><![CDATA[<p>Relevant to my earlier post about the relationship between direct democracy and laws prohibiting gay marriage, Pew Research just published poll data showing that <a href="http://pewresearch.org/databank/dailynumber/?NumberID=881">a majority of Americans disapprove of same-sex marriage.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/01/16/gay-marriage-another-data-point/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Academics&#8217; Slang: Orthogonal</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/01/12/academics-slang-orthogonal/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/01/12/academics-slang-orthogonal/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 18:14:56 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3811</guid>
		<description><![CDATA[H. G. Wells famously said that, &#8220;statistical thinking will one day be as necessary for efficient citizenship as the ability to read and write.&#8221; I think we&#8217;re getting closer to that day: even the Supreme Court of the United States plans to start using the word &#8216;orthogonal&#8217; colloquially.
]]></description>
			<content:encoded><![CDATA[<p>H. G. Wells famously said that, &#8220;statistical thinking will one day be as necessary for efficient citizenship as the ability to read and write.&#8221; I think we&#8217;re getting closer to that day: even <a href="http://www.washingtonpost.com/wp-dyn/content/article/2010/01/11/AR2010011103690.html">the Supreme Court of the United States plans to start using the word &#8216;orthogonal&#8217;</a> colloquially.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/01/12/academics-slang-orthogonal/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Outlawing Gay Marriage</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/01/10/outlawing-gay-marriage/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/01/10/outlawing-gay-marriage/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 16:25:41 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3787</guid>
		<description><![CDATA[Given the recent votes on same-sex marriage in New Jersey and Portugal, I wanted to test a seemingly innocuous claim that touches upon very broad issues in political theory: does the degree of directness of a &#8220;democratic&#8221; vote predict whether the vote will promote or prohibit same-sex marriage? Naively, it seemed clear to me that [...]]]></description>
			<content:encoded><![CDATA[<p>Given the recent votes on same-sex marriage in New Jersey and Portugal, I wanted to test a seemingly innocuous claim that touches upon very broad issues in political theory: does the degree of directness of a &#8220;democratic&#8221; vote predict whether the vote will promote or prohibit same-sex marriage? Naively, it seemed clear to me that this was true: every single direct vote in my memory has outlawed same-sex marriage, while the only decisions that have allowed same-sex marriage have originated among elected representatives or unelected judges.</p>
<p>I decided to compile some rough data to test this idea using Wikipedia&#8217;s articles on the topic. I took what seemed to be the non-redundant decisions since the 1990&#8217;s from the following three articles:</p>
<ol>
<li><a href="http://en.wikipedia.org/wiki/Same-sex_marriage_in_the_Netherlands">Same-Sex Marriage in the Netherlands</a></li>
<li><a href="http://en.wikipedia.org/wiki/Same-sex_marriage_in_Spain">Same-Sex Marriage in Spain</a></li>
<li><a href="http://en.wikipedia.org/wiki/Same-sex_marriage_in_the_United_States">Same-Sex Marriage in the United States</a></li>
</ol>
<p>And I arrived at this table:<br />
<center></p>
<table summary="Gay Marriage Statistics">
<tr>
<th>Region</th>
<th>Date</th>
<th>Type of Decision</th>
<th>Promoted</th>
</tr>
<tr>
<td>Hawaii</td>
<td>1993-05-05</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>U.S.A.</td>
<td>1996-09-21</td>
<td>Congressional Decision</td>
<td>0</td>
</tr>
<tr>
<td>Hawaii</td>
<td>1998-11-03</td>
<td>Direct Vote</td>
<td>0</td>
</tr>
<tr>
<td>Vermont</td>
<td>1999-12-20</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>Holland</td>
<td>2000-12-19</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>Massachusetts</td>
<td>2003-11-18</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>Spain</td>
<td>2005-06-30</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>California</td>
<td>2008-05-15</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>Connecticut</td>
<td>2008-10-10</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>California</td>
<td>2008-11-04</td>
<td>Direct Vote</td>
<td>0</td>
</tr>
<tr>
<td>New Hampshire</td>
<td>2009-03-26</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>Iowa</td>
<td>2009-04-03</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>Vermont</td>
<td>2009-04-07</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>Connecticut</td>
<td>2009-04-23</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>Maine</td>
<td>2009-05-06</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>New York</td>
<td>2009-05-12</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>Texas</td>
<td>2009-10-02</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>Maine</td>
<td>2009-11-03</td>
<td>Direct Vote</td>
<td>0</td>
</tr>
<tr>
<td>Washington, D.C.</td>
<td>2009-12-01</td>
<td>Court Ruling</td>
<td>1</td>
</tr>
<tr>
<td>New York</td>
<td>2009-12-02</td>
<td>Congressional Decision</td>
<td>1</td>
</tr>
<tr>
<td>New Jersey</td>
<td>2010-01-07</td>
<td>Congressional Decision</td>
<td>0</td>
</tr>
</table>
<p></center></p>
<p>You can find a CSV file with this data at <a href="http://github.com/johnmyleswhite/gay_marriage">the GitHub repository</a> where I&#8217;ve stored all of the analyses I&#8217;ve completed so far. I&#8217;d love for people to add more data or contribute some visualizations of the patterns I&#8217;m pointing out. And, as always, I&#8217;d love to hear criticisms or suggestions about my approach.</p>
<p>The summary statistics provide pretty clear support for my intuition, though they&#8217;re rarely statistically significant, given the small sample sizes:</p>
<ul>
<li>100% of court rulings promoted same-sex marriage.</li>
<li>80% of congressional decisions promoted same-sex marriage.</li>
<li>0% of direct votes promoted same-sex marriage.</li>
</ul>
<p>For me the conclusion to be drawn from all of this is not that our respect for the will of the majority should compel us to prohibit same-sex marriage, but rather that we should be more hostile to direct democracy, because it is a powerful force for promoting intolerance in American society. I imagine this will sound un-American to many readers, but I think it&#8217;s quite clear that this sentiment was foundational for the American experiment in government. You see this distrust of direct democracy repeatedly reiterated in the Federalist Papers, especially in <a href="http://en.wikipedia.org/wiki/Federalist_No._10">Federalist Number 10</a>. Indeed, one can argue that the major purpose of our Constitution is to limit the ability of direct democracy to harm minorities within our society.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/01/10/outlawing-gay-marriage/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Killing Yourself: An Addendum</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/01/09/killing-yourself-an-addendum/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/01/09/killing-yourself-an-addendum/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 15:27:28 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3785</guid>
		<description><![CDATA[In further support of the claim that a lot of deaths are partly self-induced, here&#8217;s a fascinating piece by Wired on the extraordinary rise in the percent of deaths among the young caused by their own poor decisions.
It&#8217;s remarkable that, for the young, modern science has already made the world so safe that humanity, rather [...]]]></description>
			<content:encoded><![CDATA[<p>In further support of the claim that a lot of deaths are partly self-induced, here&#8217;s <a href="http://www.wired.com/techbiz/people/magazine/17-10/ff_smartlist_keeney">a fascinating piece by Wired</a> on the extraordinary rise in the percent of deaths among the young caused by their own poor decisions.</p>
<p>It&#8217;s remarkable that, for the young, modern science has already made the world so safe that humanity, rather than nature, is now responsible for a majority of its own suffering.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/01/09/killing-yourself-an-addendum/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Announcing r-ORM: A Pure R Object-Relational Mapper</title>
		<link>http://www.johnmyleswhite.com/notebook/2010/01/05/announcing-r-orm-a-pure-r-object-relational-mapper/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2010/01/05/announcing-r-orm-a-pure-r-object-relational-mapper/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 14:47:50 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3765</guid>
		<description><![CDATA[My apologies for the long break between posts. Before the end of this week I&#8217;ll return to my series of posts on image processing in R. In the intervening time, I&#8217;ve finished a piece of code that I&#8217;d like to officially release to the public.
The code in question is a very minimal object-relational mapper written [...]]]></description>
			<content:encoded><![CDATA[<p>My apologies for the long break between posts. Before the end of this week I&#8217;ll return to my series of posts on image processing in R. In the intervening time, I&#8217;ve finished a piece of code that I&#8217;d like to officially release to the public.</p>
<p>The code in question is a very minimal object-relational mapper written entirely in R. As of today, you can find the code on GitHub <a href="http://github.com/johnmyleswhite/r-ORM"> here</a>. If you&#8217;re not familiar with using an ORM, I&#8217;d suggest reading a bit about how to use <a href="http://www.oracle.com/technology/pub/articles/tate-activeerecord.html">ActiveRecord</a>, the ORM I&#8217;ve tried to emulate.</p>
<p>As it stands, the code I have is able to connect to a MySQL database and extract information about a specified table using MySQL&#8217;s <code>SHOW COLUMNS</code>. The code then automatically builds up R code that will map the rows of the table onto R objects. This code can subsequently be eval&#8217;d or cat&#8217;d to a file.</p>
<p>Let me note right off the bat that the code I&#8217;ve written is fairly heinous stylistically: because my understanding of R metaprogramming is rather limited, I&#8217;ve produced what amounts to the ugliest sort of code generation tool. If you check out the source in <code>orm.R</code>, you&#8217;ll quickly see what I mean: the output R code is generated as a string using a long series of <code>paste()</code> operations, many of which embed <code>paste()</code> operations inside of themselves. In the future, I would like to rewrite the code in a clearer fashion, possibly constructing expressions and parse trees directly, rather than using an intermediate layer of code as string.</p>
<p>That said, I want to release the code so that I can get feedback on my approach. To help you give me feedback, here&#8217;s a quick walkthrough of how&#8217;d you use the current code:</p>
<p>(1) Download the code from its GitHub <a href="http://github.com/johnmyleswhite/r-ORM">home</a>. Place the files you get into a directory of your choice. Let&#8217;s assume you use <code>~/r-ORM</code>.</p>
<p>(2) Install the R YAML and MySQL packages if you don&#8217;t already have them on your system:</p>

<div class="wp_codebox"><table><tr id="p376516"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p3765code16"><pre class="c" style="font-family:monospace;">install.<span style="color: #202020;">packages</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'yaml'</span><span style="color: #009900;">&#41;</span>
install.<span style="color: #202020;">packages</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'RMySQL'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>(3) Create a test database in MySQL called <code>`sample_database`</code>:</p>

<div class="wp_codebox"><table><tr id="p376517"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3765code17"><pre class="c" style="font-family:monospace;">CREATE DATABASE `sample_database`<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>(4) Give permissions on this test database to <code>`sample_user`</code>:</p>

<div class="wp_codebox"><table><tr id="p376518"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3765code18"><pre class="c" style="font-family:monospace;">GRANT ALL ON `sample_database`.<span style="color: #339933;">*</span> TO <span style="color: #ff0000;">'sample_user'</span>@<span style="color: #ff0000;">'localhost'</span> IDENTIFIED BY <span style="color: #ff0000;">'sample_password'</span><span style="color: #339933;">;</span>
&nbsp;
FLUSH PRIVILEGES<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>(5) Create a test table called <code>`users`</code> in <code>`sample_database`</code>:</p>

<div class="wp_codebox"><table><tr id="p376519"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p3765code19"><pre class="c" style="font-family:monospace;">USE `sample_database`<span style="color: #339933;">;</span>
&nbsp;
CREATE TABLE `users` <span style="color: #009900;">&#40;</span>
  `user_id` INT<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">11</span><span style="color: #009900;">&#41;</span> NOT NULL AUTO_INCREMENT<span style="color: #339933;">,</span>
  `name` VARCHAR<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span> NOT NULL<span style="color: #339933;">,</span>
  `password` VARCHAR<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span> NOT NULL<span style="color: #339933;">,</span>
  PRIMARY KEY <span style="color: #009900;">&#40;</span>`user_id`<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span> ENGINE<span style="color: #339933;">=</span>MyISAM DEFAULT CHARSET<span style="color: #339933;">=</span>latin1<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>(6) Edit the <code>database.yml</code> file if you didn&#8217;t use the database name, user name or password I just suggested. You&#8217;ll also need to edit it if you&#8217;re not working on <code>localhost</code>.</p>
<p>(7) Open up an R interpreter. Set your working directory and source <code>orm.R</code>:</p>

<div class="wp_codebox"><table><tr id="p376520"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p3765code20"><pre class="c" style="font-family:monospace;">setwd<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'~/r-ORM'</span><span style="color: #009900;">&#41;</span>
source<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'orm.R'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>(8) Build code for our user objects using <code>orm.build.model('user')</code>:</p>

<div class="wp_codebox"><table><tr id="p376521"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p3765code21"><pre class="c" style="font-family:monospace;">code <span style="color: #339933;">&lt;-</span> orm.<span style="color: #202020;">build</span>.<span style="color: #202020;">model</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'user'</span><span style="color: #009900;">&#41;</span>
cat<span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>At this point, you can review the code that&#8217;s been generated to see whether it should work on your system. If it will, you can eval it now:</p>

<div class="wp_codebox"><table><tr id="p376522"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3765code22"><pre class="c" style="font-family:monospace;">eval<span style="color: #009900;">&#40;</span>parse<span style="color: #009900;">&#40;</span>text <span style="color: #339933;">=</span> code<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>With that done, you should have a working set of functions that handle creating, finding, manipulating and deleting R objects that are serialized to the database. To test out the resulting model, let&#8217;s start by creating a user object. In general, an object of class <code>foo</code> will be created using an auto-generated function called <code>create.foo</code>:</p>

<div class="wp_codebox"><table><tr id="p376523"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3765code23"><pre class="c" style="font-family:monospace;">user <span style="color: #339933;">&lt;-</span> create.<span style="color: #202020;">user</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Calling this functions builds a user object in memory. Nothing is in the database so far. To see the object, type <code>user</code> at the command line:</p>

<div class="wp_codebox"><table><tr id="p376524"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3765code24"><pre class="c" style="font-family:monospace;">user</pre></td></tr></table></div>

<p>Now you can edit this user to make it a real piece of data. The columns of the database table are mapped onto object attributes with appropriate getter and setter methods, like so:</p>

<div class="wp_codebox"><table><tr id="p376525"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p3765code25"><pre class="c" style="font-family:monospace;">name<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'test_user'</span>
password<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'test_password'</span></pre></td></tr></table></div>

<p>Once the user object is worth keeping, we store it in the database using <code>store</code>:</p>

<div class="wp_codebox"><table><tr id="p376526"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3765code26"><pre class="c" style="font-family:monospace;">user <span style="color: #339933;">&lt;-</span> store<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>This stores the user object in the database. To get the ID&#8217;s edited correctly, you have to perform the assignment as indicated above. In the future I may change this.</p>
<p>After storing something, you might want to retrieve it later. Since we know that we just created the first user object, we can get it again by using a <code>find.user</code> call:</p>

<div class="wp_codebox"><table><tr id="p376527"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3765code27"><pre class="c" style="font-family:monospace;">user <span style="color: #339933;">&lt;-</span> find.<span style="color: #202020;">user</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>In general, you can find objects of class <code>foo</code> by calling <code>find.foo()</code>. If you provide an integer as an input, you&#8217;ll get the object with that ID. If you provide the string <code>'all'</code>, you&#8217;ll get a list containing all of the objects in your database. Other inputs produce an error.</p>
<p>You can see that we got the correct object using the getter methods:</p>

<div class="wp_codebox"><table><tr id="p376528"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3765code28"><pre class="c" style="font-family:monospace;">user.<span style="color: #202020;">id</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span>
name<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">'test_user'</span>
password<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">'test_password'</span></pre></td></tr></table></div>

<p>We can edit it again to see that updating rows of the database works as expected:</p>

<div class="wp_codebox"><table><tr id="p376529"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p3765code29"><pre class="c" style="font-family:monospace;">name<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'new_user'</span>
store<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Finally, now that we&#8217;re done with it, we can delete it from the database:</p>

<div class="wp_codebox"><table><tr id="p376530"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3765code30"><pre class="c" style="font-family:monospace;">delete<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>That, in a nutshell, is the use of this ORM solution. If you have any questions, please let me know: I&#8217;ll be happy to answer them.</p>
<p>I should note that there are many weaknesses of functionality with the current code. Notably:</p>
<ol>
<li>If you want a &#8216;foo&#8217; object, it must be in a &#8216;foos&#8217; table. This is inspired by ActiveRecord&#8217;s default pluralization rule.</li>
<li>There&#8217;s no type checking. You can try to set string values on numeric columns; you&#8217;ll just get strange errors as a result.</li>
<li>There&#8217;s no checking whether you&#8217;re inserting a NULL into a column that won&#8217;t allow NULL&#8217;s.</li>
<li>The system might clobber an existing generic method or function if there&#8217;s a naming conflict. I&#8217;d recommend writing everything to disk before using this code until you&#8217;re familiar with the results.</li>
<li>The default objects are completely blank: the database default values are not used to initialize any attibutes of a new object.</li>
<li>The SQL generated is not sanitized. You should not use this system to process end-user input.</li>
</ol>
<p>That said, I find the system usable for my current needs. I hope it will help you, and I really hope that you&#8217;ll consider making suggestions for hoping to improve the code. Patches would be especially welcome.</p>
<p>I would also love feedback about the interface that&#8217;s shown to the end user of this code. Here are some outstanding questions I&#8217;d like to hear from people about:</p>
<ol>
<li>Are function names like <code>orm.build.model()</code> memorable?</li>
<li>Does the syntax of calls on the resulting objects seem sufficiently R-like?</li>
<li>Would it be useful to break out my minimal database abstraction layer into a separate package? Is there already a package to do this that I should be using instead?</li>
<li>How can I avoid the &#8220;code as string&#8221; approach I&#8217;m taking?</li>
<li>Do you have ideas about the ideal user syntax for implementing relations across tables, i.e. has_one or has_many relationships?</li>
<li>Should a caching mechanism be designed?</li>
<li>Should the database queries be minimized using some sort of pooling?</li>
<li>Should transactions be used?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2010/01/05/announcing-r-orm-a-pure-r-object-relational-mapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Complex Numbers in R</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/18/using-complex-numbers-in-r/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/18/using-complex-numbers-in-r/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 02:43:55 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3754</guid>
		<description><![CDATA[This post is a continuation of my series dealing with matrix operations for image processing. My next goal is to demonstrate the construction of simple low-pass and high-pass spatial frequency filters in R. It&#8217;s easy enough to construct simple versions of these filters in R using the Fast Fourier Transform (also known as the FFT), [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a continuation of my series dealing with matrix operations for image processing. My next goal is to demonstrate the construction of simple low-pass and high-pass spatial frequency filters in R. It&#8217;s easy enough to construct simple versions of these filters in R using the Fast Fourier Transform (also known as the FFT), but, because the FFT is a slightly complicated tool, I&#8217;m going to build up to using it progressively over a few posts.</p>
<p>For starters, I want to review the use of complex numbers in R. As always, if you&#8217;re interested in reviewing the mathematics of complex numbers, I&#8217;d start by browsing references online. The tutorial <a href="http://www.clarku.edu/~djoyce/complex/">here</a> seemed good to me at first glance, though I can&#8217;t claim to have read it through.</p>
<p>Because complex numbers are implemented in the &#8220;base&#8221; package, it&#8217;s very easy to start working with them. To construct the complex number <i>x + iy</i>, you use <code>complex</code>:</p>

<div class="wp_codebox"><table><tr id="p375441"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p3754code41"><pre class="c" style="font-family:monospace;">x <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">1</span>
y <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">1</span>
&nbsp;
z <span style="color: #339933;">&lt;-</span> complex<span style="color: #009900;">&#40;</span>real <span style="color: #339933;">=</span> x<span style="color: #339933;">,</span> imaginary <span style="color: #339933;">=</span> y<span style="color: #009900;">&#41;</span>
&nbsp;
z
<span style="color: #339933;"># [1] 1+1i</span></pre></td></tr></table></div>

<p>It&#8217;s conventional in mathematics to use <i>z</i> to refer to a complex number, so I&#8217;ll continue on with that tradition.</p>
<p>As always occurs with mathematical data types in R, you can convert other objects to class &#8220;complex&#8221; using <code>as.complex</code>:</p>

<div class="wp_codebox"><table><tr id="p375442"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p3754code42"><pre class="c" style="font-family:monospace;">as.<span style="color: #202020;">complex</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] -1+0i</span></pre></td></tr></table></div>

<p>And you can test that an object is complex using <code>is.complex</code>:</p>

<div class="wp_codebox"><table><tr id="p375443"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p3754code43"><pre class="c" style="font-family:monospace;">is.<span style="color: #202020;">complex</span><span style="color: #009900;">&#40;</span>as.<span style="color: #202020;">complex</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] TRUE</span></pre></td></tr></table></div>

<p>Beyond those standard operations, there are five essential mathematical operations you&#8217;d want to use on complex numbers.</p>
<p>First off, you want to be able to extract the real and imaginary components of a complex number. You can do this using <code>Re</code> and <code>Im</code> respectively:</p>

<div class="wp_codebox"><table><tr id="p375444"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p3754code44"><pre class="c" style="font-family:monospace;">z <span style="color: #339933;">&lt;-</span> complex<span style="color: #009900;">&#40;</span>real <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> imaginary <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
&nbsp;
Re<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 2</span>
&nbsp;
Im<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 1</span></pre></td></tr></table></div>

<p>The <i>(x, y)</i> representation of numbers is easier to understand at first, but a polar coordinates representation is often more practical. You can get the relevant components of this representation by finding the modulus and complex argument of a complex number. In R, you would use <code>Mod</code> and <code>Arg</code>:</p>

<div class="wp_codebox"><table><tr id="p375445"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p3754code45"><pre class="c" style="font-family:monospace;">z <span style="color: #339933;">&lt;-</span> complex<span style="color: #009900;">&#40;</span>real <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> imaginary <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
&nbsp;
Mod<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 1</span>
&nbsp;
Arg<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 1.570796</span>
&nbsp;
pi <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span>
<span style="color: #339933;"># [1] 1.570796</span></pre></td></tr></table></div>

<p>This corresponds to the intuition that <i>i</i> should be at a distance 1 from the origin and an angle of <i>pi / 2</i>.</p>
<p>Finally, you&#8217;ll want to be able to take the complex conjugate of a complex number; to do that in R, you can use <code>Conj</code>:</p>

<div class="wp_codebox"><table><tr id="p375446"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3754code46"><pre class="c" style="font-family:monospace;">Conj<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 0-1i</span>
&nbsp;
Mod<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> z <span style="color: #339933;">*</span> Conj<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] TRUE</span></pre></td></tr></table></div>

<p>As you can see, the modulus of <i>z</i> equals z times the conjugate of z, which is exactly what you expect.</p>
<p>Now, historically, complex numbers were invented so that you could find the square root of negative numbers. By default, <code>sqrt</code> does not return a complex number when you ask for the square root of a negative number. Instead, it produces a <code>NaN</code> error, as you can see below:</p>

<div class="wp_codebox"><table><tr id="p375447"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p3754code47"><pre class="c" style="font-family:monospace;">sqrt<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] NaN</span>
<span style="color: #339933;"># Warning message:</span>
<span style="color: #339933;"># In sqrt(-1) : NaNs produced</span></pre></td></tr></table></div>

<p>To get the complex square root, you need to cast your negative number as a complex number using <code>as.complex</code> before applying <code>sqrt</code>:</p>

<div class="wp_codebox"><table><tr id="p375448"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3754code48"><pre class="c" style="font-family:monospace;">sqrt<span style="color: #009900;">&#40;</span>as.<span style="color: #202020;">complex</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;"># [1] 0+1i</span></pre></td></tr></table></div>

<p>In practice, complex numbers can be used to solve a huge range of problems, not least of which is the efficient representation of the FFT of an input vector. For fun, I thought I&#8217;d show a simple application: building a fractal using the Mandelbrot set.</p>
<p>To quote Wikipedia,</p>
<blockquote><p>
Mathematically the Mandelbrot set can be defined as the set of complex values of <i>c</i> for which the orbit of 0 under iteration of the complex quadratic polynomial <i>z</i><sub><i>n</i>+1</sub> = <i>z</i><sub><i>n</i></sub><sup>2</sup> + <i>c</i> remains bounded. That is, a complex number, <i>c</i>, is in the Mandelbrot set if, when starting with <i>z</i><sub>0</sub> = 0 and applying the iteration repeatedly, the absolute value of <i>z</i><sub><i>n</i></sub> never exceeds a certain number (that number depends on <i>c</i>) however large <i>n</i> gets.
</p></blockquote>
<p>We can translate this definition into R pretty easily by making certain assumptions about the exactness we want in our results. For my purposes, I&#8217;ll say that a number is in the Mandelbrot set if, after 100 iterations of the Mandelbrot algorithm, it&#8217;s never once had a modulus greater than 1,000,000. As you&#8217;ll see from the image I produced, this assumption works out pretty well, though it takes some real number crunching to get results out of it for reasonable sized data sets.</p>

<div class="wp_codebox"><table><tr id="p375449"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p3754code49"><pre class="c" style="font-family:monospace;">in.<span style="color: #202020;">mandelbrot</span>.<span style="color: #202020;">set</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>c<span style="color: #339933;">,</span> iterations <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">,</span> bound <span style="color: #339933;">=</span> <span style="color: #0000dd;">1000000</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  z <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">0</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i in <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>iterations<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    z <span style="color: #339933;">&lt;-</span> z <span style="color: #339933;">**</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">+</span> c
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Mod<span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> bound<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>FALSE<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>TRUE<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once we have this algorithm, we can easily generate an image of the Mandelbrot set by producing a matrix of complex numbers and depicting their inclusion in the set using <code>image</code>:</p>

<div class="wp_codebox"><table><tr id="p375450"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p3754code50"><pre class="c" style="font-family:monospace;">resolution <span style="color: #339933;">&lt;-</span> <span style="color:#800080;">0.001</span>
&nbsp;
sequence <span style="color: #339933;">&lt;-</span> seq<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> by <span style="color: #339933;">=</span> resolution<span style="color: #009900;">&#41;</span>
&nbsp;
m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> length<span style="color: #009900;">&#40;</span>sequence<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> length<span style="color: #009900;">&#40;</span>sequence<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>x in sequence<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>y in sequence<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    mandelbrot <span style="color: #339933;">&lt;-</span> in.<span style="color: #202020;">mandelbrot</span>.<span style="color: #202020;">set</span><span style="color: #009900;">&#40;</span>complex<span style="color: #009900;">&#40;</span>real <span style="color: #339933;">=</span> x<span style="color: #339933;">,</span> imaginary <span style="color: #339933;">=</span> y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    m<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> resolution <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> resolution<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>y <span style="color: #339933;">+</span> resolution <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> resolution<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> mandelbrot
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
png<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'mandelbrot.png'</span><span style="color: #009900;">&#41;</span>
image<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
dev.<span style="color: #202020;">off</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>And here&#8217;s the resulting image:</p>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/mandelbrot.png" alt="mandelbrot.png" border="0" width="480" height="480" /></div>
<p>I&#8217;m not quite sure what the source of the lines in my image is. I&#8217;ll have to report back on that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/18/using-complex-numbers-in-r/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Continuing Inanity of Hate Speech Laws</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/18/the-continuing-inanity-of-hate-speech-laws/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/18/the-continuing-inanity-of-hate-speech-laws/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 14:50:54 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Law]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3751</guid>
		<description><![CDATA[I&#8217;ve said it before: hate speech laws, whenever they exist, will always be exploited to suppress speech that&#8217;s politically charged. Italy&#8217;s proving my point this week, as it contemplates Web restrictions after the attack on Berlusconi.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve said it before: hate speech laws, whenever they exist, will always be exploited to suppress speech that&#8217;s politically charged. Italy&#8217;s proving my point this week, as it <a href="http://www.pcworld.idg.com.au/article/330180/">contemplates Web restrictions after the attack on Berlusconi</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/18/the-continuing-inanity-of-hate-speech-laws/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Compression with the SVD in R</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/17/image-compression-with-the-svd-in-r/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/17/image-compression-with-the-svd-in-r/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 00:23:03 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3736</guid>
		<description><![CDATA[Over the next few posts, I&#8217;m going to be reviewing the use of R to implement the most commonly used matrix techniques for image manipulation. The code will be surprisingly simple to understand, because the real magic behind these techniques lies in the mathematics that R provides an abstract interface to. To start, I&#8217;m going [...]]]></description>
			<content:encoded><![CDATA[<p>Over the next few posts, I&#8217;m going to be reviewing the use of R to implement the most commonly used matrix techniques for image manipulation. The code will be surprisingly simple to understand, because the real magic behind these techniques lies in the mathematics that R provides an abstract interface to. To start, I&#8217;m going to review the singular value decomposition of a matrix, also known as the SVD.</p>
<p>If you&#8217;d like to understand the magic behind the scenes here, you can read about the mathematical definition of the SVD on <a href="http://en.wikipedia.org/wiki/Singular_value_decomposition">Wikipedia</a> or you can watch <a href="http://ocw.mit.edu/OcwWeb/Mathematics/18-06Spring-2005/VideoLectures/index.htm">Gilbert Strang&#8217;s lectures</a>. If your command of linear algebra isn&#8217;t great, I suspect that the mathematical details of the SVD will be totally opaque to you.</p>
<p>Fortunately, the details of the underlying mathematics aren&#8217;t at all necessary for appreciating the SVD&#8217;s usefulness for manipulating images: it is arguably the best way to compress matrices into smaller representations that contain the essential information from the original matrices. The easiest way to understand this is to see it in action, so I&#8217;m going to show how the SVD allows for any degree of compression of an image represented as a real-valued matrix.</p>
<p>Obviously, the first thing we have to do is to represent our example image as a matrix with real-valued entries. To do this, we&#8217;ll need to use a simple technique for loading an image into R that I found on the <a href="http://quantitative-ecology.blogspot.com/2007/05/convert-image-to-matrix-in-r.html">Quantitative Ecology blog</a>. The first thing to do is use ImageMagick to transform a TIFF file into a PPM file:</p>

<div class="wp_codebox"><table><tr id="p373659"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3736code59"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#!/bin/bash</span>
&nbsp;
convert face.<span style="color: #202020;">tiff</span> image.<span style="color: #202020;">ppm</span></pre></td></tr></table></div>

<p>Then we can use the &#8220;pixmap&#8221; package from CRAN to read the PPM image into R as an image object containing three matrices: one for the red components, one for the green components, and one for the blue components. The exact code I used was this:</p>

<div class="wp_codebox"><table><tr id="p373660"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p3736code60"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'pixmap'</span><span style="color: #009900;">&#41;</span>
&nbsp;
image <span style="color: #339933;">&lt;-</span> read.<span style="color: #202020;">pnm</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'image.ppm'</span><span style="color: #009900;">&#41;</span>
&nbsp;
red.<span style="color: #202020;">matrix</span> <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>image@red<span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> image@size<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> image@size<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
green.<span style="color: #202020;">matrix</span> <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>image@green<span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> image@size<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> image@size<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
blue.<span style="color: #202020;">matrix</span> <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>image@blue<span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> image@size<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> image@size<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Before we go any further, we should look at the image we&#8217;ll be playing with. Rather than work with the composite of the three color matrices, I found it easier to work with only a single color. For this specific image, I found that the green matrix looked best, so I&#8217;ll use only that. At the risk of seeming self-indulgent, I&#8217;m using my own Gravatar icon as an example image because faces are particularly easy to recognize under the strange color scheme I&#8217;ll be using, which I chose because it really simplifies the visualization code.</p>
<p>To visualize the matrix as an image, I discovered two very helpful graphical functions, <code>image</code> and <code>heat.colors</code>. <code>image</code> depicts the value of a matrix using a color scheme you specify; <code>heat.colors</code> produces a set of colors that could be used in a heat map with a granularity you specify as the first argument. Using these functions, we can see our raw input matrix:</p>

<div class="wp_codebox"><table><tr id="p373661"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3736code61"><pre class="c" style="font-family:monospace;">image<span style="color: #009900;">&#40;</span>green.<span style="color: #202020;">matrix</span><span style="color: #339933;">,</span> col <span style="color: #339933;">=</span> heat.<span style="color: #202020;">colors</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>The images generated by <code>image</code> are at an unfortunate angle for viewing, so I edited them post hoc with a single 90º clockwise rotation in Preview to give this sort of image:</p>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Raw-Image.png" alt="Green Raw Image.png" border="0" width="240" height="240" /></div>
<p>If you want to play with the original PPM image file, you can get it <a href="http://www.johnmyleswhite.com/content/data_sets/image.ppm">here</a>.</p>
<p>Clearly, we&#8217;ve got a recognizable face here. Feeling good about that, we can start to play with the SVD of this matrix. The R function to generate the SVD of a matrix is simply <code>svd</code>:</p>

<div class="wp_codebox"><table><tr id="p373662"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3736code62"><pre class="c" style="font-family:monospace;">green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">svd</span> <span style="color: #339933;">&lt;-</span> svd<span style="color: #009900;">&#40;</span>green.<span style="color: #202020;">matrix</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p><code>svd</code> returns a list with three entries: <code>d</code>, <code>u</code> and <code>v</code>. <code>d</code> is a vector of the singular values ordered by decreasing size; <code>u</code> and <code>v</code> are matrices that are combined with a diagonal matrix form of <code>d</code> to give the original input matrix. To simplify things, I&#8217;ll set up variables to contain the value of these elements of the SVD output list:</p>

<div class="wp_codebox"><table><tr id="p373663"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3736code63"><pre class="c" style="font-family:monospace;">d <span style="color: #339933;">&lt;-</span> green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">svd</span>$d
u <span style="color: #339933;">&lt;-</span> green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">svd</span>$u
v <span style="color: #339933;">&lt;-</span> green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">svd</span>$v</pre></td></tr></table></div>

<p>We can check that the SVD works by multiplying the relevant matrices:</p>

<div class="wp_codebox"><table><tr id="p373664"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p3736code64"><pre class="c" style="font-family:monospace;">green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">reconstruction</span> <span style="color: #339933;">&lt;-</span> u <span style="color: #339933;">%*%</span> diag<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%*%</span> t<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span>
&nbsp;
mean<span style="color: #009900;">&#40;</span>green.<span style="color: #202020;">matrix</span> <span style="color: #339933;">-</span> green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">reconstruction</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] -5.184204e-16</span></pre></td></tr></table></div>

<p>If this is the SVD by itself, it&#8217;s not clear that it&#8217;s useful computationally: we&#8217;ve just rewritten one matrix as the product of three matrices and accumulated a small bit of error along the way. The real utility of the SVD lies in the singular values: they represent, in decreasing order, the most important information about the original matrix.</p>
<p>To see this, you can shrink the input matrices and produce a compressed form of the matrix. For instance, my original matrix is a 212 by 201 image. Using the SVD, we can represent it as the product of a 212 by 2 matrix, a 2 by 2 matrix, and a 2 by 201 matrix. This massively reduces the amount of information we&#8217;re storing, but you can already see the outline of our input image in the results:</p>

<div class="wp_codebox"><table><tr id="p373665"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3736code65"><pre class="c" style="font-family:monospace;">green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">compressed</span> <span style="color: #339933;">&lt;-</span> u<span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">%*%</span> diag<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%*%</span> t<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
&nbsp;
image<span style="color: #009900;">&#40;</span>green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">compressed</span><span style="color: #339933;">,</span> col <span style="color: #339933;">=</span> heat.<span style="color: #202020;">colors</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Image-SVD-Compressed-2.png" alt="Green Image SVD Compressed 2.png" border="0" width="240" height="240" /></div>
<p>To get a feel for how this works, we can iterate over the number of singular values we include in our compressed form:</p>

<div class="wp_codebox"><table><tr id="p373666"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p3736code66"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i in c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">compressed</span> <span style="color: #339933;">&lt;-</span> u<span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">%*%</span> diag<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%*%</span> t<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	png<span style="color: #009900;">&#40;</span>paste<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'images/Green Image SVD Compressed '</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> <span style="color: #ff0000;">'.png'</span><span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	image<span style="color: #009900;">&#40;</span>green.<span style="color: #202020;">matrix</span>.<span style="color: #202020;">compressed</span><span style="color: #339933;">,</span> col <span style="color: #339933;">=</span> heat.<span style="color: #202020;">colors</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	dev.<span style="color: #202020;">off</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This produces the following images:</p>
<h4>3 Singular Values</h4>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Image-SVD-Compressed-3.png" alt="Green Image SVD Compressed 3.png" border="0" width="240" height="240" /></div>
<h4>4 Singular Values</h4>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Image-SVD-Compressed-4.png" alt="Green Image SVD Compressed 4.png" border="0" width="240" height="240" /></div>
<h4>5 Singular Values</h4>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Image-SVD-Compressed-5.png" alt="Green Image SVD Compressed 5.png" border="0" width="240" height="240" /></div>
<h4>10 Singular Values</h4>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Image-SVD-Compressed-10.png" alt="Green Image SVD Compressed 10.png" border="0" width="240" height="240" /></div>
<h4>20 Singular Values</h4>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Image-SVD-Compressed-20.png" alt="Green Image SVD Compressed 20.png" border="0" width="240" height="240" /></div>
<h4>30 Singular Values</h4>
<div style="text-align:center;"><img src="http://www.johnmyleswhite.com/notebook/wp-content/uploads/2009/12/Green-Image-SVD-Compressed-30.png" alt="Green Image SVD Compressed 30.png" border="0" width="240" height="240" /></div>
<p>As you can see, the images get better quite quickly. Indeed, this last image looks great, despite the large reduction in the number of entries we&#8217;re keeping from the original matrix. I&#8217;d say that&#8217;s a pretty compelling case for the value of the SVD. It would be easy to combine the compressed matrices for red, green and blue to get a compressed version of our input image using its original color scheme. I&#8217;ll leave that as an exercise for anyone interested in playing with this technique on their own.</p>
<p>Of course, the SVD has tons of other uses, but this simple hack for image compression struck me as pretty interesting, as well as being remarkably simple to implement in R.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/17/image-compression-with-the-svd-in-r/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Quick Review of Matrix Algebra in R</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/16/quick-review-of-matrix-algebra-in-r/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/16/quick-review-of-matrix-algebra-in-r/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 03:29:09 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Observations]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3707</guid>
		<description><![CDATA[Lately, I&#8217;ve been running a series of fMRI experiments on visual perception. In the interests of understanding the underlying properties of the images I&#8217;m using as stimuli, I&#8217;ve been trying to learn more about the matrix transformations commonly used for image compression and image manipulation. Thankfully, R provides simple-to-use implementations for all of the matrix [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I&#8217;ve been running a series of fMRI experiments on visual perception. In the interests of understanding the underlying properties of the images I&#8217;m using as stimuli, I&#8217;ve been trying to learn more about the matrix transformations commonly used for image compression and image manipulation. Thankfully, R provides simple-to-use implementations for all of the matrix operations I wanted to play around with, so it&#8217;s been quite easy to get started. For the next few posts, I thought that I&#8217;d review the standard matrix techniques for image compression and editing, giving full examples of their implementation in R and demonstrations of their real-world value.</p>
<p>Before I start, I should make sure that you&#8217;re familiar with basic matrix operations in R. I imagine almost every R user knows a little bit about matrix algebra and probably knows the basics of using R to perform matrix algebra, but here&#8217;s a quick review to make sure I don&#8217;t leave anyone in the dark:</p>
<h3>Building Matrices</h3>
<p>You can build a matrix in R using the <code>matrix</code> function:</p>

<div class="wp_codebox"><table><tr id="p370781"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code81"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> byrow <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span>
&nbsp;
m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    1    0</span>
<span style="color: #339933;"># [2,]    0    1</span></pre></td></tr></table></div>

<p>You can test whether an item you&#8217;ve been given is a matrix using <code>is.matrix</code> and you can convert appropriate objects to matrices using <code>as.matrix</code>:</p>

<div class="wp_codebox"><table><tr id="p370782"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p3707code82"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> byrow <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span>
&nbsp;
is.<span style="color: #202020;">matrix</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># TRUE</span>
&nbsp;
as.<span style="color: #202020;">matrix</span><span style="color: #009900;">&#40;</span>data.<span style="color: #202020;">frame</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#      x y</span>
<span style="color: #339933;"># [1,] 1 0</span>
<span style="color: #339933;"># [2,] 0 1</span></pre></td></tr></table></div>

<h3>Diagonal Matrices</h3>
<p>The matrix I&#8217;ve been building in the examples above is a diagonal matrix, so you could also construct it using <code>diag</code>:</p>

<div class="wp_codebox"><table><tr id="p370783"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code83"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    1    0</span>
<span style="color: #339933;"># [2,]    0    1</span></pre></td></tr></table></div>

<p>In general, you can generate the n by n identity matrix as:</p>

<div class="wp_codebox"><table><tr id="p370784"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3707code84"><pre class="c" style="font-family:monospace;">n <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">10</span>
&nbsp;
diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> n<span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> n<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>I&#8217;ll be exploiting a more interesting use of <code>diag</code> in my next post, so let&#8217;s see how you can build matrices other than the identity with <code>diag</code> by specifying a vector of entries along the diagonal:</p>

<div class="wp_codebox"><table><tr id="p370785"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code85"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    2    0</span>
<span style="color: #339933;"># [2,]    0    1</span></pre></td></tr></table></div>

<p>This form of <code>diag</code> turns out to be extremely useful, as you&#8217;ll see once I cover the SVD&#8217;s syntax in R.</p>
<h3>Matrix Algebra: Addition, Scalar Multiplication, Matrix Multiplication</h3>
<p>The three core operations that can be performed on matrices are addition, scalar multiplication and matrix multiplication. These are easy to work with in R:</p>

<div class="wp_codebox"><table><tr id="p370786"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p3707code86"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> byrow <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span>
&nbsp;
m <span style="color: #339933;">+</span> m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    4</span>
<span style="color: #339933;"># [2,]    2    0</span>
&nbsp;
<span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    4</span>
<span style="color: #339933;"># [2,]    2    0</span>
&nbsp;
m <span style="color: #339933;">%*%</span> m
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    2    0</span>
<span style="color: #339933;"># [2,]    0    2</span></pre></td></tr></table></div>

<p>Be careful with the <code>*</code> operator: it does not perform matrix multiplication, but rather an entry-wise multiplication:</p>

<div class="wp_codebox"><table><tr id="p370787"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3707code87"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">*</span> m
&nbsp;
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    4</span>
<span style="color: #339933;"># [2,]    1    0</span></pre></td></tr></table></div>

<h3>Matrix Transposes and Inverses</h3>
<p>The next few matrix operations are a little more complex: transposition and inversion. Transposition is the easier of the two. To get the transpose of a matrix, you simply call the <code>t</code> function:</p>

<div class="wp_codebox"><table><tr id="p370788"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p3707code88"><pre class="c" style="font-family:monospace;">t<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0    1</span>
<span style="color: #339933;"># [2,]    2    0</span></pre></td></tr></table></div>

<p>In contrast, inversion is a little more complex, partly because the function you&#8217;d want to use has a non-obvious name: <code>solve</code>.</p>

<div class="wp_codebox"><table><tr id="p370789"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3707code89"><pre class="c" style="font-family:monospace;">solve<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]  0.0    1</span>
<span style="color: #339933;"># [2,]  0.5    0</span></pre></td></tr></table></div>

<p>The reason that <code>solve</code> is called <code>solve</code> is that it&#8217;s a general purpose function you can use to solve matrix equations without wasting time computing the full inverse, which is often inefficient. If you want to know more about the computational efficiency issues, you should look into the ideas behind the even faster variant, <code>qr.solve</code>.</p>
<p>Now, you probably know this already, but the definition of a matrix&#8217;s inverse is that the product of the matrix and its inverse is the identity matrix, if the inverse exists. I always find this a good way to make sure that I&#8217;m correctly computing the inverse of a matrix:</p>

<div class="wp_codebox"><table><tr id="p370790"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3707code90"><pre class="c" style="font-family:monospace;">solve<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%*%</span> m <span style="color: #339933;">==</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> nrow<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> ncol<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,] TRUE TRUE</span>
<span style="color: #339933;"># [2,] TRUE TRUE</span></pre></td></tr></table></div>

<h3>Further Matrix Operations</h3>
<p>The <code>car</code> package defines an <code>inv</code> function, which is simply a new name for <code>solve</code>:</p>

<div class="wp_codebox"><table><tr id="p370791"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3707code91"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'car'</span><span style="color: #009900;">&#41;</span>
inv
&nbsp;
<span style="color: #339933;"># function (x) </span>
<span style="color: #339933;"># solve(x)</span>
<span style="color: #339933;"># &lt;environment: namespace:car&gt;</span></pre></td></tr></table></div>

<p>I think this is pretty clever, though I&#8217;m loathe to import a package just for this one snippet.</p>
<p>More interestingly, the <code>MASS</code> package defines a <code>ginv</code> function, which gives the matrix pseudoinverse, a generalization of matrix inversion that works for all matrices:</p>

<div class="wp_codebox"><table><tr id="p370792"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p3707code92"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'MASS'</span><span style="color: #009900;">&#41;</span>
&nbsp;
m <span style="color: #339933;">&lt;-</span> matrix<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
solve<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># Error in solve.default(m) : </span>
<span style="color: #339933;">#  Lapack routine dgesv: system is exactly singular</span>
&nbsp;
ginv<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]  0.1  0.1</span>
<span style="color: #339933;"># [2,]  0.2  0.2</span></pre></td></tr></table></div>

<p>In practice, you can usually get around using the pseudoinverse, but it&#8217;s nice to know that it&#8217;s at hand all the time. </p>
<h3>Eigenvalues and Eigenvectors</h3>
<p>Eigenvectors are surely the bane of every starting student of linear algebra, though their considerable power to simplify problems makes them the darling of every applied mathematician. Thankfully, R makes it easy to get these for every matrix:</p>

<div class="wp_codebox"><table><tr id="p370793"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p3707code93"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
eigen<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># $values</span>
<span style="color: #339933;"># [1] 1 1</span>
<span style="color: #339933;">#</span>
<span style="color: #339933;"># $vectors</span>
<span style="color: #339933;">#      [,1] [,2]</span>
<span style="color: #339933;"># [1,]    0   -1</span>
<span style="color: #339933;"># [2,]    1    0</span></pre></td></tr></table></div>

<h3>Matrix Metadata</h3>
<p>Last, but not least, you can get metadata about the shape of a matrix using <code>dim</code>, <code>nrow</code> and <code>ncol</code>:</p>

<div class="wp_codebox"><table><tr id="p370794"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p3707code94"><pre class="c" style="font-family:monospace;">m <span style="color: #339933;">&lt;-</span> diag<span style="color: #009900;">&#40;</span>nrow <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> ncol <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
&nbsp;
dim<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 2 2</span>
&nbsp;
nrow<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 2</span>
&nbsp;
ncol<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 2</span></pre></td></tr></table></div>

<p>Hopefully those operations were already familiar to any readers out there, as I doubt that they&#8217;ll be clear from this short explanation without prior knowledge. I just felt compelled to review them before using any of them in my next set of posts. Tomorrow, I&#8217;ll start going through more interesting matrix algorithms in R, beginning with the SVD.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/16/quick-review-of-matrix-algebra-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some Pitfalls of Object Oriented Programming in R</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/15/some-pitfalls-of-object-oriented-programming-in-r/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/15/some-pitfalls-of-object-oriented-programming-in-r/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 18:58:08 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Observations]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3691</guid>
		<description><![CDATA[Yesterday, I wrote a post about implementing setter methods for objects in R. On my way to understanding how to implement these methods correctly, I made a bunch of mistakes. To keep people from wasting their time with these same mistakes, I&#8217;m reviewing them here.
Mistake One: Naming Conventions
Every generic method will search for a class [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote a post about implementing setter methods for objects in R. On my way to understanding how to implement these methods correctly, I made a bunch of mistakes. To keep people from wasting their time with these same mistakes, I&#8217;m reviewing them here.</p>
<h3>Mistake One: Naming Conventions</h3>
<p>Every generic method will search for a class method using a single convention: if the generic method name is <code>GENERIC_METHOD</code> and the class name is <code>CLASS_NAME</code>, then the class method is always <code>GENERIC_METHOD.CLASS_NAME</code>. This is true no matter how strange the generic method looks: the generic method <code>id&lt;-</code> for class <code>user</code> has a class method called </code>id&lt;-.user</code>. The strange <code>&lt;-</code> in the method name doesn't change the fact that <code>.user</code> comes at the end of the class method name.</p>
<h3>Mistake Two: Inspecting Existing Functions with Strange Names</h3>
<p>If you want to get a sense how the function <code>class</code> works, you can simply type <code>class</code> at the R interpreter. Unfortunately, the parsing rules for R insure that this will never work for methods that end in "&lt;-". For that reason, it's not at all clear how to get the definitions of these strangely named functions to come up in the interpreter. When defining these sort of methods, it's sufficient to simply enclose the method name, but that will not work for the purposes of introspection:</p>

<div class="wp_codebox"><table><tr id="p3691100"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3691code100"><pre class="c" style="font-family:monospace;"><span style="color: #ff0000;">'class&lt;-'</span>
&nbsp;
<span style="color: #339933;"># [1] &quot;class&lt;-&quot;</span></pre></td></tr></table></div>

<p>As you can see, <code>'class&lt;-'</code> evaluates to a string, which is neither surprising nor undesirable. Unfortunately, this leaves it unclear how to get at the definition of <code>class&lt;-</code>. The secret for viewing the definitions of these strangely named functions is to enclose the function name in backticks:</p>

<div class="wp_codebox"><table><tr id="p3691101"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3691code101"><pre class="c" style="font-family:monospace;">`class<span style="color: #339933;">&lt;-</span>`
&nbsp;
<span style="color: #339933;"># function (x, value)  .Primitive(&quot;class&lt;-&quot;)</span></pre></td></tr></table></div>

<h3>Mistake Three: Wasting Time on `&lt;&lt;-`</h3>
<p>Before I learned that the trick to writing setter methods is to return an edited copy of the changed object, I was convinced that I needed to evaluate an assignment in the caller's scope. While pursuing this idea, I discovered the <code>&lt;&lt;-</code> operator. It's quite an interesting operator, but it's not the solution to the setter method definition problem.</p>
<p>You see, <code>&lt;&lt;-</code> is an assignment operator that will go searching through enclosing scopes (more precisely, R's environments) until it finds a variable that can be assigned to. Here's an example of how you could use <code>&lt;&lt;-</code> to do something surprising in R:</p>

<div class="wp_codebox"><table><tr id="p3691102"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p3691code102"><pre class="c" style="font-family:monospace;">i <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">1</span>
&nbsp;
<span style="color: #ff0000;">&quot;++&quot;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  i <span style="color: #339933;">&lt;&lt;-</span> i <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">&quot;++&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
print<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;"># [1] 2</span>
&nbsp;
<span style="color: #ff0000;">&quot;++&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
print<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;"># [1] 3</span></pre></td></tr></table></div>

<p>This is pretty cool. In fact, it was cool enough that I wasted a good thirty minutes playing with it before realizing that it wasn't helping anything.</p>
<h3>Mistake Four: UseMethod is Magic</h3>
<p>Figuring out how to write code that calls <code>UseMethod</code> for functions with many arguments is not trivial: <code>UseMethod</code>'s behavior is a little magical, and the R help docs are as spartan as ever with regard to calling <code>UseMethod</code>. If you naively try to write a function like this,</p>

<div class="wp_codebox"><table><tr id="p3691103"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p3691code103"><pre class="c" style="font-family:monospace;">i <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">2</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'myint'</span>
&nbsp;
raise.<span style="color: #202020;">power</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> power<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'raise.power'</span><span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> power<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raise.<span style="color: #202020;">power</span>.<span style="color: #202020;">myint</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> power<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>as.<span style="color: #202020;">numeric</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">**</span> power<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raise.<span style="color: #202020;">power</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>you'll succeed at defining a generic method that generates warnings every single time it's run. This is because you are never allowed to pass more than two arguments to <code>UseMethod</code>. You see, <code>UseMethod</code> automatically discovers the arguments passed to its caller when it's called and otherwise completely ignores the arguments passed to it after the second argument. For that reason, what you need to write is</p>

<div class="wp_codebox"><table><tr id="p3691104"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p3691code104"><pre class="c" style="font-family:monospace;">i <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">2</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'myint'</span>
&nbsp;
raise.<span style="color: #202020;">power</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> power<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'raise.power'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raise.<span style="color: #202020;">power</span>.<span style="color: #202020;">myint</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> power<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>as.<span style="color: #202020;">numeric</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">**</span> power<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raise.<span style="color: #202020;">power</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>with the latter arguments dropped from the call to <code>UseMethod</code>. If you're like me, you'll probably feel that you're going to lose <code>value</code> along the way the first few times that you write this. I suggest you try this approach out, convince yourself that it works, and then just take advantage of the <code>UseMethod</code> voodoo to solve your problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/15/some-pitfalls-of-object-oriented-programming-in-r/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object-Oriented Programming in R: The Setter Methods</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/14/object-oriented-programming-in-r-the-setter-methods/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/14/object-oriented-programming-in-r-the-setter-methods/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 04:49:06 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3684</guid>
		<description><![CDATA[With a little guidance from the indefatigable Hadley Wickham, I figured out today how to implement the setter methods that were missing from my example user class. To review, let&#8217;s rebuild the getter methods for my user object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
user &#60;- list&#40;id = 1,
             password [...]]]></description>
			<content:encoded><![CDATA[<p>With a little guidance from the indefatigable <a href="http://had.co.nz/">Hadley Wickham</a>, I figured out today how to implement the setter methods that were missing from my example user class. To review, let&#8217;s rebuild the getter methods for my user object:</p>

<div class="wp_codebox"><table><tr id="p3684109"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code" id="p3684code109"><pre class="c" style="font-family:monospace;">user <span style="color: #339933;">&lt;-</span> list<span style="color: #009900;">&#40;</span>id <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>
             password <span style="color: #339933;">=</span> <span style="color: #ff0000;">'286755fad04869ca523320acce0dc6a4'</span><span style="color: #339933;">,</span>
             email <span style="color: #339933;">=</span> <span style="color: #ff0000;">'jmw@johnmyleswhite.com'</span><span style="color: #009900;">&#41;</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'user'</span>
&nbsp;
id <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'id'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
id.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
password <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'password'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
password.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
email <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'email'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
email.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>With these working, my goal was to write setter methods that would work like the ideal code below:</p>

<div class="wp_codebox"><table><tr id="p3684110"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3684code110"><pre class="c" style="font-family:monospace;">id<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">2</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  print<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Succeeded in editing the user's id attribute.&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Defining this sort of setter method turned out to require a little effort. I wasted quite a lot of time walking down dead ends, but, thankfully, Hadley gave me the exact piece of information I was missing early this morning. The dead ends were interesting in themselves, though, so I&#8217;ll review them in another post tomorrow.</p>
<p>For now I&#8217;ll just explain the correct implementation for my desired setter methods, which you can see below:</p>

<div class="wp_codebox"><table><tr id="p3684111"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p3684code111"><pre class="c" style="font-family:monospace;"><span style="color: #ff0000;">'id&lt;-'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'id&lt;-'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">'id&lt;-.user'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> value
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>There are three important ideas here:</p>
<ol>
<li>The generic setter method for <code>id</code> should be called <code>'id<-'</code>, which must always be quoted to prevent parsing errors.</li>
<li>The class level setter method should be called <code>'id<-.user'</code>, which isn't surprising, though I had imagined it might be called <code>'id.user<-'</code> at one point.</li>
<li>The setter method should make a change to a copy of the original object and return the edited copy to the caller. R handles the assignment of this edited return value to the original object behind the scenes. In fact, not using this return value approach will make otherwise plausible looking code fail to edit your object correctly. I stumbled on that for quite a while before I was shown the light.</li>
</ol>
<p>With this in mind, a final user class looks like this:</p>

<div class="wp_codebox"><table><tr id="p3684112"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code" id="p3684code112"><pre class="c" style="font-family:monospace;">user <span style="color: #339933;">&lt;-</span> list<span style="color: #009900;">&#40;</span>id <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>
             password <span style="color: #339933;">=</span> <span style="color: #ff0000;">'286755fad04869ca523320acce0dc6a4'</span><span style="color: #339933;">,</span>
             email <span style="color: #339933;">=</span> <span style="color: #ff0000;">'jmw@johnmyleswhite.com'</span><span style="color: #009900;">&#41;</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'user'</span>
&nbsp;
id <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'id'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
id.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
password <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'password'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
password.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
email <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'email'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
email.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">'id&lt;-'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'id&lt;-'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">'id&lt;-.user'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> value
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">'password&lt;-'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'password&lt;-'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">'password&lt;-.user'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> value
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">'email&lt;-'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'email&lt;-'</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">'email&lt;-.user'</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  x<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> value
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/14/object-oriented-programming-in-r-the-setter-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Most Basic Elements of Object-Oriented Programming in R</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/13/the-most-basic-elements-of-object-oriented-programming-in-r/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/13/the-most-basic-elements-of-object-oriented-programming-in-r/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 17:12:25 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3674</guid>
		<description><![CDATA[Until recently, I&#8217;ve never had any reason to learn how to define my own classes in R. Having learned this week, I was surprised to find out how easy it is to start implementing classes in R. If you know nothing about creating classes and class methods in R, here&#8217;s a very quick overview of [...]]]></description>
			<content:encoded><![CDATA[<p>Until recently, I&#8217;ve never had any reason to learn how to define my own classes in R. Having learned this week, I was surprised to find out how easy it is to start implementing classes in R. If you know nothing about creating classes and class methods in R, here&#8217;s a very quick overview of the three core ideas behind R&#8217;s object system. If you already know the basics of defining classes in R, I&#8217;d suggest skipping this post, as it&#8217;s not likely to be of much value to you.</p>
<p>The first thing you should know is that the S3 object-oriented system feels like a hack that was put on top of the original S language. If you&#8217;re familiar with Perl, you&#8217;ll feel right at home with the basic ideas behind this approach; you&#8217;ll probably also appreciate the conceptual simplicity of the results.</p>
<p>With that in mind, you should be aware that any normal piece of R code is filled with objects, because the base packages and most well-designed user packages use lots of custom classes. You can see this for yourself by starting to use <code>class</code> to perform introspection on some of the items in your programs:</p>

<div class="wp_codebox"><table><tr id="p3674124"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p3674code124"><pre class="c" style="font-family:monospace;">class<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] &quot;numeric&quot;</span>
&nbsp;
class<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] &quot;character&quot;</span>
&nbsp;
x <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span><span style="color: #0000dd;">10</span>
y <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">5</span> <span style="color: #339933;">*</span> x <span style="color: #339933;">+</span> rnorm<span style="color: #009900;">&#40;</span>length<span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
&nbsp;
fit <span style="color: #339933;">&lt;-</span> lm<span style="color: #009900;">&#40;</span>y ~ x<span style="color: #009900;">&#41;</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>fit<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] &quot;lm&quot;</span></pre></td></tr></table></div>

<p>More interesting than looking at the classes of existing objects is defining new classes of your own. Thankfully, <code>class</code>, like <code>names</code> and many other functions in R, can be assigned to directly, like so:</p>

<div class="wp_codebox"><table><tr id="p3674125"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p3674code125"><pre class="c" style="font-family:monospace;">a <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">1</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'octonion'</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] &quot;octonion&quot;</span></pre></td></tr></table></div>

<p>If you&#8217;re familiar with Perl, you can think of this assignment to <code>class</code> as analogous to using <code>bless</code>. If you&#8217;re not a Perl user, hopefully this approach to defining classes still makes sense to you, because it&#8217;s so simple: the only thing that decides the class of an object is the value you&#8217;ve set for the class attribute. To convince yourself that the class of an object is just an attribute, you can use <code>attr</code>:</p>

<div class="wp_codebox"><table><tr id="p3674126"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3674code126"><pre class="c" style="font-family:monospace;">attr<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> <span style="color: #ff0000;">'class'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>This simple metadata driven approach to building objects is the core of the S3 object system, as far as I can tell.</p>
<p>Of course, setting <code>class</code> by itself is pretty useless: you want to be able to define class methods. That&#8217;s where the other two main ideas come in. The first trick is to use generic methods to get polymorphism out of the language; the second trick is to define methods on your classes using a simple naming convention. To see how this works, let&#8217;s use an example that should be familiar to anyone who&#8217;s ever built a database-backed website.</p>
<p>Instead of my would-be octonion class, we&#8217;ll consider a user object. To define an object of class &#8216;user&#8217;, we can do the following:</p>

<div class="wp_codebox"><table><tr id="p3674127"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p3674code127"><pre class="c" style="font-family:monospace;">user <span style="color: #339933;">&lt;-</span> list<span style="color: #009900;">&#40;</span>id <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>
             password <span style="color: #339933;">=</span> <span style="color: #ff0000;">'41bfe136a536b7749104415bc364df2e'</span><span style="color: #339933;">,</span>
             email <span style="color: #339933;">=</span> <span style="color: #ff0000;">'jmw@johnmyleswhite.com'</span><span style="color: #009900;">&#41;</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'user'</span>
&nbsp;
user
&nbsp;
<span style="color: #339933;">#$id</span>
<span style="color: #339933;">#[1] 1</span>
<span style="color: #339933;">#</span>
<span style="color: #339933;">#$password</span>
<span style="color: #339933;">#[1] &quot;41bfe136a536b7749104415bc364df2e&quot;</span>
<span style="color: #339933;">#</span>
<span style="color: #339933;">#$email</span>
<span style="color: #339933;">#[1] &quot;jmw@johnmyleswhite.com&quot;</span>
<span style="color: #339933;">#</span>
<span style="color: #339933;">#attr(,&quot;class&quot;)</span>
<span style="color: #339933;">#[1] &quot;user&quot;</span></pre></td></tr></table></div>

<p>The first thing that comes to mind after building this object is that we should define getter and setter methods for accessing and modifying the contents of this user object. For example, we want a way to get the id attribute for our object. We&#8217;d also like our approach to generalize to other objects with an id attribute. Ideally, we should be able to write something like this:</p>

<div class="wp_codebox"><table><tr id="p3674128"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p3674code128"><pre class="c" style="font-family:monospace;">id<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Now, it is obviously possible to define an <code>id</code> function that operates only on user objects, but that&#8217;s not the right approach if we also want to have another sort of object that would have an <code>id</code> method as well. This polymorphism concern is the problem that generic methods and specialized naming conventions solve.</p>
<p>First, we are going to define a specialized <code>id</code> method that only operates on user objects. Because it will only work on &#8216;user&#8217; objects, we&#8217;ll call it <code>id.user</code>:</p>

<div class="wp_codebox"><table><tr id="p3674129"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p3674code129"><pre class="c" style="font-family:monospace;">id.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user.<span style="color: #202020;">object</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user.<span style="color: #202020;">object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
id.<span style="color: #202020;">user</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 1</span></pre></td></tr></table></div>

<p>Given this, we can then define a generic method that will operate on objects of many classes and reroute our general function calls to the correct class-level method:</p>

<div class="wp_codebox"><table><tr id="p3674130"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p3674code130"><pre class="c" style="font-family:monospace;">id <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'id'</span><span style="color: #339933;">,</span> object<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
id<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #339933;"># [1] 1</span></pre></td></tr></table></div>

<p>Here <code>UseMethod</code> just searches for an <code>id.user</code> function, finds it and then calls it with <code>user</code> as its argument. If we had a &#8216;profile&#8217; object, <code>id</code> would search for an <code>id.profile</code> function and call that. You can see this by trying <code>id</code> on a variable whose class we set to be <code>profile</code> using <code>class</code>.</p>

<div class="wp_codebox"><table><tr id="p3674131"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p3674code131"><pre class="c" style="font-family:monospace;">profile <span style="color: #339933;">&lt;-</span> list<span style="color: #009900;">&#40;</span>id <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
&nbsp;
class<span style="color: #009900;">&#40;</span>profile<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'profile'</span>
&nbsp;
id<span style="color: #009900;">&#40;</span>profile<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;"># Error in UseMethod(&quot;id&quot;, user) : </span>
<span style="color: #339933;">#  no applicable method for 'id' applied to an object of class &quot;profile&quot;</span></pre></td></tr></table></div>

<p>With these ideas in mind, it&#8217;s easy to do something similar for the rest of our attributes as well:</p>

<div class="wp_codebox"><table><tr id="p3674132"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p3674code132"><pre class="c" style="font-family:monospace;">password.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user.<span style="color: #202020;">object</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user.<span style="color: #202020;">object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
password <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'password'</span><span style="color: #339933;">,</span> user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
email.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user.<span style="color: #202020;">object</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user.<span style="color: #202020;">object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
email <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'email'</span><span style="color: #339933;">,</span> user<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Defining setter methods is a little more tricky. We could use a trick like the <code>eval</code> hacks I used to implement <code>push</code> and <code>pop</code> recently, but Hadley Wickham wisely chastened me for doing that. I&#8217;m still trying to decide what&#8217;s the best approach.</p>
<p>As I see it, there are least three possible method call styles you might define:</p>

<div class="wp_codebox"><table><tr id="p3674133"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p3674code133"><pre class="c" style="font-family:monospace;">user <span style="color: #339933;">&lt;-</span> password<span style="color: #009900;">&#40;</span>user<span style="color: #339933;">,</span> <span style="color: #ff0000;">'new_value'</span><span style="color: #009900;">&#41;</span>
&nbsp;
password<span style="color: #009900;">&#40;</span>user<span style="color: #339933;">,</span> <span style="color: #ff0000;">'new_value'</span><span style="color: #009900;">&#41;</span>
&nbsp;
password<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">'new_value'</span></pre></td></tr></table></div>

<p>I would really like to start implementing this last sort, but I haven&#8217;t figured out how to yet. The others can be implemented using the generic methods approach I just outlined above, along with some ugly calls to <code>eval</code> for the second call style. To implement the first, simply do something like this:</p>

<div class="wp_codebox"><table><tr id="p3674134"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p3674code134"><pre class="c" style="font-family:monospace;">password.<span style="color: #202020;">user</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #339933;">,</span> new.<span style="color: #202020;">value</span> <span style="color: #339933;">=</span> NULL<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>is.<span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#40;</span>new.<span style="color: #202020;">value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span>
  <span style="color: #009900;">&#123;</span>
    user<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> new.<span style="color: #202020;">value</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
password <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #339933;">,</span> new_value <span style="color: #339933;">=</span> NULL<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  UseMethod<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'password'</span><span style="color: #339933;">,</span> user<span style="color: #339933;">,</span> new_value<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you have suggestions on how to implement the third approach, please let me know. Also, I am not at all happy with the use of <code>NULL</code> in my current setter implementation, because that makes it impossible to set the value of an attribute to <code>NULL</code>. If people have suggestions, I&#8217;d very much appreciate them. Does R implement arity-specific function definitions?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/13/the-most-basic-elements-of-object-oriented-programming-in-r/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Lot of Deaths are Partly Self-Induced</title>
		<link>http://www.johnmyleswhite.com/notebook/2009/12/11/a-lot-of-deaths-are-partly-self-induced/</link>
		<comments>http://www.johnmyleswhite.com/notebook/2009/12/11/a-lot-of-deaths-are-partly-self-induced/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 15:31:01 +0000</pubDate>
		<dc:creator>John Myles White</dc:creator>
				<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://www.johnmyleswhite.com/?p=3672</guid>
		<description><![CDATA[I&#8217;m a little surprised by Andrew Gelman&#8217;s post today, doubting the wisdom of a passage from Gary Becker&#8217;s work that reads:

According to the economic approach, therefore, most (if not all!) deaths are to some extent &#8220;suicides&#8221; in the sense that they could have been postponed if more resources had been invested in prolonging life.

I disagree [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a little surprised by <a href="http://scienceblogs.com/appliedstatistics/2009/12/all_deaths_are_suicides.php">Andrew Gelman&#8217;s post today</a>, doubting the wisdom of a passage from Gary Becker&#8217;s work that reads:</p>
<blockquote><p>
According to the economic approach, therefore, most (if not all!) deaths are to some extent &#8220;suicides&#8221; in the sense that they could have been postponed if more resources had been invested in prolonging life.
</p></blockquote>
<p>I disagree with Gelman in finding this passage unimpressive or even slightly ridiculous. I think there is an important insight that Becker is trying to push on: we humans have far more control over our lives than we care to admit. I also think that not admitting the extent of our power to control our lives exculpates us for many of our failures, which is why it is one of the more popular approaches to dealing with the problems in our lives. As <a href="http://en.wikiquote.org/wiki/François_de_La_Rochefoucauld">La Rochefoucauld</a> always pointed out, nothing trumps self-love.</p>
<p>And I don&#8217;t think that emphasizing the unexpectedly large amount of control we have in our lives is just an idea that the economic approach has to offer us. I think you&#8217;d get the same idea out of reading Sartre&#8217;s discussion of a young man choosing whether or not to fight in the French Resistance in <a href="http://www.marxists.org/reference/archive/sartre/works/exist/sartre.htm">Existentialism is a Humanism.</a></p>
<p>But, to focus on the thrust of Gelman&#8217;s argument, my problems are really with the following passage:</p>
<blockquote><p>
My impression, though, is that people are dying all the time without wanting to do so, and Becker&#8217;s argument seems pretty silly to me. To spell it out in a little more detail: Suppose a person is standing on the sidewalk and is run over by an out-of-control car. I don&#8217;t see how you can call it suicidal of the pedestrian that the driver was not paying attention. Nor do I see it as suicidal if someone develops kidney cancer and dies, nor do I see it as suicidal if a kid is playing and falls out of a high window, or if someone in the Middle East is hit by a bomb while sitting in a school, etc. Beyond this, just as there used to be millions of people who smoked and died of cancer before people knew that smoking kills, I&#8217;m sure there are millions of people now doing something that they don&#8217;t realize is dangerous.
</p></blockquote>
<p>The first section is worrisome, because it seems to be catering to the availability bias: we humans are almost certain to overestimate the number of deaths caused by car accidents, so, by referring to it without explicit numbers, we&#8217;re likely to anchor ourselves on inaccurate quantities. In general, once any emotional topic comes up, I feel the need to find something more immutable than intuitions to trust in. Once a kid falls out of a window, rationality tends to follow them.</p>
<p>Now, I freely concede that all of the examples Gelman lists are not at all suicide-like, but what I worry about is the relative importance of those causes of death amid the frequent causes of death, at least in the U.S. I especially worry about this given the last two sentences of the above-cited passage: why focus on the &#8220;millions of people now doing something that they don&#8217;t realize is dangerous?&#8221; Why not focus instead on the millions of people killing themselves by smoking, fully cognizant of their self-destruction all the while? What possible reason would you have for focusing on one or the other?</p>
<p>I&#8217;d say that, in general, you can focus on those who smoke if you want to prove Becker right, and that you can focus on those who don&#8217;t if you want to prove him wrong. Either way, I am reasonably certain that you&#8217;ll implicitly distort your internal estimates of the relative numbers.</p>
<p>With that in mind, I think the right approach is to get some plausible estimate from real data. Here&#8217;s a simplistic start:</p>
<p>(1) Go to the CDC website and find a reasonably current list of the leading causes of death in the U.S. <a href="http://www.cdc.gov/nchs/FASTATS/lcod.htm">here</a>.</p>
<p>(2) Mark each item as a possible point for Becker&#8217;s case. This is  clearly debatable, but I&#8217;ll take as granted that &#8220;heart disease&#8221; (over-eating and under-exercising),  &#8220;chronic lower respiratory diseases&#8221; (smoking), &#8220;diabetes&#8221; (over-eating and under-exercising), and &#8220;influenza and pneumonia&#8221; (refusing vaccination) are sometimes self-induced. My own bias is to assume that they&#8217;re usually self-induced, but that&#8217;s pure intuition, so please don&#8217;t trust that idea. In fact, what I&#8217;d really like is for you, dear reader, to prove me wrong with more elaborate statistics.</p>
<p>With these marked data points, I get a data set that looks like this:</p>
<table>
<th>Cause of Death</th>
<th>Deaths</th>
<th>Partly Self-Induced</th>
<tr>
<td>Heart disease</td>
<td>631636</td>
<td>1</td>
</tr>
<tr>
<td>Cancer</td>
<td>559888</td>
<td>0</td>
</tr>
<tr>
<td>Stroke (cerebrovascular diseases)</td>
<td>137119</td>
<td>0</td>
</tr>
<tr>
<td>Chronic lower respiratory diseases</td>
<td>124583</td>
<td>1</td>
</tr>
<tr>
<td>Accidents (unintentional injuries)</td>
<td>121599</td>
<td>0</td>
</tr>
<tr>
<td>Diabetes</td>
<td>72449</td>
<td>1</td>
</tr>
<tr>
<td>Alzheimer&#8217;s disease</td>
<td>72432</td>
<td>0</td>
</tr>
<tr>
<td>Influenza and Pneumonia</td>
<td>56326</td>
<td>1</td>
</tr>
<tr>
<td>Nephritis, nephrotic syndrome, and nephrosis</td>
<td>45344</td>
<td>0</td>
</tr>
<tr>
<td>Septicemia</td>
<td>34234</td>
<td>0</td>
</tr>
</table>
<p>Copying and pasting the resulting table and running two lines of R code lets me avoid the relevant mental arithmetic:</p>

<div class="wp_codebox"><table><tr id="p3672136"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p3672code136"><pre class="c" style="font-family:monospace;">death.<span style="color: #202020;">data</span> <span style="color: #339933;">&lt;-</span> read.<span style="color: #202020;">csv</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'death_data.csv'</span><span style="color: #339933;">,</span> header <span style="color: #339933;">=</span> TRUE<span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span><span style="color: #009900;">&#41;</span>
&nbsp;
with<span style="color: #009900;">&#40;</span>subset<span style="color: #009900;">&#40;</span>death.<span style="color: #202020;">data</span><span style="color: #339933;">,</span> Partly.<span style="color: #202020;">Self</span>.<span style="color: #202020;">Induced</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> sum<span style="color: #009900;">&#40;</span>Deaths<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> with<span style="color: #009900;">&#40;</span>death.<span style="color: #202020;">data</span><span style="color: #339933;">,</span> sum<span style="color: #009900;">&#40;</span>Deaths<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>And I get 48% of all deaths as being self-induced. That&#8217;s definitely not &#8220;almost all&#8221;, but plainly a close call for &#8220;most.&#8221; Obviously this number is dubious at best: my point is only that so many cognitive biases operate in thinking about this sort of issue that you have to use numbers as a form of mental hygiene. Obviously pushing this issue into the realm of statistics only endangers my claims, since Gelman is so much more capable than I am as a statistician, but I think my general point is accurate: whether a given person thinks the numbers support Becker or disprove him says more about that person&#8217;s general outlook on life than about the actual question at hand.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnmyleswhite.com/notebook/2009/12/11/a-lot-of-deaths-are-partly-self-induced/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.907 seconds -->
