<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-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://stoltzmaniac.com/crime-analysis-denver-part-3/"> R-Projects - Stoltzmaniac</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h4>Marijuana, Alcohol, and Other Drugs</h4>
<hr>
<p><strong>Continuing our</strong> <strong><em>Exploration</em></strong> <strong>of the Data</strong> <br>
After identifying the sources of crime growth, it’s time to investigate specific crime rates. This blog post addresses drug and alcohol crimes in Denver over the past few years. </p>
<p>This is a <strong><em>very</em></strong> simplistic view because it will only focus on trend data, which never tells the whole story.</p>
<p>Crime Analysis of Denver – Part 3</p>
<ul>
<li><a target="_blank" rel="nofollow" href="https://stoltzmaniac.com/crime-analysis-denver-part-1/">Crime Analysis – Denver-Part 1</a></li>
<li><a target="_blank" rel="nofollow" href="https://stoltzmaniac.com/crime-analysis-denver-part-2/">Crime Analysis – Denver-Part 2</a></li>
</ul>
<p><strong>The Itinerary</strong></p>
<ul>
<li>Identify top crimes related to drugs and alcohol
<ul>
<li>Achieved by filtering “drug-alcohol” in the OFFENSE CATEGORY ID </li>
</ul>
</li>
<li>Compare most from January – October while excluding November and December to compare apples-to-apples with previous years
<ul>
<li>This adjustment helps simplify things but will also introduce a level of bias</li>
</ul>
</li>
</ul>
<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><strong>Import the necessary libraries and data</strong></p>
<pre><code>library(dplyr)  
library(lubridate)  
library(ggplot2)  
library(tidyr)  
####
# Data from: http://data.denvergov.org/dataset/city-and-county-of-denver-crime
# File name: crime.csv
CWD = getwd()  
data = read.csv(paste(CWD,'/data/crime.csv',sep=''))  
data.backup=data  
####
</code></pre>
<p><strong>Drugs and Alcohol</strong> </p>
<ul>
<li>What are the top 15 crimes types (as defined by CRIME TYPE ID)?</li>
<li>How have the number of incidents reported changed over the last few years? </li>
</ul>
<pre><code>topCrimes = data %>%  
  filter(OFFENSE_CATEGORY_ID == 'drug-alcohol') %>%
  filter(year >= 2014) %>%
  filter(month &lt; maxMonthYTD) %>%
  group_by(OFFENSE_TYPE_ID) %>%
  summarise(incidents = sum(incidents)) %>%
  arrange(desc(incidents))
topCrimes = topCrimes$OFFENSE_TYPE_ID[1:15]

df = data %>%  
  filter(OFFENSE_CATEGORY_ID == 'drug-alcohol') %>%
  filter(OFFENSE_TYPE_ID %in% topCrimes) %>%
  filter(year >= 2014) %>%
  filter(month &lt; maxMonthYTD) %>%
  group_by(OFFENSE_TYPE_ID,year) %>%
  summarise(incidents = sum(incidents))

p = ggplot(df,aes(x=reorder(OFFENSE_TYPE_ID,-incidents),y=incidents,group=factor(year),col=factor(year)))  
p + geom_line() + geom_point() + theme(axis.text.x = element_text(angle = 45,hjust=1)) + ggtitle(paste(&quot;Annual Incidents Reported&quot;)) + labs(x = &quot;OFFENSE CATEGORY&quot;)
</code></pre>
<p><strong>Observations</strong></p>
<ul>
<li>Liquor possession ranked highest in the drug-alcohol violations</li>
<li>Possession of drug paraphernalia ranked second, followed by possession of marijuana</li>
<li>Methamphetamine possession breaks the pattern of annual declines in incidents – it actually grew from 2014-2016</li>
<li>Cocaine and heroin possession also grew, but at a much smaller volume than methamphetamine </li>
</ul>
<p><img alt="barplotCrime" src="https://i2.wp.com/stoltzmaniac.com/content/images/2016/12/crime-analysis-denver-part-3/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p><strong>Marijuana Approved for Recreational Usage</strong> </p>
<pre><code>df = data %>%  
  filter(date &lt;= '2016-10-31') %>%
  filter(year >= 2013) %>%
  filter(grepl('marijuana',OFFENSE_TYPE_ID)) %>%
  group_by(OFFENSE_TYPE_ID,year,month) %>%
  summarise(incidents = sum(incidents))
df$yearMonth = as.Date(paste(as.character(df$year),&quot;-&quot;,as.character(df$month),&quot;-01&quot;,sep=''))  
p = ggplot(df,aes(x=yearMonth,y=incidents,col=OFFENSE_TYPE_ID,fill=OFFENSE_TYPE_ID))  
p + geom_line() + theme(legend.position = 'bottom',legend.title=element_blank()) + ggtitle(expression(atop(paste(&quot;Incidents With 'Marijuana' Classification&quot;),atop(italic('Note Spikes in Possession of Marijuana on April 20th of Each Year and Sharp Drop-off in 2016'),&quot;&quot;)))) + labs(x='Year by Month')
</code></pre>
<p><strong>Observations</strong> </p>
<ul>
<li>The middle of 2016 showed a dramatic decrease in incidents</li>
<li>Possession incidents were a much larger percentage of the violations until the middle of 2016
<ul>
<li>The timing of the sharp decrease in 2016 seems to be a couple of years too late</li>
<li>Marijuana was legalized for recreational purposes in Colorado in 2014. It would make more sense if the drop occurred when this was implemented – <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Cannabis_in_Colorado">Wikipedia Article</a> </li>
<li>It’s important to note that this has nothing to do with sentencing and is only what is reported to the police</li>
</ul>
</li>
<li>Apparently April 20th (aka 420) is a widely celebrated in the marijuana users community – <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/420_(cannabis_culture)">Wikipedia Article</a></li>
</ul>
<p><img alt="barplotCrime" src="https://i1.wp.com/stoltzmaniac.com/content/images/2016/12/crime-analysis-denver-part-3/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p><strong>What % Changes Occurred</strong> </p>
<pre><code>df2 = data %>%  
  filter(date &lt;= '2016-10-31') %>%
  filter(year >= 2013) %>%
  filter(month &lt;= maxMonthYTD) %>%
  filter(grepl('marijuana',OFFENSE_TYPE_ID)) %>%
  group_by(OFFENSE_TYPE_ID,year) %>%
  summarise(incidents = sum(incidents)) %>%
  arrange(OFFENSE_TYPE_ID,year) %>%
  mutate(year,YoYchange=round(100*((incidents-lag(incidents))/lag(incidents))),0) 
df2[is.na(df2)] = 0  
p = ggplot(df2,aes(x=factor(year),y=YoYchange,group=OFFENSE_TYPE_ID,fill=OFFENSE_TYPE_ID,label=YoYchange))  
p + geom_bar(stat='identity',position='dodge') + ggtitle('% Change in Crime Incidents vs Previous Year') + xlab('Year') + ylab('YoY % Change in Incidents') + theme(plot.title = element_text(hjust = 0.5)) + guides(fill = guide_legend(title='Year')) + geom_text(position = position_dodge(width=1),col='black',size=5,fontface='bold')  
</code></pre>
<p><strong>Observations</strong> </p>
<ul>
<li>Marijuana cultivation grew each year (no pun intended)</li>
<li>Incidents of marijuana possession dropped by 51% from 2015 to 2016</li>
<li>Incidents of marijuana sales dropped by 12% from 2015 to 2016
<ul>
<li>This makes sense – if there are legal channels, it would make sense to take advantage of them as long as the financial incentives are in place</li>
</ul>
</li>
</ul>
<p><img title="" alt="barplotCrime" src="https://i2.wp.com/stoltzmaniac.com/content/images/2016/12/crime-analysis-denver-part-3/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"> </p>
<p><strong>Liquor &amp; Alcohol</strong> </p>
<pre><code>df = data %>%  
  filter(date &lt;= '2016-10-31') %>%
  filter(year >= 2013) %>%
  filter(grepl('alcohol|liquor',OFFENSE_TYPE_ID)) %>%
  group_by(OFFENSE_TYPE_ID,year,month) %>%
  summarise(incidents = sum(incidents))
df$yearMonth = as.Date(paste(as.character(df$year),&quot;-&quot;,as.character(df$month),&quot;-01&quot;,sep=''))  
p = ggplot(df,aes(x=yearMonth,y=incidents,col=OFFENSE_TYPE_ID,fill=OFFENSE_TYPE_ID))  
p + geom_line() + theme(legend.position = 'right',legend.title=element_blank()) + ggtitle(expression(atop(paste(&quot;Incidents With 'Liquor' Classification&quot;),atop(italic('Note Declining Trend in Liquor Incidents'),&quot;&quot;)))) + labs(x='Year by Month')
</code></pre>
<p><strong>Observations</strong> </p>
<ul>
<li>With incidents of liquor manufacturing so low, it appears as if bootleggers and moonshiners probably don’t thrive in Colorado</li>
<li>There’s a downward trend in the number of incidents each year</li>
</ul>
<p><em>Liquor ranked as #1 in the “drug-alcohol” Category</em></p>
<p><img alt="barplotCrime" src="https://i0.wp.com/stoltzmaniac.com/content/images/2016/12/crime-analysis-denver-part-3/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"></p>
<p><strong>What About Methamphetamine?</strong> </p>
<pre><code>df = data %>%  
  filter(date &lt;= '2016-10-31') %>%
  filter(year >= 2012) %>%
  filter(grepl('methamphetamine|methampetamine',OFFENSE_TYPE_ID)) %>%
  group_by(OFFENSE_TYPE_ID,year,month) %>%
  summarise(incidents = sum(incidents))
df$yearMonth = as.Date(paste(as.character(df$year),&quot;-&quot;,as.character(df$month),&quot;-01&quot;,sep=''))  
p = ggplot(df,aes(x=yearMonth,y=incidents,col=OFFENSE_TYPE_ID,fill=OFFENSE_TYPE_ID))  
p + geom_line() + theme(legend.position = 'bottom',legend.title=element_blank()) + ggtitle(expression(atop(paste(&quot;Incidents With 'Methamphetamine' Classification&quot;),atop(italic('Note Posession Increasing Much Faster Than Selling'),&quot;&quot;)))) + labs(x='Year by Month')  
</code></pre>
<p><strong>Observations</strong> </p>
<ul>
<li>There has been a rapid and steady growth in methamphetamine incidents since 2012
<ul>
<li>Possession had a monthly minimum of 11 incidents back in 2012 and grew to a maximum of 87 in 2016</li>
</ul>
</li>
<li>FYI – I didn’t misspell methamphetamine, as methampetamine (it exists in the data that way)</li>
</ul>
<p><img alt="barplotCrime" src="https://i0.wp.com/stoltzmaniac.com/content/images/2016/12/crime-analysis-denver-part-3/unnamed-chunk-8-1.png?w=456" data-recalc-dims="1"></p>
<p><strong>Growth % for Methamphetamines</strong> </p>
<pre><code>df2 = data %>%  
  filter(year >= 2012) %>%
  filter(month &lt;= maxMonthYTD) %>%
  filter(grepl('methamphetamine|methampetamine',OFFENSE_TYPE_ID)) %>%
  group_by(OFFENSE_TYPE_ID,year) %>%
  summarise(incidents = sum(incidents)) %>%
  arrange(OFFENSE_TYPE_ID,year) %>%
  mutate(year,YoYchange=round(100*((incidents-lag(incidents))/lag(incidents))),0) 
df2[is.na(df2)] = 0  
p = ggplot(df2,aes(x=factor(year),y=YoYchange,group=OFFENSE_TYPE_ID,fill=OFFENSE_TYPE_ID,label=YoYchange))  
p + geom_bar(stat='identity',position='dodge') + ggtitle('% Change in Crime Incidents vs Previous Year') + xlab('Year') + ylab('YoY % Change in Incidents') + theme(plot.title = element_text(hjust = 0.5)) + guides(fill = guide_legend(title='Year')) + geom_text(position = position_dodge(width=1),col='black',size=5,fontface='bold')  
</code></pre>
<p><strong>Observations</strong> </p>
<ul>
<li>While the growth in terms of percentage was very high for manufacturing, the volume was very low (change of 1-2 incidents per year)</li>
<li>Large percentage growth occurred in possession but not in sales crimes</li>
<li>When did <strong><em>Breaking Bad</em></strong> end? Perhaps this is more than a coincidence… </li>
</ul>
<p><img alt="barplotCrime" src="https://i0.wp.com/stoltzmaniac.com/content/images/2016/12/crime-analysis-denver-part-3/unnamed-chunk-9-1.png?w=456" data-recalc-dims="1"></p>
<p><strong>Final Thoughts (for now)</strong> </p>
<p>The population of Denver has been increasing at a large rate for the last decade or so. None of this data has been population adjusted which could be painting the wrong picture. However, it’s interesting to notice alcohol and marijuana incidents on the decline with methamphetamine and heroin incidents increasing. According to what I remember from D.A.R.E., meth and heroin are pretty nasty drugs, which is worrisome for the area. However, issues with liquor and alcohol still rank #1 for now. </p>
<p>Colorado is well known for large growth in the microbrewery and marijuana industries. The crime data seems to indicate a downward trend in both alcohol and marijuana related violations. Perhaps a negative correlation between marijuana and alcohol crime incidents and business openings in those industries exists. Once you can sell and purchase something legally it may reduce those <strong><em>crimes</em></strong> but may not reduce <strong><em>consumption</em></strong>. </p>
<p><strong>Up Next:</strong> </p>
<ul>
<li>Identify patterns by location</li>
<li>Geo-Spatial crime visualization</li>
</ul>
<p><strong><em>Feel free to comment and take some of this data and run with some ideas of your own!</em></strong></p>
<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>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://stoltzmaniac.com/crime-analysis-denver-part-3/"> 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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/z2RYitvTa64"></a></div>