<?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>Obscure Analytics</title>
	<atom:link href="http://www.obscureanalytics.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.obscureanalytics.com</link>
	<description>Just trying to know some stuff before I die...</description>
	<lastBuildDate>Sun, 10 Feb 2013 05:57:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Yeah yeah we had a baby but BUT BUT CHECK OUT THESE NEW TOYS!!!</title>
		<link>http://www.obscureanalytics.com/2012/12/20/yeah-yeah-we-had-a-baby-but-but-but-check-out-these-new-toys/</link>
		<comments>http://www.obscureanalytics.com/2012/12/20/yeah-yeah-we-had-a-baby-but-but-but-check-out-these-new-toys/#comments</comments>
		<pubDate>Thu, 20 Dec 2012 03:49:38 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Incredible Overkill]]></category>
		<category><![CDATA[The Grand Narrative of My Life?]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=432</guid>
		<description><![CDATA[Um yes those ARE plush toys of statistical distributions. My wife had to physically take them away from me and hand them to the child. Poisson is by far my favorite. The child seems to like Normal and T the...]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/2012-12-19-16.24.56.jpg"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/2012-12-19-16.24.56.jpg" alt="" title="AnyaWithDistribPlushToys" width="3264" height="2448" class="alignnone size-full wp-image-433" /></a></p>
<p>Um yes those ARE plush toys of statistical distributions.</p>
<p>My wife had to physically take them away from me and hand them to the child. </p>
<p>Poisson is by far my favorite.</p>
<p>The child seems to like Normal and T the best. Typical.</p>
<p>These were gifted to her by my wonderful co-workers at newBrandAnalytics. oh you guys&#8230;</p>
<p>Buy LOTS of them for yourself <a href='http://www.etsy.com/listing/71739287/collection-of-10-distribution-plushies'>here</a>. </p>
<p>Oh right and that&#8217;s my adorable wonderful magical new baby girl Anya!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/12/20/yeah-yeah-we-had-a-baby-but-but-but-check-out-these-new-toys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visualizing Baltimore 3.1: Crime and Vacant Properties, Neighborhood Level, Bit More Polished</title>
		<link>http://www.obscureanalytics.com/2012/12/11/visualizing-baltimore-3-1-crime-and-vacant-properties-neighborhood-level-bit-more-polished/</link>
		<comments>http://www.obscureanalytics.com/2012/12/11/visualizing-baltimore-3-1-crime-and-vacant-properties-neighborhood-level-bit-more-polished/#comments</comments>
		<pubDate>Tue, 11 Dec 2012 18:32:28 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Baltimore]]></category>
		<category><![CDATA[GIS]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=424</guid>
		<description><![CDATA[Redos of the plots from this post: Bit more communicative, though the overplotting is a bit annoying. Code: ## gis libraries library&#40;spBayes&#41; library&#40;MBA&#41; library&#40;geoR&#41; library&#40;fields&#41; library&#40;sp&#41; library&#40;maptools&#41; library&#40;rgdal&#41; library&#40;classInt&#41; library&#40;lattice&#41; library&#40;xtable&#41; library&#40;spatstat&#41; library&#40;splancs&#41; &#160; ## Other packages library&#40;ggplot2&#41; library&#40;foreign&#41; library&#40;stringr&#41;...]]></description>
				<content:encoded><![CDATA[<p>Redos of the plots from <a href="http://www.obscureanalytics.com/2012/12/10/visualizing-baltimore-3-crime-and-vacant-properties-neighborhood-level/" title="Visualizing Baltimore 3: Crime and Vacant Properties, Neighborhood Level">this post</a>:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/SandtownWinchesterVacantsandCrime1.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/SandtownWinchesterVacantsandCrime1.png" alt="" title="SandtownWinchesterVacantsandCrime" width="3300" height="2550" class="aligncenter size-full wp-image-425" /></a></p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HarlemParkVacantsandCrime1.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HarlemParkVacantsandCrime1.png" alt="" title="HarlemParkVacantsandCrime" width="3300" height="2550" class="aligncenter size-full wp-image-426" /></a></p>
<p>Bit more communicative, though the overplotting is a bit annoying.</p>
<p>Code:</p>

<div class="wp_codebox"><table><tr id="p4242"><td class="code" id="p424code2"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## gis libraries</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>spBayes<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>MBA<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>geoR<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>fields<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>sp<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>maptools<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>rgdal<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>classInt<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>lattice<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>xtable<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>spatstat<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>splancs<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Other packages</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>foreign<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>stringr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>lubridate<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>plyr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>xtable<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>scales<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>RColorBrewer<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/grid.html"><span style="color: #0000FF; font-weight: bold;">grid</span></a><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggmap<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>gridExtra<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggmcmc<span style="color: #080;">&#41;</span>
&nbsp;
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/setwd.html"><span style="color: #0000FF; font-weight: bold;">setwd</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'/home/rmealey/Dropbox/school/gisClass/FinalProject'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/options.html"><span style="color: #0000FF; font-weight: bold;">options</span></a><span style="color: #080;">&#40;</span>digits<span style="color: #080;">=</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span>
&nbsp;
Save <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>projName<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	<span style="color: #0000FF; font-weight: bold;">savehistory</span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>projName,<span style="color: #ff0000;">'.Rhistory'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/save.image.html"><span style="color: #0000FF; font-weight: bold;">save.<span style="">image</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>projName,<span style="color: #ff0000;">'.RData'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
sv <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> Save<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'FinalProject'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;">## Utility Functions</span>
&nbsp;
<span style="color: #228B22;">## Read lat/lng coords function</span>
str2LatLong <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>in_df<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	latlng <span style="color: #080;">&lt;-</span> str_replace<span style="color: #080;">&#40;</span>str_replace<span style="color: #080;">&#40;</span>in_df$Location.1,<span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>('</span>,<span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">')'</span>,<span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>
	latlng <span style="color: #080;">&lt;-</span> str_split<span style="color: #080;">&#40;</span>latlng,<span style="color: #ff0000;">', '</span><span style="color: #080;">&#41;</span>
	latlng_df <span style="color: #080;">&lt;-</span> ldply<span style="color: #080;">&#40;</span>latlng<span style="color: #080;">&#91;</span>in_df$Location.1 <span style="color: #080;">!=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	out_df <span style="color: #080;">&lt;-</span> in_df
	out_df$lat <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>latlng_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	out_df$long <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>latlng_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>out_df<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">## convert projection function</span>
convProj <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>in_df,in_proj,out_proj<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	latlong <span style="color: #080;">&lt;-</span> in_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
	latlong_spdf <span style="color: #080;">&lt;-</span> SpatialPoints<span style="color: #080;">&#40;</span>latlong, 
	proj4string<span style="color: #080;">=</span>in_proj<span style="color: #080;">&#41;</span>
	latlong_spdf <span style="color: #080;">&lt;-</span>  spTransform<span style="color: #080;">&#40;</span>latlong_spdf,out_proj<span style="color: #080;">&#41;</span>
	latlong_spdf_coords <span style="color: #080;">&lt;-</span> coordinates<span style="color: #080;">&#40;</span>latlong_spdf<span style="color: #080;">&#41;</span>
	out_df <span style="color: #080;">&lt;-</span> in_df
	out_df$long <span style="color: #080;">&lt;-</span> latlong_spdf_coords<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
	out_df$lat <span style="color: #080;">&lt;-</span> latlong_spdf_coords<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>out_df<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># City Boundary Shape File</span>
city_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Baltcity_20Line/baltcity_line.dbf'</span><span style="color: #080;">&#41;</span>
city_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Baltcity_20Line'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'baltcity_line'</span><span style="color: #080;">&#41;</span>
origProj <span style="color: #080;">&lt;-</span> city_shp@proj4string <span style="color: #228B22;">## Store original projection</span>
<span style="color: #228B22;">#city_shp = spTransform(city_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span>
city_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>city_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span>
cityLineCoords <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>city_shp@<a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/lines.html"><span style="color: #0000FF; font-weight: bold;">lines</span></a><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>@Lines<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>@coords<span style="color: #080;">&#41;</span>
cityLinePoly <span style="color: #080;">&lt;-</span> Polygon<span style="color: #080;">&#40;</span>cityLineCoords<span style="color: #080;">&#41;</span>
cityLinePolys <span style="color: #080;">&lt;-</span> Polygons<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span>cityLinePoly<span style="color: #080;">&#41;</span>, ID<span style="color: #080;">=</span><span style="color: #ff0000;">'cityline'</span><span style="color: #080;">&#41;</span>
cityLineSpPoly <span style="color: #080;">&lt;-</span> SpatialPolygons<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span>cityLinePolys<span style="color: #080;">&#41;</span>,proj4string<span style="color: #080;">=</span>origProj<span style="color: #080;">&#41;</span>
&nbsp;
cityLineCoords <span style="color: #080;">&lt;-</span> cityLineCoords<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Neighborhood Shape Files</span>
<span style="color: #228B22;"># Source:</span>
&nbsp;
<span style="color: #228B22;">## Neighborhood Shape Files read in v1</span>
nbhds_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Neighborhood_202010/nhood_2010.dbf'</span><span style="color: #080;">&#41;</span>
nbhds_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Neighborhood_202010'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'nhood_2010'</span><span style="color: #080;">&#41;</span>
origProj <span style="color: #080;">&lt;-</span> nbhds_shp@proj4string <span style="color: #228B22;">## Store original projection</span>
<span style="color: #228B22;">#nbhds_shp = spTransform(nbhds_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span>
nbhds_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>nbhds_shp@polygons<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> nbhds_shp@<span style="color: #0000FF; font-weight: bold;">data</span>$LABEL
&nbsp;
<span style="color: #228B22;">## Neighborhood Shape Files read in v2 (from spatstat docs)</span>
<span style="color: #228B22;">#nbhds_shp &lt;- readShapePoly('Neighborhood_202010/nhood_2010.shp')</span>
<span style="color: #228B22;">#nbhds_sp &lt;- as(nbhds_shp, &quot;SpatialPolygons&quot;)</span>
<span style="color: #228B22;">#nbhds_owin &lt;- as(nbhds_sp, &quot;owin&quot;)</span>
<span style="color: #228B22;">#centroids &lt;- coordinates(nbhds_shp)</span>
&nbsp;
hoodNames <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Mount Vernon'</span>
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>nbhds_pl_df<span style="color: #080;">&#91;</span>nbhds_pl_df$id<span style="color: #080;">==</span>hoodNames,<span style="color: #080;">&#93;</span>,
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_path<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
ggtitle<span style="color: #080;">&#40;</span>hoodNames<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Parcel Shape Polygon Data</span>
parcel_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Parcel_Shp'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'parcel'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Deduplicate polygons and dataframe</span>
parcel_shp2 <span style="color: #080;">&lt;-</span> parcel_shp<span style="color: #080;">&#91;</span><span style="color: #080;">!</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/duplicated.html"><span style="color: #0000FF; font-weight: bold;">duplicated</span></a><span style="color: #080;">&#40;</span>parcel_shp$BLOCKLOT<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
parcel_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>coordinates<span style="color: #080;">&#40;</span>parcel_shp2<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>parcel_mtrx<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>parcel_mtrx<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> parcel_shp2$BLOCKLOT
parcel_shp2$Type <span style="color: #080;">&lt;-</span> NA
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Buildings</span>
vacantBuildings_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/Vacant_Buildings.csv'</span><span style="color: #080;">&#41;</span>
vacantBuildings_df <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>vacantBuildings_df<span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
vacantBuildings_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>vacantBuildings_df, inProj, outProj<span style="color: #080;">&#41;</span>
vacantBuildings_df$type <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Vacant Building'</span>
vacBld_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>vacantBuildings_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
vacantBuildings_parc <span style="color: #080;">&lt;-</span> parcel_shp2<span style="color: #080;">&#91;</span>parcel_shp2$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span>vacantBuildings_df$blockLot,<span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Lots</span>
<span style="color: #228B22;"># Source:</span>
vacantLots_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/Vacant_Lots.csv'</span><span style="color: #080;">&#41;</span>
vacantLots_df <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>vacantLots_df<span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
vacantLots_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>vacantLots_df, inProj, outProj<span style="color: #080;">&#41;</span>
vacantLots_df$type <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Vacant Lot'</span>
vacantLots_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>vacantLots_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
vacantLots_parc <span style="color: #080;">&lt;-</span> parcel_shp2<span style="color: #080;">&#91;</span>parcel_shp2$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span>vacantLots_df$blockLot,<span style="color: #080;">&#93;</span>
&nbsp;
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;">## Crime Data</span>
crimeData <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/BPD_Part_1_Victim_Based_Crime_Data.csv'</span><span style="color: #080;">&#41;</span>
crimeData_NoCoords <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$Location.1 <span style="color: #080;">==</span> <span style="color: #ff0000;">''</span>,<span style="color: #080;">&#93;</span>
crimeData <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$Location.1 <span style="color: #080;">!=</span> <span style="color: #ff0000;">''</span>,<span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">## Get and convert projection</span>
crimeData <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>crimeData<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Incidents already in correct proj</span>
crimeData_ProjOrig <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$lat<span style="color: #080;">&gt;</span><span style="color: #ff0000;">100</span>,<span style="color: #080;">&#93;</span>
crimeData <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$lat<span style="color: #080;">&lt;</span><span style="color: #ff0000;">100</span>,<span style="color: #080;">&#93;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
crimeData <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>crimeData, inProj, outProj<span style="color: #080;">&#41;</span>
&nbsp;
crime_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>crimeData<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Parse Dates</span>
crimeData$crimeDate2 <span style="color: #080;">&lt;-</span> parse_date_time<span style="color: #080;">&#40;</span>
	crimeData$crimeDate,
	orders<span style="color: #080;">=</span><span style="color: #ff0000;">'%m/%d/%Y'</span>
<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Get Burglary Incidents</span>
burg_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
burg_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>burg_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
burg_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>burg_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>burg_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Get Street Robbery Incidents</span>
robbStr_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">&quot;ROBBERY - STREET&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
robbStr_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>robbStr_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
robbStr_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>robbStr_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>robbStr_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Homicide</span>
homic_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
homic_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>homic_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
homic_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>homic_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>homic_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Aggravated Assault</span>
aggrAslt_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
aggrAslt_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>aggrAslt_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
aggrAslt_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>aggrAslt_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>aggrAslt_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Plot by Neighborhood</span>
&nbsp;
nbhd_name <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Sandtown-Winchester'</span>
plot_title <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">&quot;Sandtown-<span style="color: #000099; font-weight: bold;">\n</span>Winchester<span style="color: #000099; font-weight: bold;">\n</span>Vacant Properties<span style="color: #000099; font-weight: bold;">\n</span>and Crime&quot;</span>
plot_title_x <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1415200</span>
plot_title_y <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">598300</span>
file_name <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'SandtownWinchesterVacantsandCrime'</span>
&nbsp;
<span style="color: #228B22;">##border</span>
nbhd_border_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp@polygons<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>nbhd_name<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
sw_mtr <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>nbhd_border_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Parcels in nbhd</span>
sw_props <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>parcel_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_polys <span style="color: #080;">&lt;-</span> parcel_shp2<span style="color: #080;">&#91;</span>parcel_shp2$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
sw_polys_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>sw_polys<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Vacants in nbhd</span>
sw_vb <span style="color: #080;">&lt;-</span> vacantBuildings_parc<span style="color: #080;">&#91;</span>vacantBuildings_parc$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
sw_vl <span style="color: #080;">&lt;-</span> vacantLots_parc<span style="color: #080;">&#91;</span>vacantLots_parc$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">## Crime in nbhd</span>
sw_crime <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>crime_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_crime <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_crime<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
sw_crime_2012 <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>sw_crime, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">2012</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_vacB<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_vacL<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;"># https://github.com/wch/ggplot2/wiki/New-theme-system</span>
new_theme_empty <span style="color: #080;">&lt;-</span> theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$line <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$rect <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$strip.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$plot.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$legend.<span style="">position</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'bottom'</span>
new_theme_empty$plot.<span style="">margin</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/structure.html"><span style="color: #0000FF; font-weight: bold;">structure</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">0</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, 
unit <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;lines&quot;</span>, valid.<span style="">unit</span> <span style="color: #080;">=</span> 3L, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/class.html"><span style="color: #0000FF; font-weight: bold;">class</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;unit&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> brewer.<span style="">pal</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span>,<span style="color: #ff0000;">'Paired'</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeTypes <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'RAPE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ARSON'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'COMMON ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'SHOOTING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - STREET'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - CARJACKING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - RESIDENCE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - COMMERCIAL'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AUTO THEFT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY FROM AUTO'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.data.frame.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">data</span>.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="color: #0000FF; font-weight: bold;">t</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
col_cols <span style="color: #080;">&lt;-</span> crimeCols<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span>
&nbsp;
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> nbhd_border_df<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_polygon<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color<span style="color: #080;">=</span><span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_path<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>sw_polys_df,
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat,group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>,
size<span style="color: #080;">=</span>.3<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_polygon<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_vb, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color <span style="color: #080;">=</span> <span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'pink'</span>,size<span style="color: #080;">=</span>.3<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_polygon<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_vl, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color <span style="color: #080;">=</span> <span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'pink'</span>,size<span style="color: #080;">=</span>.3<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_jitter<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_crime_2012, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, color<span style="color: #080;">=</span>description, shape<span style="color: #080;">=</span>description<span style="color: #080;">&#41;</span>,
size<span style="color: #080;">=</span><span style="color: #ff0000;">2</span>, alpha<span style="color: #080;">=</span><span style="color: #ff0000;">'.8'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
scale_color_manual<span style="color: #080;">&#40;</span>values <span style="color: #080;">=</span> col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
scale_shape_manual<span style="color: #080;">&#40;</span>values <span style="color: #080;">=</span> crime_shapes<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
annotate<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;text&quot;</span>, x <span style="color: #080;">=</span> plot_title_x, y <span style="color: #080;">=</span> plot_title_y,
label<span style="color: #080;">=</span>plot_title, 
size<span style="color: #080;">=</span><span style="color: #ff0000;">6</span>, color<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;black&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
new_theme_empty <span style="color: #080;">+</span>
guides<span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>guide_legend<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span>,shape<span style="color: #080;">=</span>guide_legend<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
ggsave<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/'</span>,file_name,<span style="color: #ff0000;">'.png'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,width<span style="color: #080;">=</span><span style="color: #ff0000;">11</span>, height<span style="color: #080;">=</span><span style="color: #ff0000;">8.5</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Lots</span>
nbhd_name <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Harlem Park'</span>
plot_title <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">&quot;Harlem Park<span style="color: #000099; font-weight: bold;">\n</span>Vacant Properties<span style="color: #000099; font-weight: bold;">\n</span>and Crime&quot;</span>
plot_title_x <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1416400</span>
plot_title_y <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">594500</span>
file_name <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'HarlemParkVacantsandCrime'</span>
&nbsp;
<span style="color: #228B22;">##border</span>
nbhd_border_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp@polygons<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>nbhd_name<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
sw_mtr <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>nbhd_border_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Parcels in nbhd</span>
sw_props <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>parcel_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_polys <span style="color: #080;">&lt;-</span> parcel_shp2<span style="color: #080;">&#91;</span>parcel_shp2$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
sw_polys_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>sw_polys<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Vacants in nbhd</span>
sw_vb <span style="color: #080;">&lt;-</span> vacantBuildings_parc<span style="color: #080;">&#91;</span>vacantBuildings_parc$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
sw_vl <span style="color: #080;">&lt;-</span> vacantLots_parc<span style="color: #080;">&#91;</span>vacantLots_parc$BLOCKLOT<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">## Crime in nbhd</span>
sw_crime <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>crime_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_crime <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_crime<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
sw_crime_2012 <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>sw_crime, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">2012</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_vacB<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_vacL<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;"># https://github.com/wch/ggplot2/wiki/New-theme-system</span>
new_theme_empty <span style="color: #080;">&lt;-</span> theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$line <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$rect <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$strip.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$plot.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$legend.<span style="">position</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'bottom'</span>
new_theme_empty$plot.<span style="">margin</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/structure.html"><span style="color: #0000FF; font-weight: bold;">structure</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">0</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, 
unit <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;lines&quot;</span>, valid.<span style="">unit</span> <span style="color: #080;">=</span> 3L, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/class.html"><span style="color: #0000FF; font-weight: bold;">class</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;unit&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> brewer.<span style="">pal</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span>,<span style="color: #ff0000;">'Paired'</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeTypes <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'RAPE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'①'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ARSON'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'②'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'COMMON ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'③'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'④'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'SHOOTING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'⑤'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'⑥'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - STREET'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'⑦'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - CARJACKING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'⑧'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - RESIDENCE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'⑨'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - COMMERCIAL'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'⑩'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'Ⓐ'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'Ⓑ'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AUTO THEFT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'Ⓒ'</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY FROM AUTO'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span>,<span style="color: #ff0000;">'Ⓓ'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.data.frame.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">data</span>.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="color: #0000FF; font-weight: bold;">t</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
col_cols <span style="color: #080;">&lt;-</span> crimeCols<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
crime_shapes <span style="color: #080;">&lt;-</span> crimeCols<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crime_shapes<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span>
&nbsp;
sw_crime_2012$description <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ordered.html"><span style="color: #0000FF; font-weight: bold;">ordered</span></a><span style="color: #080;">&#40;</span>sw_crime_2012$description,
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/levels.html"><span style="color: #0000FF; font-weight: bold;">levels</span></a><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> nbhd_border_df<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_polygon<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color<span style="color: #080;">=</span><span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_path<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>sw_polys_df,
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat,group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>,
size<span style="color: #080;">=</span>.3<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_polygon<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_vb, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color <span style="color: #080;">=</span> <span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'pink'</span>,size<span style="color: #080;">=</span>.3<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_polygon<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_vl, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color <span style="color: #080;">=</span> <span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'pink'</span>,size<span style="color: #080;">=</span>.3<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_jitter<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_crime_2012, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, color<span style="color: #080;">=</span>description, shape<span style="color: #080;">=</span>description<span style="color: #080;">&#41;</span>,
size<span style="color: #080;">=</span><span style="color: #ff0000;">2</span>, alpha<span style="color: #080;">=</span><span style="color: #ff0000;">'.8'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
scale_color_manual<span style="color: #080;">&#40;</span>values <span style="color: #080;">=</span> col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
scale_shape_manual<span style="color: #080;">&#40;</span>values <span style="color: #080;">=</span> crime_shapes<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
annotate<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;text&quot;</span>, x <span style="color: #080;">=</span> plot_title_x, y <span style="color: #080;">=</span> plot_title_y,
label<span style="color: #080;">=</span>plot_title, 
size<span style="color: #080;">=</span><span style="color: #ff0000;">6</span>, color<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;black&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
new_theme_empty <span style="color: #080;">+</span>
guides<span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>guide_legend<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span>,shape<span style="color: #080;">=</span>guide_legend<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
ggsave<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/'</span>,file_name,<span style="color: #ff0000;">'.png'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,width<span style="color: #080;">=</span><span style="color: #ff0000;">11</span>, height<span style="color: #080;">=</span><span style="color: #ff0000;">8.5</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/12/11/visualizing-baltimore-3-1-crime-and-vacant-properties-neighborhood-level-bit-more-polished/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visualizing Baltimore 3: Crime and Vacant Properties, Neighborhood Level</title>
		<link>http://www.obscureanalytics.com/2012/12/10/visualizing-baltimore-3-crime-and-vacant-properties-neighborhood-level/</link>
		<comments>http://www.obscureanalytics.com/2012/12/10/visualizing-baltimore-3-crime-and-vacant-properties-neighborhood-level/#comments</comments>
		<pubDate>Mon, 10 Dec 2012 12:49:18 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Baltimore]]></category>
		<category><![CDATA[GIS]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=410</guid>
		<description><![CDATA[A few quick plots of West Baltimore neighborhoods, first Sandtown-Winchester: and Harlem Park: These aren&#8217;t very polished, I&#8217;ll put up better versions. Here&#8217;s the code for those that want it: ## gis libraries library&#40;spBayes&#41; library&#40;MBA&#41; library&#40;geoR&#41; library&#40;fields&#41; library&#40;sp&#41; library&#40;maptools&#41; library&#40;rgdal&#41;...]]></description>
				<content:encoded><![CDATA[<p>A few quick plots of West Baltimore neighborhoods, first Sandtown-Winchester:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/SandtownWinchesterVacantsandCrime.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/SandtownWinchesterVacantsandCrime-1024x1024.png" alt="" title="SandtownWinchesterVacantsandCrime" width="1024" height="1024" class="aligncenter size-large wp-image-411" /></a></p>
<p>and Harlem Park:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HarlemParkVacantsandCrime.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HarlemParkVacantsandCrime.png" alt="" title="HarlemParkVacantsandCrime" width="3300" height="2550" class="aligncenter size-full wp-image-414" /></a></p>
<p>These aren&#8217;t very polished, I&#8217;ll put up better versions.</p>
<p>Here&#8217;s the code for those that want it:</p>

<div class="wp_codebox"><table><tr id="p4104"><td class="code" id="p410code4"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## gis libraries</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>spBayes<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>MBA<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>geoR<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>fields<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>sp<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>maptools<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>rgdal<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>classInt<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>lattice<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>xtable<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>spatstat<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>splancs<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Other packages</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>foreign<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>stringr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>lubridate<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>plyr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>xtable<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>scales<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>RColorBrewer<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/grid.html"><span style="color: #0000FF; font-weight: bold;">grid</span></a><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggmap<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>gridExtra<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggmcmc<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># City Boundary Shape File</span>
city_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Baltcity_20Line/baltcity_line.dbf'</span><span style="color: #080;">&#41;</span>
city_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Baltcity_20Line'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'baltcity_line'</span><span style="color: #080;">&#41;</span>
origProj <span style="color: #080;">&lt;-</span> city_shp@proj4string <span style="color: #228B22;">## Store original projection</span>
<span style="color: #228B22;">#city_shp = spTransform(city_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span>
city_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>city_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span>
cityLineCoords <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>city_shp@<a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/lines.html"><span style="color: #0000FF; font-weight: bold;">lines</span></a><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>@Lines<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>@coords<span style="color: #080;">&#41;</span>
cityLinePoly <span style="color: #080;">&lt;-</span> Polygon<span style="color: #080;">&#40;</span>cityLineCoords<span style="color: #080;">&#41;</span>
cityLinePolys <span style="color: #080;">&lt;-</span> Polygons<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span>cityLinePoly<span style="color: #080;">&#41;</span>, ID<span style="color: #080;">=</span><span style="color: #ff0000;">'cityline'</span><span style="color: #080;">&#41;</span>
cityLineSpPoly <span style="color: #080;">&lt;-</span> SpatialPolygons<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span>cityLinePolys<span style="color: #080;">&#41;</span>,proj4string<span style="color: #080;">=</span>origProj<span style="color: #080;">&#41;</span>
&nbsp;
cityLineCoords <span style="color: #080;">&lt;-</span> cityLineCoords<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Neighborhood Shape Files</span>
<span style="color: #228B22;"># Source:</span>
&nbsp;
<span style="color: #228B22;">## Neighborhood Shape Files read in v1</span>
nbhds_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Neighborhood_202010/nhood_2010.dbf'</span><span style="color: #080;">&#41;</span>
nbhds_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Neighborhood_202010'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'nhood_2010'</span><span style="color: #080;">&#41;</span>
origProj <span style="color: #080;">&lt;-</span> nbhds_shp@proj4string <span style="color: #228B22;">## Store original projection</span>
<span style="color: #228B22;">#nbhds_shp = spTransform(nbhds_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span>
nbhds_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>nbhds_shp@polygons<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> nbhds_shp@<span style="color: #0000FF; font-weight: bold;">data</span>$LABEL
&nbsp;
<span style="color: #228B22;">## Neighborhood Shape Files read in v2 (from spatstat docs)</span>
<span style="color: #228B22;">#nbhds_shp &lt;- readShapePoly('Neighborhood_202010/nhood_2010.shp')</span>
<span style="color: #228B22;">#nbhds_sp &lt;- as(nbhds_shp, &quot;SpatialPolygons&quot;)</span>
<span style="color: #228B22;">#nbhds_owin &lt;- as(nbhds_sp, &quot;owin&quot;)</span>
<span style="color: #228B22;">#centroids &lt;- coordinates(nbhds_shp)</span>
&nbsp;
hoodNames <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Mount Vernon'</span>
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>nbhds_pl_df<span style="color: #080;">&#91;</span>nbhds_pl_df$id<span style="color: #080;">==</span>hoodNames,<span style="color: #080;">&#93;</span>,
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_path<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
ggtitle<span style="color: #080;">&#40;</span>hoodNames<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## plot actual city shape using empty nbhd boundaries</span>
city_plot <span style="color: #080;">&lt;-</span> bound_plot <span style="color: #080;">+</span> 
geom_polygon<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>nbhds_pl_df, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span>,color<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span><span style="color: #080;">&#41;</span>
ggsave<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/emptyCity.png'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## plot nbhd boundaries</span>
nbhds_plot <span style="color: #080;">&lt;-</span> bound_plot <span style="color: #080;">+</span> 
geom_polygon<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>nbhds_pl_df,color<span style="color: #080;">=</span><span style="color: #ff0000;">'gray'</span>,fill<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span><span style="color: #080;">&#41;</span>
ggsave<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/nbhds.png'</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Parcel Shape Polygon Data</span>
<span style="color: #228B22;"># Source:</span>
parcel_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Parcel_Shp/parcel.dbf'</span><span style="color: #080;">&#41;</span>
parcel_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Parcel_Shp'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'parcel'</span><span style="color: #080;">&#41;</span>
parcel_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>parcel_df, coordinates<span style="color: #080;">&#40;</span>parcel_shp<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
parcel_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>coordinates<span style="color: #080;">&#40;</span>parcel_shp<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Buildings</span>
<span style="color: #228B22;"># Source:</span>
vacantBuildings_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/Vacant_Buildings.csv'</span><span style="color: #080;">&#41;</span>
vacantBuildings_df <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>vacantBuildings_df<span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
vacantBuildings_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>vacantBuildings_df, inProj, outProj<span style="color: #080;">&#41;</span>
vacantBuildings_df$type <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Vacant Building'</span>
vacBld_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>vacantBuildings_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Lots</span>
<span style="color: #228B22;"># Source:</span>
vacantLots_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/Vacant_Lots.csv'</span><span style="color: #080;">&#41;</span>
vacantLots_df <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>vacantLots_df<span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
vacantLots_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>vacantLots_df, inProj, outProj<span style="color: #080;">&#41;</span>
vacantLots_df$type <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Vacant Lot'</span>
vacantLots_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>vacantLots_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;">## Plot by neighborhood</span>
&nbsp;
crimeData <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/BPD_Part_1_Victim_Based_Crime_Data.csv'</span><span style="color: #080;">&#41;</span>
crimeData_NoCoords <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$Location.1 <span style="color: #080;">==</span> <span style="color: #ff0000;">''</span>,<span style="color: #080;">&#93;</span>
crimeData <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$Location.1 <span style="color: #080;">!=</span> <span style="color: #ff0000;">''</span>,<span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">## Get and convert projection</span>
crimeData <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>crimeData<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Incidents already in correct proj</span>
crimeData_ProjOrig <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$lat<span style="color: #080;">&gt;</span><span style="color: #ff0000;">100</span>,<span style="color: #080;">&#93;</span>
crimeData <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$lat<span style="color: #080;">&lt;</span><span style="color: #ff0000;">100</span>,<span style="color: #080;">&#93;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
crimeData <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>crimeData, inProj, outProj<span style="color: #080;">&#41;</span>
&nbsp;
crime_mtrx <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>crimeData<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Parse Dates</span>
crimeData$crimeDate2 <span style="color: #080;">&lt;-</span> parse_date_time<span style="color: #080;">&#40;</span>
	crimeData$crimeDate,
	orders<span style="color: #080;">=</span><span style="color: #ff0000;">'%m/%d/%Y'</span>
<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Get Burglary Incidents</span>
burg_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
burg_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>burg_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
burg_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>burg_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>burg_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Get Street Robbery Incidents</span>
robbStr_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">&quot;ROBBERY - STREET&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
robbStr_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>robbStr_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
robbStr_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>robbStr_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>robbStr_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Homicide</span>
homic_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
homic_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>homic_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
homic_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>homic_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>homic_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Aggravated Assault</span>
aggrAslt_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
aggrAslt_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>aggrAslt_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
aggrAslt_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>aggrAslt_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>aggrAslt_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Lots</span>
SandtownWinchester_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp@polygons<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">'Sandtown-Winchester'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
sw_mtr <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>SandtownWinchester_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
sw_props <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>parcel_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_vacB <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>vacBld_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_vacL <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>vacBld_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_crime <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>crime_mtrx, sw_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
sw_crime <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>sw_crime<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
sw_crime_2012 <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>sw_crime, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">2012</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_props<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_vacB<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>sw_vacL<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;"># https://github.com/wch/ggplot2/wiki/New-theme-system</span>
new_theme_empty <span style="color: #080;">&lt;-</span> theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$line <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$rect <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$strip.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$plot.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$legend.<span style="">position</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'bottom'</span>
new_theme_empty$plot.<span style="">margin</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/structure.html"><span style="color: #0000FF; font-weight: bold;">structure</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">0</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, unit <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;lines&quot;</span>, valid.<span style="">unit</span> <span style="color: #080;">=</span> 3L, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/class.html"><span style="color: #0000FF; font-weight: bold;">class</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;unit&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> brewer.<span style="">pal</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span>,<span style="color: #ff0000;">'Paired'</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeTypes <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'RAPE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ARSON'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'COMMON ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'SHOOTING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - STREET'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - CARJACKING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - RESIDENCE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - COMMERCIAL'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AUTO THEFT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY FROM AUTO'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.data.frame.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">data</span>.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="color: #0000FF; font-weight: bold;">t</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
col_cols <span style="color: #080;">&lt;-</span> crimeCols<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span>
&nbsp;
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> SandtownWinchester_df<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_polygon<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color<span style="color: #080;">=</span><span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_point<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_props, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span>, 
shape <span style="color: #080;">=</span> <span style="color: #ff0000;">0</span>, color <span style="color: #080;">=</span> <span style="color: #ff0000;">'gray'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_point<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_vacB, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span>, 
shape <span style="color: #080;">=</span> <span style="color: #ff0000;">4</span>, color <span style="color: #080;">=</span> <span style="color: #ff0000;">'red'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_point<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> sw_crime_2012, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, color<span style="color: #080;">=</span>description<span style="color: #080;">&#41;</span>, shape <span style="color: #080;">=</span> <span style="color: #ff0000;">'o'</span>,size<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
scale_color_manual<span style="color: #080;">&#40;</span>values <span style="color: #080;">=</span> col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
annotate<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;text&quot;</span>, x <span style="color: #080;">=</span> <span style="color: #ff0000;">1415200</span>, y <span style="color: #080;">=</span> <span style="color: #ff0000;">598300</span>,
label<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Sandtown-<span style="color: #000099; font-weight: bold;">\n</span>Winchester<span style="color: #000099; font-weight: bold;">\n</span>Vacant Properties<span style="color: #000099; font-weight: bold;">\n</span>and Crime&quot;</span>, 
size<span style="color: #080;">=</span><span style="color: #ff0000;">6</span>, color<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;black&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
new_theme_empty <span style="color: #080;">+</span>
guides<span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>guide_legend<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
ggsave<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/SandtownWinchesterVacantsandCrime.png'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Lots</span>
HarlemPark_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp@polygons<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">'Harlem Park'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
hp_mtr <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>HarlemPark_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
hp_props <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>parcel_mtrx, hp_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
hp_vacB <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>vacBld_mtrx, hp_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
hp_vacL <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>vacBld_mtrx, hp_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
hp_crime <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>pip<span style="color: #080;">&#40;</span>crime_mtrx, hp_mtr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
hp_crime <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>hp_crime<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
hp_crime_2012 <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>hp_crime, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">2012</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>hp_props<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>hp_vacB<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>hp_vacL<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;"># https://github.com/wch/ggplot2/wiki/New-theme-system</span>
new_theme_empty <span style="color: #080;">&lt;-</span> theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$line <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$rect <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$strip.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$plot.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$axis.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
new_theme_empty$legend.<span style="">position</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'bottom'</span>
new_theme_empty$plot.<span style="">margin</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/structure.html"><span style="color: #0000FF; font-weight: bold;">structure</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">0</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, unit <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;lines&quot;</span>, valid.<span style="">unit</span> <span style="color: #080;">=</span> 3L, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/class.html"><span style="color: #0000FF; font-weight: bold;">class</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;unit&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> brewer.<span style="">pal</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span>,<span style="color: #ff0000;">'Paired'</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeTypes <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'RAPE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ARSON'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'COMMON ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'SHOOTING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - STREET'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - CARJACKING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - RESIDENCE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - COMMERCIAL'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AUTO THEFT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY FROM AUTO'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeCols <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.data.frame.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">data</span>.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/t.html"><span style="color: #0000FF; font-weight: bold;">t</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
col_cols <span style="color: #080;">&lt;-</span> crimeCols<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span>
&nbsp;
hpplot <span style="color: #080;">&lt;-</span> ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> HarlemPark_df<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_polygon<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>, 
color<span style="color: #080;">=</span><span style="color: #ff0000;">'black'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_point<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> hp_props, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span>, 
shape <span style="color: #080;">=</span> <span style="color: #ff0000;">0</span>, color <span style="color: #080;">=</span> <span style="color: #ff0000;">'gray'</span>,size<span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_point<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> hp_vacB, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span>, 
shape <span style="color: #080;">=</span> <span style="color: #ff0000;">4</span>, color <span style="color: #080;">=</span> <span style="color: #ff0000;">'red'</span>,size<span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
geom_point<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> hp_crime_2012, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, color<span style="color: #080;">=</span>description<span style="color: #080;">&#41;</span>, shape <span style="color: #080;">=</span> <span style="color: #ff0000;">'o'</span>,size<span style="color: #080;">=</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
scale_color_manual<span style="color: #080;">&#40;</span>values <span style="color: #080;">=</span> col_cols<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
annotate<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;text&quot;</span>, x <span style="color: #080;">=</span> <span style="color: #ff0000;">1416400</span>, y <span style="color: #080;">=</span> <span style="color: #ff0000;">594500</span>,
label<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Harlem Park<span style="color: #000099; font-weight: bold;">\n</span>Vacant Properties<span style="color: #000099; font-weight: bold;">\n</span>and Crime&quot;</span>, 
size<span style="color: #080;">=</span><span style="color: #ff0000;">4</span>, color<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;black&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
new_theme_empty <span style="color: #080;">+</span>
guides<span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>guide_legend<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
ggsave<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/HarlemParkVacantsandCrime.png'</span>,width<span style="color: #080;">=</span><span style="color: #ff0000;">11</span>, height<span style="color: #080;">=</span><span style="color: #ff0000;">8.5</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/12/10/visualizing-baltimore-3-crime-and-vacant-properties-neighborhood-level/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Visualizing Baltimore 2: Vacant Property and Some More Crime</title>
		<link>http://www.obscureanalytics.com/2012/12/10/visualizing-baltimore-2-vacant-property-and-some-more-crime/</link>
		<comments>http://www.obscureanalytics.com/2012/12/10/visualizing-baltimore-2-vacant-property-and-some-more-crime/#comments</comments>
		<pubDate>Mon, 10 Dec 2012 11:38:06 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Baltimore]]></category>
		<category><![CDATA[GIS]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=392</guid>
		<description><![CDATA[One of the key predictors in my model for this crime project I&#8217;m working on is vacant houses and lots. I&#8217;ll speak to some findings about the relationship between levels of the different types of crime and vacant property in...]]></description>
				<content:encoded><![CDATA[<p>One of the key predictors in my model for this crime project I&#8217;m working on is vacant houses and lots. I&#8217;ll speak to some findings about the relationship between levels of the different types of crime and vacant property in a later post. But I wanted to put some of these images up now, before I&#8217;m done with that, after a conversation at a party tonight.</p>
<p>This plot is of 15,928 vacant buildings and 17,169 vacant lots (according to the datasets <a href='https://data.baltimorecity.gov'>here</a>) across the city of Baltimore:<br />
<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/Vacants.png"><img class="aligncenter size-large wp-image-393" title="Vacants" src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/Vacants-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p>Here are visualizations of the 2-dimensional kernel density estimates for both of them. A density estimate essentially gives values at every point on a plane that communicate how close that point is to how many observations of the variable or point process you care about. So the more red, the more vacant properties are clustered together in that area.</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/vacLotsKde2d.png"><img class="aligncenter size-large wp-image-395" title="vacLotsKde2d" src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/vacLotsKde2d-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/VacBldgKde2d.png"><img class="aligncenter size-large wp-image-394" title="VacBldgKde2d" src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/VacBldgKde2d-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p>And here are kernel density visualizations for homicide and aggravated assault:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HomicideKde2d.png"><img class="aligncenter size-large wp-image-398" title="HomicideKde2d" src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HomicideKde2d-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/aggrAsltKde2d.png"><img class="aligncenter size-large wp-image-396" title="aggrAsltKde2d" src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/aggrAsltKde2d-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p>And if you&#8217;re interested, all the data is from <a href='data.baltimorecity.gov'>here</a> and here&#8217;s the code:</p>

<div class="wp_codebox"><table><tr id="p3926"><td class="code" id="p392code6"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## gis libraries</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>spBayes<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>MBA<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>geoR<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>fields<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>sp<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>maptools<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>rgdal<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>classInt<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>lattice<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>xtable<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>spatstat<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>splancs<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Other packages</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>foreign<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>stringr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>lubridate<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>plyr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>xtable<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>scales<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>RColorBrewer<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/grid.html"><span style="color: #0000FF; font-weight: bold;">grid</span></a><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggmap<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>gridExtra<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggmcmc<span style="color: #080;">&#41;</span>
&nbsp;
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/setwd.html"><span style="color: #0000FF; font-weight: bold;">setwd</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'/home/rmealey/Dropbox/school/gisClass/FinalProject'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/options.html"><span style="color: #0000FF; font-weight: bold;">options</span></a><span style="color: #080;">&#40;</span>digits<span style="color: #080;">=</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span>
&nbsp;
Save <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>projName<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	<span style="color: #0000FF; font-weight: bold;">savehistory</span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>projName,<span style="color: #ff0000;">'.Rhistory'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/save.image.html"><span style="color: #0000FF; font-weight: bold;">save.<span style="">image</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>projName,<span style="color: #ff0000;">'.RData'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
sv <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> Save<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'FinalProject'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># City Boundary Shape File</span>
city_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Baltcity_20Line/baltcity_line.dbf'</span><span style="color: #080;">&#41;</span>
city_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Baltcity_20Line'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'baltcity_line'</span><span style="color: #080;">&#41;</span>
origProj <span style="color: #080;">&lt;-</span> city_shp@proj4string <span style="color: #228B22;">## Store original projection</span>
<span style="color: #228B22;">#city_shp = spTransform(city_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span>
city_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>city_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span>
cityLineCoords <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>city_shp@<a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/lines.html"><span style="color: #0000FF; font-weight: bold;">lines</span></a><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>@Lines<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>@coords<span style="color: #080;">&#41;</span>
cityLinePoly <span style="color: #080;">&lt;-</span> Polygon<span style="color: #080;">&#40;</span>cityLineCoords<span style="color: #080;">&#41;</span>
cityLinePolys <span style="color: #080;">&lt;-</span> Polygons<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span>cityLinePoly<span style="color: #080;">&#41;</span>, ID<span style="color: #080;">=</span><span style="color: #ff0000;">'cityline'</span><span style="color: #080;">&#41;</span>
cityLineSpPoly <span style="color: #080;">&lt;-</span> SpatialPolygons<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span>cityLinePolys<span style="color: #080;">&#41;</span>,proj4string<span style="color: #080;">=</span>origProj<span style="color: #080;">&#41;</span>
&nbsp;
cityLineCoords <span style="color: #080;">&lt;-</span> cityLineCoords<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;">## Neighborhood Shape Files read in v1</span>
nbhds_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Neighborhood_202010/nhood_2010.dbf'</span><span style="color: #080;">&#41;</span>
nbhds_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Neighborhood_202010'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'nhood_2010'</span><span style="color: #080;">&#41;</span>
origProj <span style="color: #080;">&lt;-</span> nbhds_shp@proj4string <span style="color: #228B22;">## Store original projection</span>
<span style="color: #228B22;">#nbhds_shp = spTransform(nbhds_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span>
nbhds_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;">## Utility Functions</span>
&nbsp;
<span style="color: #228B22;">## Read lat/lng coords function</span>
str2LatLong <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>in_df<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	latlng <span style="color: #080;">&lt;-</span> str_replace<span style="color: #080;">&#40;</span>str_replace<span style="color: #080;">&#40;</span>in_df$Location.1,<span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>('</span>,<span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">')'</span>,<span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>
	latlng <span style="color: #080;">&lt;-</span> str_split<span style="color: #080;">&#40;</span>latlng,<span style="color: #ff0000;">', '</span><span style="color: #080;">&#41;</span>
	latlng_df <span style="color: #080;">&lt;-</span> ldply<span style="color: #080;">&#40;</span>latlng<span style="color: #080;">&#91;</span>in_df$Location.1 <span style="color: #080;">!=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	out_df <span style="color: #080;">&lt;-</span> in_df
	out_df$lat <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>latlng_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	out_df$long <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>latlng_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>out_df<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">## convert projection function</span>
convProj <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>in_df,in_proj,out_proj<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	latlong <span style="color: #080;">&lt;-</span> in_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
	latlong_spdf <span style="color: #080;">&lt;-</span> SpatialPoints<span style="color: #080;">&#40;</span>latlong, 
	proj4string<span style="color: #080;">=</span>in_proj<span style="color: #080;">&#41;</span>
	latlong_spdf <span style="color: #080;">&lt;-</span>  spTransform<span style="color: #080;">&#40;</span>latlong_spdf,out_proj<span style="color: #080;">&#41;</span>
	latlong_spdf_coords <span style="color: #080;">&lt;-</span> coordinates<span style="color: #080;">&#40;</span>latlong_spdf<span style="color: #080;">&#41;</span>
	out_df <span style="color: #080;">&lt;-</span> in_df
	out_df$long <span style="color: #080;">&lt;-</span> latlong_spdf_coords<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
	out_df$lat <span style="color: #080;">&lt;-</span> latlong_spdf_coords<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>out_df<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;">## Preprocess Crime Data</span>
crimeData <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/BPD_Part_1_Victim_Based_Crime_Data.csv'</span><span style="color: #080;">&#41;</span>
crimeData_NoCoords <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$Location.1 <span style="color: #080;">==</span> <span style="color: #ff0000;">''</span>,<span style="color: #080;">&#93;</span>
crimeData <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$Location.1 <span style="color: #080;">!=</span> <span style="color: #ff0000;">''</span>,<span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">## Get and convert projection</span>
crimeData <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>crimeData<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Incidents already in correct proj</span>
crimeData_ProjOrig <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$lat<span style="color: #080;">&gt;</span><span style="color: #ff0000;">100</span>,<span style="color: #080;">&#93;</span>
crimeData <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>crimeData$lat<span style="color: #080;">&lt;</span><span style="color: #ff0000;">100</span>,<span style="color: #080;">&#93;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
crimeData <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>crimeData, inProj, outProj<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Parse Dates</span>
crimeData$crimeDate2 <span style="color: #080;">&lt;-</span> parse_date_time<span style="color: #080;">&#40;</span>
	crimeData$crimeDate,
	orders<span style="color: #080;">=</span><span style="color: #ff0000;">'%m/%d/%Y'</span>
<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Get Burglary Incidents</span>
burg_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
burg_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>burg_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
burg_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>burg_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>burg_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Get Street Robbery Incidents</span>
robbStr_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">&quot;ROBBERY - STREET&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
robbStr_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>robbStr_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
robbStr_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>robbStr_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>robbStr_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Homicide</span>
homic_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
homic_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>homic_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
homic_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>homic_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>homic_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## Aggravated Assault</span>
aggrAslt_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData, description<span style="color: #080;">==</span><span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Hold Out 2012 Incidents</span>
aggrAslt_df_ho <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>aggrAslt_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
aggrAslt_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>aggrAslt_df, year<span style="color: #080;">&#40;</span>crimeDate2<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">'2012'</span><span style="color: #080;">&#41;</span> 
ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>aggrAslt_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long,y<span style="color: #080;">=</span>lat<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Religous Building Locations</span>
relig_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'geocoded/Religious_Buildings_gc.csv'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">## Remove na rows</span>
relig_df <span style="color: #080;">&lt;-</span> relig_df<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">complete.<span style="">cases</span></span><span style="color: #080;">&#40;</span>relig_df<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
relig_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>relig_df, inProj, outProj<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Police Station Locations</span>
police_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'geocoded/Police_Stations_gc.csv'</span><span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
police_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>police_df, inProj, outProj<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Hospitals Locations</span>
hospitals_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'geocoded/Hospitals.csv'</span><span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
hospitals_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>hospitals_df, inProj, outProj<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># CCTV Locations</span>
cams_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/CCTV_Locations.csv'</span><span style="color: #080;">&#41;</span>
cams_df <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>cams_df<span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
cams_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>cams_df, inProj, outProj<span style="color: #080;">&#41;</span>
cams_df$type <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">&quot;CCTV Camera&quot;</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Buildings</span>
vacantBuildings_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/Vacant_Buildings.csv'</span><span style="color: #080;">&#41;</span>
vacantBuildings_df <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>vacantBuildings_df<span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
vacantBuildings_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>vacantBuildings_df, inProj, outProj<span style="color: #080;">&#41;</span>
vacantBuildings_df$type <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Vacant Building'</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;"># Vacant Lots</span>
vacantLots_df <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/Vacant_Lots.csv'</span><span style="color: #080;">&#41;</span>
vacantLots_df <span style="color: #080;">&lt;-</span> str2LatLong<span style="color: #080;">&#40;</span>vacantLots_df<span style="color: #080;">&#41;</span>
inProj <span style="color: #080;">&lt;-</span> CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span>
outProj <span style="color: #080;">&lt;-</span> origProj
vacantLots_df <span style="color: #080;">&lt;-</span> convProj<span style="color: #080;">&#40;</span>vacantLots_df, inProj, outProj<span style="color: #080;">&#41;</span>
vacantLots_df$type <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'Vacant Lot'</span>
&nbsp;
<span style="color: #228B22;">########################################################################</span>
<span style="color: #228B22;">## Get kernel density estimates</span>
kde2dRange <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span>burg_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>, <span style="color: #ff0000;">2</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/range.html"><span style="color: #0000FF; font-weight: bold;">range</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
getKde <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>in_df, N<span style="color: #080;">=</span><span style="color: #ff0000;">400</span>, Lims<span style="color: #080;">=</span>kde2dRange<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	pts <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.matrix.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">matrix</span></span></a><span style="color: #080;">&#40;</span>in_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
	dens <span style="color: #080;">&lt;-</span> kde2d<span style="color: #080;">&#40;</span>pts<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,pts<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span>, n<span style="color: #080;">=</span>N, lims<span style="color: #080;">=</span>Lims<span style="color: #080;">&#41;</span>
	dens_df <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/expand.grid.html"><span style="color: #0000FF; font-weight: bold;">expand.<span style="">grid</span></span></a><span style="color: #080;">&#40;</span>dens$x, dens$y<span style="color: #080;">&#41;</span>, z <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>dens$z<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>dens_df<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'x'</span>,<span style="color: #ff0000;">'y'</span>,<span style="color: #ff0000;">'z'</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>dens_df<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
plotKde2d <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>in_df<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	fillCols <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rev.html"><span style="color: #0000FF; font-weight: bold;">rev</span></a><span style="color: #080;">&#40;</span>brewer.<span style="">pal</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">11</span>,<span style="color: #ff0000;">'Spectral'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>
		ggplot<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
		geom_tile<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span> <span style="color: #080;">=</span> in_df, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>x, y<span style="color: #080;">=</span>y, fill<span style="color: #080;">=</span>z, group<span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
		scale_fill_gradientn<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/summary.lm.html"><span style="color: #0000FF; font-weight: bold;">colours</span></a><span style="color: #080;">=</span>fillCols<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
		theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
		coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
saveKde2Plot <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>plotDf, plotName, plotTitle,titlCol<span style="color: #080;">=</span><span style="color: #ff0000;">'white'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	<span style="color: #228B22;"># https://github.com/wch/ggplot2/wiki/New-theme-system</span>
	new_theme_empty <span style="color: #080;">&lt;-</span> theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	new_theme_empty$line <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	new_theme_empty$rect <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	new_theme_empty$strip.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	new_theme_empty$axis.<span style="">text</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	new_theme_empty$plot.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	new_theme_empty$axis.<span style="">title</span> <span style="color: #080;">&lt;-</span> element_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	new_theme_empty$legend.<span style="">position</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">'none'</span>
	new_theme_empty$plot.<span style="">margin</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/structure.html"><span style="color: #0000FF; font-weight: bold;">structure</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">0</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span>, <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, unit <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;lines&quot;</span>, valid.<span style="">unit</span> <span style="color: #080;">=</span> 3L, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/class.html"><span style="color: #0000FF; font-weight: bold;">class</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;unit&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
	nbhds_pl_df2 <span style="color: #080;">&lt;-</span> nbhds_pl_df<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span>,<span style="color: #ff0000;">'group'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>nbhds_pl_df2<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'x'</span>,<span style="color: #ff0000;">'y'</span>,<span style="color: #ff0000;">'group'</span><span style="color: #080;">&#41;</span>
	plotKde2d<span style="color: #080;">&#40;</span>plotDf<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
		geom_path<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>nbhds_pl_df2,aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>x,y<span style="color: #080;">=</span>y,
		group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span>,color<span style="color: #080;">=</span><span style="color: #ff0000;">'black'</span>,alpha<span style="color: #080;">=</span><span style="color: #ff0000;">0.4</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
		new_theme_empty <span style="color: #080;">+</span> 
		annotate<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;text&quot;</span>, x <span style="color: #080;">=</span> <span style="color: #ff0000;">1405000</span>, y <span style="color: #080;">=</span> <span style="color: #ff0000;">568000</span>,
		label<span style="color: #080;">=</span>plotTitle, size<span style="color: #080;">=</span><span style="color: #ff0000;">8</span>, color<span style="color: #080;">=</span>titlCol<span style="color: #080;">&#41;</span>
		ggsave<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/'</span>,plotName,<span style="color: #ff0000;">'.png'</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">## Get all simple gaussian 2d kernel density estimates</span>
burgDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>burg_df<span style="color: #080;">&#41;</span> 				<span style="color: #228B22;">## Burglary, 7</span>
robbStrDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>robbStr_df<span style="color: #080;">&#41;</span> 			<span style="color: #228B22;">## Street Robbery, 7</span>
homicDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>homic_df<span style="color: #080;">&#41;</span> 				<span style="color: #228B22;">## Homicide, 7</span>
aggrAsltDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>aggrAslt_df<span style="color: #080;">&#41;</span> 			<span style="color: #228B22;">## Aggr Assault, 7</span>
hospitalsDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>hospitals_df<span style="color: #080;">&#41;</span>		<span style="color: #228B22;">## Hospitals</span>
policeDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>police_df<span style="color: #080;">&#41;</span>				<span style="color: #228B22;">## Police Stations</span>
religDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>relig_df<span style="color: #080;">&#41;</span>				<span style="color: #228B22;">## Religous Buildings</span>
camsDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>cams_df<span style="color: #080;">&#41;</span>					<span style="color: #228B22;">## Cameras, 1</span>
vacBldDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>vacantBuildings_df<span style="color: #080;">&#41;</span>	<span style="color: #228B22;">## Vacant Buildings, 5</span>
vacLotsDens <span style="color: #080;">&lt;-</span> getKde<span style="color: #080;">&#40;</span>vacantLots_df<span style="color: #080;">&#41;</span>		<span style="color: #228B22;">## Vacant Lots, 6</span>
&nbsp;
<span style="color: #228B22;">## plot densities</span>
saveKde2Plot<span style="color: #080;">&#40;</span>burgDens, <span style="color: #ff0000;">'BurglaryKde2d'</span>, <span style="color: #ff0000;">'Burglary<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>robbStrDens, <span style="color: #ff0000;">'StreetRobberyKde2d'</span>, <span style="color: #ff0000;">'Street<span style="color: #000099; font-weight: bold;">\n</span> Robbery<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>homicDens, <span style="color: #ff0000;">'HomicideKde2d'</span>, <span style="color: #ff0000;">'Homicide<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>aggrAsltDens, <span style="color: #ff0000;">'aggrAsltKde2d'</span>, <span style="color: #ff0000;">'Aggravated<span style="color: #000099; font-weight: bold;">\n</span> Assault<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>hospitalsDens, <span style="color: #ff0000;">'HospitalKde2d'</span>, <span style="color: #ff0000;">'Hospital<span style="color: #000099; font-weight: bold;">\n</span> Location<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>policeDens, <span style="color: #ff0000;">'PoliceKde2d'</span>, <span style="color: #ff0000;">'Police<span style="color: #000099; font-weight: bold;">\n</span> Station<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>religDens, <span style="color: #ff0000;">'ReligiousKde2d'</span>, <span style="color: #ff0000;">'Religous<span style="color: #000099; font-weight: bold;">\n</span> Building<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>camsDens, <span style="color: #ff0000;">'CCTVCamsKde2d'</span>, <span style="color: #ff0000;">'CCTV<span style="color: #000099; font-weight: bold;">\n</span> Cameras<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>vacBldDens, <span style="color: #ff0000;">'VacBldgKde2d'</span>, <span style="color: #ff0000;">'Vacant<span style="color: #000099; font-weight: bold;">\n</span> Building<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span>
saveKde2Plot<span style="color: #080;">&#40;</span>vacLotsDens, <span style="color: #ff0000;">'vacLotsKde2d'</span>, <span style="color: #ff0000;">'Vacant<span style="color: #000099; font-weight: bold;">\n</span> Lot<span style="color: #000099; font-weight: bold;">\n</span> Density'</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/12/10/visualizing-baltimore-2-vacant-property-and-some-more-crime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visualizing Baltimore with R and ggplot2: Crime Data</title>
		<link>http://www.obscureanalytics.com/2012/12/07/visualizing-baltimore-with-r-and-ggplot2-crime-data/</link>
		<comments>http://www.obscureanalytics.com/2012/12/07/visualizing-baltimore-with-r-and-ggplot2-crime-data/#comments</comments>
		<pubDate>Fri, 07 Dec 2012 06:05:51 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Baltimore]]></category>
		<category><![CDATA[GIS]]></category>
		<category><![CDATA[Incredible Overkill]]></category>
		<category><![CDATA[Rstats]]></category>
		<category><![CDATA[The Grand Narrative of My Life?]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=367</guid>
		<description><![CDATA[The advent of municipal open data initiatives has been both a blessing and curse for my particular brand of data nerd. On one hand, it has opened up the possibility of developing deep and useful knowledge about the places we...]]></description>
				<content:encoded><![CDATA[<p>The advent of municipal open data initiatives has been both a blessing and curse for my particular brand of data nerd. On one hand, it has opened up the possibility of developing deep and useful knowledge about the places we live and work. On the other, it opens up the possibility of starting projects to develop deep and useful knowledge about the places we live and work that inevitably get sidelined by the next deadline at work or by the basement that needs cleaning.</p>
<p>I collect such projects. There are about a dozen currently on a list that I have invested some amount of time in. At the current rate, I will finish about 12 by the time I die&#8230;but the list will have quadrupled.</p>
<p>My wife and I recently purchased a home in the Mount Vernon neighborhood of Baltimore, moving up from Washington, DC. One of Baltimore&#8217;s many nicknames is &quot;the City of Neighborhoods&quot;, and it is probably the most apt. The city is full of clusters, and arbitrary but obvious lines that separate this place from that place, and these people from those people.</p>
<p>The only exercise regime that I have been able to get myself to stick to over the years is running outside, no matter the weather. This is because the only way I can trick myself into keeping moving is to give myself an artificial destination somewhere X miles away or to give myself a direction to run in towards places I haven&#8217;t yet been. It&#8217;s a way for me to romanticize the process of making sure my stress levels stay manageable and my body doesn&#8217;t slowly atrophy in front of this computer.</p>
<p>This habit has allowed me to cross a lot of those lines in a relatively short time here and I&#8217;ve tried within reason to cross some that maybe white dudes in jogging pants aren&#8217;t expected to cross. No matter where you are in this city, one of those particular lines isn&#8217;t far and once you cross one, you know it.</p>
<p>All that to say that I&#8217;m currently finishing up an intro to analytics in GIS class, and for my final project I chose one of those interests I&#8217;d collected but done very little about: using the fantastic wealth of data <a href='https://data.baltimorecity.gov/'> here </a> to learn more about this city that I&#8217;m now calling home.</p>
<p>I&#8217;m building a lot of maps using good old <a href='http://docs.ggplot2.org/current/'> ggplot2 </a> for this project, and they&#8217;re so pretty. There&#8217;s already lots of ggplot2 mapping blog posts but in the interest of sharing that pretty, here&#8217;s another.</p>
<p>Obviously:</p>

<div class="wp_codebox"><table><tr id="p36720"><td class="code" id="p367code20"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## Crime Incident Plots</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>foreign<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>stringr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>lubridate<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>plyr<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>xtable<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>scales<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>RColorBrewer<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggmap<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">## gis libraries</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>maptools<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>sp<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>rgdal<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>spatstat<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Then pulling in the data &#8211; shape files &#8211; using some of the great (but mostly HORRIBLY documented) GIS packages available in R, first the city boundary:</p>

<div class="wp_codebox"><table><tr id="p36721"><td class="code" id="p367code21"><pre class="rsplus" style="font-family:monospace;">city_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Baltcity_20Line'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'baltcity_line'</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>and I store the original map projection. I&#8217;ve always had a bit of a map fetish, and learning details about the different projections have been way more fun than they should be. First thing to note is, these shapefiles are not in the latitude/longitude coordinate system. If I want to convert them to lat/long, there&#8217;s a function for that:</p>

<div class="wp_codebox"><table><tr id="p36722"><td class="code" id="p367code22"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#city_shp &lt;- spTransform(city_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span></pre></td></tr></table></div>

<p>But it&#8217;s commented out because I don&#8217;t want to do that. The projection they&#8217;re currently in allows me to treat the distances between points as though they were on a plane, as opposed to a sphere. This is ok as my window of analysis is fairly small (just Bmore) and makes clustering and model fitting much more simple mathematically. It allows me to use more general tools in that part of my analysis. In fact, I&#8217;ll store the original projection, and convert other data given to me in lat/long to it later on:</p>

<div class="wp_codebox"><table><tr id="p36723"><td class="code" id="p367code23"><pre class="rsplus" style="font-family:monospace;">origProj <span style="color: #080;">&lt;-</span> city_shp@proj4string <span style="color: #228B22;">## Store original projection</span></pre></td></tr></table></div>

<p>ggplot2 only takes data frames, so I gotta convert the shape files to a data frame representation:</p>

<div class="wp_codebox"><table><tr id="p36724"><td class="code" id="p367code24"><pre class="rsplus" style="font-family:monospace;">city_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>city_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>For all the city-wide plots, I use the city line as the first layer, so I&#8217;m going to store it as my &quot;bound&quot; blot and gray out the surrounding area in the plot background:</p>

<div class="wp_codebox"><table><tr id="p36725"><td class="code" id="p367code25"><pre class="rsplus" style="font-family:monospace;">bound_plot <span style="color: #080;">&lt;-</span> ggplot<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>city_pl_df, 
aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>long, y<span style="color: #080;">=</span>lat, group<span style="color: #080;">=</span>group<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
geom_polygon<span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span><span style="color: #ff0000;">'gray'</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'lightblue'</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
coord_equal<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> theme_nothing<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>By itself, eh:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/cityBoundary.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/cityBoundary.png" alt="" title="cityBoundary" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>So how about all those neighborhoods then? Pull in the shape files and convert them to a data frame the same way:</p>

<div class="wp_codebox"><table><tr id="p36726"><td class="code" id="p367code26"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## Neighborhood Shape Files read in v1</span>
nbhds_df <span style="color: #080;">&lt;-</span> read.<span style="">dbf</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Neighborhood_202010/nhood_2010.dbf'</span><span style="color: #080;">&#41;</span>
nbhds_shp <span style="color: #080;">&lt;-</span> readOGR<span style="color: #080;">&#40;</span>dsn<span style="color: #080;">=</span><span style="color: #ff0000;">'Neighborhood_202010'</span>, layer<span style="color: #080;">=</span><span style="color: #ff0000;">'nhood_2010'</span><span style="color: #080;">&#41;</span>
origProj <span style="color: #080;">&lt;-</span> nbhds_shp@proj4string <span style="color: #228B22;">## Store original projection</span>
<span style="color: #228B22;">#nbhds_shp &lt;- spTransform(nbhds_shp,CRS(&quot;+proj=longlat +datum=WGS84&quot;))</span>
nbhds_pl_df <span style="color: #080;">&lt;-</span> fortify<span style="color: #080;">&#40;</span>nbhds_shp, region<span style="color: #080;">=</span><span style="color: #ff0000;">'LABEL'</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>and THIS is why Baltimore is the &quot;City of Neighborhoods&quot;:</p>

<div class="wp_codebox"><table><tr id="p36727"><td class="code" id="p367code27"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## plot nbhd boundaries</span>
nbhds_plot <span style="color: #080;">&lt;-</span> bound_plot <span style="color: #080;">+</span> 
geom_path<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>nbhds_pl_df,color<span style="color: #080;">=</span><span style="color: #ff0000;">'gray'</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/nbhds.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/nbhds.png" alt="" title="nbhds" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>I&#8217;m looking at lots of different datasets for this project. Some are point datasets, like 311 calls and crime incidents. Some are region or place data, like building footprints, or land use. And others are pre-summarized data by area, like demographic or economic data at the census block group or neighborhood level. Visualizing your data is important in all types of analysis, but in GIS data, it&#8217;s essential. For instance, crime incidents. The crime data <a href='https://data.baltimorecity.gov/Crime/BPD-Part-1-Victim-Based-Crime-Data/wsfq-mvij'> here </a> locked and loaded like:</p>

<div class="wp_codebox"><table><tr id="p36728"><td class="code" id="p367code28"><pre class="rsplus" style="font-family:monospace;">crimeData <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">csv</span></span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'OpenDataSets/BPD_Part_1_Victim_Based_Crime_Data.csv'</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>The data are 285,415 individual incidents reported by victims of crime, in the categories:</p>
<ul>
<li>
AGG. ASSAULT: 31,507 incidents
</li>
<li>
ARSON: 1,948 incidents incidents
</li>
<li>
AUTO THEFT: 2,6954 incidents incidents
</li>
<li>
BURGLARY: 4,5168 incidents
</li>
<li>
COMMON ASSAULT: 54,226 incidents
</li>
<li>
HOMICIDE: 1,342 incidents
</li>
<li>
LARCENY: 57,247 incidents
</li>
<li>
LARCENY FROM AUTO: 40,260 incidents
</li>
<li>
RAPE: 1,170 incidents
</li>
<li>
ROBBERY &#8211; CARJACKING: 1,225 incidents
</li>
<li>
ROBBERY &#8211; COMMERCIAL: 3,592 incidents
</li>
<li>
ROBBERY &#8211; RESIDENCE: 2,720 incidents
</li>
<li>
ROBBERY &#8211; STREET: 15,288 incidents
</li>
<li>
SHOOTING: 2,768 incidents
</li>
</ul>
<p>The coordinates are given as text, so:</p>

<div class="wp_codebox"><table><tr id="p36729"><td class="code" id="p367code29"><pre class="rsplus" style="font-family:monospace;">latlng <span style="color: #080;">&lt;-</span> str_replace<span style="color: #080;">&#40;</span>str_replace<span style="color: #080;">&#40;</span>crimeData$Location.1,<span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>('</span>,<span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">')'</span>,<span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>
latlng <span style="color: #080;">&lt;-</span> str_split<span style="color: #080;">&#40;</span>latlng,<span style="color: #ff0000;">', '</span><span style="color: #080;">&#41;</span>
latlng_df <span style="color: #080;">&lt;-</span> ldply<span style="color: #080;">&#40;</span>latlng<span style="color: #080;">&#91;</span>crimeData$Location.1 <span style="color: #080;">!=</span> <span style="color: #ff0000;">''</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
crimeData$lat <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>latlng_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
crimeData$long <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>latlng_df<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>The coordinates are given mostly (4,477 rows with no coordinates, and 6 rows in the same projection as the shapefiles) in latitude/longitude, and like I said before, distance between two points in lat/long gives distance on the surface of a sphere. so I gotta convert it:</p>

<div class="wp_codebox"><table><tr id="p36730"><td class="code" id="p367code30"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## Convert lat/long to maryland grid</span>
latlng_df2 <span style="color: #080;">&lt;-</span> crimeData<span style="color: #080;">&#91;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'long'</span>,<span style="color: #ff0000;">'lat'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
latlng_spdf <span style="color: #080;">&lt;-</span> SpatialPoints<span style="color: #080;">&#40;</span>latlng_df2, 
proj4string<span style="color: #080;">=</span>CRS<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;+proj=longlat +datum=WGS84&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
latlng_spdf <span style="color: #080;">&lt;-</span>  spTransform<span style="color: #080;">&#40;</span>latlng_spdf,origProj<span style="color: #080;">&#41;</span>
latlng_spdf_coords <span style="color: #080;">&lt;-</span> coordinates<span style="color: #080;">&#40;</span>latlng_spdf<span style="color: #080;">&#41;</span>
crimeData$long <span style="color: #080;">&lt;-</span>  latlng_spdf_coords<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
crimeData$lat <span style="color: #080;">&lt;-</span>  latlng_spdf_coords<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span></pre></td></tr></table></div>

<p>When I&#8217;m doing this kind of exploratory visualization, I like to store my plot parameters in a named list like this:</p>

<div class="wp_codebox"><table><tr id="p36731"><td class="code" id="p367code31"><pre class="rsplus" style="font-family:monospace;">crimeCols <span style="color: #080;">&lt;-</span> brewer.<span style="">pal</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span>,<span style="color: #ff0000;">'Paired'</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeTypes <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'RAPE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ARSON'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'COMMON ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AGG. ASSAULT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'SHOOTING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'HOMICIDE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - STREET'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - CARJACKING'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - RESIDENCE'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'ROBBERY - COMMERCIAL'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">7</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'BURGLARY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">9</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'AUTO THEFT'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,
<span style="color: #ff0000;">'LARCENY FROM AUTO'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#93;</span>,crimeCols<span style="color: #080;">&#91;</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
crimeTypeNames <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>crimeTypes<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Because that lets me loop through and plot all the subsets much more easily.</p>

<div class="wp_codebox"><table><tr id="p36732"><td class="code" id="p367code32"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">## By crime type</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>crimeType <span style="color: #0000FF; font-weight: bold;">in</span> crimeTypeNames<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
<span style="color: #228B22;">## All Incidents Densities</span>
    ttl <span style="color: #080;">&lt;-</span> str_replace_all<span style="color: #080;">&#40;</span>str_replace_all<span style="color: #080;">&#40;</span>crimeType, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span>s'</span>, <span style="color: #ff0000;">'_'</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">'_-_'</span>,<span style="color: #ff0000;">'_'</span><span style="color: #080;">&#41;</span>
    crimeDataSubset <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>crimeData,
    <span style="color: #080;">&#40;</span>description<span style="color: #080;">==</span>crimeType<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    p <span style="color: #080;">&lt;-</span> nbhds_plot <span style="color: #080;">+</span> 
    geom_point<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>crimeDataSubset,aes<span style="color: #080;">&#40;</span>group<span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, 
        shape<span style="color: #080;">=</span><span style="color: #ff0000;">'x'</span>,
        color<span style="color: #080;">=</span>crimeTypes<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>crimeType<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>,
        alpha<span style="color: #080;">=</span><span style="color: #ff0000;">'.8'</span>, guide<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/F.html"><span style="color: #0000FF; font-weight: bold;">F</span></a><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
    stat_density2d<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>crimeDataSubset,aes<span style="color: #080;">&#40;</span>group<span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, 
    color <span style="color: #080;">=</span> crimeTypes<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>crimeType<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
    annotate<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;text&quot;</span>, x <span style="color: #080;">=</span> <span style="color: #ff0000;">1405000</span>, y <span style="color: #080;">=</span> <span style="color: #ff0000;">565000</span>,
    label<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span>
    str_replace_all<span style="color: #080;">&#40;</span>str_replace<span style="color: #080;">&#40;</span>ttl, <span style="color: #ff0000;">'_'</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">'_'</span>,<span style="color: #ff0000;">' '</span><span style="color: #080;">&#41;</span>
    , sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">8</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
    ggsave<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'img/'</span>,ttl,<span style="color: #ff0000;">'.png'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>The loop above plots incidents and 2d kernel density estimates for all the crime types, allowing us to compare and contrast. </p>
<p>This allows us to see that while people get beat up all over the city&#8230;</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/AGG._ASSAULT.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/AGG._ASSAULT.png" alt="" title="AGG_ASSAULT" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>&#8230;they really get shot and/or killed in mostly just East or West Baltimore.</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/SHOOTING.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/SHOOTING.png" alt="" title="SHOOTING" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a><br />
<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HOMICIDE.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/HOMICIDE.png" alt="" title="HOMICIDE" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>And while people steal FROM cars downtown a lot&#8230;</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/LARCENY_FROM_AUTO.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/LARCENY_FROM_AUTO.png" alt="" title="LARCENY_FROM_AUTO" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>&#8230;they steal the cars themselves pretty much everywhere BUT downtown.</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/AUTO_THEFT.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/AUTO_THEFT.png" alt="" title="AUTO_THEFT" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>And other, very similar city wide patterns for larceny vs burglary:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/LARCENY.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/LARCENY.png" alt="" title="LARCENY" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a><br />
<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/BURGLARY.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/BURGLARY.png" alt="" title="BURGLARY" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>The different types of robbery: first, where the people are&#8230; </p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_COMMERCIAL.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_COMMERCIAL.png" alt="" title="ROBBERY_COMMERCIAL" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a><br />
<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_STREET.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_STREET.png" alt="" title="ROBBERY_STREET" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>&#8230;and then where the property is&#8230; </p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_CARJACKING.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_CARJACKING.png" alt="" title="ROBBERY_CARJACKING" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a><br />
<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_RESIDENCE.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/12/ROBBERY_RESIDENCE.png" alt="" title="ROBBERY_RESIDENCE" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>I know, I know. Everyone plots crime data. Boring. I&#8217;ll put up some of the other stuff I&#8217;ve been doing for this project as well. But I gotta tease it out, you know?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/12/07/visualizing-baltimore-with-r-and-ggplot2-crime-data/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Data Science Presentation Slides</title>
		<link>http://www.obscureanalytics.com/2012/10/02/data-science-presentation-slides/</link>
		<comments>http://www.obscureanalytics.com/2012/10/02/data-science-presentation-slides/#comments</comments>
		<pubDate>Tue, 02 Oct 2012 15:40:15 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Incredible Overkill]]></category>
		<category><![CDATA[Laplacian Ambitions]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=277</guid>
		<description><![CDATA[Thanks to everyone for coming out. Slides can be found here. Doesn&#8217;t work well in mobile and touchscreen browsers. Code for simulation can be found here. Code for polling data example can be found here. I learned a lot from...]]></description>
				<content:encoded><![CDATA[<p>Thanks to everyone for coming out.</p>
<p>Slides can be found <a href='http://obscureanalytics.com/dsdcSlides/polished.html'>here</a>. Doesn&#8217;t work well in mobile and touchscreen browsers.</p>
<p>Code for simulation can be found <a href='https://github.com/robbymeals/dsdcSimulation'>here</a>.</p>
<p>Code for polling data example can be found <a href='https://github.com/robbymeals/dsdcElectionExample'>here</a>.</p>
<p>I learned a lot from giving the talk and really appreciated the opportunity.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/10/02/data-science-presentation-slides/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Topic Modeling 1: Simulated LDA Corpus</title>
		<link>http://www.obscureanalytics.com/2012/09/07/topic-modeling-1-simulated-lda-corpus/</link>
		<comments>http://www.obscureanalytics.com/2012/09/07/topic-modeling-1-simulated-lda-corpus/#comments</comments>
		<pubDate>Fri, 07 Sep 2012 04:27:28 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Incredible Overkill]]></category>
		<category><![CDATA[Laplacian Ambitions]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=245</guid>
		<description><![CDATA[Because I am self-taught in many of the areas of computer science and more advanced statistics and probability theory I am most interested in, and because I have a deep aversion both to looking foolish and being full of it...]]></description>
				<content:encoded><![CDATA[<p>Because I am self-taught in many of the areas of computer science and more advanced statistics and probability theory I am most interested in, and because I have a deep aversion both to looking foolish and being full of it (in no way related to any long, troubled history of doing both or anything), I tend to try, whenever possible, to do things the hard way.</p>
<p>This means I use the command line for way more stuff than is healthy and I often rewrite when I could just copy and paste, obnoxious things like that. It also means that, when I&#8217;m learning a new model or tool, I try and write elaborate simulations that I usually never share to make sure I actually understand what&#8217;s going in and what&#8217;s coming out. This also happens when I&#8217;m trying to explain a concept or problem to someone, as evidenced by the earlier Monty Hall Monte Carlo post. You&#8217;ll have to ask my coworkers, friends and family members whether this makes me more capable at imparting any of this knowledge to others or doing useful things with it, I guess. (On second thought, don&#8217;t.) Part of the point of this site is to share these instances of &#8220;Incredible Overkill&#8221;, as one of my professors once called it in the (unlikely?) event that someone else may find them useful.</p>
<p>Probabilistic topic modeling was added to my ever-growing list of obsessions in the last year and I have spent a lot of time consuming research papers, tutorials and tool documentation in an effort to apply this exciting area of research to problems at work and in personal projects. <a href="http://en.wikipedia.org/wiki/Topic_model">Wikipedia</a> defines a topic model as &#8220;a type of statistical model for discovering abstract &#8216;topics&#8217; that occur in a collection of documents.&#8221; The research in this area is quite new, with the major developments of Probabilistic Latent Semantic Indexing and the most common topic model, Latent Dirichlet allocation models, in 1999 and 2003, respectively. The chief developer of the Latent Dirichlet allocation models, <a href="http://www.cs.princeton.edu/~blei/">David Blei of Princeton&#8217;s computer science department</a>, has written many useful and accessible treatments of the technique, such as those available <a href="http://www.cs.princeton.edu/~blei/papers/Blei2012.pdf">here</a>, <a href="http://www.cs.princeton.edu/~blei/kdd-tutorial.pdf">here</a>, and <a href="http://www.cs.princeton.edu/~blei/papers/BleiLafferty2009.pdf">here</a>. Many of the most exciting areas of research in computational linguistics involve extensions of LDA, and many of those areas are being pursued by talented local machine learning, computer science and computation linguistics professionals and academics in the Baltimore/Washington DC area, where I call home.</p>
<p>This is the first of a set of R scripts I wrote a couple months ago, in an effort to understand all the moving parts and assumptions of the basic model and understand what I am putting in and what I am getting out. When I do these things for myself, I will usually lay out the basic model in (excruciating?) detail and attempt to examine the assumptions inherent in it, write code that simulates data generated according to those assumptions and that fits the model to that simulated data. I usually also try to also break those assumptions and see how those affect the model output. The output of this effort is usually not fit for public consumption &#8211; though it is ALWAYS well commented &#8211; so this post is the result of a fair amount of polishing. That said, it could still be wrong in any number of ways and if you think it is, please do let me know.</p>
<p>This simulated generative process is based essentially on Blei&#8217;s descriptions and equations in the papers linked above. As he describes it:</p>
<blockquote><p>
&#8220;We formally define a topic to be a distribution over a fixed vocabulary&#8230; </p>
<p>We assume that these topics are specified before any data has been generated.</p>
<p>Now for each document in the collection, we generate the words in a two-stage process.</p>
<ol>
<li>Randomly choose a distribution over topics.</li>
<li>For each word in the document:
<ol>
<li>Randomly choose a topic from the distribution over topics in step #1.</li>
<li>Randomly choose a word from the corresponding distribution over the vocabulary.</li>
</ol>
<p></item>
</ol>
</blockquote>
<p>Code that simulates an LDA corpus and source simulation function:</p>

<div class="wp_codebox"><table><tr id="p24534"><td class="code" id="p245code34"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">### Basic LDA Topic Model Simulation ###</span>
<span style="color: #228B22;">### Generate Simulated Corpus ###</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>tm<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>MCMCpack<span style="color: #080;">&#41;</span>
&nbsp;
simulateCorpus <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>
	M, <span style="color: #228B22;"># number of documents</span>
	nTerms, 
	docLengths, 
	K,  	<span style="color: #228B22;"># Number of Topics</span>
	alphA, 	<span style="color: #228B22;"># parameter for symmetric </span>
                <span style="color: #228B22;"># Document/Topic dirichlet distribution</span>
	betA, 	<span style="color: #228B22;"># parameter for Topic/Term dirichlet distribution</span>
	Alpha<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#40;</span>alphA,K<span style="color: #080;">&#41;</span>, <span style="color: #228B22;"># number-of-topics length vector </span>
                            <span style="color: #228B22;"># set to symmetric alpha parameter </span>
                            <span style="color: #228B22;"># across all topics</span>
	Beta<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#40;</span>betA,nTerms<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>  <span style="color: #228B22;"># number-of-terms length vector </span>
                                <span style="color: #228B22;"># set to symmetric beta parameter </span>
                                <span style="color: #228B22;"># across all terms</span>
	<span style="color: #080;">&#123;</span>
	<span style="color: #228B22;"># Labels</span>
	Terms <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Term&quot;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>nTerms<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	Topics <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Topic&quot;</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>K<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
	Documents <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Document&quot;</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>M<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
	<span style="color: #228B22;">## Generate latent topic and term distributions</span>
	<span style="color: #228B22;"># &quot;True&quot; Document/Topic distribution matrix</span>
	Theta <span style="color: #080;">&lt;-</span> rdirichlet<span style="color: #080;">&#40;</span>M, Alpha<span style="color: #080;">&#41;</span> 
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>Theta<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Topics
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>Theta<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Documents
&nbsp;
	<span style="color: #228B22;"># &quot;True&quot; Topic/Term Distribution Matrix</span>
	Phi <span style="color: #080;">&lt;-</span> rdirichlet<span style="color: #080;">&#40;</span>K, Beta<span style="color: #080;">&#41;</span> 
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>Phi<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Terms
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>Phi<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Topics
&nbsp;
	<span style="color: #228B22;">## Function to generate individual document</span>
	generateDoc <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>docLength, topic_dist, terms_topics_dist<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
		<span style="color: #228B22;"># docLength is specific document length</span>
		<span style="color: #228B22;"># topic_dist is specific topic distribution for this document</span>
		<span style="color: #228B22;"># terms_topics_dist is terms distribution matrix over all topics</span>
		document <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
		<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>docLength<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
			<span style="color: #228B22;"># For each word in a document, </span>
			<span style="color: #228B22;"># choose a topic from that </span>
			<span style="color: #228B22;"># document's topic distribution</span>
			topic <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rmultinom</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">1</span>, topic_dist<span style="color: #080;">&#41;</span> 
&nbsp;
			<span style="color: #228B22;"># Then choose a term from that topic's term distribution</span>
			term <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rmultinom</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>, <span style="color: #ff0000;">1</span>, terms_topics_dist<span style="color: #080;">&#91;</span>topic,<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span> 
&nbsp;
			<span style="color: #228B22;"># and append term to document vector</span>
			document <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>document, 
			<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>terms_topics_dist<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/which.max.html"><span style="color: #0000FF; font-weight: bold;">which.<span style="">max</span></span></a><span style="color: #080;">&#40;</span>term<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span> 
			<span style="color: #080;">&#125;</span>
		<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>document<span style="color: #080;">&#41;</span>
		<span style="color: #080;">&#125;</span>
&nbsp;
	<span style="color: #228B22;">## generate &quot;observed&quot; corpus as list of terms</span>
	corpus <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>M<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
		corpus<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> generateDoc<span style="color: #080;">&#40;</span>docLengths<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>, Theta<span style="color: #080;">&#91;</span>i,<span style="color: #080;">&#93;</span>, Phi<span style="color: #080;">&#41;</span>
		<span style="color: #080;">&#125;</span>
&nbsp;
	<span style="color: #228B22;">## convert document term vectors to frequency vectors</span>
	freqsLists <span style="color: #080;">&lt;-</span> llply<span style="color: #080;">&#40;</span>corpus, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/table.html"><span style="color: #0000FF; font-weight: bold;">table</span></a><span style="color: #080;">&#41;</span>
&nbsp;
	<span style="color: #228B22;">## write values to termFreqMatrix</span>
	termFreqMatrix <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="color: #0000FF; font-weight: bold;">matrix</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span>M, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a><span style="color: #080;">=</span>nTerms, <span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>termFreqMatrix<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Terms
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rownames.html"><span style="color: #0000FF; font-weight: bold;">rownames</span></a><span style="color: #080;">&#40;</span>termFreqMatrix<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Documents
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>M<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
		termFreqMatrix<span style="color: #080;">&#91;</span>i,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>freqsLists<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> freqsLists<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
		<span style="color: #080;">&#125;</span>
&nbsp;
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/stopifnot.html"><span style="color: #0000FF; font-weight: bold;">stopifnot</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rowSums.html"><span style="color: #0000FF; font-weight: bold;">rowSums</span></a><span style="color: #080;">&#40;</span>termFreqMatrix<span style="color: #080;">&#41;</span> <span style="color: #080;">==</span> docLengths<span style="color: #080;">&#41;</span>
&nbsp;
	<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;docs&quot;</span><span style="color: #080;">=</span>corpus, 
	<span style="color: #ff0000;">'termFreqMatrix'</span><span style="color: #080;">=</span>termFreqMatrix, 
	<span style="color: #ff0000;">&quot;Theta&quot;</span><span style="color: #080;">=</span>Theta, 
	<span style="color: #ff0000;">&quot;Phi&quot;</span><span style="color: #080;">=</span>Phi<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>I will finish polishing the inferential and viz code and put that up shortly. But you should be able, if you&#8217;re exploring topic models, to use this code to see how best to tune the canned R packages and play with the &#8220;true&#8221; hyperparameters and see what effect they have on the output. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/09/07/topic-modeling-1-simulated-lda-corpus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>To the Basics: Bayesian Inference on A Binomial Proportion</title>
		<link>http://www.obscureanalytics.com/2012/07/04/to-the-basics-bayesian-inference-on-a-binomial-proportion/</link>
		<comments>http://www.obscureanalytics.com/2012/07/04/to-the-basics-bayesian-inference-on-a-binomial-proportion/#comments</comments>
		<pubDate>Wed, 04 Jul 2012 06:40:05 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Laplacian Ambitions]]></category>
		<category><![CDATA[Rstats]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=3</guid>
		<description><![CDATA[Think of something observable &#8211; countable &#8211; that you care about with only one outcome or another. It could be the votes cast in a two-way election in your town, or the free throw shots the center on your favorite...]]></description>
				<content:encoded><![CDATA[<p>Think of something observable &#8211; countable &#8211; that you care about with only one outcome or another.</p>
<p>It could be the votes cast in a two-way election in your town, or the free throw shots the center on your favorite basketball team takes, the survival of the people diagnosed with a specific form of cancer after five years, all the red/black bets ever placed on a specific roulette wheel, the gender of all the children in 18th century France; many phenomena in the world either fit this discription or can be thought of in this way. The most important thing is that the outcome of this phenomenon &#8211; or the way you group the outcomes &#8211; can only take one of two values: $$A$$ or $$B$$:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PossibleOutcomes.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PossibleOutcomes-1024x614.png" alt="" title="PossibleOutcomes" width="1024" height="614" class="aligncenter size-large wp-image-241" /></a></p>
<p>Let&#8217;s call this phenomenon $$Z$$.</p>
<p>Let&#8217;s also define a number, $$N$$, as the total number of occurances of $$Z$$. So it is the total number of shots taken, the total count of people diagnosed, the total number of votes cast.</p>
<p>Now remember, $$Z$$ is something you care about. It is likely, since you care so much about $$Z$$, that you have some preference for one of the two possible outcomes. For the purposes of this example, let&#8217;s say we prefer $$A$$. $$A$$ is a shot made, a vote recieved, a survival. $$A$$ is a &#8220;success&#8221;, from your perspective. And an outcome of $$B$$ is a failure.</p>
<p>Lets define another value, given that we prefer $$A$$ outcomes over $$B$$ outcomes. $$Y$$ is the number of $$A$$ outcomes out of all the times $$N$$ happens. It is the number of votes that your candidate is going to get in that election, the number of free throws the big man makes, the number of cancer patients who survive.</p>
<p>Of course, we can define this kind of process in this way even if we don&#8217;t actually prefer one outcome over the other. In the case of the gender of newborns, for instance: we can define $$A$$ as the birth of a girl and $$B$$ as the birth of a boy, and treat the birth of a boy as a failure, just for the purposes of our model and not because all little boys are born clinically insane.</p>
<p>You, as a keen, long-time observer of $$Z$$, probably have some opinion on the share of $$A$$ outcomes in the total of all occurrences of $$Z$$ that you likely express as an opinion on the value of the ratio of $$A$$ outcomes to all occurances of $$Z$$, or $$Y/N$$, e.g.:</p>
<ul>
<li>More often than not, $$Z$$ equals $$B$$ or $$Y/N &lt; 0.5$$</li>
<li>Most of the time, $$Z$$ equal $$A$$ or $$Y/N &gt; 0.5$$</li>
<li>$$Z$$ is as likely to equal $$A$$ as it is to equal $$B$$ or $$Y/N = 0.5$$</li>
</ul>
<p>It would be nice, probably, to have an actual number as an estimate for $$Y/N$$, or perhaps a range of numbers you can be confident contains the value of $$Y/N$$.</p>
<p>And perhaps you want to make some prediction about future occurances of $$Z$$. You want to know if someone you know with that particular form of cancer is likely to still be alive five years from now.</p>
<p>Or maybe your friend, also a fan of that same basketball team, thinks that your guy actually only misses about half the time. Or a political talking head says that  your candidate is going to lose big. You probably want a way to compare your beliefs with theirs.</p>
<p>Essentially, we would like to estimate the unknown quantity $$\theta$$, preferably with some additional estimate of our uncertainty of this value, use that estimate to predict future values of trials of $$Z$$, and, given that estimate, get an idea of who is more likely right about that quantity given disagreements.</p>
<p>Translating our above situation into the language of probability, this phenomenon $$Z$$ &#8211; any phenomenon with a &#8220;this&#8221; or &#8220;that&#8221; outcome &#8211; can be modeled mathematically as a &#8220;random variable&#8221; $$Y$$, with a binomial probability distribution:</p>
<p>$$p(Y) = {N \choose Y} \theta^Y (1-\theta)^{(N-Y)}$$</p>
<p>where $$N$$ is number of trials &#8211; the total number of cancer patients, shots taken, votes cast &#8211; $$Y$$ is the number of successes or cases where the outcome equals A, and $$\theta$$ is that unknown value between 0 and 1 that equals the proportion $$Y/N$$. This distribution describes the probability that $$Z$$ equals $$A$$ $$Y$$ times in $$N$$  occurances (also called trials) of $$Z$$.</p>
<p>$$Z$$ can also be modeled as the product of $$N$$ individual occurances of $$Y$$, $$y_i$$, where $$y_i$$ equals 1 if the outcome equals A and 0 if the outcome equals B, and  $$\theta$$ is still the unknown proportion of A outcomes in all occurances of $$Y$$:</p>
<p>$$p(z_i=A) = p(y_i)$$</p>
<p>$$p(y_i) = \theta^{y_i} (1-\theta)^{(1-y_i)}$$</p>
<p>This formulation &#8211; really a special case of the binomial distribution where N equals 1 &#8211; is often refered to a bernoulli random variable. The product of $$N$$ individual occurances &#8211; $$y_1, y_2, y_3, &#8230;, y_N $$- is also equal to the conditional probability of the total number of successes Y on the value of $$\theta$$, because the probability of a number of independent events occuring together is the product of all of their individual probabilities:</p>
<p>$$p(Y) = \Pi_{i=1}^{N} \theta^{y_i} (1-\theta)^{(1-y_i)}$$</p>
<p>And in fact this product simplifies to the formula for the binomial distribution above.</p>
<p>It is important to realize that the value most useful for us to know the most about in the formulas above is not $$N$$ or $$Y$$ or any single $$y_i$$ but $$\theta$$.</p>
<p>To illustrate all of this further, I&#8217;m going to let R simulate an &#8220;true&#8221;, unknown $$\theta$$ or $$Y/N$$ and hide that value from myself as a fixed quantity $$\theta_{true}$$:</p>
<script src="https://gist.github.com/3985488.js?file=thetaTrue.R"></script><noscript><pre><code class="language-r r">######### 1. Unknown Probability of Success ########
## Using built-in R pseudo-random number generator
theta_true &lt;- runif(1,0,1)</code></pre></noscript>
<p>That function call generates a single pseudo-random number between 0 and 1 from a uniform distribution, meaning that the value is equally as likely to be anywhere in that (0,1) interval.</p>
<p>I&#8217;m going to use that hidden proportion to generate a &#8220;true population&#8221; Z, with an unknown N total number of occurances, distributed according to our hidden $$\theta_{true}$$:</p>
<script src="https://gist.github.com/3985488.js?file=genPop.R"></script><noscript><pre><code class="language-r r">## Generate population
N = sample(seq(100000,400000),1)
A = round(theta_true*N)
B = N - A
Zpop &lt;- sample(c(rep(1,A),rep(0,B)))</code></pre></noscript>
<p>Since Z is very large, and we are pretending it is not data in memory on my computer but a set of outcomes that are very difficult or impossible to count in their entireity, we will be working with a sample pulled from Z, some small subset that we can count and infer something about the value of $$\theta_{true}$$ from. But as I said above, we know something about Z already. We follow Z very closely. We talk to everyone we know about the election, even if we don&#8217;t keep a running tally of who they say they&#8217;re voting for. We watch every game, even if we don&#8217;t have an exact count of shots missed vs shots made. We have some prior understanding of Z already. Of course, most of the time, our first prior looks something like this:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PopulationPlot4.png"><img class="aligncenter size-large wp-image-201" title="PopulationPlot" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PopulationPlot4-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p>I can&#8217;t calculate $$\theta_{true}$$ from that understanding, at least not just by looking at it. It probably wouldn&#8217;t be possible to count all the $$A$$ outcomes up there, given how jumbled they are, and even if it was, there&#8217;s no way I can count them in any reasonable time. Maybe most of them haven&#8217;t even happened yet, in which case I definitely can&#8217;t count them. Actually, I don&#8217;t even know what the total value of N is.</p>
<p>But I can tell some things from that picture, right? For instance, I know that there are definitely at least some trials where the outcome of $$Z$$ is $$A$$. I know that there are at least some trials where the outcome of $$Z$$ is $$B$$. In this case, actually, $$A$$ outcomes look pretty rare and it seems a pretty safe bet to say even more than that: our boy misses most of his free throws, or our guy is going to lose this election.</p>
<p>Lets get our sample, which we will call $$N_{samp}$$, from our population:</p>
<script src="https://gist.github.com/3985488.js?file=popSamp.R"></script><noscript><pre><code class="language-r r">### Pull random sample from population
N_samp &lt;- 500
Zsamp &lt;- sample(Zpop,N_samp)</code></pre></noscript>
<p>In this case, we were able to obtain a sample of 500 trials of Z, and they look, before we do anything to them, like this:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/SamplePlot1.png"><img class="aligncenter size-large wp-image-203" title="SamplePlot" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/SamplePlot1-1024x614.png" alt="" width="1024" height="614" /></a></p>
<p>Since our random variable stores $$A$$ outcomes as successes or 1&#8242;s and $$B$$ outcomes as failures or 0&#8242;s, we can easily obtain our sample $$Y$$ &#8211; $$Y_{samp}$$ by summing up all our $$N_{samp}$$ occurances of $$Z$$.</p>
<script src="https://gist.github.com/3985488.js?file=Y_samp.R"></script><noscript><pre><code class="language-r r">Y_samp &lt;- sum(Zsamp)</code></pre></noscript>
<p>Which in this case turns out to be 26.</p>
<p>We could just calculate the sample proportion of $$Y_{samp}/N_{samp}$$ and take that for our estimate of $$\theta_{true}$$. In this case, that would be 0.052.</p>
<p>But that estimate only allows us to achieve part of one of our three goals above. We can&#8217;t really compare our opinions to anyone elses with any meaning, and we can&#8217;t use that number by itself to predict future values.</p>
<p>But what if we treat our $$\theta = Y/N$$ parameter as a random variable? What if we assign it a &#8216;mean&#8217; or a most likely value, and a variance, or some quantification of uncertainty around that mean?</p>
<p>If we have probability distributions for all of our values of interest, we can use Bayes theorem:</p>
<p>$$p(Y_{samp},N_{samp},\theta_{true}) = p(Y_{samp},N_{samp}|\theta_{true}) p(\theta_{true})$$</p>
<p>$$p(\theta_{true},Y_{samp},N_{samp}) = p(\theta_{true}|Y_{samp}) p(Y_{samp})$$</p>
<p>$$p(Y_{samp},\theta_{true}) = p(\theta_{true},Y_{samp})$$</p>
<p>$$p(Y_{samp},N_{samp}|\theta_{true}) p(\theta_{true}) = p(\theta_{true}|Y_{samp},N_{samp}) p(Y_{samp},N_{samp})$$</p>
<p>$$p(\theta_{true}|Y_{samp},N_{samp}) = \frac{p(Y_{samp},N_{samp}|\theta_{true}) p(\theta_{true})}{p(Y_{samp},N_{samp})}$$</p>
<p>In this case, the posterior probabilility is the conditional probability distribution we get for $$\theta_{true}$$ given the data $$Y,N$$ and our prior distribution for $$\theta_{true}$$.</p>
<p>The generalization of Bayes&#8217; theorem for use in inference involving the entire probability distribution of a random variable instead of just a point estimate of a probability allows us to, in essence, ignore the term $$ p(Y_{samp},N_{samp}) $$ in the expression:</p>
<p>$$p(\theta_{true}|Y_{samp},N_{samp}) = \frac{p(Y_{samp},N_{samp}|\theta_{true}) p(\theta_{true})}{p(Y_{samp},N_{samp})}$$</p>
<p>because we know that it, with respect to the conditional distribution of $$\theta$$ &#8211; $$p(\theta_{true}|Y_{samp},N_{samp})$$ &#8211; is just a constant. And since $$p(\theta_{true}|Y_{samp},N_{samp})$$ is a probability distribution, we know that it has to integrate to 1 in the end, so determining that normalizing constant after we have the non-normalized distribution shouldn&#8217;t be a problem.</p>
<p>This allows us to work with the proportional relationship, giving us our model:</p>
<p>$$p(\theta_{true}|Y_{samp},N_{samp}) \propto p(\theta_{true}) p(Y_{samp},N_{samp}|\theta_{true})$$</p>
<p>or:</p>
<h3>The posterior is proportional to the product of the prior and the likelihood.</h3>
<p>This is the central &#8211; really the only &#8211; tool of Bayesian statistical inference. And it suggests one of the central appeals, to me, of the approach: every input into a Bayesian framework is expressed as probability and every output of a Bayesian framework is expressed as probability.</p>
<p>To use this generalization of Bayes&#8217; theorem to answer our above questions, we first need to come up with a model for $$\theta_{true}$$&#8217;s distribution. There are a number of ways that $$\theta_{true}$$ could be distributed. In fact, any distribution that ensures that the value of $$\theta_{true}$$ will be between 0 and 1 will do.</p>
<p>In this first example, we will take advantage of the fact that there exists aconjugateprior for the binomial distribution: the beta distribution.</p>
<p>The beta distribution</p>
<p>$$p(\theta) = \theta^{\alpha-1} (1-\theta)^{\beta-1}$$</p>
<p>looks very similar in form to the binomial distribution</p>
<p>$$p(Y) = {N \choose Y} \theta^Y (1-\theta)^{(N-Y)}$$</p>
<p>except it represents the probabilities assigned to values of $$\theta$$ in the domain $$(0,1)$$ given values for the parameters $$\alpha$$ and $$\beta$$, as opposed to the binomial distribution above, which represents the probability of values of $$Y$$ given $$\theta$$.</p>
<p>The concept of conjugacy is fairly simple. It just means that the functional forms of the distributions of which you are calculating the product are the same, so they multiply easily. The product of a beta and a binomial, given their identical functional forms, is simply:</p>
<p>$$\theta^{\alpha-1} (1-\theta)^{\beta-1} * {N \choose Y} \theta^Y (1-\theta)^{(N-Y)} = {N \choose Y} \theta^{(Y+\alpha-1)} (1-\theta)^{(N-Y+\beta-1)}$$</p>
<p>and since $${N \choose Y}$$ is just a constant in relationship to $$\theta$$, our final Bayes formulation of our beta prior, binomial likelihood model is:</p>
<p>$$p(\theta_{true}|Y_{samp},N_{samp}) \propto \theta^{(Y+\alpha-1)} (1-\theta)^{(N-Y+\beta-1)}$$</p>
<p>This also is a beta probability distribution, with $$\alpha_{posterior}$$ equal to $$Y+\alpha_{prior}$$ and $$\beta_{posterior}$$ equal to $$N-Y+\beta_{prior}$$.</p>
<p>But how do we choose our beta priors?</p>
<p>The shape of a beta distribution is dictated by the values of those $$\alpha$$ and $$\beta$$ parameters and shifting those values can allow you to represent a wide range of different prior beliefs about the distribution of $$\theta$$. Priors can be &#8220;uninformative&#8221; or &#8220;informative&#8221;, meaning we can weight our prior probabilities very low in relationship to the data or we can weight them higher, informing our outcome &#8211; the posterior &#8211; more as we weight them more.</p>
<p>A simple function &#8211; using ggplot2&#8242;s qplot &#8211; to examine different values of $$\alpha$$ and $$\beta$$ and their effect on the shape of the distribution allows us to show this:</p>
<script src="https://gist.github.com/3985488.js?file=betaplot.R"></script><noscript><pre><code class="language-r r">betaplot &lt;- function(a,b){
theta = seq(0,1,0.005)
p_theta = dbeta(theta, a, b)
p &lt;- qplot(theta, p_theta, geom='line')
p &lt;- p + theme_bw()
p &lt;- p + ylab(expression(paste('p(',theta,')', sep = '')))
p &lt;- p + xlab(expression(theta))
return(p)}</code></pre></noscript>
<p>Setting $$\alpha$$ and $$\beta$$ both equal to 1 gives us an non-informative uniform prior, allowing us to express that we believe $$\theta_{true}$$ could be anywhere in the interval $$(0,1)$$ with equal probability, meaning that the proportion of successes to failures &#8211; A outcomes to B outcomes &#8211; could be anything:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/UniformBeta.png"><img class="aligncenter size-large wp-image-169" title="UniformBeta" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/UniformBeta-1024x409.png" alt="" width="1024" height="409" /></a></p>
<p>Setting $$\alpha$$ and $$\beta$$ both equal to 0.5 gives us an weakly informative uniform prior that expresses a belief that $$\theta_{true}$$ is more likely to be at either extreme end of the distribution than anywhere in the center of it, meaning it is more likely that we get all successes or all failures than it is we get some more even mixture of outcomes:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/BimodalBeta.png"><img class="aligncenter size-large wp-image-170" title="BimodalBeta" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/BimodalBeta-1024x409.png" alt="" width="1024" height="409" /></a></p>
<p>Setting $$\alpha$$ and $$\beta$$ both equal to a high value gives us an more strongly informative prior expressing that we believe that $$\theta_{true}$$ is likely to be at the center or that it is equally likely to see successes and failures:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongCentralBeta.png"><img class="aligncenter size-large wp-image-172" title="StrongCentralBeta" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongCentralBeta-1024x409.png" alt="" width="1024" height="409" /></a></p>
<p>We could express a stronger belief that $$\theta_{true}$$ is high &#8211; that success is very likely &#8211; with a higher $$\alpha$$ and a lower $$\beta$$:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongHighSuccessBeta.png"><img class="aligncenter size-large wp-image-173" title="StrongHighSuccessBeta" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongHighSuccessBeta-1024x409.png" alt="" width="1024" height="409" /></a></p>
<p>or a stronger belief that $$\theta_{true}$$ is very low &#8211; that success is unlikely &#8211; with a lower $$\alpha$$ and a higher $$\beta$$:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongLowSuccessBeta.png"><img class="aligncenter size-large wp-image-174" title="StrongLowSuccessBeta" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongLowSuccessBeta-1024x409.png" alt="" width="1024" height="409" /></a></p>
<p>Essentially, higher values of the ratio of $$\alpha$$ to $$\beta$$ weights higher values of $$\theta$$ higher, lower values of that ratio place greater weight on lower $$\theta$$ values, and higher value of $$\alpha + \beta$$ indicates higher certainty.</p>
<p>Still, choosing these $$\alpha$$&#8217;s and $$\beta$$&#8217;s may seem a bit arbitrary. Perhaps a more intuitive way to choose an informative prior is to allow ourselves the ability to calculate analogous values to $$\theta_{true}$$ and $$N$$ &#8211; essentially a value that actually quantifies our prior belief about the likelihood of success and a value that quantifies how strongly we weigh that belief as a prior &#8220;sample size&#8221;. We want to be able to express the &#8216;mean&#8217; of our prior distribution &#8211; its most likely value &#8211; and something like a variance or how tightly clustered it is around that mean.</p>
<p>The mean of a beta distribution is:</p>
<p>$$m = \frac{\alpha}{\alpha + \beta}$$</p>
<p>and the &#8220;sample size&#8221; $$n$$ is:</p>
<p>$$n = \alpha + \beta$$</p>
<p>and solving those two equations for $$\alpha$$ and $$\beta$$ gives us</p>
<p>$$\alpha = n * m $$<br />
$$\beta = n * (1 &#8211; m)$$</p>
<p>where, again, $$n$$ expresses how large our prior &#8220;sample size&#8221; is &#8211; i.e. the higher it is, the stronger our beliefs &#8211; and $$m$$ expresses our actual prior belief for the value of $$\theta_{true}$$.</p>
<p>Getting the values for our prior distribution using any chosen values for $$m$$ and $$n$$ can be acomplished with a simple R function:</p>
<script src="https://gist.github.com/3987839.js?file=prior.R"></script><noscript><pre><code class="language-r r">### Function: Prior Plot Values
prior &lt;- function(m,n){
a = n * m
b = n * (1 - m)
dom &lt;- seq(0,1,0.005)
val &lt;- dbeta(dom,a,b)
return(data.frame('x'=dom, 'y'=val))
}</code></pre></noscript>
<p>And expressing the likelihood &#8211; a binomial &#8211; as a beta where $$\alpha$$ equals $$Y + 1$$ and $$\beta$$ equals $$N &#8211; Y + 1$$ is another simple function.</p>
<script src="https://gist.github.com/3987839.js?file=likelihood.R"></script><noscript><pre><code class="language-r r">### Function: Likelihood Plot Values
likelihood &lt;- function(N,Y){
a &lt;- Y + 1
b &lt;- N - Y + 1
dom &lt;- seq(0,1,0.005)
val &lt;- dbeta(dom,a,b)
return(data.frame('x'=dom, 'y'=val))
}</code></pre></noscript>
<p>And combining them into the posterior beta distribution:</p>
<script src="https://gist.github.com/3987839.js?file=posterior.R"></script><noscript><pre><code class="language-r r">### Function: Posterior Plot Values
posterior &lt;- function(m,n,N,Y){
a &lt;- Y + (n*m) -1
b &lt;- N - Y + (n*(1-m)) - 1
dom &lt;- seq(0,1,0.005)
val &lt;- dbeta(dom,a,b)
return(data.frame('x'=dom, 'y'=val))
}</code></pre></noscript>
<p>and getting the mean:</p>
<script src="https://gist.github.com/3987839.js?file=mean_of_posterior.R"></script><noscript><pre><code class="language-r r">### Function: Mean of Posterior Beta
mean_of_posterior &lt;- function(m,n,N,Y){
a &lt;- Y + (n*m) -1
b &lt;- N - Y + (n*(1-m)) - 1
E_posterior &lt;- a / (a + b)
return(E_posterior)
}</code></pre></noscript>
<p>the mode:</p>
<script src="https://gist.github.com/3987839.js?file=mode_of_posterior.R"></script><noscript><pre><code class="language-r r">### Function: Mode of Posterior Beta
mode_of_posterior &lt;- function(m,n,N,Y){
a &lt;- Y + (n*m) -1
b &lt;- N - Y + (n*(1-m)) - 1
mode_posterior &lt;- (a-1)/(a+b-2)
return(mode_posterior)
}</code></pre></noscript>
<p>and the standard deviation of the posterior:</p>
<script src="https://gist.github.com/3987839.js?file=sd_of_posterior.R"></script><noscript><pre><code class="language-r r">### Function: Std Dev of Posterior Beta
sd_of_posterior &lt;- function(m,n,N,Y){
a &lt;- Y + (n*m) -1
b &lt;- N - Y + (n*(1-m)) - 1
sigma_posterior &lt;- sqrt((a*b)/(((a+b)^2)*(a+b+1)))
return(sigma_posterior)
}</code></pre></noscript>
<p>can all be accomplished using functions of similar structure.</p>
<p>First, we generate a model with a uniform prior:</p>
<script src="https://gist.github.com/3985488.js?file=unif_prior.R"></script><noscript><pre><code class="language-r r">m = 0.5
n = 2
pr &lt;- prior(m,n)
lk &lt;- likelihood(N_samp,Y_samp)
po &lt;- posterior(m,n,N_samp,Y_samp)
model_plot &lt;- data.frame('Dist'=c(rep('Prior',nrow(pr)), 
rep('Likelihood',nrow(lk)), 
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist &lt;- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po &lt;- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po &lt;- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po &lt;- sd_of_posterior(m,n,N_samp,Y_samp)</code></pre></noscript>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/UniformPrior1.png"><img class="aligncenter size-large wp-image-205" title="UniformPrior" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/UniformPrior1-1024x614.png" alt="" width="1024" height="614" /></a></p>
<p>where the dotted gray lines indicate the outer bounds of our credibility interval and the dotted blue line indicates our mean.</p>
<p>The mean of our posterior distribution equals 0.052, the mode equals 0.05, and the standard deviation equals 0.01.</p>
<p>This gives us a 95% (normal-approximation) credibility interval of 0.033 to 0.071.</p>
<p>Our posterior and our likelihood distributions are almost identical, as would be expected, since our prior is essentially that we have no idea and the data should give us all of the information in our posterior.</p>
<p>A weak, equal probability prior gives:</p>
<script src="https://gist.github.com/3985488.js?file=weak_eq_prior.R"></script><noscript><pre><code class="language-r r">m = 0.5
n = 10
pr &lt;- prior(m,n)
lk &lt;- likelihood(N_samp,Y_samp)
po &lt;- posterior(m,n,N_samp,Y_samp)
model_plot &lt;- data.frame('Dist'=c(rep('Prior',nrow(pr)), 
rep('Likelihood',nrow(lk)), 
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist &lt;- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po &lt;- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po &lt;- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po &lt;- sd_of_posterior(m,n,N_samp,Y_samp)&lt;/code&gt;</code></pre></noscript>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/WeakEqualProportions1.png"><img class="aligncenter size-large wp-image-209" title="WeakEqualProportions" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/WeakEqualProportions1-1024x614.png" alt="" width="1024" height="614" /></a></p>
<p>The mean of our posterior distribution equals 0.059, the mode equals 0.057, and the standard deviation equals 0.01.</p>
<p>This gives us a 95% (normal-approximation) credibility interval of 0.039 to 0.08.</p>
<p>Our mean and mode is a bit higher than before, as we weighted our prior beliefs a little bit, but our posterior is very close to our likelihood, meaning that most of the result was informed by the data.</p>
<p>A strong equal prior gives:</p>
<script src="https://gist.github.com/3985488.js?file=str_eq_prior.R"></script><noscript><pre><code class="language-r r">m = 0.5
n = 500
pr &lt;- prior(m,n)
lk &lt;- likelihood(N_samp,Y_samp)
po &lt;- posterior(m,n,N_samp,Y_samp)
model_plot &lt;- data.frame('Dist'=c(rep('Prior',nrow(pr)), 
rep('Likelihood',nrow(lk)), 
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist &lt;- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po &lt;- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po &lt;- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po &lt;- sd_of_posterior(m,n,N_samp,Y_samp)&lt;/code&gt;</code></pre></noscript>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongEqualProportions1.png"><img class="aligncenter size-large wp-image-206" title="StrongEqualProportions" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/StrongEqualProportions1-1024x614.png" alt="" width="1024" height="614" /></a></p>
<p>The mean of our posterior distribution equals 0.276, the mode equals 0.275, and the standard deviation equals 0.014.</p>
<p>This gives us a 95% (normal-approximation) credibility interval of 0.248 to 0.303.</p>
<p>A model with a medium, high success prior looks like:</p>
<script src="https://gist.github.com/3985488.js?file=med_high_prior.R"></script><noscript><pre><code class="language-r r">m = 0.95
n = 100
pr &lt;- prior(m,n)
lk &lt;- likelihood(N_samp,Y_samp)
po &lt;- posterior(m,n,N_samp,Y_samp)
model_plot &lt;- data.frame('Dist'=c(rep('Prior',nrow(pr)), 
rep('Likelihood',nrow(lk)), 
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist &lt;- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po &lt;- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po &lt;- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po &lt;- sd_of_posterior(m,n,N_samp,Y_samp)</code></pre></noscript>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/MediumHighProportions1.png"><img class="aligncenter size-large wp-image-211" title="MediumHighProportions" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/MediumHighProportions1-1024x614.png" alt="" width="1024" height="614" /></a></p>
<p>The mean of our posterior distribution equals 0.201, the mode equals 0.2, and the standard deviation equals 0.016.</p>
<p>This gives us a 95% (normal-approximation) credibility interval of 0.169 to 0.233.</p>
<p>And finally a medium, low success proportion prior:</p>
<script src="https://gist.github.com/3985488.js?file=med_low_prior.R"></script><noscript><pre><code class="language-r r">m = 0.05
n = 100
pr &lt;- prior(m,n)
lk &lt;- likelihood(N_samp,Y_samp)
po &lt;- posterior(m,n,N_samp,Y_samp)
model_plot &lt;- data.frame('Dist'=c(rep('Prior',nrow(pr)), 
rep('Likelihood',nrow(lk)), 
rep('Posterior',nrow(po))),
rbind(pr,lk,po))
with(model_plot, Dist &lt;- factor(Dist, levels = c('Prior', 'Likelihood',
'Posterior'), ordered = TRUE))
mean_po &lt;- mean_of_posterior(m,n,N_samp,Y_samp)
mode_po &lt;- mode_of_posterior(m,n,N_samp,Y_samp)
sd_po &lt;- sd_of_posterior(m,n,N_samp,Y_samp)&lt;/code&gt;</code></pre></noscript>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/MediumLowProportions1.png"><img class="aligncenter size-large wp-image-212" title="MediumLowProportions" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/MediumLowProportions1-1024x614.png" alt="" width="1024" height="614" /></a></p>
<p>The mean of our posterior distribution equals 0.05, the mode equals 0.049, and the standard deviation equals 0.009.</p>
<p>This gives us a 95% (normal-approximation) credibility interval of 0.033 to 0.068.</p>
<p>So how did each of our models do, in this case? Well, since we simulated this data, we can discover that $$\theta_{true}$$ actually equals 0.042, or about 4% of all occurances of $$Z$$ result in $$A$$ outcomes.</p>
<p>The results of all of our models are:</p>
<p>&nbsp;</p>
<table>
<tbody>
<tr>
<td align="right"></td>
<td align="right">Mean of Dist</td>
<td align="right">Mode of Dist</td>
<td align="right">Std Dev of Dist</td>
</tr>
<tr>
<td align="right">Uniform Prior</td>
<td align="right">0.052</td>
<td align="right">0.050</td>
<td align="right">0.010</td>
</tr>
<tr>
<td align="right">Weak, Equal Proportions</td>
<td align="right">0.059</td>
<td align="right">0.057</td>
<td align="right">0.010</td>
</tr>
<tr>
<td align="right">Strong, Equal Proportions</td>
<td align="right">0.276</td>
<td align="right">0.275</td>
<td align="right">0.014</td>
</tr>
<tr>
<td align="right">Medium, High Success</td>
<td align="right">0.201</td>
<td align="right">0.200</td>
<td align="right">0.016</td>
</tr>
<tr>
<td align="right">Medium, Low Success</td>
<td align="right">0.050</td>
<td align="right">0.049</td>
<td align="right">0.009</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>And it is obvious that our two initial priors &#8211; the non-informative uniform and the weakly informative equal proportions &#8211; and our last prior &#8211; the medium confidence of a low proportion of success outcomes &#8211; all gave fairly accurate estimates of $$\theta_{true}$$.</p>
<p>Our strong, equal proportions prior and medium, high sucess prior &#8211; what could be called, with a terminology nod to John Myles White, our &#8220;strong, wrong&#8221; priors &#8211; gave pretty bad estimates, obviously, though the likelihood moved our posterior much closer to the truth in the second case, and our updated belief is much better in both cases than where we started.</p>
<p>This susceptability to strong, wrong priors is a common critique of Bayesian inference. But these results aren&#8217;t incorrect, are they? My results in the cases of my strong, wrong priors are the correct highest probability distributions of $$\theta_{true}$$ <strong>conditional on my incorrect priors</strong>. But I would be an idiot to choose those priors, given what I already know about $$\theta$$, which is, remember:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PopulationPlot4.png"><img class="aligncenter size-large wp-image-201" title="PopulationPlot" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PopulationPlot4-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p>And if I didn&#8217;t know anything about $$\theta_{true}$$ or I had an idea but not a lot of confidence in it, why wouldn&#8217;t I choose either of my first two priors, both of which arrived at perfectly serviceable estimates for $$\theta_{true}$$?</p>
<p>As importantly, in both my strong, wrong priors, my assumptions are clearly stated and easy to interpret and critique. If I published something using those assumptions, and everyone and their mother could just look out and see that:</p>
<p><a href="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PopulationPlot4.png"><img class="aligncenter size-large wp-image-201" title="PopulationPlot" src="http://www.obscureanalytics.com/wp-content/uploads/2012/07/PopulationPlot4-1024x1024.png" alt="" width="1024" height="1024" /></a></p>
<p>It would be easy to establish that my analysis was based on those flawed assumptions. It could even be done by someone who has only a cursory understanding of how I actually arrived at those estimates.</p>
<p>In future posts, I would like to continue this example and examine the effects of smaller sample sizes and the ease of updating beliefs using a series of smaller samples, much as Kruschke and Bolstadt do in their texts. I think their choices for visualizations and some of the explanations in Bolstadt are sometimes more confusing than necessary, and establishing a stronger single thread through the explanations would make things more intuitive, so I&#8217;m going to attempt to actually do that here. Krushke and Gelman have excellent explanations for all of this that are well worth the read. I would also like to look at comparing beliefs and predicting future values in binomial proportions as well.</p>
<p>At his seminar on Bayesian methods back in April, John Myles White said something about traditional statisticians being better at actually getting things done over the course of the development of modern statistics. I didn&#8217;t really understand what he meant until recently.</p>
<p>The basic toolkit of Bayesian statistics produces intuitive, easier to understand &#8211; and use and update and compare &#8211; outputs through comparatively difficult computational and mathematical procedures. Everything in and out of a Bayesian analysis is probability and can be combined or broken apart according to the rules of probability. But understanding code and sampling algorithms &#8211; really understanding algorithms and computation generally &#8211; and a much deeper grasp of probability distribution theory are much more important in understanding Bayesian inference much earlier on.</p>
<p>Basic traditional statistical methods produce output that is fairly difficult to understand through comparatively simple computational and mathematical procedures. Most results in traditional statistics depend on logical appeals to unseen &#8211; really un-see-able &#8211; asymptotic properties of the estimators being used and assumptions and relationships between samples and populations that may be valid or not in any given case.</p>
<p>This is a very real catch-22: always easier to understand and use, much harder to do initially versus always harder to understand and use, much easier to do initially. I think that much of the difficulty so many have when faced with statistics comes from the fact that traditional OUTPUTS are so unintuitive and seem to exist in isolation or only in relationship to something with a touch of the `other&#8217; about it.</p>
<p>The concept of maximum likelihood and the MLE methods that comprise the basis of much of traditional methods are very elegant &#8211; actually quite beautiful &#8211; logical constructs that manage to give one the ability to say SOMETHING when faced with the problem of lots of data and not a lot of computational power.</p>
<p>But that&#8217;s not our problem anymore. Now we have lots of data AND lots of computational power. Our problem now is statistical literacy, and building on the body of human knowledge in a way that is both rigorous and democratic.</p>
<p>Credit where credit is due: I&#8217;ve studied and digested the work of the following to learn this stuff and everything I&#8217;m going to post here and much of what I use on a daily basis in my work:</p>
<ul>
<li>Hadley Wickam&#8217;s ggplot2 &#8211; this R package is something of an obsession of mine. Learn it well and essentially any static visualization is available to you. Incredibly powerful tool.</li>
<li>Scott Lynch&#8217;s <em>Introduction to Applied Bayesian Statistics and Estimation for Social Scientists</em> &#8211; This book wasn&#8217;t on my original list, but it has become my first stop. Especially if you think in code instead of equations, his explanations are fantastic and his walkthroughs of sampling algorithms and MCMC are great. It can be purchased <a href=http://www.amazon.com/Introduction-Statistics-Estimation-Scientists-Behavioral/dp/038771264X/ref=tmm_hrd_title_0?ie=UTF8&#038;qid=1281996492&#038;sr=8-2>here</a>.
</li>
</ul>
<p>and of course the Bolstadt, Kruschke and Gelman books and the work of John Myles White mentioned in my initial post <a title="By the books..." href="http://www.obscureanalytics.com/?p=54">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/07/04/to-the-basics-bayesian-inference-on-a-binomial-proportion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Monty Hall Monte Carlo, Part 1? (Oh God)</title>
		<link>http://www.obscureanalytics.com/2012/05/21/a-monty-hall-monte-carlo-part-1-oh-god/</link>
		<comments>http://www.obscureanalytics.com/2012/05/21/a-monty-hall-monte-carlo-part-1-oh-god/#comments</comments>
		<pubDate>Mon, 21 May 2012 04:44:37 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Incredible Overkill]]></category>
		<category><![CDATA[Rstats]]></category>
		<category><![CDATA[The Grand Narrative of My Life?]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=70</guid>
		<description><![CDATA[While I dig into conjugacy and the calculation of Bayesian credibility intervals, I figured it&#8217;d be good to put some of my other little rabbit holes up here on the off chance they&#8217;re interesting to someone. For some reason I...]]></description>
				<content:encoded><![CDATA[<p>While I dig into conjugacy and the calculation of Bayesian credibility intervals, I figured it&#8217;d be good to put some of my other little rabbit holes up here on the off chance they&#8217;re interesting to someone. For some reason I have been in the position of attempting to explain the Monty Hall problem and its curious result to a few people in the last month (me, the reason is me, I am the reason, I randomly bring it up in conversation apropos of nothing).</p>
<p>For anyone who doesn&#8217;t know, the Monty Hall problem is a now classic probability thought experiment that goes something like:</p>
<blockquote><p>You are a contestant on the famous television game show &#8220;Lets Make A Deal&#8221;. The host of the show, Monty Hall, points towards a wall with three doors, numbered 1, 2 and 3, and says, &#8220;Behind two of those doors is your garden variety mountain goat, completely worthless to you, but behind just one of those doors is A NEW CAR!&#8221;. He then tells you to pickadooranydoor, after which instead of opening the door you chose, he proceeds to open one of the two remaining doors, revealing a goat behind. He then gives you the chance to either stay on your door and claim the prize behind, or switch to the only remaining door and claim the prize behind that. The question is: should you switch?</p></blockquote>
<p>If you&#8217;re like most people, and you haven&#8217;t seen this before in some probability lecture, you&#8217;d likely say that it doesn&#8217;t matter, that either door is likely to contain the car or the goat. 50/50. But actually, the logical and mathematical answer to the question is that <strong>it is ALWAYS better to switch</strong>. </p>
<p>You have a one-in-three chance of choosing the right door in the first place and so a two-in-three chance of choosing the wrong door. It is more likely that you choose the wrong door initially. If you choose the right door initially, Monty can open either of the other two doors to reveal a goat and if you switch you will lose. If you choose the wrong door initially, Monty only has one door left to open to reveal a goat, as he can&#8217;t open your door and the other door reveals the car. </p>
<p>Probabilistically, after Monty opens that door, the door you chose initially still has that same one-in-three chance of containing a car. But now the door that is left has all of that left-over TWO-in-three chance of containing that car because of the initial two-in-three chance that YOU chose the wrong door. So always switch.</p>
<p>The initial reaction of my wife and a few other people to this was something along the lines of: &#8220;Idon&#8217;tlikethatthatmakesmeangrystoptalking.&#8221; To be fair, that is most people&#8217;s reaction to my nonsense. Nevertheless, it got me thinking about how best to visualize this problem to make the result more intuitive.</p>
<p>Aside: There is at least one other thing that SHOULD bother you about all this. A goat is ONLY completely worthless to silly fat Americans, right? I mean, yes, most people would value a new car over a goat, but it&#8217;s a bit much to equate a GOAT with WORTHLESS. You can literally live off a goat. Drink its milk. Make cheese that people will buy for ten dollars and call &#8216;chev-RA&#8217; or however you pronounce that. Goat meat is delicious when cooked right. A goat definitely is not worthless.</p>
<p>Okay. So my first attempt was just to code up the simulation (a &#8220;Monte Carlo&#8221; simulation, as it&#8217;s referred to, hence the clever title that has probably been used ten thousand times) in R:</p>

<div class="wp_codebox"><table><tr id="p7037"><td class="code" id="p70code37"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">########## Simulation Loop #########</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
<span style="color: #228B22;">## 1. Randomly place prize behind one of three doors</span>
PlacePrize <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sample.html"><span style="color: #0000FF; font-weight: bold;">sample</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
<span style="color: #228B22;">## 2. Randomly pick one of three doors</span>
YouPick <span style="color: #080;">&lt;-</span> Doors<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sample.html"><span style="color: #0000FF; font-weight: bold;">sample</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
<span style="color: #228B22;">## 3. Monty either randomly opens one of the two doors left over if</span>
<span style="color: #228B22;">## you happen to pick the correct door or picks the only door left</span>
<span style="color: #228B22;">## if you pick one of two incorrect doors</span>
MontyOpens <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span>PlacePrize<span style="color: #080;">&#91;</span>Doors<span style="color: #080;">==</span>YouPick<span style="color: #080;">&#93;</span><span style="color: #080;">==</span><span style="color: #ff0000;">1</span>,
Doors<span style="color: #080;">&#91;</span><span style="color: #080;">!</span>Doors<span style="color: #080;">%</span>in<span style="color: #080;">%</span>YouPick<span style="color: #080;">&#93;</span><span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sample.html"><span style="color: #0000FF; font-weight: bold;">sample</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>,
Doors<span style="color: #080;">&#91;</span><span style="color: #080;">&#40;</span><span style="color: #080;">!</span>Doors<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>YouPick,Doors<span style="color: #080;">&#91;</span>PlacePrize<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
PrizeIsBehind <span style="color: #080;">&lt;-</span> Doors<span style="color: #080;">&#91;</span>PlacePrize<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
<span style="color: #228B22;">## 4. If the prize is behind the leftover door, you win if you switch.</span>
<span style="color: #228B22;">## Else you win if you stick on your original choice.</span>
WinIfSwitch <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span>PlacePrize<span style="color: #080;">&#91;</span><span style="color: #080;">!</span>Doors<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>YouPick,MontyOpens<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">==</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
Picks <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>Picks, YouPick<span style="color: #080;">&#41;</span>
Opens <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>Opens, MontyOpens<span style="color: #080;">&#41;</span>
WinningDoor <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>WinningDoor, PrizeIsBehind<span style="color: #080;">&#41;</span>
WinsIfSwitch <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>WinsIfSwitch, WinIfSwitch<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">### Write results to data frames</span>
PlacedDf<span style="color: #080;">&#91;</span>i,<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> PlacePrize
PicksDf<span style="color: #080;">&#91;</span>i,YouPick<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">2</span>
OpensDf<span style="color: #080;">&#91;</span>i,MontyOpens<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">3</span><span style="color: #080;">&#125;</span>
<span style="color: #228B22;">########## End Simulation Loop #########</span></pre></td></tr></table></div>

<p>The key in the above code is that all the choices, the placement of the prize, your choice of a door, your choice to switch, are random EXCEPT Monty&#8217;s choice of which door to open. That choice is conditional on your choice, as if you choose the wrong door, he is constrained to only one of the two remaining. That&#8217;s why #3 is not just a simple random function call, it is a conditional statement on the results of a random function.</p>
<p>Plotting the results, first of three trials or runs of the simulation, gives us:<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms3.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms3-1024x614.png" alt="" title="MontyMonteHistograms3" width="900" height="539" class="aligncenter size-large wp-image-81" /></a></p>
<p>In which, probably confusingly, randomness didn&#8217;t go our way and for all three trials, we would have won if we switched doors. That&#8217;s the thing about probability, though. It is not that the course of action a probability-based analysis suggests always turns out to be the right one each time an event occurs, but that in the aggregate, given what you know about the situation and the type of uncertainty you are dealing with, the course of action suggested is the right one to take BEFORE you know the outcome.</p>
<p>For 10 trials:<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms10.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms10-1024x614.png" alt="" title="MontyMonteHistograms10" width="900" height="539" class="aligncenter size-large wp-image-81" /></a></p>
<p>Also likely to be confusing, as in these particular 10 runs, we got unlucky again, and only 50 percent of the time we were likely to win if we switched. </p>
<p>But as n (number of trials) gets higher and higher, the result becomes more and more obvious, first for 100:<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms100.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms100-1024x614.png" alt="" title="MontyMonteHistograms100" width="900" height="539" class="aligncenter size-large wp-image-83" /></a></p>
<p>And for 1,000:<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms1000.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms1000-1024x614.png" alt="" title="MontyMonteHistograms1000" width="900" height="539" class="aligncenter size-large wp-image-84" /></a></p>
<p>And for 10,000:<a href="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms10000.png"><img src="http://www.obscureanalytics.com/wp-content/uploads/2012/05/MontyMonteHistograms10000-1024x614.png" alt="" title="MontyMonteHistograms10000" width="900" height="539" class="aligncenter size-large wp-image-85" /></a></p>
<p>It is obvious from these plots of high numbers of simulations that it is the right choice to switch. I mean, the orange bar is almost exactly twice as large as the purple. Don&#8217;t you see? Yeah, my wife was still confused and mildly annoyed as well. This plot shows the RESULT clearly but gives no additional intuition to help understand the WHY. Fail.</p>
<p>I have another plot that I&#8217;m slowly tinkering with that I hope will actually demonstrate the why of the thing, because I REALLY want to explain this to my wife for some reason. But for now, here&#8217;s my quick and dirty R code to simulate the data and generate the above plots:</p>

<div class="wp_codebox"><table><tr id="p7038"><td class="code" id="p70code38"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#### Monty Hall Monte Carlo</span>
<span style="color: #228B22;">#### Rob Mealey</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>RColorBrewer<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>reshape2<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">### Function: Run simulation n times and plot results in stacked bar histograms</span>
montyMonte <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>n,titleSize<span style="color: #080;">=</span><span style="color: #ff0000;">7</span>,legendTitle<span style="color: #080;">=</span><span style="color: #ff0000;">5</span>,ytextSize<span style="color: #080;">=</span><span style="color: #ff0000;">5</span>,xtextSize<span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
Picks <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
Opens <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
WinningDoor <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
WinsIfSwitch <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
PlacedDf <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="color: #0000FF; font-weight: bold;">matrix</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span>n, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>
OpensDf <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="color: #0000FF; font-weight: bold;">matrix</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span>n, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>
PicksDf <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/matrix.html"><span style="color: #0000FF; font-weight: bold;">matrix</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">=</span>n, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>
Doors <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Door 1'</span>, <span style="color: #ff0000;">'Door 2'</span>, <span style="color: #ff0000;">'Door 3'</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>PlacedDf<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Doors
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>PicksDf<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Doors
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>OpensDf<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> Doors
<span style="color: #228B22;">########## Simulation Loop #########</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span>n<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
<span style="color: #228B22;">## 1. Randomly place prize behind one of three doors</span>
PlacePrize <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sample.html"><span style="color: #0000FF; font-weight: bold;">sample</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
<span style="color: #228B22;">## 2. Randomly pick one of three doors</span>
YouPick <span style="color: #080;">&lt;-</span> Doors<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sample.html"><span style="color: #0000FF; font-weight: bold;">sample</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
<span style="color: #228B22;">## 3. Monty either randomly opens one of the two doors left over if</span>
<span style="color: #228B22;">## you happen to pick the correct door or picks the only door left</span>
<span style="color: #228B22;">## if you pick one of two incorrect doors</span>
MontyOpens <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span>PlacePrize<span style="color: #080;">&#91;</span>Doors<span style="color: #080;">==</span>YouPick<span style="color: #080;">&#93;</span><span style="color: #080;">==</span><span style="color: #ff0000;">1</span>,
Doors<span style="color: #080;">&#91;</span><span style="color: #080;">!</span>Doors<span style="color: #080;">%</span>in<span style="color: #080;">%</span>YouPick<span style="color: #080;">&#93;</span><span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sample.html"><span style="color: #0000FF; font-weight: bold;">sample</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>,
Doors<span style="color: #080;">&#91;</span><span style="color: #080;">&#40;</span><span style="color: #080;">!</span>Doors<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>YouPick,Doors<span style="color: #080;">&#91;</span>PlacePrize<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
PrizeIsBehind <span style="color: #080;">&lt;-</span> Doors<span style="color: #080;">&#91;</span>PlacePrize<span style="color: #080;">==</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span>
<span style="color: #228B22;">## 4. If the prize is behind the leftover door, you win if you switch.</span>
<span style="color: #228B22;">## Else you win if you stick on your original choice.</span>
WinIfSwitch <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span>PlacePrize<span style="color: #080;">&#91;</span><span style="color: #080;">!</span>Doors<span style="color: #080;">%</span>in<span style="color: #080;">%</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>YouPick,MontyOpens<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">==</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
Picks <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>Picks, YouPick<span style="color: #080;">&#41;</span>
Opens <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>Opens, MontyOpens<span style="color: #080;">&#41;</span>
WinningDoor <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>WinningDoor, PrizeIsBehind<span style="color: #080;">&#41;</span>
WinsIfSwitch <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>WinsIfSwitch, WinIfSwitch<span style="color: #080;">&#41;</span>
<span style="color: #228B22;">### Write results to data frames</span>
PlacedDf<span style="color: #080;">&#91;</span>i,<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> PlacePrize
PicksDf<span style="color: #080;">&#91;</span>i,YouPick<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">2</span>
OpensDf<span style="color: #080;">&#91;</span>i,MontyOpens<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">3</span><span style="color: #080;">&#125;</span>
<span style="color: #228B22;">########## End Simulation Loop #########</span>
WinsIfSwitches <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span>WinsIfSwitch<span style="color: #080;">==</span><span style="color: #ff0000;">1</span>,
<span style="color: #ff0000;">'Switch Door = Win'</span>,<span style="color: #ff0000;">'Switch Door = Lose'</span><span style="color: #080;">&#41;</span>
Games <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>Picks, Opens, WinningDoor, WinsIfSwitches<span style="color: #080;">&#41;</span>
Wins <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span>WinsIfSwitch<span style="color: #080;">&#41;</span><span style="color: #080;">/</span>n
Games <span style="color: #080;">&lt;-</span> melt<span style="color: #080;">&#40;</span>Games,measure.<span style="">vars</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Picks'</span>, <span style="color: #ff0000;">'Opens'</span>,
<span style="color: #ff0000;">'WinningDoor'</span>, <span style="color: #ff0000;">'WinsIfSwitches'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
Games$variable <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ordered.html"><span style="color: #0000FF; font-weight: bold;">ordered</span></a><span style="color: #080;">&#40;</span>Games$variable, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/levels.html"><span style="color: #0000FF; font-weight: bold;">levels</span></a><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'WinsIfSwitches'</span>,
<span style="color: #ff0000;">'WinningDoor'</span>,
<span style="color: #ff0000;">'Opens'</span>,<span style="color: #ff0000;">'Picks'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
PicksDf<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/is.na.html"><span style="color: #0000FF; font-weight: bold;">is.<span style="">na</span></span></a><span style="color: #080;">&#40;</span>PicksDf<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0</span>
OpensDf<span style="color: #080;">&#91;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/is.na.html"><span style="color: #0000FF; font-weight: bold;">is.<span style="">na</span></span></a><span style="color: #080;">&#40;</span>OpensDf<span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0</span>
ResultsDf <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rbind.html"><span style="color: #0000FF; font-weight: bold;">rbind</span></a><span style="color: #080;">&#40;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Type'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Placed'</span>,n<span style="color: #080;">*</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,melt<span style="color: #080;">&#40;</span>PlacedDf,measure.<span style="">vars</span><span style="color: #080;">=</span>Doors<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Type'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Picked'</span>,n<span style="color: #080;">*</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,melt<span style="color: #080;">&#40;</span>PicksDf,measure.<span style="">vars</span><span style="color: #080;">=</span>Doors<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Type'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Opens'</span>,n<span style="color: #080;">*</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,melt<span style="color: #080;">&#40;</span>OpensDf,measure.<span style="">vars</span><span style="color: #080;">=</span>Doors<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/colnames.html"><span style="color: #0000FF; font-weight: bold;">colnames</span></a><span style="color: #080;">&#40;</span>ResultsDf<span style="color: #080;">&#41;</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Type'</span>,<span style="color: #ff0000;">'Trial'</span>,<span style="color: #ff0000;">'Door'</span>,<span style="color: #ff0000;">'value'</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;"># Plot stacked bar histograms of your picks, monty's opens, winning doors</span>
<span style="color: #228B22;"># and win if switch</span>
ggplot<span style="color: #080;">&#40;</span>Games, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>variable, fill<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/factor.html"><span style="color: #0000FF; font-weight: bold;">factor</span></a><span style="color: #080;">&#40;</span>value<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
last_plot<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_histogram<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
last_plot<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> scale_x_discrete<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/labels.html"><span style="color: #0000FF; font-weight: bold;">labels</span></a><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rev.html"><span style="color: #0000FF; font-weight: bold;">rev</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Your Picks'</span>,<span style="color: #ff0000;">&quot;Monty's Opens&quot;</span>,
<span style="color: #ff0000;">'Winning Door'</span>,<span style="color: #ff0000;">'Switch=Win/Lose'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
last_plot<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> scale_fill_brewer<span style="color: #080;">&#40;</span>type<span style="color: #080;">=</span><span style="color: #ff0000;">'qual'</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/summary.lm.html"><span style="color: #0000FF; font-weight: bold;">palette</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> xlab<span style="color: #080;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> ylab<span style="color: #080;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>
last_plot<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> coord_flip<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
last_plot<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> opts<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/title.html"><span style="color: #0000FF; font-weight: bold;">title</span></a> <span style="color: #080;">=</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Monty Hall Monte Carlo Total Simulation Results, N = '</span>,n,<span style="color: #ff0000;">', Pct Switches Win = '</span>,Wins,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,
legend.<span style="">position</span><span style="color: #080;">=</span><span style="color: #ff0000;">'bottom'</span>,legend.<span style="">title</span><span style="color: #080;">=</span>theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
last_plot<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> opts<span style="color: #080;">&#40;</span>plot.<span style="">title</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>size<span style="color: #080;">=</span>titleSize<span style="color: #080;">&#41;</span>,
legend.<span style="">text</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>size<span style="color: #080;">=</span>legendTitle<span style="color: #080;">&#41;</span>,
axis.<span style="">text</span>.<span style="">y</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>size<span style="color: #080;">=</span>ytextSize<span style="color: #080;">&#41;</span>,
axis.<span style="">text</span>.<span style="">x</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>size<span style="color: #080;">=</span>xtextSize<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
ggsave<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'MontyMonteHistograms'</span>,n,<span style="color: #ff0000;">'.png'</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,width<span style="color: #080;">=</span><span style="color: #ff0000;">5</span>, height<span style="color: #080;">=</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>
WinsIfSwitches <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/factor.html"><span style="color: #0000FF; font-weight: bold;">factor</span></a><span style="color: #080;">&#40;</span>WinsIfSwitches<span style="color: #080;">&#41;</span>
ResultsDf$lineTypes <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ordered.html"><span style="color: #0000FF; font-weight: bold;">ordered</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#40;</span>WinsIfSwitches,<span style="color: #ff0000;">3</span><span style="color: #080;">*</span><span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/levels.html"><span style="color: #0000FF; font-weight: bold;">levels</span></a><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rev.html"><span style="color: #0000FF; font-weight: bold;">rev</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/levels.html"><span style="color: #0000FF; font-weight: bold;">levels</span></a><span style="color: #080;">&#40;</span>WinsIfSwitches<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
ResultsDf$Trial <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ordered.html"><span style="color: #0000FF; font-weight: bold;">ordered</span></a><span style="color: #080;">&#40;</span>ResultsDf$Trial,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/levels.html"><span style="color: #0000FF; font-weight: bold;">levels</span></a><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/rev.html"><span style="color: #0000FF; font-weight: bold;">rev</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/nrow.html"><span style="color: #0000FF; font-weight: bold;">nrow</span></a><span style="color: #080;">&#40;</span>ResultsDf<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>ResultsDf<span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>
&nbsp;
trialLengths <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">10</span>,<span style="color: #ff0000;">100</span>,<span style="color: #ff0000;">1000</span>,<span style="color: #ff0000;">10000</span><span style="color: #080;">&#41;</span>
resultsList <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/list.html"><span style="color: #0000FF; font-weight: bold;">list</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a> <span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>trialLengths<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>resultsList<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> montyMonte<span style="color: #080;">&#40;</span>trialLengths<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>Good times.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/05/21/a-monty-hall-monte-carlo-part-1-oh-god/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>By the books&#8230;</title>
		<link>http://www.obscureanalytics.com/2012/05/18/by-the-books/</link>
		<comments>http://www.obscureanalytics.com/2012/05/18/by-the-books/#comments</comments>
		<pubDate>Fri, 18 May 2012 02:18:08 +0000</pubDate>
		<dc:creator>Rob Mealey</dc:creator>
				<category><![CDATA[Laplacian Ambitions]]></category>

		<guid isPermaLink="false">http://www.obscureanalytics.com/?p=54</guid>
		<description><![CDATA[Most of the applications of Bayesian methods I&#8217;ve encountered and used to date are in the areas of text mining and machine learning, such as topic modeling using LDA models, naive bayes classifiers, and in time series analysis (Kalman filters...]]></description>
				<content:encoded><![CDATA[<p>Most of the applications of Bayesian methods I&#8217;ve encountered and used to date are in the areas of text mining and machine learning, such as topic modeling using LDA models, naive bayes classifiers, and in time series analysis (Kalman filters and state space models). But I have employed these largely as gray box solutions to specific problems I&#8217;ve faced. I want to develop an understanding of the Bayesian framework that allows me to apply it to all the problems to which I would apply my traditional maximum likelihood based modeling toolkit. In order to really master these tools, I am going to work through some basic statistical problems using them. To get these problems to work through, and to have references along the way, I&#8217;m going to be using a number of textbooks, as I have obsessively and expensively amassed quite a little collection. In perusing them all and working through the introductory chapters of each, I&#8217;ve decided on three of them as the core of my &#8216;curriculum&#8217;:</p>
<ol>
<li> <a href='http://www.powells.com/biblio/62-9780470141151-1'> Introduction to Bayesian Statistics, William Bolstad </a> </li>
<li> <a href='http://www.powells.com/biblio/1-9780123814852-0'> Doing Bayesian Data Analysis, John K Kruschke </a> </li>
<li> <a href='http://www.powells.com/biblio/72-9781584883883-0'> Bayesian Data Analysis, Andrew Gelman, John B. Carlin, Hal S. Stern and Donald B. Rubin </a> </li>
</ol>
<p>The first book is a true introductory textbook, whose audience is first-time statistics students. It aims to introduce probability and statistics in a Bayesian framework. Kruschke&#8217;s book is great for the practical R and BUGS code, and is entertainingly written, and Gelman&#8217;s book has a lot of great detail on distribution theory and social science examples that have more immediate traction in my brain. I think between the three, I will be able get anything I could want from a textbook on bayesian statistics. Well&#8230;not anything&#8230;but no textbook can actually provide that&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.obscureanalytics.com/2012/05/18/by-the-books/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
