<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/crime-analysis-denver-part-2/"></div></div><div><a data-url="https://www.r-bloggers.com/crime-analysis-denver-part-2/" data-counturl="https://www.r-bloggers.com/crime-analysis-denver-part-2/" data-count="horizontal" data-text="Crime Analysis – Denver-Part 2" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://stoltzmaniac.com/crime-analysis-denver-part-2/"> R-Projects - Stoltzmaniac</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h4>Getting More Granular</h4>
<p><strong>Where we’re going</strong> <br>
In having noticed the crime rates heading up over the last few years, taking a better look seemed more important. I want to first look at “CRIME” before looking into “TRAFFIC” in the data set. It sounds more interesting and I hope the results don’t keep me up at night. </p>
<p><strong>What we’ll do in this post</strong></p>
<ul>
<li>Load the csv, format the data
<ul>
<li>This will all be hidden and can be found in the previous post (Part 1)</li>
</ul>
</li>
<li>Look into apparent growth in crime rates from 2012 – 2014</li>
<li>We’ll focus only on those that fit the “IS<em>CRIME” definition and not “IS</em>TRAFFIC”</li>
</ul>
<p>Let’s dive in! </p>
<p><strong>Exploration of Data</strong> <br>
Data provided by <a target="_blank" rel="nofollow" href="http://data.denvergov.org/">http://data.denvergov.org</a></p>
<p><em>Data Import &amp; Formatting – shown in prior post: <a target="_blank" rel="nofollow" href="http://stoltzmaniac.com/crime-analysis-denver-part-1/">Crime Analysis – Denver-Part 2</a></em></p>
<p><strong>Looking at Crime Incidents by Year</strong> </p>
<pre><code>df = data %>%  
  filter(IS_CRIME==1) %>%
  filter(year!=max(year(date))) %>%
  group_by(year) %>%
  summarise(incidents=sum(IS_CRIME)) %>%
  arrange(year)

p = ggplot(df,aes(x=year,y=incidents,label=incidents))  
p + geom_bar(stat='identity') + geom_text(fontface='bold',size=6,col='white',vjust=1)+ ggtitle('Crime Volume by Year') + xlab('Year') + ylab('Incidents') + theme(plot.title = element_text(hjust = 0.5))  
</code></pre>
<p><img alt="barplotCrime" src="https://i0.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-2/crime-analysis-denver-part-2-plot1.png?w=456" data-recalc-dims="1"></p>
<pre><code>df = data %>%  
  filter(IS_CRIME==1) %>%
  filter(year!=max(year(date))) %>%
  group_by(year) %>%
  summarise(incidents=sum(IS_CRIME)) %>%
  arrange(year) %>%
  mutate(year,YoYPercentageChange=round(100*(incidents-lag(incidents))/lag(incidents)),0)
df = df[!is.na(df$YoYPercentageChange),]  
p = ggplot(df,aes(x=year,y=YoYPercentageChange,label=YoYPercentageChange))  
p + geom_bar(stat='identity') + geom_text(fontface='bold',size=6,col='white',vjust=1)+ ggtitle('Crime Percentage Change Year-Over-Year') + xlab('Year') + ylab('YoY Incident % Change') + theme(plot.title = element_text(hjust = 0.5))  
</code></pre>
<p><img alt="barplotCrimeChange" src="https://i2.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-2/crime-analysis-denver-part-2-plot2.png?w=456" data-recalc-dims="1"></p>
<p><strong>Observations*</strong><br>
* Crime rose the most between 2013 and 2012 (39% increase)<br>
* Crime increased each year after but at a decreasing rate<br>
* Examine years 2012 – 2014 to see growth changes </p>
<p><strong>Highest volume of “IS<em>CRIME” types</em></strong> <br>
Identify the offense by OFFENSECATEGORY_ID and exclude months we have not seen so far this year.</p>
<pre><code>#Isolate Years 2012 - 2014
data = data[data$year &lt;= 2014 &amp; data$year >= 2012,]  

#Sum up all incidents IS_CRIME AND IS_TRAFFIC
maxYear = max(data$year)  
maxMonthYTD = max(data$month[data$year==maxYear])

#Look into IS_CRIME only
df = data %>%  
  filter(IS_CRIME==1) %>%
  group_by(year,OFFENSE_CATEGORY_ID) %>%
  summarise(incidents=sum(IS_CRIME)) %>%
  arrange(desc(incidents))

p = ggplot(df,aes(x=factor(year),y=incidents,fill=year))  
p + geom_bar(stat='identity') + ggtitle('Crime Incidents Reported by Year') + xlab('Year') + ylab('Incidents') + theme(plot.title = element_text(hjust = 0.5),legend.position = 'none') + guides(fill = guide_legend(title='Year')) + coord_flip() + facet_wrap(~OFFENSE_CATEGORY_ID,ncol=3)
</code></pre>
<p><img alt="barplotCrimeCategories" src="https://i2.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-2/crime-analysis-denver-part-2-plot3.png?w=456" data-recalc-dims="1"></p>
<p><strong>Observations</strong> <br>
It would appear as if “all-other-crimes” has moved the needle the most between 2012 – 2014. This is not a very specific category. It’s also worth noticing that “other-crimes-against-persons” has grown as well. Both of these leads to some speculation that perhaps these vague types of crimes started being reported during this period and perhaps hadn’t been documented before. </p>
<ul>
<li>
<p>Growth categories: </p>
<ul>
<li>“larceny”</li>
<li>“drug-alcohol”</li>
<li>“public-disorder” </li>
</ul>
</li>
<li>
<p>Declining categories: </p>
<ul>
<li>“theft-from-motor-vehicle”</li>
<li>“robbery”</li>
<li>“burglary” </li>
</ul>
</li>
</ul>
<p>Many of the other categories have a much lower volume of incidents. Growth is more difficult to see in visualizatoins for these cases.</p>
<p><strong>Here’s a look at growth year-over-year:</strong></p>
<pre><code>df2 = df %>%  
  group_by(OFFENSE_CATEGORY_ID) %>%
  arrange(OFFENSE_CATEGORY_ID,year) %>%
  mutate(year,YoYchange=round((incidents-lag(incidents))),0) %>% filter(year != 2012)

p = ggplot(df2,aes(x=factor(year),y=YoYchange,fill=year,label=YoYchange))  
p + geom_bar(stat='identity') + ggtitle('Change in Crime Incidents vs Previous Year') + xlab('Year') + ylab('YoY Change in Incidents') + theme(plot.title = element_text(hjust = 0.5),legend.position = 'none') + guides(fill = guide_legend(title='Year')) + coord_flip() + facet_wrap(~OFFENSE_CATEGORY_ID,ncol=3) + geom_text(hjust=0.5, size=5,col='red', fontface='bold')  
</code></pre>
<p><img alt="barplotCrimeCategoryChange" src="https://i2.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-2/crime-analysis-denver-part-2-plot4.png?w=456" data-recalc-dims="1"></p>
<p><strong>Here’s a look at % growth year-over-year:</strong></p>
<pre><code>df2 = df %>%  
  group_by(OFFENSE_CATEGORY_ID) %>%
  arrange(OFFENSE_CATEGORY_ID,year) %>%
  mutate(year,YoYchange=round(100*((incidents-lag(incidents))/lag(incidents))),0) %>% filter(year != 2012)

p = ggplot(df2,aes(x=factor(year),y=YoYchange,fill=year,label=YoYchange))  
p + geom_bar(stat='identity') + ggtitle('% Change in Crime Incidents vs Previous Year') + xlab('Year') + ylab('YoY % Change in Incidents') + theme(plot.title = element_text(hjust = 0.5),legend.position = 'none') + guides(fill = guide_legend(title='Year')) + coord_flip() + facet_wrap(~OFFENSE_CATEGORY_ID,ncol=3) + geom_text(hjust=1,col='red',size=5,fontface='bold')  
</code></pre>
<p><img alt="barplotCrimeCategoryPercentageChange" src="https://i0.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-2/crime-analysis-denver-part-2-plot5.png?w=456" data-recalc-dims="1"></p>
<p><strong>Observations</strong> </p>
<ul>
<li>“all-other-crimes” is the outright leader in change in both volume and percentage growth year-over-year with an astonishing 380% increase between 2012 and 2013</li>
<li>“drug-alcohol” grew by 173% between 2012 and 2013 but dropped down to only 27% growth the next year</li>
<li>“murder” didn’t change too much in volume compared to everything else (swinging up 7 and down 10) but was a 19% growth and a 23% decline in 2013 and 2014 respectively </li>
</ul>
<p><strong>Final Thoughts (for now)</strong> <br>
Due to the vague nature of the types of crimes which grew the most, I can’t determine exactly what happened in Denver during 2013. In the less vague crimes, “drug-alcohol” saw the largest increase. This was followed by “public-disorder” and perhaps there’s a relationship there. My assumption is that one may perhaps cause the other… </p>
<p>I’m still curious about the seasonality and month-to-month effects. Perhaps certain types of crimes are more common during certain times. I’m also very interested to see if a new population was perhaps added to the mix in 2013. If a certain part of Denver was added in 2013 that would certainly help to explain the situation. </p>
<p><strong>What I’ll do in the next crime posts</strong></p>
<ul>
<li>Look for patterns by location</li>
<li>Lay out some visualizations on maps</li>
<li>Try to identify areas with high volumes of traffic incidents (maybe I can avoid a ticket)</li>
<li>Answer the question: What types of crimes have grown the most in the last 5 years?</li>
</ul>
<p>Code used in this post is on my <a target="_blank" rel="nofollow" href="https://github.com/stoltzmaniac/Denver-Crime-Analysis.git">GitHub</a></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/crime-analysis-denver-part-2/"></div></div><div><a data-url="https://www.r-bloggers.com/crime-analysis-denver-part-2/" data-counturl="https://www.r-bloggers.com/crime-analysis-denver-part-2/" data-count="vertical" data-text="Crime Analysis – Denver-Part 2" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://stoltzmaniac.com/crime-analysis-denver-part-2/"> R-Projects - Stoltzmaniac</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/H1Yp84_wjns"></a></div>