<?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>James Keirstead</title>
	<atom:link href="http://www.jameskeirstead.ca/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jameskeirstead.ca</link>
	<description></description>
	<lastBuildDate>Tue, 29 Nov 2011 10:58:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Book review: The Evolution of Great World Cities</title>
		<link>http://www.jameskeirstead.ca/general/book-review-the-evolution-of-great-world-cities/</link>
		<comments>http://www.jameskeirstead.ca/general/book-review-the-evolution-of-great-world-cities/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 10:57:50 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Reading]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[cities]]></category>
		<category><![CDATA[detroit]]></category>
		<category><![CDATA[economics]]></category>
		<category><![CDATA[sustainability]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=813</guid>
		<description><![CDATA[A fascinating new book on urban economics, infrastructure, and the links between them.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.amazon.co.uk/gp/product/1442611529/ref=as_li_ss_il?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=1442611529"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/11/9781442611528.jpg" alt="The Evolution of Great World Cities" title="The Evolution of Great World Cities" width="160" height="240" class="alignright size-full wp-image-817" /></a>Looking at Yves Marchand and Romain Meffre&#8217;s recent <a href="http://www.marchandmeffre.com/detroit/index.html">photoessay on Detroit</a>, one can&#8217;t help but wonder what happened.  How did a city that was literally the engine of the American economy sputter and decay into a mass of peeling paint, broken windows, and faded twentieth-century glamour?  And on the flip side, how can a city like Dubai, with its <a href="http://en.wikipedia.org/wiki/Burj_Khalifa">Burj Khalifa</a> tower stretching nearly 1 km into the sky, rise out of the desert in such a short period of time?</p>
<p>These are some of the questions that Chris Kennedy&#8217;s new book, <a href="http://www.amazon.co.uk/gp/product/1442611529/ref=as_li_ss_tl?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=1442611529">The Evolution of Great World Cities</a>, seeks to answer.  Kennedy is a professor of civil engineering at the University of Toronto with a soft spot for cities and economics.  Launching the book in London last week, he noted that he originally wanted to investigate the wealth of cities in much the same way that Adam Smith had done for the wealth of nations.  But along the way, the book changed into something subtly different and the result is a fascinating mix of macroeconomics, infrastructure engineering, history, and ecology.</p>
<p><div id="attachment_821" class="wp-caption alignleft" style="width: 253px"><a href="http://en.wikipedia.org/wiki/Philadelphia_City_Hall"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/11/City_Hall_Philadelphia-243x300.jpg" alt="" title="Philadelphia&#039;s City Hall" width="243" height="300" class="size-medium wp-image-821" /></a><p class="wp-caption-text">Philadelphia's City Hall</p></div>The book has three main themes.  First, it provides a useful definition for the wealth of cities, namely as the cumulative assets of its citizens.  This omits the value of public buildings and infrastructure, a distinction that seems counter-intuitive at first.  How can a city&#8217;s most prominent buildings, such as Philadelphia&#8217;s $6 billion dollar town hall <i>not</i> be included in such a total?  However as Kennedy describes, the value of these facilities is reflected in the locational value of people&#8217;s homes;  a home with access to first-rate transport, water, and energy supplies will have a higher value than a cabin in the woods with no such services and amenities.  Using the example of 16th century Seville, the importance of citizen ownership is also demonstrated.  Tonnes of Incan gold may have flowed through Seville&#8217;s gates, but most of it was destined for the hands of foreign owners, namely the bankers in Antwerp and Genoa that financed many of the trans-Atlantic expeditions.</p>
<p>The second theme is the connection between economic growth and the physical structure of cities.  Take the automobile as an example.  Its introduction in the early twentieth century led to a new mode of urban development, suburban sprawl, which although it has many disadvantages certainly leads to increased consumption.  Cars need to manufactured, sold, and serviced; larger suburban homes need to be constructed with more materials and filled with consumer goods.  The key issue here is not the infrastructure itself, but the modes of consumption that it necessitates.  For example, one might expect that the 1990s IT revolution would have led to a demand side crisis.  Just like Smith&#8217;s pin-makers, those displaced by the productivity gains of IT &ndash; bank tellers, backroom operations, and so on &ndash; would be unemployed and unable to consume.  However IT also created new opportunities in PC manufacturing, software design, and innovative business models like EBay and Amazon.  This new online way of life drove a new cycle of demand, rejuvenating the economy.</p>
<p>To me, this is the book&#8217;s most important contribution.  This idea of the <i>autonomous consumption of infrastructure</i>, that is the societally mandatory level of minimum consumption created by these systems, is hugely important for understanding not just the economic growth that Kennedy is concerned about, but more broadly the sustainability of cities.  North American urban sprawl is a perfect example.  While it engenders high levels of consumption, maintaining that lifestyle depends on a throughput of resources, most importantly abundant and affordable transport fuel.  Should these fuels become significantly more expensive, these cities would grind to a halt.  The same level of infrastructure-mandated autonomous consumption would still be there, but it would now consume a much larger portion of a household&#8217;s income, leading to reduced savings, reduced investment in new opportunities, and eventual stagnation.  In the language of sustainable development, the autonomous consumption of infrastructure represents a liability that must be serviced on an on-going basis.</p>
<p>The third theme is an analysis of urban economic processes as ecological systems.  The relevant chapter offers several noteworthy ideas but it felt incomplete compared to the rest of the tightly-argued book.  Nevertheless, an excellent description of Detroit&#8217;s decline is provided and the statistics cannot fail to astound: a population decline of 50% between 1930 and today, over 60 square miles of vacant abandoned land (44% of the city&#8217;s area), local tree species poking through factory floors that once produced millions of automobiles.  Kennedy argues that, in much the same way that an ecosystem needs diversity to survive a changing environment, Detroit was too focused on cars in order to survive, both in terms of the limited diversity of its economy and the inflexibility of its infrastructure.  One of the great what-ifs posed by the book is what Detroit might look like now had a  1923 proposal for a subway and integrated transit system been implemented.</p>
<p>Illustrated with case studies on cities as diverse as Toronto and Montreal, Philadelphia and New York, Seville, Paris, Dubai, and London, <a href="http://www.amazon.co.uk/gp/product/1442611529/ref=as_li_ss_tl?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=1442611529">The Evolution of Great World Cities</a> is a unique work of economic geography.  Engineers often complain that economic models are too abstract to offer a meaningful understanding of the real world.  Kennedy has therefore done both professions a great service by presenting a strong argument that it is the links between our built environment and economies that matter most.</p>
<p><em>Chris Kennedy has also written a <a href="http://blogs.worldbank.org/sustainablecities/the-evolution-of-great-world-cities-insights-for-developing-world-cities">blog post about the book</a> over at the World Bank&#8217;s Sustainable Cities site.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/general/book-review-the-evolution-of-great-world-cities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grrr&#8230;</title>
		<link>http://www.jameskeirstead.ca/r/grrr/</link>
		<comments>http://www.jameskeirstead.ca/r/grrr/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 09:27:06 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Rstats]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=807</guid>
		<description><![CDATA[<p>I&#8217;ve been working through Gelman et al.&#8217;s otherwise excellent <a href="http://www.amazon.co.uk/gp/product/158488388X/ref=as_li_ss_tl?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=158488388X">Bayesian Data Analysis</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=smallisbeauti-21&#038;l=as2&#038;o=2&#038;a=158488388X" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> and it&#8217;s going reasonably well.  My statistics is a little bit rusty so it&#8217;s taken time to work through all of the exercises and really understand what&#8217;s going on.  But I say &#8220;otherwise excellent&#8221; because yesterday I spent ages trying to figure out a problem, only to discover that the data published in the book don&#8217;t correspond to the text discussion.</p>
<p><a href="http://www.jameskeirstead.ca/r/grrr/" class="more-link">Read more on Grrr&#8230;&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working through Gelman et al.&#8217;s otherwise excellent <a href="http://www.amazon.co.uk/gp/product/158488388X/ref=as_li_ss_tl?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=158488388X">Bayesian Data Analysis</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=smallisbeauti-21&#038;l=as2&#038;o=2&#038;a=158488388X" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> and it&#8217;s going reasonably well.  My statistics is a little bit rusty so it&#8217;s taken time to work through all of the exercises and really understand what&#8217;s going on.  But I say &#8220;otherwise excellent&#8221; because yesterday I spent ages trying to figure out a problem, only to discover that the data published in the book don&#8217;t correspond to the text discussion.</p>
<p>The troublemaker is the SAT problem in section 5.5.  The authors give values for two variables <img src='http://s.wordpress.com/latex.php?latex=y_j&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y_j' title='y_j' class='latex' /> and <img src='http://s.wordpress.com/latex.php?latex=%5Csigma_j&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\sigma_j' title='\sigma_j' class='latex' /> for <img src='http://s.wordpress.com/latex.php?latex=j%3D1%5Cldots8&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='j=1\ldots8' title='j=1\ldots8' class='latex' /> schools, rounded to integer values. Using the formulas given in the text, I then calculated estimates for the complete pooling statistics and came up with a posterior treatment effect, <img src='http://s.wordpress.com/latex.php?latex=y&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y' title='y' class='latex' />, of 7.7 and a variance of 16.6.  However the text says these values should be 7.9 and 17.4 respectively.  </p>
<p>I puzzled over this for quite a while, thinking that maybe I&#8217;d missed a prior/posterior distinction somewhere and my estimates were supposed to be subtlely shifted.  But no, when I went and checked the <a href="http://www.jstor.org/stable/1164617">original data source</a>, I found that the values were reported to 4 sig figs.  Repeating the calculation with the new values gives the expected results.  Grr&#8230;</p>
<p>Here&#8217;s the data and R-code if anyone&#8217;s interested.</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">### Load SAT data from the Gelman et al book and the original Rubin paper</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a> <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">&#40;</span>school=<span style="color: #000000; font-weight: bold;">LETTERS</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="">:</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                   book.y=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">28</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="">-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span><span style="">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">18</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                   rubin.y=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">28.39</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7.94</span><span style="color: #339933;">,</span><span style="">-</span><span style="color: #cc66cc;">2.75</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6.82</span><span style="color: #339933;">,</span><span style="">-</span><span style="color: #cc66cc;">0.64</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.63</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">18.01</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">12.16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                   book.sigma=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">11</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">11</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">18</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                   rubin.sigma=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">14.9</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10.2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">16.3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">11</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">11.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">17.6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">### Rearrange data into a handy form</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a> <span style="">&lt;-</span> melt<span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="color: #339933;">,</span>id=<span style="color: #0000ff;">&quot;school&quot;</span><span style="color: #009900;">&#41;</span>
vals <span style="">&lt;-</span> colsplit<span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="">$</span>variable<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.&quot;</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;source&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;metric&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a> <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="color: #339933;">,</span>vals<span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a> <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><span style="">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a> <span style="">&lt;-</span> cast<span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="color: #339933;">,</span>school<span style="">+</span><a href="http://inside-r.org/r-doc/base/source"><span style="color: #003399; font-weight: bold;">source</span></a> <span style="">~</span> metric<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">### Calculate the summary statistics</span>
ddply<span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="color: #339933;">,</span>.<span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/source"><span style="color: #003399; font-weight: bold;">source</span></a><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>summarize<span style="color: #339933;">,</span>y=<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">&#40;</span>y<span style="">/</span>sigma<span style="">^</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="">/</span>sum<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="">/</span>sigma<span style="">^</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>sig=<span style="color: #cc66cc;">1</span><span style="">/</span>sum<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="">/</span>sigma<span style="">^</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/r/grrr/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sampling for Monte Carlo simulations with R</title>
		<link>http://www.jameskeirstead.ca/r/sampling-for-monte-carlo-simulations-with-r/</link>
		<comments>http://www.jameskeirstead.ca/r/sampling-for-monte-carlo-simulations-with-r/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 10:26:39 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Modelling]]></category>
		<category><![CDATA[Monte Carlo]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=798</guid>
		<description><![CDATA[I've knocked together a quick function for generating efficient Monte Carlo samples.  It takes a bit of the legwork out of running Monte Carlo simulations.]]></description>
			<content:encoded><![CDATA[<p>When doing <a href="http://en.wikipedia.org/wiki/Monte_Carlo_method">Monte Carlo simulation</a>, it&#8217;s important to pick your parameter values efficiently especially if your model is computationally expensive to run.  If the model takes two days to run, and a parameter <img src='http://s.wordpress.com/latex.php?latex=x&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x' title='x' class='latex' /> ranges from 0 to 10, it doesn&#8217;t make much sense to run it once at <img src='http://s.wordpress.com/latex.php?latex=x%3D4.9&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x=4.9' title='x=4.9' class='latex' /> and again at <img src='http://s.wordpress.com/latex.php?latex=x%3D5.1&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x=5.1' title='x=5.1' class='latex' /> if <img src='http://s.wordpress.com/latex.php?latex=x%20%5Cin%20%280%2C4%29%2C%20%286%2C10%29&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='x \in (0,4), (6,10)' title='x \in (0,4), (6,10)' class='latex' /> hasn&#8217;t been explored at all.</p>
<p>To get around this problem, one can use <a href="http://en.wikipedia.org/wiki/Low-discrepancy_sequences">quasi-random low-discrepancy sequences</a> which are designed to fill a parameter space efficiently.  The R package, <a href="http://cran.r-project.org/web/packages/randtoolbox/">randtoolbox</a>, provides implementations of common sequences, like the Halton or Sobol&#8217;, but the process involves a couple of steps that beg to be automated.  The general process is:</p>
<ol>
<li>Generate a <em>n</em> x <em>p</em> matrix of uniformly distributed quasi-random values, where <em>n</em> is the number of simulations you wish to run and <em>p</em> is the number of parameters.</li>
<li>For each column of the matrix, convert the quasi-random value to the parameter&#8217;s actual distribution by inverting the <a href="http://en.wikipedia.org/wiki/Cumulative_distribution_function">cdf curve</a>.  So if you have a uniformly distributed value of 0.5, and you want to convert it to a normal distribution with mean <img src='http://s.wordpress.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu' title='\mu' class='latex' /> you would get a parameter value of <img src='http://s.wordpress.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu' title='\mu' class='latex' /> for use in your simulation.</li>
<li>Run your simulation with these parameter values, and analyse the results</li>
</ol>
<p>I&#8217;ve written a little R function to make this process easier.  You simply pass it the number of simulations you want to run, and a list describing each parameter, and it will return the Monte Carlo sample as a data frame.  At the moment, it&#8217;s pretty rudimentry, and each parameter is described by a name, a distribution name (matching the R abbreviations, e.g. &#8220;unif&#8221; for the uniform distribution, &#8220;norm&#8221; for the normal), and two parameters to describe the distribution.</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Generate a Monte Carlo sample</span>
generateMCSample <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">,</span> vals<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Packages to generate quasi-random sequences</span>
  <span style="color: #666666; font-style: italic;"># and rearrange the data</span>
  <a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/packages/cran/randtoolbox"><span style="">randtoolbox</span></a><span style="color: #009900;">&#41;</span>
  <a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/packages/cran/plyr"><span style="">plyr</span></a><span style="color: #009900;">&#41;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Generate a Sobol' sequence</span>
  sob <span style="">&lt;-</span> sobol<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">&#40;</span>vals<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Fill a matrix with the values</span>
  <span style="color: #666666; font-style: italic;"># inverted from uniform values to</span>
  <span style="color: #666666; font-style: italic;"># distributions of choice</span>
  samp <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>n<span style="">*</span><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">&#40;</span>vals<span style="color: #009900;">&#41;</span><span style="">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/nrow"><span style="color: #003399; font-weight: bold;">nrow</span></a>=n<span style="color: #009900;">&#41;</span>
  samp<span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="">&lt;-</span> <span style="color: #cc66cc;">1</span><span style="">:</span>n
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span><span style="">:</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">&#40;</span>vals<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    l <span style="">&lt;-</span> vals<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
    <a href="http://inside-r.org/r-doc/stats/dist"><span style="color: #003399; font-weight: bold;">dist</span></a> <span style="">&lt;-</span> l<span style="">$</span>dist
    params <span style="">&lt;-</span> l<span style="">$</span>params
    samp<span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span>i<span style="">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/eval"><span style="color: #003399; font-weight: bold;">eval</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/call"><span style="color: #003399; font-weight: bold;">call</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/paste"><span style="color: #003399; font-weight: bold;">paste</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;q&quot;</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/dist"><span style="color: #003399; font-weight: bold;">dist</span></a><span style="color: #339933;">,</span>sep=<span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>sob<span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>params<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>params<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Convert matrix to data frame and label</span>
  samp <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">&#40;</span>samp<span style="color: #009900;">&#41;</span>
  <a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">&#40;</span>samp<span style="color: #009900;">&#41;</span> <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">,</span>laply<span style="color: #009900;">&#40;</span>vals<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span> l<span style="">$</span>var<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">&#40;</span>samp<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
<p>Here&#8217;s a simple example to show how it can be used.</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;">n <span style="">&lt;-</span> <span style="color: #cc66cc;">1000</span>  <span style="color: #666666; font-style: italic;"># number of simulations to run</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># List described the distribution of each variable</span>
vals <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a>=<span style="color: #0000ff;">&quot;Uniform&quot;</span><span style="color: #339933;">,</span>
               <a href="http://inside-r.org/r-doc/stats/dist"><span style="color: #003399; font-weight: bold;">dist</span></a>=<span style="color: #0000ff;">&quot;unif&quot;</span><span style="color: #339933;">,</span>
               params=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a>=<span style="color: #0000ff;">&quot;Normal&quot;</span><span style="color: #339933;">,</span>
               <a href="http://inside-r.org/r-doc/stats/dist"><span style="color: #003399; font-weight: bold;">dist</span></a>=<span style="color: #0000ff;">&quot;norm&quot;</span><span style="color: #339933;">,</span>
               params=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a>=<span style="color: #0000ff;">&quot;Weibull&quot;</span><span style="color: #339933;">,</span>
               <a href="http://inside-r.org/r-doc/stats/dist"><span style="color: #003399; font-weight: bold;">dist</span></a>=<span style="color: #0000ff;">&quot;weibull&quot;</span><span style="color: #339933;">,</span>
               params=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Generate the sample</span>
samp <span style="">&lt;-</span> generateMCSample<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">,</span>vals<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Plot with ggplot2</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/packages/cran/ggplot2"><span style="">ggplot2</span></a><span style="color: #009900;">&#41;</span>
samp.mt <span style="">&lt;-</span> melt<span style="color: #009900;">&#40;</span>samp<span style="color: #339933;">,</span>id=<span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #009900;">&#41;</span>
gg <span style="">&lt;-</span> <a href="http://inside-r.org/packages/cran/ggplot"><span style="">ggplot</span></a><span style="color: #009900;">&#40;</span>samp.mt<span style="color: #339933;">,</span>aes<span style="color: #009900;">&#40;</span>x=value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="">+</span>
  geom_histogram<span style="color: #009900;">&#40;</span>binwidth=<span style="color: #cc66cc;">0.1</span><span style="color: #009900;">&#41;</span> <span style="">+</span>
  theme_bw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="">+</span>
  facet_wrap<span style="color: #009900;">&#40;</span><span style="">~</span>variable<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/scale"><span style="color: #003399; font-weight: bold;">scale</span></a>=<span style="color: #0000ff;">&quot;free&quot;</span><span style="color: #009900;">&#41;</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
<p>And here&#8217;s the resulting picture:<br />
<div id="attachment_799" class="wp-caption aligncenter" style="width: 460px"><a href="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/10/histogram.png"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/10/histogram.png" alt="Histogram of three parameters in a Monte Carlo sample" title="Histogram of three parameters in a Monte Carlo sample" width="450" height="300" class="size-full wp-image-799" /></a><p class="wp-caption-text">Histogram of three parameters in a Monte Carlo sample</p></div></p>
<p>Any suggestions on how to improve this function so that it has a more generic description of a distribution would be appreciated (e.g. for distributions with n!=2 parameters).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/r/sampling-for-monte-carlo-simulations-with-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Announcing Lectures, my first GitHub project</title>
		<link>http://www.jameskeirstead.ca/general/announcing-lectures-my-first-github-project/</link>
		<comments>http://www.jameskeirstead.ca/general/announcing-lectures-my-first-github-project/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 15:04:08 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[latex]]></category>
		<category><![CDATA[lectures]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=777</guid>
		<description><![CDATA[I've recently shifted from SVN to Git for version control and it's working great.  In fact, I've just finished my first project which lets you easily build slides and notes for lectures.]]></description>
			<content:encoded><![CDATA[<p>For the past couple years, I&#8217;ve been using SVN for version control.  It was my first introduction to these tools and it took a while to get the hang of branching, tagging, and resolving the dreaded conflicts.  But now it&#8217;s an integral part of my workflow and I can&#8217;t count the number of times it&#8217;s saved my digital bacon.</p>
<p>However I recently switched to <a href="http://github.com">GitHub</a> for some of my projects.  This was partly out of curiosity, as a number of prominent projects and coders seem to swear by it (<a href="http://github.com/twitter">Twitter</a>, <a href="https://github.com/hadley">Hadley Wickham</a>, <a href="https://github.com/kjhealy">Kieran Healy</a>).  But more practically, I didn&#8217;t want to be tied to a single server any more and the idea of a Git repository as a self-standing archive was very appealing.</p>
<p>I&#8217;ve now been using Git for a couple weeks and, again, I&#8217;ve had to figure out a new mystical language of push, pull, fetch, merge, etc.  If you&#8217;re familiar with SVN or CVS, the trickiest thing is trying to figure out the Git analogues of all your usual tricks.  <a href="http://ftp.newartisans.com/pub/git.from.bottom.up.pdf">Git from the bottom up</a> is a fantastic resource in this regard and provides a clear explanation of Git&#8217;s internal structure and how to work with it.  For more mundane, how-do-I-do-x type things, Github&#8217;s <a href="http://help.github.com/">Help</a> is very, umm, helpful.</p>
<p>Anyway, all that&#8217;s not really here nor there.  I mainly wanted to announce my first real Git project: <a href="https://github.com/jkeirstead/lectures">lectures</a>!  It&#8217;s a collection of LaTeX style files and Python glue to make the creation of both lecture notes and slides much easier.  If you&#8217;ve used <a href="https://bitbucket.org/rivanvx/beamer/wiki/Home">beamer</a>, you&#8217;ll know that you can build these two sets of documents from a single source file &ndash; provided you&#8217;ve got two separate header files.  With lectures, all you have to do is write a single source file and run a simple command:<br />
<code><br />
> build-lecture source style<br />
</code> </p>
<p>Out pops two PDF files, one for presentation and one with your full notes.  There is also support for some basic style configuration.  I&#8217;ve tried to keep this to a minimum, such as changing fonts (with XeLaTeX for OpenType support) and colours.</p>
<p>Have a go, <a href="https://github.com/jkeirstead/lectures">try the code</a>, and let me know what you think!</p>
<p>PS: I&#8217;ve just noticed that a similar project exists called <a href="http://faculty.chicagobooth.edu/donald.eisenstein/more/BeamerLecture.html">BeamerLecture</a>.  Looks like the main difference is that it produces four outputs, hiding answers in pre-class notes, which is a nice feature.  The formatting is closer to the beamer defaults though and it doesn&#8217;t use XeLaTeX for easy font changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/general/announcing-lectures-my-first-github-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getters and setters in R</title>
		<link>http://www.jameskeirstead.ca/r/getters-and-setters-in-r/</link>
		<comments>http://www.jameskeirstead.ca/r/getters-and-setters-in-r/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 14:56:26 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=757</guid>
		<description><![CDATA[<p>When I first started using R, one of the things that attracted me was <a href="http://cran.r-project.org/doc/manuals/R-lang.html#Object_002doriented-programming">its claim</a> to be an object-oriented programming (OOP) language.  Coming from a Java background, I was used to designing software with OOP concepts like <a href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">encapsulation </a>and <a href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)">inheritance</a> but, when I turned my hand to R, I quickly realized that &#8220;object-oriented&#8221; meant something subtlely different.  </p>
<p><a href="http://www.jameskeirstead.ca/r/getters-and-setters-in-r/" class="more-link">Read more on Getters and setters in R&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>When I first started using R, one of the things that attracted me was <a href="http://cran.r-project.org/doc/manuals/R-lang.html#Object_002doriented-programming">its claim</a> to be an object-oriented programming (OOP) language.  Coming from a Java background, I was used to designing software with OOP concepts like <a href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">encapsulation </a>and <a href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)">inheritance</a> but, when I turned my hand to R, I quickly realized that &#8220;object-oriented&#8221; meant something subtlely different.  </p>
<p>For those who are interested, the technical detail is explained at length in <a href="http://www.omegahat.org/OOP/oop.pdf">this paper</a> and <a href="http://www.johnmyleswhite.com/notebook/2009/12/13/the-most-basic-elements-of-object-oriented-programming-in-r/">this blog (with examples)</a>.  But what I want to do here is quickly illustrate how you can implement the common get/set method structure in R for a slightly different purpose.</p>
<p>In &#8220;traditional&#8221; OOP, you might have an object like a <code>Shape</code> with some attribute, say its area.  In Java, this attribute would be accessed and altered using <a href="http://en.wikipedia.org/wiki/Mutator_method">get and set methods</a>, as in:<br />
<code><br />
Shape s = new Shape();<br />
s.setArea(10);  // set the attribute value<br />
int area = s.getArea(); // get the attribute value<br />
</code></p>
<p>Using this pattern offers advantages like protecting access to the value of the <code>area</code> field and ensuring that only valid values are set.  While there is <a href="http://typicalprogrammer.com/?p=23">some</a> <a href="http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html">disagreement</a> about whether or not mutator methods represent good practice, every OOP coder must have had at least one occasion where a getter/setter was needed.</p>
<p>Which brings us back to R.  I recently had a problem where I wanted to use something like a getter/setter in order to access a global variable consistently, from both inside and outside functions.  Since <a href="http://cran.r-project.org/doc/manuals/R-lang.html#Scope-of-variables">variable scoping in R</a> isn&#8217;t very intuitive, I wasn&#8217;t sure how to do this at first and so I thought the get/set paradim might be helpful, even though the value I was getting and setting wasn&#8217;t really associated with an object.  </p>
<p>Here&#8217;s what I ended up using.  The trick is to create an explicit environment that stores the variable&#8217;s value behind the scenes, so that the user doesn&#8217;t have to worry about scope.</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Declare an explicit environment to hold the variable</span>
e1 <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/new.env"><span style="color: #003399; font-weight: bold;">new.env</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Sets the value of the variable</span>
setArea <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <a href="http://inside-r.org/r-doc/base/assign"><span style="color: #003399; font-weight: bold;">assign</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;area&quot;</span><span style="color: #339933;">,</span> value<span style="color: #339933;">,</span> env=e1<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Gets the value of the variable</span>
getArea <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/get"><span style="color: #003399; font-weight: bold;">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;area&quot;</span><span style="color: #339933;">,</span> e1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
<p>Again, this isn&#8217;t really object-oriented programming as I&#8217;m not manipulating an object (other than the environment <code>e1</code>) but the get/set pattern has an OOP pedigree.  So if you&#8217;re coming from a Java or C++ background and are having trouble figuring out variable scoping, the above code might be useful for you.  However if you want to do proper object-oriented getting and setting, I highly recommend John Myles White&#8217;s <a href="http://www.johnmyleswhite.com/notebook/2009/12/13/the-most-basic-elements-of-object-oriented-programming-in-r/">example</a> of R object-oriented <a href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming">polymorphism</a> in get/set methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/r/getters-and-setters-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Positive coefficient regression in R</title>
		<link>http://www.jameskeirstead.ca/r/positive-coefficient-regression-in-r/</link>
		<comments>http://www.jameskeirstead.ca/r/positive-coefficient-regression-in-r/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 15:13:34 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[energy]]></category>
		<category><![CDATA[Modelling]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=744</guid>
		<description><![CDATA[Ever have a regression model where the coefficients don't make sense?  I've been trying to predict electricity and gas consumption from daily activity schedules but a simple linear regression kept saying that demands should go <emph>down</emph> the more an activity is performed.  Fortunately I found the nnls package and show here how you can use it to ensure that your regression coefficients stay positive.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a paper about simulating urban demands for electricity and gas at 5 minute resolution.  To do this, I have a simple regression model that tries to explain observed consumption based on local population figures and simulated levels of activity demands (e.g. minutes spent at work, leisure, etc).  The data set looks like this, where each of the letters is an activity code:</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><span style="">&gt;</span> <a href="http://inside-r.org/r-doc/utils/head"><span style="color: #003399; font-weight: bold;">head</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">&#41;</span>
  zone  pop     <a href="http://inside-r.org/packages/cran/elec"><span style="">elec</span></a>       gas  A    B   <a href="http://inside-r.org/r-doc/base/I"><span style="color: #003399; font-weight: bold;">I</span></a>    J  L M     O   R    S     W
<span style="color: #cc66cc;">1</span>    <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">7412</span> <span style="color: #cc66cc;">46221768</span> <span style="color: #cc66cc;">124613714</span>  <span style="color: #cc66cc;">0</span>    <span style="color: #cc66cc;">0</span>   <span style="color: #cc66cc;">0</span>    <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span>     <span style="color: #cc66cc;">0</span>   <span style="color: #cc66cc;">0</span>    <span style="color: #cc66cc;">0</span>     <span style="color: #cc66cc;">0</span>
<span style="color: #cc66cc;">2</span>    <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">7428</span> <span style="color: #cc66cc;">37345875</span> <span style="color: #cc66cc;">100944002</span> <span style="color: #cc66cc;">60</span> <span style="color: #cc66cc;">3060</span> <span style="color: #cc66cc;">120</span> <span style="color: #cc66cc;">1020</span>  <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">4900</span> <span style="color: #cc66cc;">390</span>  <span style="color: #cc66cc;">510</span> <span style="color: #cc66cc;">28635</span>
<span style="color: #cc66cc;">3</span>    <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">7464</span> <span style="color: #cc66cc;">20914281</span>  <span style="color: #cc66cc;">64109628</span>  <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1155</span> <span style="color: #cc66cc;">510</span>  <span style="color: #cc66cc;">255</span>  <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">11000</span> <span style="color: #cc66cc;">225</span> <span style="color: #cc66cc;">2475</span> <span style="color: #cc66cc;">29580</span>
<span style="color: #cc66cc;">4</span>   <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">7412</span> <span style="color: #cc66cc;">46221768</span> <span style="color: #cc66cc;">124613714</span>  <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">680</span>   <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">390</span>  <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">5145</span>   <span style="color: #cc66cc;">0</span>    <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">9300</span>
<span style="color: #cc66cc;">5</span>   <span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">7128</span> <span style="color: #cc66cc;">69233086</span>  <span style="color: #cc66cc;">36611811</span>  <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">1335</span> <span style="color: #cc66cc;">105</span>  <span style="color: #cc66cc;">210</span> <span style="color: #cc66cc;">60</span> <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">5970</span>   <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">2520</span> <span style="color: #cc66cc;">14910</span>
<span style="color: #cc66cc;">6</span>   <span style="color: #cc66cc;">17</span> <span style="color: #cc66cc;">7608</span> <span style="color: #cc66cc;">40783190</span>  <span style="color: #cc66cc;">59343776</span>  <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">150</span>   <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">150</span>  <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span>  <span style="color: #cc66cc;">1500</span>   <span style="color: #cc66cc;">0</span>    <span style="color: #cc66cc;">0</span>   <span style="color: #cc66cc;">555</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
<p>I then performed a basic regression using <code>lm</code>, removing the intercept (the &#8220;- 1&#8243;) as I want the population coefficient to serve a similar purpose for this analysis:</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;">lm.elec <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">&#40;</span>elec<span style="">/</span><span style="color: #cc66cc;">365</span> <span style="">~</span> pop <span style="">+</span> A <span style="">+</span> B <span style="">+</span> <a href="http://inside-r.org/r-doc/base/I"><span style="color: #003399; font-weight: bold;">I</span></a> <span style="">+</span> J <span style="">+</span> L <span style="">+</span> M <span style="">+</span> O <span style="">+</span> R <span style="">+</span> S <span style="">+</span> W <span style="">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">&#41;</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
<p>Using Andrew Gelman&#8217;s helpful <a href="http://cran.r-project.org/web/packages/arm/">arm </a> package, I can get a quick overview of the result as shown below.  It doesn&#8217;t look too bad at first, but then I noticed all sorts of negative coefficients for the activity levels.  This makes no sense: when individuals perform activities such as going to work, going to school, or shopping, we expect that their demand for electricity should go up, not down.</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><span style="">&gt;</span> display<span style="color: #009900;">&#40;</span>lm.elec<span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a> = elec<span style="">/</span><span style="color: #cc66cc;">365</span> <span style="">~</span> pop <span style="">+</span> A <span style="">+</span> B <span style="">+</span> <a href="http://inside-r.org/r-doc/base/I"><span style="color: #003399; font-weight: bold;">I</span></a> <span style="">+</span> J <span style="">+</span> L <span style="">+</span> M <span style="">+</span> O <span style="">+</span> R <span style="">+</span>
    S <span style="">+</span> W <span style="">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> = <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">&#41;</span>
    coef.est coef.se
pop    <span style="color: #cc66cc;">13.99</span>     <span style="color: #cc66cc;">1.35</span>
A     <span style="color: #cc66cc;">177.07</span>    <span style="color: #cc66cc;">83.87</span>
B     <span style="">-</span><span style="color: #cc66cc;">63.90</span>    <span style="color: #cc66cc;">27.80</span>
<a href="http://inside-r.org/r-doc/base/I"><span style="color: #003399; font-weight: bold;">I</span></a>      <span style="">-</span><span style="color: #cc66cc;">0.80</span>    <span style="color: #cc66cc;">21.05</span>
J      <span style="color: #cc66cc;">91.27</span>    <span style="color: #cc66cc;">34.19</span>
L   <span style="">-</span><span style="color: #cc66cc;">1075.76</span>   <span style="color: #cc66cc;">391.53</span>
M      <span style="">-</span><span style="color: #cc66cc;">5.57</span>    <span style="color: #cc66cc;">73.50</span>
O      <span style="color: #cc66cc;">55.47</span>     <span style="color: #cc66cc;">9.90</span>
R    <span style="">-</span><span style="color: #cc66cc;">336.14</span>   <span style="color: #cc66cc;">178.98</span>
S      <span style="color: #cc66cc;">28.12</span>     <span style="color: #cc66cc;">3.84</span>
W      <span style="">-</span><span style="color: #cc66cc;">8.19</span>     <span style="color: #cc66cc;">3.16</span>
<span style="">---</span>
n = <span style="color: #cc66cc;">391</span><span style="color: #339933;">,</span> k = <span style="color: #cc66cc;">11</span>
residual <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a> = <span style="color: #cc66cc;">159994.17</span><span style="color: #339933;">,</span> R<span style="">-</span>Squared = <span style="color: #cc66cc;">0.65</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
<p>Since a linear regression is essentially an optimization problem, my immediate thought was: can I just constrain the coefficient values so that they are all positive?  This would mean that some activities might have no significant effect on consumption, but at least they couldn&#8217;t have a negative impact.  And it turns out, yes, you can do this using the <a href="http://cran.r-project.org/web/packages/nnls/">nnls </a> package and function.</p>
<p>The <code>nnls</code> function is not quite as user-friendly as <code>lm</code> so the first thing you have to do is manually define your input variable matrix and output vector.  For example:</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;">A <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/as.matrix"><span style="color: #003399; font-weight: bold;">as.matrix</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pop&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;A&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;B&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;I&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;J&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;L&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;M&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;O&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;R&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;S&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;W&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
b.elec <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="">$</span>elec<span style="">/</span><span style="color: #cc66cc;">365</span></pre>
</div>
</div>
<p>The analysis can then be run as:</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;">nnls.elec <span style="">&lt;-</span> <a href="http://inside-r.org/packages/cran/nnls"><span style="">nnls</span></a><span style="color: #009900;">&#40;</span>A<span style="color: #339933;">,</span>b.elec<span style="color: #009900;">&#41;</span></pre>
</div>
</div>
<p>Similarly, we can&#8217;t use <code>predict</code> to generate our results and have to manually perform the matrix multiplication.  This can be done as shown below.</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;">coef.elec <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a><span style="color: #009900;">&#40;</span>nnls.elec<span style="color: #009900;">&#41;</span>
pred.elec.nnls <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/as.vector"><span style="color: #003399; font-weight: bold;">as.vector</span></a><span style="color: #009900;">&#40;</span>coef.elec<span style="">%*%</span><a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a><span style="color: #009900;">&#40;</span>A<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre>
</div>
</div>
<p>This method also doesn&#8217;t give you an r<sup>2</sup> value per se, but you can estimate it with the following dummy regression:</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><span style="">&gt;</span> lm.elec.dummy <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">&#40;</span>b.elec<span style="">~</span>pred.elec.nnls <span style="">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="">&gt;</span> display<span style="color: #009900;">&#40;</span>lm.elec.dummy<span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a> = b.elec <span style="">~</span> pred.elec.nnls <span style="">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
               coef.est coef.se
pred.elec.nnls <span style="color: #cc66cc;">1.00</span>     <span style="color: #cc66cc;">0.04</span>
<span style="">---</span>
n = <span style="color: #cc66cc;">391</span><span style="color: #339933;">,</span> k = <span style="color: #cc66cc;">1</span>
residual <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a> = <span style="color: #cc66cc;">164861.13</span><span style="color: #339933;">,</span> R<span style="">-</span>Squared = <span style="color: #cc66cc;">0.62</span></pre>
</div>
</div>
<p>As you can see, the r<sup>2</sup> value is slightly lower in this case than the standard <code>lm</code> model but in terms of interpreting the coefficients the result makes much more sense.  This can be clearly seen in the following graph, where demands for electricity and gas rise during the day as expected, rather than sinking in the basic case.</p>
<div id="attachment_745" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/08/profiles.png"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/08/profiles.png" alt="Simulated profiles for electricity and gas, comparing lm and nnls regressions" title="Simulated profiles for electricity and gas, comparing lm and nnls regressions" width="500" height="800" class="size-full wp-image-745" /></a><p class="wp-caption-text">Simulated profiles for electricity and gas, comparing lm and nnls regressions</p></div>
<p>So there you go.  If you ever need to run a regression and ensure that all the coefficients are greater than or equal to zero, nnls is your friend.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/r/positive-coefficient-regression-in-r/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Slopegraphs in R</title>
		<link>http://www.jameskeirstead.ca/r/slopegraphs-in-r/</link>
		<comments>http://www.jameskeirstead.ca/r/slopegraphs-in-r/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 14:10:31 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[Rstats]]></category>
		<category><![CDATA[slopegraph]]></category>
		<category><![CDATA[tufte]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=733</guid>
		<description><![CDATA[The internet seems abuzz this week with the "discovery" of a long-lost Edward Tufte plot type: the slopegraph.  In this post, I'll show you how to create these elegant compact plots using R and ggplot2.]]></description>
			<content:encoded><![CDATA[<p>The internet seems abuzz this week with the &#8220;discovery&#8221; of a long-lost Edward Tufte plot type: the <a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0003nk&#038;topic_id=1">slopegraph</a>.  The full story is explained over at <a href="http://charliepark.org/slopegraphs/">Charlie Park&#8217;s website</a> and you can catch some of the debate about whether this is actually a novel plot type at <a href="http://www.stat.columbia.edu/~cook/movabletype/archives/2011/07/i_like_lineplot.html">Andrew Gelman&#8217;s blog</a>, <a href="http://www.metafilter.com/105431/Edward-Tuftes-Slopegraphs">Metafilter</a> or on <a href="http://twitter.com/#!/charliepark">Twitter</a>.  </p>
<p>But whatever you want to call, there&#8217;s no denying that these plots are an elegant and concise way to display trends in grouped data.  In this post, I&#8217;ll show you an attempt I&#8217;ve made at creating a slopegraph in R using ggplot.  Here&#8217;s the original figure from Tufte&#8217;s book, <a href="http://www.amazon.co.uk/gp/product/0961392177/ref=as_li_ss_tl?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=0961392177">Beautiful Evidence</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=&#038;l=as2&#038;o=2&#038;a=0961392177" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, to give you an idea of what we&#8217;re aiming for.</p>
<div id="attachment_734" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0003nk&#038;topic_id=1"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/0003nk-10292.gif" alt="Edward Tufte's slopegraph of cancer survival rates" title="Edward Tufte's slopegraph of cancer survival rates" width="500" height="1071" class="size-full wp-image-734" /></a><p class="wp-caption-text">Edward Tufte's slopegraph of cancer survival rates</p></div>
<p>First, we need to get some data.  The basic structure of a data set for this kind of plot is a <a href="http://duncanjg.wordpress.com/2008/12/09/reshaping-data-in-r/">long table</a> with columns for each group, measurement year, and measurement value.  In Tufte&#8217;s plot, the groups are various types of cancer, and the measure is the estimated survival rate (in %) taken at 5 year intervals.   You can find the raw data <a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0003nk&#038;topic_id=1">here</a> but I&#8217;ve already arranged it in the necessary format for you in the <a href="https://github.com/jkeirstead/r-slopegraph">Github project</a>. </p>
<p>A slopegraph is intended to &#8220;compare changes over time for a list of nouns located on an ordinal or interval scale&#8221;.  It does this by plotting the relative rank of each group at a given point in time, the measured value, and a line indicating the change in value between two measurement points.  At first glance, this should be a fairly easy plotting task especially with <a href="http://had.co.nz/ggplot2/">ggplot2</a> and the naive code might look something like this:</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><a href="http://inside-r.org/packages/cran/ggplot"><span style="">ggplot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span> aes<span style="color: #009900;">(</span>x=year<span style="color: #339933;">,</span>y=value<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a>=<a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="">+</span> geom_line<span style="color: #009900;">(</span><span style="color: #009900;">)</span></pre>
</div>
</div>
<p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>
<p>However this produces all sorts of nasty collisons, as there may be many labels plotted at similar values.  Tufte solves these sorts of problems by creating his figures largely by hand (well, with <a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=00000p">Illustrator</a>).  This gives full control over the typography, spacing and so on, but it&#8217;s not very reproduceable.  </p>
<p>With R, we can automate the plotting process although our aesthetic tweaks can only be approximate.  What I&#8217;ve done is to add an offset between each group at a given year to ensure that a certain amount of minimum spacing is maintained.  The algorithm isn&#8217;t perfect, as a value of x in a given year may be positioned higher than x + 1 in an adjacent year depending on the characteristics of each year&#8217;s data.  (For a particularly ugly example, look at the liver and intraheptic bile duct group in the plot below.)  Tufte&#8217;s plot also has this characteristic, but he arranges the lines so that they never cross: clearer to read but potentially confusing when making horizontal comparisons.</p>
<p>Once the data set is prepared, we need to plot it.  The first thing to do here is to create a new ggplot theme which strips away most of the plot decoration.  I did this by typing <code>theme_bw</code> at the command line, copying the resulting output, and modifying it to my needs.  We can then make the plot using standard ggplot commands.  The plot consists of (in plotting order):</p>
<ol>
<li>Lines linking each group</li>
<li>White points to provide a background for each number</li>
<li>Text labels for each numeric value</li>
<li>Text labels for the groups</li>
<li>A scale adjustment to ensure that the labels appear properly</li>
</ol>
<p>The final result is below.  It&#8217;s kind of a big plot, so CFB.</p>
<div id="attachment_735" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/slopegraph.png"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/slopegraph-640x1024.png" alt="R version of slopegraph for cancer survival rates" title="R version of slopegraph for cancer survival rates" width="500" height="800" class="size-large wp-image-735" /></a><p class="wp-caption-text">R version of slopegraph for cancer survival rates</p></div>
<p>I&#8217;ve put the <a href="https://github.com/jkeirstead/r-slopegraph">code and data up on Github</a> so feel free to fork it and tweak things to your liking.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/r/slopegraphs-in-r/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CHP planning restrictions and the efficiency of urban energy systems</title>
		<link>http://www.jameskeirstead.ca/general/chp-planning-restrictions-and-the-efficiency-of-urban-energy-systems/</link>
		<comments>http://www.jameskeirstead.ca/general/chp-planning-restrictions-and-the-efficiency-of-urban-energy-systems/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 16:04:51 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[CHP]]></category>
		<category><![CDATA[cities]]></category>
		<category><![CDATA[energy]]></category>
		<category><![CDATA[Modelling]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=728</guid>
		<description><![CDATA[We have a new paper out that looks at how planning restrictions on the use of combined heat and power might affect the overall energy efficiency of an urban energy system.  The method is the key innovation here, as we use a series of generic city designs together with an integer programming optimization model to identify some general design principles.]]></description>
			<content:encoded><![CDATA[<p>We have a new paper out in <em>Energy</em>:</p>
<blockquote><p>Cities account for approximately two-thirds of global primary energy consumption and have large heat and power demands. CHP (combined heat and power) systems offer significant primary energy-efficiency gains and emissions reductions, but they can have high upfront investment costs and create nuisance pollution within the urban environment. Urban planners therefore need to understand the tradeoffs between limitations on CHP plant size and the performance of the overall energy system. This paper uses a mixed-integer linear programming model to evaluate urban energy system designs for a range of city sizes and technology scenarios. The results suggest that the most cost-effective and energy-efficient scenarios require a mix of technology scales including CHP plants of appropriate size for the total urban demand. For the cities studied here (less than 200,000 people), planning restrictions that prevent the use of CHP technologies could lead to total system cost penalties of 2% (but with significantly different cost structures) and energy-efficiency penalties of up to 24% when measured against a boiler-only business-as-usual case.</p></blockquote>
<p>The performance of this particular model could be better, as several of the scenario results had large <a href="http://glossary.computing.society.informs.org/index.php?page=O.html#Optimality%20gap">solution gaps</a> which makes it difficult to see the underlying trends.  However I think the method we used, applying an integer programming model to design multiple energy systems for generic cities, is innovative and will be of interest to others.</p>
<p><span style="float: left; padding: 5px;"><a href="http://www.researchblogging.org"><img alt="ResearchBlogging.org" src="http://www.researchblogging.org/public/citation_icons/rb2_large_gray.png" style="border:0;"/></a></span><span class="Z3988" title="ctx_ver=Z39.88-2004&#038;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&#038;rft.jtitle=Energy&#038;rft_id=info%3Adoi%2F10.1016%2Fj.energy.2011.06.011&#038;rfr_id=info%3Asid%2Fresearchblogging.org&#038;rft.atitle=The+impact+of+CHP+%28combined+heat+and+power%29+planning+restrictions+on+the+efficiency+of+urban+energy+systems&#038;rft.issn=03605442&#038;rft.date=2011&#038;rft.volume=&#038;rft.issue=&#038;rft.spage=&#038;rft.epage=&#038;rft.artnum=http%3A%2F%2Flinkinghub.elsevier.com%2Fretrieve%2Fpii%2FS0360544211003884&#038;rft.au=Keirstead%2C+J.&#038;rft.au=Samsatli%2C+N.&#038;rft.au=Shah%2C+N.&#038;rft.au=Weber%2C+C.&#038;rfe_dat=bpr3.included=1;bpr3.tags=Computer+Science+%2F+Engineering%2CMathematics%2CEcology+%2F+Conservation%2CEnergy%2C+Chemical+Engineering%2C+Combinatorics">Keirstead, J., Samsatli, N., Shah, N., &#038; Weber, C. (2011). The impact of CHP (combined heat and power) planning restrictions on the efficiency of urban energy systems <span style="font-style: italic;">Energy</span> DOI: <a rev="review" href="http://dx.doi.org/10.1016/j.energy.2011.06.011">10.1016/j.energy.2011.06.011</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/general/chp-planning-restrictions-and-the-efficiency-of-urban-energy-systems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Britain&#8217;s Low Carbon Rush Hour</title>
		<link>http://www.jameskeirstead.ca/general/britains-low-carbon-rush-hour/</link>
		<comments>http://www.jameskeirstead.ca/general/britains-low-carbon-rush-hour/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 14:09:35 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[carbon]]></category>
		<category><![CDATA[energy]]></category>
		<category><![CDATA[transport]]></category>
		<category><![CDATA[UK]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=717</guid>
		<description><![CDATA[Do you cycle to work?  How about drive the kids to school?  Or maybe hop a bus downtown for an early morning coffee with a friend?  Whatever it is you're doing, these trips all contribute to the carbon footprint of Britain's rush hour. Here's a closer look at some of the results from our recent study of commuting patterns in Britain.]]></description>
			<content:encoded><![CDATA[<p>Do you cycle to work?  How about drive the kids to school?  Or maybe hop a bus downtown for an early morning coffee with a friend?  Whatever it is you&#8217;re doing, these trips all contribute to the carbon footprint of Britain&#8217;s rush hour. </p>
<p>I recently analysed these emissions as part of a research project for EDF Energy.  The idea was to assess the carbon footprint of rush hour, looking at regional variations in distance travelled, transport mode, and total emissions. If you&#8217;re interested in the official results, the study was covered in the <a href="http://www.thisislondon.co.uk/standard/article-23966881-walk-ride-or-cycle---the-greener-ways-to-get-to-work-in-london.do">Evening Standard</a>, <a href="http://www.independent.co.uk/environment/green-living/a-lowcarbon-rushhour-2306487.html">Independent</a>, <a href="http://www.businessgreen.com/bg/news/2083685/london-commuters-lowest-emissions-reveals-survey">Business Green</a> and a few blogs, and here&#8217;s the <a href="http://www.edfenergy.com/media-centre/press-news/Commuters-in-the-capital-found-to-release-the-lowest-rush-hour-carbon-emissions-of-any-region-in-the-UK.shtml">official press release</a>.  But I wanted to add a few supplementary results here.<sup>*</sup>  </p>
<p>To calculate the carbon emissions of rush hour, I used data from the <a href="http://www.esds.ac.uk/findingData/ntsTitles.asp">2008 UK National Travel Survey</a> and tallied up all the trips between 700&ndash;1000 and 1600&ndash;1900.  Each trip can be characterized by its distance, purpose, the transport mode used, the location of the trip, and many other attributes.  This was the first time I&#8217;d used this data set and I was surprised to see just how much information is collected.  </p>
<p>For me, the most interesting result was to see how distinct Greater London is from the other British regions.  Look at the figures below: loads of public transport, lots of commuting travel, and surprisingly high ownership of gas-guzzlers (band J and above).</p>
<div id="attachment_719" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/mode.png"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/mode.png" alt="Modal share of rush-hour trips" title="Modal share of rush-hour trips" width="500" height="350" class="size-full wp-image-719" /></a><p class="wp-caption-text">Modal share of rush-hour trips.  Private transport in red, public transport in blue.</p></div>
<div id="attachment_720" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/purpose.png"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/purpose.png" alt="Purpose of rush-hour trips" title="Purpose of rush-hour trips" width="500" height="350" class="size-full wp-image-720" /></a><p class="wp-caption-text">Purpose of rush-hour trips</p></div>
<div id="attachment_721" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/gasguzz1.png"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/gasguzz1.png" alt="Vehicle ownership by area type" title="Vehicle ownership by area type" width="500" height="350" class="size-full wp-image-721" /></a><p class="wp-caption-text">Vehicle ownership by area type</p></div>
<p>Another useful plot is the distribution of activities by time of day.  You can see here that the evening rush &#8220;hour&#8221; is more dispersed than the morning rush, with the afternoon school-run starting around 3 pm and heavy travel continuing until about 7 o&#8217;clock.  </p>
<div id="attachment_722" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/timing.png"><img src="http://www.jameskeirstead.ca/wordpress/wp-content/uploads/2011/07/timing.png" alt="Distribution of daily trips, shaded by purpose" title="Distribution of daily trips, shaded by purpose" width="500" height="350" class="size-full wp-image-722" /></a><p class="wp-caption-text">Distribution of daily trips, shaded by purpose</p></div>
<p>It&#8217;s a rich data source and we&#8217;ve only scratched the surface.  Hopefully we can do some follow-up research to look in detail at variety of trips and their suitability to different low carbon transport options.</p>
<p><sup>*</sup> Of course, these additional comments are my own opinions and have nothing to do with EDF.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/general/britains-low-carbon-rush-hour/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Review: The God Species by Mark Lynas</title>
		<link>http://www.jameskeirstead.ca/general/review-the-god-species-by-mark-lynas/</link>
		<comments>http://www.jameskeirstead.ca/general/review-the-god-species-by-mark-lynas/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 09:38:45 +0000</pubDate>
		<dc:creator>James Keirstead</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[book review]]></category>
		<category><![CDATA[climate-change]]></category>

		<guid isPermaLink="false">http://www.jameskeirstead.ca/?p=681</guid>
		<description><![CDATA[Mark Lynas's new book is an excellent introduction to a pragmatic view of environmental problems.  But it also contains a tale of how the environmental movement uses science in its campaigns, a story that deserves to be told at greater length.]]></description>
			<content:encoded><![CDATA[<p>In 1959, the mathematician and philosopher <a href="http://en.wikipedia.org/wiki/Bertrand_Russell">Bertrand Russell</a> was interviewed for the BBC programme &#8220;<a href="http://en.wikipedia.org/wiki/Face_to_Face_%28British_TV_series%29">Face-to-Face</a>&#8220;.  At the end of the discussion, the interviewer posed one last question: &#8220;What would you think it&#8217;s worth telling [future generations] about the life you&#8217;ve lived and the lessons you&#8217;ve learned from it?&#8221;  Russell&#8217;s answer came in two parts, one moral &ndash; about the need to love one another in an increasingly interconnected world &ndash; and one intellectual:</p>
<blockquote><p>
&#8220;When you are studying any matter, or considering any philosophy, ask yourself only what are the facts and what is the truth that the facts bear out.  Never let yourself be diverted either by what you wish to believe or by what you think could have beneficient social effects if it were believed.  But look only and solely at what are the facts.&#8221;
</p></blockquote>
<p><iframe width="425" height="349" src="http://www.youtube.com/embed/L7I9pgqiLo0#t=07m38s" frameborder="0" allowfullscreen></iframe></p>
<p>At first glance Mark Lynas&#8217;s new book, <a href="http://www.amazon.co.uk/gp/product/000731342X/ref=as_li_ss_tl?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=000731342X">The God Species: How the Planet Can Survive the Age of Humans</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=&#038;l=as2&#038;o=2&#038;a=000731342X" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, has nothing to do with Professor Russell.  It is ostensibly about planetary boundaries, the nine physical limits which humanity must respect if we are to maintain our current quality of life on Earth.  These boundaries &ndash; in biodiversity, climate, nitrogen, land use, freshwater, toxics, aerosols, ocean acidification, and the ozone layer &ndash; are well-described with clear explanations of the relevant science, the implications of violating the boundary, and the technologies and policies that could help us avoid crossing these limits.  Lynas also draws out the links between the boundaries, for example between climate change and ocean acidification, to illustrate both the win-win benefits of certain mitigation options and conversely the negative feedbacks that might occur as boundaries are crossed.  There are a few minor presentational niggles &ndash; a comparative sentence that uses two units for area, a throwaway reference to declining wood consumption without accounting for fuel substitution effects, the subtitle (do we care about the planet itself or our life on it?) &ndash; and like many other popular science books, editors seem to be adverse to including graphs even when something like the <a href="http://www.esrl.noaa.gov/gmd/ccgg/trends/">Mauna Lua CO2 timeseries</a> with the 350 ppm threshold marked would be a concise illustration of the climate boundary.  But overall it&#8217;s an excellent introduction to an emerging and important way of viewing global environmental issues.  </p>
<p>But in many ways, the real story of <em>The God Species</em> is Lynas&#8217;s re-discovery Russell&#8217;s maxim (knowingly or not).  Lynas has been a devoted environmental campaigner for years and throughout the text, he is at pains to point out that the planetary boundaries view of environmental problems means adopting solutions that mainstream environmental groups may find hard to swallow.  For example, discussions on the potential of nuclear power, genetic engineering, or geo-engineering include lines such as &#8220;I know many environmentalists will read all this with a sense of growing horror.  A technofix! How outrageous! What a cop-out!&#8221;.   While Lynas&#8217;s pragmatic view is to be commended, I found the &#8220;you&#8217;re not going to like what I have to tell you&#8230;&#8221; tone somewhat distracting.  It felt as though the reader had missed out on a long and tormented backstory, like catching a bitter passing aside between two ex-lovers at a party.</p>
<p><em>The God Species</em>, then, has the feeling of two strong themes shoe-horned into one volume.  The first subject, the science of planetary boundaries, is fascinating and the author brings these issues to the attention of a wider audience with a deft touch.  But a longer treatment would permit a more detailed discussion of the science and the suggested solutions.  For example, the discussion on the sustainability of economic growth would benefit from reference to Bill Nordhaus, William Ayres and others in the resource economics literature.  Similarly, when assessing the role of privatisation in the delivery of freshwater, the choice is presented as a fairly strict dichotomy between &#8220;efficient&#8221; private and &#8220;inefficient&#8221; public provision without really getting into the role of institutions and regulations to ensure that either system works effectively.  More detail on the science of how planetary boundaries are identified and monitored could also be valuable.</p>
<p>The second theme, on the environmental movement&#8217;s selective use of scientific data, also deserves more attention.  Lynas hints at this in the last chapter, providing a fascinating eyewitness account of the Copenhagen climate negotiations (see also this <a href="http://www.guardian.co.uk/environment/2009/dec/22/copenhagen-climate-change-mark-lynas">Guardian piece</a>), but a more detailed analysis of how environmental groups have painted themselves into corners on climate change and other issues by arguing against politically feasible outcomes for the sake of environmental purism would be literary and political dynamite.  </p>
<p>But perhaps that&#8217;s something for Lynas&#8217;s next project.  On its own terms, <em>The God Species</em> is an excellent introduction to a new pragmatic way of looking at the environment, one that as Russell noted is supported only by the facts and &#8220;the truth that the facts bear out&#8221;.</p>
<p><em>‘The God Species’ by Mark Lynas is published by Fourth Estate on July 7th and is available for pre-order from <a href="http://www.amazon.co.uk/gp/product/000731342X/ref=as_li_ss_tl?ie=UTF8&#038;tag=smallisbeauti-21&#038;linkCode=as2&#038;camp=1634&#038;creative=19450&#038;creativeASIN=000731342X">Amazon.co.uk</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jameskeirstead.ca/general/review-the-god-species-by-mark-lynas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

