Introduction

What crimes are increasing?

Homicide - Criminal and Weapon Violations (map)

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.

Reading in the data

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

Broken Windows?

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)