What crimes are increasing?
Homicide - Criminal is one category of crime that has increased this year. Weapon Violations is another category of crime that has dramatically increased this year as well.
The Weapon Violations shown on the map are only from the beginning of 2016.
Note: kernel code for the above Google map can be found here.
library(dplyr)
library(ggplot2)
library(tidyr)
library(reshape2)
library(data.table)
library(DT)
library(d3heatmap)
library(lubridate)
library(xts)
library(dygraphs)
library(forecast)
library(corrplot)
# Functions
wd <- function(x) as.POSIXct(strptime(x, '%Y-%m-%d %H:%M:%S'))
byMonth <- function(x) strftime(x, '%Y-%m-01')
backDate <- function(x) as.POSIXct(strptime(x, '%Y-%m-%d'))
monthName <- function(x) strftime(x, '%b')
weekDayName <- function(x) strftime(x,'%a')
hourName <- function(x) strftime(x,'%H')
# Reading Input
d <- read.csv("../input/crime.csv", sep=",")
# Apply Functions
d$Dispatch_Date_Time <- wd(d$Dispatch_Date_Time)
d$month <- byMonth(d$Dispatch_Date_Time)
d$Month <- cut(d$Dispatch_Date_Time, breaks= "month")
d$Year <- cut(d$Dispatch_Date_Time, breaks= "year")
d$monthName <- monthName(d$Dispatch_Date_Time)
d$day = weekDayName(d$Dispatch_Date_Time)
d$hour = hourName(d$Dispatch_Date_Time)
# Handy for Year-to-date
d$monthN <- month(d$Dispatch_Date_Time)
# Don't want NA when grouping
d[is.na(d)] <- 0
Below we’re comparing year to year using YTD (Year-to-Date) values.
this_day <- today()
this_month <- month(this_day)
this_year <- year(this_day)
t = d[d$monthN < this_month,]
counts <- summarise(group_by(t, Text_General_Code,Year), Counts=length(Text_General_Code))
counts <- counts[order(counts$Year),]
colnames(counts) <- c("Text_General_Code",
"YTD","Counts")
p = dcast(counts, YTD ~ Text_General_Code)
p$Var.2 <- NULL # Not sure what this is, but take him out.
p$YTD <- year(p$YTD)
# Safety check
p[is.na(p)] <- 0
# Only crimes that are increasing from last YTD
#
tmp <- p[p$YTD == (this_year),] - p[p$YTD == (this_year - 1),]
p <- p[ , colSums(tmp) > 0]
# Make YTD the row name
row.names(p) <- p$YTD
# Remove first
p = p[,-1]
p <- p[ rev(row.names(p)), ]
# Save the category
colsNames_increasing <- colnames(p)
myTable <- datatable(p, extensions = 'FixedColumns', options = list(
dom = 't',
deferRender = TRUE,
scrollX = TRUE,
scroller = TRUE,
fixedColumns = list(leftColumns = 1, rightColumns = 0)))
# Select values only on the increase
t <- d[d$Text_General_Code %in% colsNames_increasing, ]
counts <- summarise(group_by(t, Text_General_Code,month), Counts=length(Text_General_Code))
counts <- counts[order(counts$month),]
counts$month <- ymd(counts$month)
p <- dcast(counts, month ~ Text_General_Code)
p[is.na(p)] <- 0
dxts <- xts(p[,-1], order.by=p$month)
g <- dygraph(dxts,main="Increased from Last Year. (Monthly Totals)") %>%
dyRangeSelector(height = 10)
g
Note the correlation with Vandalism/Criminal Mischief.
library(corrplot)
t <- d[d$Text_General_Code %in% colsNames_increasing, ]
counts <- summarise(group_by(t, Text_General_Code,month),Counts=length(Text_General_Code))
counts <- counts[order(counts$month),]
p <- dcast(counts,month ~ Text_General_Code, value.var = "Counts" )
p[is.na(p)] <- 0
# Make month row names
row.names(p) <- p$month
# Remove first
p = p[,-1]
M <- cor(p)
corrplot(M, type = "upper", order = "hclust",
tl.col = "black", tl.srt = 45,number.cex=0.75,tl.cex = 0.68)