-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchicago_crimes_analysis.Rmd
295 lines (210 loc) · 8.47 KB
/
chicago_crimes_analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
title: "Chicago Crimes Analysis."
output: html_notebook
---
Content
ID - Unique identifier for the record.
Case Number - The Chicago Police Department RD Number (Records Division Number), which is unique to the incident.
Date - Date when the incident occurred. this is sometimes a best estimate.
Block - The partially redacted address where the incident occurred, placing it on the same block as the actual address.
IUCR - The Illinois Unifrom Crime Reporting code. This is directly linked to the Primary Type and Description. See the list of IUCR codes at https://data.cityofchicago.org/d/c7ck-438e.
Primary Type - The primary description of the IUCR code.
Description - The secondary description of the IUCR code, a subcategory of the primary description.
Location Description - Description of the location where the incident occurred.
Arrest - Indicates whether an arrest was made.
Domestic - Indicates whether the incident was domestic-related as defined by the Illinois Domestic Violence Act.
Beat - Indicates the beat where the incident occurred. A beat is the smallest police geographic area – each beat has a dedicated police beat car. Three to five beats make up a police sector, and three sectors make up a police district. The Chicago Police Department has 22 police districts. See the beats at https://data.cityofchicago.org/d/aerh-rz74.
District - Indicates the police district where the incident occurred. See the districts at https://data.cityofchicago.org/d/fthy-xz3r.
Ward - The ward (City Council district) where the incident occurred. See the wards at https://data.cityofchicago.org/d/sp34-6z76.
Community Area - Indicates the community area where the incident occurred. Chicago has 77 community areas. See the community areas at https://data.cityofchicago.org/d/cauq-8yn6.
FBI Code - Indicates the crime classification as outlined in the FBI's National Incident-Based Reporting System (NIBRS). See the Chicago Police Department listing of these classifications at http://gis.chicagopolice.org/clearmap_crime_sums/crime_types.html.
X Coordinate - The x coordinate of the location where the incident occurred in State Plane Illinois East NAD 1983 projection. This location is shifted from the actual location for partial redaction but falls on the same block.
Y Coordinate - The y coordinate of the location where the incident occurred in State Plane Illinois East NAD 1983 projection. This location is shifted from the actual location for partial redaction but falls on the same block.
Year - Year the incident occurred.
Updated On - Date and time the record was last updated.
Latitude - The latitude of the location where the incident occurred. This location is shifted from the actual location for partial redaction but falls on the same block.
Longitude - The longitude of the location where the incident occurred. This location is shifted from the actual location for partial redaction but falls on the same block.
Location - The location where the incident occurred in a format that allows for creation of maps and other geographic operations on this data portal. This location is shifted from the actual location for partial redaction but falls on the same block.
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=4, fig.asp=0.3,out.width = "70%",fig.align ="center",echo=FALSE, warning=FALSE, message=FALSE, rows.print=30,cols.print=10)
```
```{r load_libs}
library(tidyverse)
library(reshape2)
library(data.table)
library(tictoc)
theme_set(theme_bw())
```
```{r}
file_loca<-"/Users/vinaychuri/VINAY/dataset/chicago_crimes/Crimes_-_2001_to_present.csv"
# file_loca<-"/Users/vichuri/VINAY/DATA_SETS/chicago_crimes/Crimes_-_2001_to_present.csv"
df<- fread(file_loca)
names(df)<- printvin::vin_setdfnames(names(df))
fbi_code_df<- fread("fbi_codes.csv")
names(fbi_code_df)<- printvin::vin_setdfnames(names(fbi_code_df))
```
The structure of the data...
```{r}
glimpse(df)
glimpse(fbi_code_df)
```
```{r}
fmt <- "%F %T"
df2<- df%>%head(10)
df2 <- df2[, date2:=fasttime::fastPOSIXct(date)]
df2%>%select(date,date2)
```
```{r main_updates}
df$date<-lubridate::mdy_hms(df$date)
df<- df%>%
filter(year(date)<2019)
```
How many columns have NA values...
```{r}
tic()
sapply(df, function(x) sum(is.na(x)))
toc()
```
Lets see the count of unique values per columns
```{r}
tic()
sapply(df, function(x) length(unique(x)))
toc()
```
Lets check the trend of crimes across the data...
```{r fig.width=4, fig.asp=0.3}
df%>%
mutate(yyyy=year(date))%>%
count(yyyy)%>%
ggplot(aes(as.factor(yyyy),n,group=1))+
geom_line()+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Year wise trend of crimes",x="Year",y="Total Crimes")
```
Which are the primary crimes in the data...
```{r }
df%>%
count(primary_type)%>%
ggplot(aes(reorder(primary_type,n),n))+
geom_col()+
coord_flip()+
# scale_y_continuous(labels=scales::comma_format())+
labs(title="Crime types",x="Type",y="Total Crimes")
```
What is the frequency of arrests in the data...
```{r fig.width=5, fig.asp=0.3}
# prop.table(table(df$arrest))
df%>%
count(arrest)%>%
mutate(props=paste(round(prop.table(n),2)*100,"%"))%>%
ggplot(aes(reorder(arrest,n),n))+
geom_col()+
coord_flip()+
geom_label(aes(label=props))+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Arrests",x="Type",y="Total Crimes")
```
Any trends to the arrest category per year
```{r fig.width=5, fig.asp=0.3}
df%>%
filter(arrest==TRUE)%>%
mutate(yyyy=year(date))%>%
count(yyyy)%>%
ggplot(aes(as.factor(yyyy),n,group=1))+
geom_line()+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Year wise trend of crime arrests",x="Year",y="Total Crimes")
```
```{r fig.width=5, fig.asp=0.3}
df%>%
mutate(yyyy=year(date))%>%
count(yyyy,arrest)%>%
ggplot(aes(as.factor(yyyy),n,group=arrest,color=arrest))+
geom_line()+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Year wise trend of crime arrests",x="Year",y="Total Crimes")
```
Lets check on various FBI codes...
```{r}
df%>%
count(fbi_code)%>%
ggplot(aes(reorder(fbi_code,n),n))+
geom_col()+
coord_flip()+
scale_y_continuous(labels=scales::comma_format())+
geom_label(aes(label=n),size=2)+
labs(title="Crime by FBI Codes",x="FBI Code",y="Total Crimes")
```
Lets get the full description of the FBI codes
```{r}
df%>%
count(fbi_code)%>%
left_join(.,fbi_code_df%>%select(crime_code,crime_type),by=c("fbi_code"="crime_code"))%>%
group_by(crime_type)%>%
summarise(n=sum(n))%>%
ggplot(aes(reorder(crime_type,n),n))+
geom_col()+
coord_flip()+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Crime by FBI Codes",x="FBI Code",y="Total Crimes")
```
```{r}
df%>%
count(fbi_code)%>%
left_join(.,fbi_code_df%>%select(crime_code,crime_description),by=c("fbi_code"="crime_code"))%>%
group_by(crime_description)%>%
summarise(n=sum(n))%>%
ggplot(aes(reorder(crime_description,n),n))+
geom_col()+
coord_flip()+
scale_y_continuous(labels=scales::comma_format())+
geom_label(aes(label=n),size=2)+
labs(title="Crime by FBI Codes",x="FBI Code",y="Total Crimes")
```
```{r}
df%>%
count(fbi_code)%>%
left_join(.,fbi_code_df,by=c("fbi_code"="crime_code"))%>%
filter(crime_type=="More serious offenses")%>%
group_by(crime_description)%>%
summarise(n=sum(n))%>%
ggplot(aes(reorder(crime_description,n),n))+
geom_col()+
coord_flip()+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Crime by FBI Codes",subtitle = "More serious offenses",x="FBI Code",y="Total Crimes")
```
```{r}
df%>%
count(fbi_code)%>%
left_join(.,fbi_code_df,by=c("fbi_code"="crime_code"))%>%
filter(crime_type=="Less serious offenses")%>%
group_by(crime_description)%>%
summarise(n=sum(n))%>%
ggplot(aes(reorder(crime_description,n),n))+
geom_col()+
coord_flip()+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Crime by FBI Codes",subtitle = "Less serious offenses",x="FBI Code",y="Total Crimes")
```
```{r}
df%>%
count(fbi_code)%>%
left_join(.,fbi_code_df,by=c("fbi_code"="crime_code"))%>%
filter(crime_type=="Crime related to Property")%>%
group_by(crime_description)%>%
summarise(n=sum(n))%>%
ggplot(aes(reorder(crime_description,n),n))+
geom_col()+
coord_flip()+
scale_y_continuous(labels=scales::comma_format())+
labs(title="Crime by FBI Codes",subtitle = "Less serious offenses",x="FBI Code",y="Total Crimes")
```
################### ROUGH WORK##################
```{r}
df[1,'date']
df%>%
head(5)%>%
select(date)%>%
mutate(date2=lubridate::mdy_hms(date))
```