Site icon R-bloggers

Crime Analysis – Denver-Part 3

[This article was first published on R-Projects - Stoltzmaniac, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Marijuana, Alcohol, and Other Drugs


Continuing our Exploration of the Data
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.

This is a very simplistic view because it will only focus on trend data, which never tells the whole story.

Crime Analysis of Denver – Part 3

The Itinerary

Exploration of Data
Data provided by http://data.denvergov.org

Import the necessary libraries and data

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  
####

Drugs and Alcohol

topCrimes = data %>%  
  filter(OFFENSE_CATEGORY_ID == 'drug-alcohol') %>%
  filter(year >= 2014) %>%
  filter(month < 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 < 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("Annual Incidents Reported")) + labs(x = "OFFENSE CATEGORY")

Observations

Marijuana Approved for Recreational Usage

df = data %>%  
  filter(date <= '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),"-",as.character(df$month),"-01",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("Incidents With 'Marijuana' Classification"),atop(italic('Note Spikes in Possession of Marijuana on April 20th of Each Year and Sharp Drop-off in 2016'),"")))) + labs(x='Year by Month')

Observations

What % Changes Occurred

df2 = data %>%  
  filter(date <= '2016-10-31') %>%
  filter(year >= 2013) %>%
  filter(month <= 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,face='bold')  

Observations

Liquor & Alcohol

df = data %>%  
  filter(date <= '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),"-",as.character(df$month),"-01",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("Incidents With 'Liquor' Classification"),atop(italic('Note Declining Trend in Liquor Incidents'),"")))) + labs(x='Year by Month')

Observations

Liquor ranked as #1 in the “drug-alcohol” Category

What About Methamphetamine?

df = data %>%  
  filter(date <= '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),"-",as.character(df$month),"-01",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("Incidents With 'Methamphetamine' Classification"),atop(italic('Note Posession Increasing Much Faster Than Selling'),"")))) + labs(x='Year by Month')  

Observations

Growth % for Methamphetamines

df2 = data %>%  
  filter(year >= 2012) %>%
  filter(month <= 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,face='bold')  

Observations

Final Thoughts (for now)

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.

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 crimes but may not reduce consumption.

Up Next:

Feel free to comment and take some of this data and run with some ideas of your own!

Code used in this post is on my GitHub

To leave a comment for the author, please follow the link and comment on their blog: R-Projects - Stoltzmaniac.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.