<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace V5 Site Server v5.13.159 (http://www.squarespace.com) on Thu, 23 May 2013 03:06:31 GMT--><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Frank Davenport's Blog on R, Statistics, and all Things Spatial</title><link>http://www.frankdavenport.com/blog/</link><description></description><lastBuildDate>Wed, 20 Jun 2012 00:45:10 +0000</lastBuildDate><copyright></copyright><language>en-US</language><generator>Squarespace V5 Site Server v5.13.159 (http://www.squarespace.com)</generator><item><title>Notes from A Recent Spatial R Class I Gave</title><category>R</category><category>R</category><category>Spatial</category><category>ggplot2</category><dc:creator>Frank Davenport</dc:creator><pubDate>Wed, 20 Jun 2012 00:31:28 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/6/19/notes-from-a-recent-spatial-r-class-i-gave.html</link><guid isPermaLink="false">709683:8312210:16842943</guid><description><![CDATA[<p style="text-align: justify;">Below is a <a href="https://dl.dropbox.com/u/9577903/broomspatial.pdf">link to a pdf </a>(compiled with the amazing <a href="http://yihui.name/knitr/">knitr</a> package) and some accompanying <a href="https://dl.dropbox.com/u/9577903/data.zip">data</a> for a recent short course I gave on basic spatial data import/analysis/visualization in R. The class was only two hours and some of the participants were being exposed to R for the first time so the material is limited. The class was a follow up to a previous one I did on ArcGIS. The idea was to show how to perform the same functions in R and ArcGIS and then let users decide which worked best for them (I use R for about 90% of my spatial analysis and data handling but find ArcGIS (or some GUI based GIS) pretty essential for that last 10%).&nbsp;</p>
<p style="text-align: justify;">The content/main points of the course:</p>
<ul>
<li>Basic intro to R</li>
<li>Reading in a Shapefile</li>
<li>Doing a table join with a shapefile and a data.frame</li>
<li>Generating random points</li>
<li>Doing a point in polygon spatial join</li>
<li>Reading and cropping raster data</li>
<li>Doing a pixel in polygon spatial join (using extract() from the raster pacakge)</li>
<li>Plotting and annotating a shapefile in ggplot2</li>
<li>Making panel maps in ggplot2</li>
</ul>
<p style="text-align: justify;">The <a href="https://dl.dropbox.com/u/9577903/broomspatial.pdf">notes</a> contain all the R-code and a lot of grammatical and spelling errors. I hope to improve and update this class over time. Please let me know if:</p>
<ol>
<li>You find the material useful</li>
<li>You use it/modify for a class of your own (and please share the results)</li>
<li>You have any major suggestions for improvements (I'm aware of most of the minor issues)</li>
</ol>
<p>&nbsp;</p><p></p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-16842943.xml</wfw:commentRss></item><item><title>The Epic Search for the Perfect R Text Editor</title><category>R</category><dc:creator>Frank Davenport</dc:creator><pubDate>Wed, 09 May 2012 01:39:00 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/5/8/the-epic-search-for-the-perfect-r-text-editor.html</link><guid isPermaLink="false">709683:8312210:16182113</guid><description><![CDATA[<p>&nbsp;</p>
<p>I can never seem to get exactly what I want from an R text editor. Let me correct that, I can never seem to get exactly what I want from an R text editor on a MAC. I used to use <a href="http://www.sciviews.org/Tinn-R/ ">Tinn-R</a>&nbsp; which met most&nbsp; my needs:</p>
<ul>
<li>Free,lightweight with an easy install and a slim UI </li>
<li>Good commenting and code folding tools</li>
<li> Customizable short cuts</li>
<li>Support for multiple monitors</li>
<li>An R console that was separate from the editor</li>
<li>A flexible, simple, and powerful Find and Replace Interface</li>
<li>&nbsp;Good SWeave support with some built in LaTeX tools</li>
</ul>
<ul>
</ul>
<p>&nbsp;</p>
<p>When I made the switch to Mac I went with <a href="http://www.walware.de/goto/statet">StatET</a>&nbsp; on <a href="http://www.eclipse.org/">Eclipse</a>&nbsp;&nbsp; StatET has more or less all those things, but it is not simple or straightforward (or maybe just Eclipse is too complex for me). It also has somewhat lousy graphics support and does not allow for fuzzy help searches (??&rsquo;help&rsquo;). However, it worked so I&rsquo;ve been using it. But I just upgraded to R 2.15 and the built in graphics driver (and a few other options) no longer work. &nbsp;I was never that attached to StatET anyway because the Eclipse environment is just too complex for my needs.</p>
<p>&nbsp;</p>
<p>I really like <a href="http://rstudio.org/">R Studio</a>&nbsp;<a href="http://rstudio.org/"></a> but it&rsquo;s missing some very key features:</p>
<ul>
<li> The ability to Find and Replace on a selection, rather than the whole document, plus general &lsquo;fancy&rsquo; text editing find and replace tools</li>
<li>Multiple monitor support (I&rsquo;d like to be able to detach one of the panes and move them to another monitor)</li>
<li>Customizable shortcuts</li>
<li>The ability to restart the R console without restarting the editor (this I can live without, but it&rsquo;s nice to have)</li>
</ul>
<ul>
</ul>
<p>&nbsp;</p>
<p>So what else is out there? Maybe it&rsquo;s time to try TextMate? In general I prefer free and cross platform, but I can make exceptions.</p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-16182113.xml</wfw:commentRss></item><item><title>R in the CPB and the role of Open Source in Promoting Transparency and Austerity</title><category>R</category><category>open source</category><dc:creator>Frank Davenport</dc:creator><pubDate>Fri, 13 Apr 2012 01:37:10 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/4/12/r-in-the-cpb-and-the-role-of-open-source-in-promoting-transp.html</link><guid isPermaLink="false">709683:8312210:15822182</guid><description><![CDATA[<p style="text-align: justify;">The <a href="http://blog.revolutionanalytics.com/2012/04/r-at-the-consumer-financial-protection-bureau.html">Revolution blog</a> links to an <a href="http://radar.oreilly.com/2012/04/open-source-government-cfpb.html">O'Reilly&nbsp; interview </a>with two CIO's from the Consumer Protection Bureau. The gist of the interview is "Open Source is Great, we are using it for everything; R and Big Data are the next hot thing, et cetera".&nbsp; I don't mean to belittle those points, as I mostly agree them, but they are covered well in both the Revolution post and the original interview.&nbsp; However what is explicitly missing from the interview (but implied by the tone) are how Open Source technologies directly answer two recent and recurring demands of government: Transparency and Austerity. At at a time when most of the electorate is calling for more transparent government (and at least in public, government seems to agree) and maybe 60% of the electorate wants less government spending open source is an obvious answer. Open source tools do not directly make government more transparent (I think legislation, institutional/cultural change are the real drivers), but at least partially addresses it: If government reports and official statistics are produced in R, for example,&nbsp; and the code posted on <a href="https://github.com/">git</a> (as is suggested by the interview) then that allows the numeric public to cross check the nitty gritty details of whatever assumptions, models, et cetera go into those figures.<br /><br />The more obvious benefit is austerity. Especially with regards to spatial software, contracts with private vendors are huge (but I have no idea what portion of government spending goes into it). Switching to open source, at least during times of austerity would not only save money but also force some competition into the relative monopolies held by ESRI, SAS, and others.&nbsp; When I have some more time in the future I'll try to back those assertions regarding government spending on private vendor software contracts with some hard data.</p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-15822182.xml</wfw:commentRss></item><item><title>R Structure Explained</title><category>R</category><dc:creator>Frank Davenport</dc:creator><pubDate>Thu, 05 Apr 2012 05:43:23 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/4/5/r-structure-explained.html</link><guid isPermaLink="false">709683:8312210:15728889</guid><description><![CDATA[<p><a href="http://obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/">This post by Suraj Gupta</a> explains it all. This is the first time I have seen a&nbsp; concise and accessible explanation of the R environment structure and why it matters.</p>
<p>&nbsp;</p>
<p>Addendum: <a href="http://digitheadslabnotebook.blogspot.com/2011/06/environments-in-r.html">This one by Digithead</a> is also pretty good</p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-15728889.xml</wfw:commentRss></item><item><title>Plotting forecast() objects in ggplot part 2: Visualize Observations, Fits, and Forecasts</title><category>R</category><dc:creator>Frank Davenport</dc:creator><pubDate>Thu, 22 Mar 2012 00:12:10 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/3/21/plotting-forecast-objects-in-ggplot-part-2-visualize-observa.html</link><guid isPermaLink="false">709683:8312210:15535505</guid><description><![CDATA[<p style="text-align: justify;">In <a href="http://davenportspatialanalytics.squarespace.com/blog/2012/3/14/plotting-forecast-objects-in-ggplot-part-1-extracting-the-da.html">my last post</a> I presented a function for extracting data from a forecast() object and formatting the data so that it can be plotted in ggplot.&nbsp; The scenario is that you are fitting a model to a time series object with training data, then forecasting out, and then visually evaluating the fit with the observations that your forecast tried to duplicate. Then you want a plot that includes: the original observations, the fitted values, the forecast values, and the observations in the forecast period. The function I presented in the last post extracts all that information in a nice ggplot ready data.frame. In this post I simulate data from an Arima process, fit an incorrect model, use the function from the last post to extract the data, and then plot in ggplot.</p>
<p>&nbsp;</p>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#----------Simulate an Arima (2,1,1) Process-------------</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;">(</span><a href="http://inside-r.org/packages/cran/forecast"><span>forecast</span></a><span style="color: #009900;">)</span><br /><br />set.seed(1234)&nbsp;&nbsp;
y<span>&lt;-</span><a href="http://inside-r.org/r-doc/stats/arima.sim"><span style="color: #003399; font-weight: bold;">arima.sim</span></a><span style="color: #009900;">(</span>model=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/ar"><span style="color: #003399; font-weight: bold;">ar</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">.3</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>ma=<span style="color: #cc66cc;">0.3</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>n=<span style="color: #cc66cc;">144</span><span style="color: #009900;">)</span>
y<span>&lt;-</span><a href="http://inside-r.org/r-doc/stats/ts"><span style="color: #003399; font-weight: bold;">ts</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span>freq=<span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/start"><span style="color: #003399; font-weight: bold;">start</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2000</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#-- Extract Training Data, Fit the Wrong Model, and Forecast</span>
yt<span>&lt;-</span><a href="http://inside-r.org/r-doc/stats/window"><span style="color: #003399; font-weight: bold;">window</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/end"><span style="color: #003399; font-weight: bold;">end</span></a>=<span style="color: #cc66cc;">2009.99</span><span style="color: #009900;">)</span>
&nbsp;
yfit<span>&lt;-</span>Arima<span style="color: #009900;">(</span>yt<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
&nbsp;
yfor<span>&lt;-</span><a href="http://inside-r.org/packages/cran/forecast"><span>forecast</span></a><span style="color: #009900;">(</span>yfit<span style="color: #009900;">)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#---Extract the Data for ggplot using funggcast()</span>
pd<span>&lt;-</span>funggcast<span style="color: #009900;">(</span>y<span style="color: #339933;">,</span>yfor<span style="color: #009900;">)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#---Plot in ggplot2 0.9</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;">(</span><a href="http://inside-r.org/packages/cran/ggplot2"><span>ggplot2</span></a><span style="color: #009900;">)</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;">(</span>scales<span style="color: #009900;">)</span>
&nbsp;
&nbsp;
p1a<span>&lt;-</span><a href="http://inside-r.org/packages/cran/ggplot"><span>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>=pd<span style="color: #339933;">,</span>aes<span style="color: #009900;">(</span>x=<a href="http://inside-r.org/r-doc/base/date"><span style="color: #003399; font-weight: bold;">date</span></a><span style="color: #339933;">,</span>y=observed<span style="color: #009900;">)</span><span style="color: #009900;">)</span> 
p1a<span>&lt;-</span>p1a<span>+</span>geom_line<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: #0000ff;">'red'</span><span style="color: #009900;">)</span>
p1a<span>&lt;-</span>p1a<span>+</span>geom_line<span style="color: #009900;">(</span>aes<span style="color: #009900;">(</span>y=<a href="http://inside-r.org/r-doc/stats/fitted"><span style="color: #003399; font-weight: bold;">fitted</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: #0000ff;">'blue'</span><span style="color: #009900;">)</span>
p1a<span>&lt;-</span>p1a<span>+</span>geom_line<span style="color: #009900;">(</span>aes<span style="color: #009900;">(</span>y=<a href="http://inside-r.org/packages/cran/forecast"><span>forecast</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span>+</span>geom_ribbon<span style="color: #009900;">(</span>aes<span style="color: #009900;">(</span>ymin=lo95<span style="color: #339933;">,</span>ymax=hi95<span style="color: #009900;">)</span><span style="color: #339933;">,</span>alpha=<span style="color: #cc66cc;">.25</span><span style="color: #009900;">)</span>
p1a<span>&lt;-</span>p1a<span>+</span>scale_x_date<span style="color: #009900;">(</span>name=<span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>breaks=<span style="color: #0000ff;">'1 year'</span><span style="color: #339933;">,</span>minor_breaks=<span style="color: #0000ff;">'1 month'</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=date_format<span style="color: #009900;">(</span><span style="color: #0000ff;">"%b-%y"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/Matrix/expand"><span style="color: #003399; font-weight: bold;">expand</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
p1a<span>&lt;-</span>p1a<span>+</span>scale_y_continuous<span style="color: #009900;">(</span>name=<span style="color: #0000ff;">'Units of Y'</span><span style="color: #009900;">)</span>
p1a<span>&lt;-</span>p1a<span>+</span>opts<span style="color: #009900;">(</span>axis.text.x=theme_text<span style="color: #009900;">(</span>size=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/graphics/title"><span style="color: #003399; font-weight: bold;">title</span></a>=<span style="color: #0000ff;">'Arima Fit to Simulated Data<span style="color: #000099; font-weight: bold;">\n</span> (black=forecast, blue=fitted, red=data, shadow=95% conf. interval)'</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#p1a</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p>&nbsp;</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://www.frankdavenport.com/storage/Blog_Forecastggplot.png?__SQUARESPACE_CACHEVERSION=1332375898773" alt="" /></span></span></p>
<p>&nbsp;</p><p><br/></p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-15535505.xml</wfw:commentRss></item><item><title>Plotting forecast() objects in ggplot part 1: Extracting the Data</title><category>R</category><category>forecasting</category><dc:creator>Frank Davenport</dc:creator><pubDate>Wed, 14 Mar 2012 04:17:32 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/3/14/plotting-forecast-objects-in-ggplot-part-1-extracting-the-da.html</link><guid isPermaLink="false">709683:8312210:15425051</guid><description><![CDATA[<p style="text-align: justify;">Lately I've been using <a href="http://robjhyndman.com/">Rob J Hyndman</a>'s excellent <a href="http://robjhyndman.com/software/forecast/">forecast</a> package. The package comes with some built in plotting functions but I found I wanted to customize and make my own plots in ggplot. In order to do that, I need a generalizable function that will extract all the data I want (forecasts, fitted values, training data, actual observations in the forecast period, confidence intervals, et cetera) and place it into a data.frame with a properly formatted date field (ie, not&nbsp; a ts() object).</p>
<p style="text-align: justify;">The function below does all that and should work for any forecast object (though I've only tested it on Arima() outputs). The only arguments it takes are the original observations and the forecast object (whatever results from calling forecast()). In my next post I'll give some examples of plotting the results using ggplot and explain why I wanted more than the default plot.forecast() function.</p>
<p>&nbsp;</p>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#--Produces a data.frame with the Source Data+Training Data, Fitted Values+Forecast Values, forecast data Confidence Intervals</span>
funggcast<span>&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;">(</span>dn<span style="color: #339933;">,</span>fcast<span style="color: #009900;">)</span><span style="color: #009900;">{</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;">(</span><a href="http://inside-r.org/packages/cran/zoo"><span>zoo</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#needed for the 'as.yearmon()' function</span>
&nbsp;
	en<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/time"><span style="color: #003399; font-weight: bold;">time</span></a><span style="color: #009900;">(</span>fcast<span>$</span>mean<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#extract the max date used in the forecast</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Extract Source and Training Data</span>
	ds<span>&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;">(</span><a href="http://inside-r.org/r-doc/stats/window"><span style="color: #003399; font-weight: bold;">window</span></a><span style="color: #009900;">(</span>dn<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/end"><span style="color: #003399; font-weight: bold;">end</span></a>=en<span style="color: #009900;">)</span><span style="color: #009900;">)</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;">(</span>ds<span style="color: #009900;">)</span><span>&lt;-</span><span style="color: #0000ff;">'observed'</span>
	ds<span>$</span>date<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/as.Date"><span style="color: #003399; font-weight: bold;">as.Date</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/time"><span style="color: #003399; font-weight: bold;">time</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/window"><span style="color: #003399; font-weight: bold;">window</span></a><span style="color: #009900;">(</span>dn<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/end"><span style="color: #003399; font-weight: bold;">end</span></a>=en<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Extract the Fitted Values (need to figure out how to grab confidence intervals)</span>
	dfit<span>&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;">(</span>fcast<span>$</span>fitted<span style="color: #009900;">)</span>
	dfit<span>$</span>date<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/as.Date"><span style="color: #003399; font-weight: bold;">as.Date</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/time"><span style="color: #003399; font-weight: bold;">time</span></a><span style="color: #009900;">(</span>fcast<span>$</span>fitted<span style="color: #009900;">)</span><span style="color: #009900;">)</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;">(</span>dfit<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span>&lt;-</span><span style="color: #0000ff;">'fitted'</span>
&nbsp;
	ds<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>ds<span style="color: #339933;">,</span>dfit<span style="color: #339933;">,</span>all.x=T<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#Merge fitted values with source and training data</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Exract the Forecast values and confidence intervals</span>
	dfcastn<span>&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;">(</span>fcast<span style="color: #009900;">)</span>
	dfcastn<span>$</span>date<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/as.Date"><span style="color: #003399; font-weight: bold;">as.Date</span></a><span style="color: #009900;">(</span>as.yearmon<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/row.names"><span style="color: #003399; font-weight: bold;">row.names</span></a><span style="color: #009900;">(</span>dfcastn<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</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;">(</span>dfcastn<span style="color: #009900;">)</span><span>&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;">(</span><span style="color: #0000ff;">'forecast'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'lo80'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'hi80'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'lo95'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'hi95'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'date'</span><span style="color: #009900;">)</span>
&nbsp;
	pd<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>ds<span style="color: #339933;">,</span>dfcastn<span style="color: #339933;">,</span>all.x=T<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#final data.frame for use in ggplot</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;">(</span>pd<span style="color: #009900;">)</span>
&nbsp;
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p>&nbsp;</p><p></p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-15425051.xml</wfw:commentRss></item><item><title>Property rights and the economic origins of the Sicilian mafia</title><category>Crime</category><category>Economics</category><category>Spatial</category><dc:creator>Frank Davenport</dc:creator><pubDate>Mon, 12 Mar 2012 18:59:14 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/3/12/property-rights-and-the-economic-origins-of-the-sicilian-maf.html</link><guid isPermaLink="false">709683:8312210:15402944</guid><description><![CDATA[<p style="text-align: justify;"><a href="http://ftalphaville.ft.com/blog/2012/03/12/918741/property-rights-and-the-economic-origins-of-the-sicilian-mafia/">This</a> is fascinating. Originally posted on <a href="http://www.ft.com/home/us">FT Alphaville</a>, the paper seems like a great example of work that intersects econ, physical geography, and political science.&nbsp; I'll just post the link for now, and revisit once I've had time to read the actual paper.</p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-15402944.xml</wfw:commentRss></item><item><title>Recovering Marginal Effects and Standard Errors of Interactions Terms Pt. II: Implement and Visualize</title><category>R</category><category>Stats</category><dc:creator>Frank Davenport</dc:creator><pubDate>Sat, 10 Mar 2012 01:36:52 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/3/9/recovering-marginal-effects-and-standard-errors-of-interacti.html</link><guid isPermaLink="false">709683:8312210:15371380</guid><description><![CDATA[<p style="text-align: justify;">In the last post I presented a function for recovering marginal effects of interaction terms. Here we implement the function with simulated data and plot the results using ggplot2. &nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#---Simulate Data and Fit a linear model with an interaction term</span>
y<span>&lt;-</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
x<span>&lt;-</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
d<span>&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;">(</span>y=y<span style="color: #339933;">,</span>x=x<span style="color: #339933;">,</span>fac=<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #000000; font-weight: bold;">letters</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span>:</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
&nbsp;
mod<span>&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;">(</span>y<span>~</span>x<span>*</span>fac<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>=d<span style="color: #009900;">)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#========================================================</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#---Extract the Main Effects, including the baseline, into a data.frame</span>
dusp<span>&lt;-</span>funinteff<span style="color: #009900;">(</span>mod<span style="color: #339933;">,</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#returns a data.frame of the Estimate and Standard Error, row.names correspond to the variables</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#----Now Set the data up to visualize in ggplot-----</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;">(</span><a href="http://inside-r.org/packages/cran/ggplot2"><span>ggplot2</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#------Quick ggplot (move into graph code later)</span>
<span style="color: #666666; font-style: italic;">#quick convenience function to compute significance at .95</span>
funsig<span>&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;">(</span>d<span style="color: #009900;">)</span><span style="color: #009900;">{</span>
	tstat<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/abs"><span style="color: #003399; font-weight: bold;">abs</span></a><span style="color: #009900;">(</span>d<span>$</span>b<span>/</span>d<span>$</span>se<span style="color: #009900;">)</span>
	sig<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/ifelse"><span style="color: #003399; font-weight: bold;">ifelse</span></a><span style="color: #009900;">(</span>tstat<span>&gt;=</span><span style="color: #cc66cc;">1.96</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'yes'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'no'</span><span style="color: #009900;">)</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;">(</span>sig<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
&nbsp;
&nbsp;
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>dusp<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span>:</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span>&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;">(</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'se'</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#change the names to to make typing easier</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Add confidence intervals and signficance test</span>
dusp<span>$</span>hi<span>&lt;-</span>dusp<span>$</span>b<span>+</span><span style="color: #cc66cc;">1.96</span><span>*</span>dusp<span>$</span>se
dusp<span>$</span>lo<span>&lt;-</span>dusp<span>$</span>b<span>-</span><span style="color: #cc66cc;">1.96</span><span>*</span>dusp<span>$</span>se
dusp<span>$</span>sig95<span>&lt;-</span>funsig<span style="color: #009900;">(</span>dusp<span style="color: #009900;">)</span>
&nbsp;
dusp<span>$</span>var<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/row.names"><span style="color: #003399; font-weight: bold;">row.names</span></a><span style="color: #009900;">(</span>dusp<span style="color: #009900;">)</span>
&nbsp;
&nbsp;
pd<span>&lt;-</span>dusp
&nbsp;
p1<span>&lt;-</span><a href="http://inside-r.org/packages/cran/ggplot"><span>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>=pd<span style="color: #339933;">,</span>aes<span style="color: #009900;">(</span>x=<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #339933;">,</span>y=b<span style="color: #339933;">,</span><a href="http://inside-r.org/packages/cran/shape"><span>shape</span></a>=sig95<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
p1<span>&lt;-</span>p1<span>+</span>geom_hline<span style="color: #009900;">(</span>yintercept=<span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: #0000ff;">'grey'</span><span style="color: #009900;">)</span><span>+</span>geom_line<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
p1<span>&lt;-</span>p1<span>+</span>geom_pointrange<span style="color: #009900;">(</span>aes<span style="color: #009900;">(</span>ymin=lo<span style="color: #339933;">,</span>ymax=<a href="http://inside-r.org/packages/cran/HI"><span>hi</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#+coord_flip() #uncomment coord_flip to switch the axes</span>
p1<span>&lt;-</span>p1<span>+</span>scale_y_continuous<span style="color: #009900;">(</span>name=<span style="color: #0000ff;">'Marginal Effect of Interaction Terms'</span><span style="color: #009900;">)</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p>&nbsp;</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://www.frankdavenport.com/storage/MarginalEffectsofInteractions_example.pdf?__SQUARESPACE_CACHEVERSION=1331343548772" alt="" /></span></span></p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-15371380.xml</wfw:commentRss></item><item><title>Recovering Marginal Effects and Standard Errors from Interaction Terms in R</title><category>R</category><category>Stats</category><dc:creator>Frank Davenport</dc:creator><pubDate>Tue, 06 Mar 2012 03:28:42 +0000</pubDate><link>http://www.frankdavenport.com/blog/2012/3/5/recovering-marginal-effects-and-standard-errors-from-interac.html</link><guid isPermaLink="false">709683:8312210:15314381</guid><description><![CDATA[<p style="text-align: justify;">When I fit models with interactions, I often want to recover not only the interaction effect but also the marginal effect (the main effect + the interaction) and of course the standard errors. There are a couple of ways to do this in R but I ended writing my own function (essentially a wrapper around the deltaMethod() function) to fit my needs.<br /> <br /> In this case, I have a model where a continuous variable has been interacted with a discrete variable. I want to create a data.frame that stores the main effect, the marginal effects of the interactions, and their standard errors.<br /> <br /> I also want the standard errors for the marginal effects on the interaction to match the standard errors on the main effect which in this case were <span class="caps">HAC </span>(Heteroskedastic and Autocorrelation Consistent ie. Newey-West).<br /> <br /> The function below is a little sloppy as I built it iteratively while I was working through a specific problem, but it is generalized to any lm() type object. The basic use case is that you have one or more models, each with one or more interactions and the discrete terms in the interactions have more than two levels (it will work with two levels, but in that case the function might be overkill). In my case I fit a series of region specific panel models where two of the main effects were interacted with a year term. I wanted have a generalizable function where I could just feed it the model object, variable name, and covariance estimator, and then return a data.frame with estimates and standard errors. In the next post I'll demonstrate the function with simulated data and show how to visualize the marginal in ggplot.</p>
<p style="text-align: justify;">&nbsp;<em>Note: The code below assumes you have already loaded car (for deltaMethod()), sandwich (for vcovHAC), and lmtest (for coeftest()).<br /></em></p>
<p style="text-align: justify;">&nbsp;</p>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">##Calls this function which is a wrapper for coeftest from the sandwhich package</span>
sehac<span>&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;">(</span>fit<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/vcov"><span style="color: #003399; font-weight: bold;">vcov</span></a>=vcovHAC<span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">#Convenience function for HAC standard erros</span>
	coeftest<span style="color: #009900;">(</span>fit<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/vcov"><span style="color: #003399; font-weight: bold;">vcov</span></a><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
&nbsp;
funinteff<span>&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;">(</span>mod<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/vcov"><span style="color: #003399; font-weight: bold;">vcov</span></a>=vcovHAC<span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #666666; font-style: italic;">#mod is an lm() object, var is the name of the main effect that was interacted, vcov is the type of variance covariance method you want to use </span>
	<span style="color: #666666; font-style: italic;">#Extract Coefficient names create 'beta names' to feed to deltaMethod()</span>
	cnames<span>&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;">(</span>mod<span style="color: #009900;">)</span>
	pnams<span>&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;">(</span><span style="color: #0000ff;">'b'</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;">(</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span>:</span><span style="color: #009900;">(</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;">(</span>cnames<span style="color: #009900;">)</span><span>-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>sep=<span style="color: #0000ff;">""</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'est'</span>=cnames<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#assign parameter names so that deltaMethod does not throw an error</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#Extract the specific parameters of interest</span>
	vars<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/grep"><span style="color: #003399; font-weight: bold;">grep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #339933;">,</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;">(</span>cnames<span style="color: #009900;">)</span><span style="color: #339933;">,</span>value=T<span style="color: #009900;">)</span>
	var1<span>&lt;-</span><a href="http://inside-r.org/packages/cran/vars"><span>vars</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
	intvars<span>&lt;-</span><a href="http://inside-r.org/packages/cran/vars"><span>vars</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span>:</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;">(</span><a href="http://inside-r.org/packages/cran/vars"><span>vars</span></a><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
	bi<span>&lt;-</span>pnams<span style="color: #009900;">[</span>var1<span style="color: #339933;">,</span><span style="color: #0000ff;">'b'</span><span style="color: #009900;">]</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#--Create Data Frame to store Main Effect</span>
	int<span>&lt;-</span>sehac<span style="color: #009900;">(</span>mod<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/vcov"><span style="color: #003399; font-weight: bold;">vcov</span></a>=<a href="http://inside-r.org/r-doc/stats/vcov"><span style="color: #003399; font-weight: bold;">vcov</span></a><span style="color: #009900;">)</span><span style="color: #009900;">[</span>var1<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;">(</span><span style="color: #0000ff;">'Estimate'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Std. Error'</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
	int<span>&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;">(</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;">(</span>int<span style="color: #009900;">)</span><span style="color: #009900;">)</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;">(</span>int<span style="color: #009900;">)</span><span>&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;">(</span><span style="color: #0000ff;">'Estimate'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'SE'</span><span style="color: #009900;">)</span>
	<a href="http://inside-r.org/r-doc/base/row.names"><span style="color: #003399; font-weight: bold;">row.names</span></a><span style="color: #009900;">(</span>int<span style="color: #009900;">)</span><span>&lt;-</span>var1
&nbsp;
	<span style="color: #666666; font-style: italic;">#Loop through and store the results in a data.frame</span>
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">(</span>i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span><span>:</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;">(</span>intvars<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
		bint<span>&lt;-</span>pnams<span style="color: #009900;">[</span>intvars<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'b'</span><span style="color: #009900;">]</span>
		eq<span>&lt;-</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;">(</span>bi<span style="color: #339933;">,</span>bint<span style="color: #339933;">,</span>sep=<span style="color: #0000ff;">"+"</span><span style="color: #009900;">)</span>
		interac<span>&lt;-</span>deltaMethod<span style="color: #009900;">(</span>mod<span style="color: #339933;">,</span>eq<span style="color: #339933;">,</span>parameterNames=pnams<span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/vcov"><span style="color: #003399; font-weight: bold;">vcov</span></a>=<a href="http://inside-r.org/r-doc/stats/vcov"><span style="color: #003399; font-weight: bold;">vcov</span></a><span style="color: #009900;">)</span>
		<a href="http://inside-r.org/r-doc/base/row.names"><span style="color: #003399; font-weight: bold;">row.names</span></a><span style="color: #009900;">(</span>interac<span style="color: #009900;">)</span><span>&lt;-</span>intvars<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>
&nbsp;
		int<span>&lt;-</span><a href="http://inside-r.org/r-doc/base/rbind"><span style="color: #003399; font-weight: bold;">rbind</span></a><span style="color: #009900;">(</span>int<span style="color: #339933;">,</span>interac<span style="color: #009900;">)</span>
	<span style="color: #009900;">}</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;">(</span>int<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-15314381.xml</wfw:commentRss></item><item><title>Spatial Tunnel Vision</title><category>Spatial</category><dc:creator>Frank Davenport</dc:creator><pubDate>Fri, 12 Nov 2010 02:23:43 +0000</pubDate><link>http://www.frankdavenport.com/blog/2010/11/11/spatial-tunnel-vision.html</link><guid isPermaLink="false">709683:8312210:9448607</guid><description><![CDATA[<p>Geography has a problem I call 'spatial tunnel vision'. In a nerdy nutshell you could summarize it as the failure to realize the absence of Tobler's Law as a null-hypothesis that should be rejected. Somewhat more plainly put: We always assume spatial correlation, and what's worse, we almost always assume that correlation is the result of some underlying spatial process, rather than the result of some unobserved and spatially concentrated influence.</p>
<p>For example, unemployment often exhibits high spatial correlation. Does this mean that if your neighbor loses her job it will likely happen to you too, simply because you live next door? No, people with similar incomes, and often those who work in similar industries, tend to live in close proximity to one another. So the same forces that impacted your neighbors employer might also impact yours.</p>
<p>This may seem like an obvious argument, but I see this mistake alot. People, usually geographers, use the fact that many things are spatially correlated to argue that 'place matters' and 'spatial is special'. Well, sort of. In the above example, yes place does matter, but only as a special case of cross-sectional correlation, that if properly controlled for, will not impact whatever broader statistical inference you want to make with the data.</p>
<p>I think this problem stems from the back-seat that Geography had in academia for much of the 20th century. Prior to the quantitative revolution (in geography) and especially the availability of consumer accessible GIS packages, Geographers spent alot of time persuading others that what they did was relevant. This has created a great sense of camaderie within the community, but, especially after the rise of GIS and Remote Sensing, has created a sense that every other discipline is misguided until they see the spatial light.</p>
<p>I use to feel this way. As a PhD student in a prominent geography department I see (and sympathize) with this view alot. It was not until I really delved into statistics and started working with economists that I realized that yeah, spatial is special, but not all time, and the burden of proof lies with the geographers. And, even if spaital is not special, there may still be a lot of interesting questions to ask of spatially correlated data.</p>]]></description><wfw:commentRss>http://www.frankdavenport.com/blog/rss-comments-entry-9448607.xml</wfw:commentRss></item></channel></rss>