<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/point-pattern-analysis-using-ecological-methods-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://spatial.ly/2017/12/pointpatterns/"> R – Spatial.ly</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><span>Here is a quick example for how to get started with some of the more sophisticated point pattern analysis tools that have been developed for ecologists – principally the </span><code>adehabitathr</code><span> package – but that are very useful for human data. Ecologists deploy point pattern analysis to establish the “home range” of a particular animal based on the know locations it has been sighted (either directly or remotely via camera traps). Essentially it is where the animal spends most of its time. In the case of human datasets the analogy can be extended to identify areas where most crimes are committed – hotspots – or to identify the activity spaces of individuals or the catchment areas of services such as schools and hospitals.</span></p>
<p>This tutorial offers a rough analysis of crime data in London so the maps should not be taken as definitive – I’ve just used them as a starting point here.</p>
<p>Police crime data have been taken from here <a rel="nofollow" href="https://data.police.uk/data/" target="_blank">https://data.police.uk/data/</a>. This tutorial London uses data from London’s Metropolitan Police (September 2017).</p>
<p>For the files used below <a rel="nofollow" href="http://spatial.ly/wp-content/uploads/2017/12/adehabitat_eg.zip" target="_blank">click here.</a></p>
<pre><code>#Load in the library and the csv file.

library(&quot;rgdal&quot;)
library(raster)
library(adehabitatHR)

input&lt;- read.csv(&quot;2017-09-metropolitan-street.csv&quot;) 

input&lt;- input[,1:10] #We only need the first 10 columns

input&lt;- input[complete.cases(input),] #This line of code removes rows with NA values in the data.</code></pre>
<p>At the moment <code>input</code> is a basic data frame. We need to convert the data frame into a spatial object. Note we have first specified our epsg code as 4326 since the coordinates are in WGS84. We then use <code>spTransform</code> to reproject the data into British National Grid – so the coordinate values are in meters.</p>
<pre><code>Crime.Spatial&lt;- SpatialPointsDataFrame(input[,5:6], input, proj4string = CRS(&quot;+init=epsg:4326&quot;))

Crime.Spatial&lt;- spTransform(Crime.Spatial, CRS(&quot;+init=epsg:27700&quot;)) #We now project from WGS84 for to British National Grid

plot(Crime.Spatial) #Plot the data</code></pre>
<p>The plot reveals that we have crimes across the UK, not just in London. So we need an outline of London to help limit the view. Here we load in a shapefile of the Greater London Authority Boundary.</p>
<pre><code>London&lt;- readOGR(&quot;.&quot;, layer=&quot;GLA_outline&quot;)</code></pre>
<p>Thinking ahead we may wish to compare a number of density estimates, so they need to be performed across a consistently sized grid. Here we create an empty grid in advance to feed into the <code>kernelUD</code> function.</p>
<pre><code>Extent&lt;- extent(London) #this is the geographic extent of the grid. It is based on the London object.

#Here we specify the size of each grid cell in metres (since those are the units our data are projected in).

resolution&lt;- 500

#This is some magic that creates the empty grid
x &lt;- seq(Extent[1],Extent[2],by=resolution)  # where resolution is the pixel size you desire 
y &lt;- seq(Extent[3],Extent[4],by=resolution) 
xy &lt;- expand.grid(x=x,y=y) 
coordinates(xy) &lt;- ~x+y 
gridded(xy) &lt;- TRUE 

#You can see the grid here (this may appear solid black if the cells are small)

plot(xy)
plot(London, border=&quot;red&quot;, add=T)</code></pre>
<p>OK now run the density estimation note we use <code>grid= xy</code> utlise the grid we just created. This is for all crime in London.</p>
<pre><code>all &lt;- raster(kernelUD(Crime.Spatial, h=&quot;href&quot;, grid = xy)) #Note we are running two functions here - first KernelUD then converting the result to a raster object.

#First results
plot(all)
plot(London, border=&quot;red&quot;, add=T)</code></pre>
<p><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i0.wp.com/spatial.ly/wp-content/uploads/2017/12/Unknown.png?w=450" alt="" class="wp-image-6344" data-recalc-dims="1"></p>
<p>Unsurprisingly we have a hotpot over the centre of London. Are there differences for specific crime types? We may, for example, wish to look at the density of burglaries.</p>
<pre><code>plot(Crime.Spatial[Crime.Spatial$Crime.type==&quot;Burglary&quot;,]) # quick plot of burglary points

burglary&lt;- raster(kernelUD(Crime.Spatial[Crime.Spatial$Crime.type==&quot;Burglary&quot;,], h=&quot;href&quot;, grid = xy))

plot(burglary)
plot(London, border=&quot;red&quot;, add=T)</code></pre>
<p><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i2.wp.com/spatial.ly/wp-content/uploads/2017/12/Unknown-2.png?w=450" alt="" class="wp-image-6343" data-recalc-dims="1"></p>
<p>There’s a slight difference but still it’s tricky to see if there are areas where burglaries concentrate more compared to the distribution of all crimes. A very rough way to do this is to divide one density grid by the other.</p>
<pre><code>both&lt;-burglary/all

plot(both)
plot(London, border=&quot;red&quot;, add=T)</code></pre>
<p><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i2.wp.com/spatial.ly/wp-content/uploads/2017/12/Unknown-3.png?w=450" alt="" class="wp-image-6342" data-recalc-dims="1"></p>
<p>This hasn’t worked particularly well since there are edge effects on the density grid that are causing issues due to a few stray points at the edge of the grid. We can solve this by capping the values we map – in this we are only showing values of between 0 and 1. Some more interesting structures emerge with burglary occuring in more residential areas, as expected.</p>
<pre><code>both2 &lt;- both
both2[both &lt;= 0] &lt;- NA
both2[both >= 1] &lt;- NA

#Now we can see the hotspots much more clearly.
plot(both2)
plot(London, add=T)</code></pre>
<p><img sizes="(max-width: 1344px) 100vw, 1344px" src="https://i0.wp.com/spatial.ly/wp-content/uploads/2017/12/Unknown-4.png?w=450" alt="" class="wp-image-6341" data-recalc-dims="1"></p>
<p>There’s many more sophisticated approaches to this kind of analysis – I’d encourgae you to look at the <code>adehabitatHR</code> vignette on the package page.</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://spatial.ly/2017/12/pointpatterns/"> R – Spatial.ly</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/rPrUd4L0bIo" height="1"></a></div>