-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsights.Rmd
264 lines (223 loc) · 10.6 KB
/
insights.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
---
title: "Project"
output: html_notebook
---
```{r}
rm(list=ls())
ls()
setwd("F:/Senior-2-Spring/Big-Data/Project-Repo/Big-Data-CrimeAnalysis-R-Hadoop")
```
load csv
```{r}
dataset <- read.csv("Major_Crime_Indicators.csv")
anyNA(dataset$mci_category)
anyNA(dataset$reportedday)
anyNA(dataset$reportedyear)
anyNA(dataset$reportedmonth)
anyNA(dataset$reportedhour)
anyNA(dataset$offence)
anyNA(dataset$Neighbourhood)
anyNA(dataset$premises_type)
anyNA(dataset$occurrenceday)
anyNA(dataset$occurrencehour)
anyNA(dataset$occurrencemonth)
anyNA(dataset$occurrenceyear)
anyNA(dataset$occurrencedate)
#=========================Filling missing value====================================
date_year<-c(dataset$occurrencedate[is.na(dataset$occurrenceyear)])
#filling missing data in occurrence year from the column occurrence date
dataset$occurrenceyear[is.na(dataset$occurrenceyear)]<-substr(date_year,1,4)
#checking that they are all filled
anyNA(dataset$occurrenceyear)
levels(factor(dataset$occurrenceyear))
date_day<-c(dataset$occurrencedate[is.na(dataset$occurrenceday)])
#filling missing data in occurrence day from the column occurrence date
dataset$occurrenceday[is.na(dataset$occurrenceday)]<-substr(date_day,9,10)
#checking that they are all filled
anyNA(dataset$occurrenceday)
levels(factor(dataset$occurrenceday))
date_month<-c(dataset$occurrencedate[dataset$occurrencemonth==""])
substr(date_month[1],6,7)
#filling missing data in occurrence day from the column occurrence date
dataset$occurrencemonth[dataset$occurrencemonth==""]<-substr(date_month,6,7)
levels(factor(dataset$occurrencemonth))
dataset$occurrencemonth[which(dataset$occurrencemonth=="January")] <- "1"
dataset$occurrencemonth[which(dataset$occurrencemonth=="February")] <- "2"
dataset$occurrencemonth[which(dataset$occurrencemonth=="March")] <- "3"
dataset$occurrencemonth[which(dataset$occurrencemonth=="April")] <- "4"
dataset$occurrencemonth[which(dataset$occurrencemonth=="May")] <- "5"
dataset$occurrencemonth[which(dataset$occurrencemonth=="June")] <- "6"
dataset$occurrencemonth[which(dataset$occurrencemonth=="July")] <- "7"
dataset$occurrencemonth[which(dataset$occurrencemonth=="August")] <- "8"
dataset$occurrencemonth[which(dataset$occurrencemonth=="September")] <- "9"
dataset$occurrencemonth[which(dataset$occurrencemonth=="October")] <- "10"
dataset$occurrencemonth[which(dataset$occurrencemonth=="November")] <- "11"
dataset$occurrencemonth[which(dataset$occurrencemonth=="December")] <- "12"
dataset$occurrencemonth <- as.numeric(dataset$occurrencemonth)
levels(factor(dataset$occurrencemonth))
week_day<-c(dataset$occurrencedate[dataset$occurrencedayofweek == ""])
week_day
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "")] <-strftime(substr(week_day,1,10), format = "%A")
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "Friday ")]<-"Friday"
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "Monday ")]<-"Monday"
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "Saturday ")]<-"Saturday"
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "Sunday ")]<-"Sunday"
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "Thursday ")]<-"Thursday"
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "Tuesday ")]<-"Tuesday"
dataset$occurrencedayofweek[(dataset$occurrencedayofweek == "Wednesday ")]<-"Wednesday"
levels(factor(dataset$occurrencedayofweek))
```
insights
Most frequent crime of all times
Match works as the map function it collects the rows of the unique categories in the given column
tabulate then counts the number of rows for each category
which.max gets the max category
```{r}
uniqx <- unique(na.omit(dataset$mci_category))
uniqx[which.max(tabulate(match(dataset$mci_category, uniqx)))]
```
The graph shows that assault is the most frequent type of crime of all times
```{r}
uniqx
counts<-tabulate(match(dataset$mci_category, uniqx))
barplot(counts, main = "Total crimes for each category", names.arg = uniqx,
xlab = "Category type", ylab = "Total number",
col = "red")
```
Total crimes for different categories of Theft Over as it is the least frequent crime type
```{r}
theft_over_rows<-dataset[dataset$mci_category == 'Theft Over',]
theft_over_offence<-unique(na.omit(theft_over_rows$offence))
theft_over_offence[2]
counts<-tabulate(match(theft_over_rows$offence, theft_over_offence))
#par(mar = c(10, 4, 10, 1))
barplot(counts, main = "Total for different categories of Theft Over ", names.arg = theft_over_offence, xlab = "Category type", ylab = "Total number", col = "blue", las = 2)
library(ggplot2)
library(scales)
df<-data.frame(counts,theft_over_offence)
p<-ggplot(data=df, aes(x=theft_over_offence, y=counts,fill=counts))+geom_bar(stat="identity") +scale_x_discrete(labels = wrap_format(10))+geom_text(aes(label = counts), vjust = 0)+ggtitle(label="Total for different categories of Theft Over")+xlab(label="Category Type")+ylab("count")
p
```
Most frequent Category of Auto theft
```{r}
theft_over_rows<-dataset[dataset$mci_category == 'Auto Theft',]
theft_over_offence<-unique(na.omit(theft_over_rows$offence))
theft_over_offence
```
Assault insights
Total crimes for different categories of Assault as it is the most frequent category
```{r}
theft_over_rows<-dataset[dataset$mci_category == 'Assault',]
theft_over_offence<-unique(na.omit(theft_over_rows$offence))
theft_over_offence
counts<-tabulate(match(theft_over_rows$offence, theft_over_offence))
#par(mar = c(10, 4, 10, 1))
barplot(counts, main = "Total for different categories of Assault", names.arg = theft_over_offence,
xlab = "Category type", ylab = "Total number",
col = "blue", las = 2)
library(ggplot2)
library(scales)
df <- data.frame(counts, theft_over_offence)
options(repr.plot.width = 500, repr.plot.height =3)
theme_update(plot.title = element_text(hjust = 0.5))
p<-ggplot(data=df, aes(x=theft_over_offence, y=counts,fill=counts))+geom_bar(stat="identity") +scale_x_discrete(labels = wrap_format(5))+geom_text(aes(label = counts), vjust = 0)+ggtitle(label="Total for different categories of Assault")+xlab(label="Category Type")+ylab("count")
p
```
Neighbourhood insights
Most dangerous Neighborhood analysis
Waterfront Communities-The Island is the most unsafe place
```{r}
Neighbour <- unique(na.omit(dataset$Neighbourhood))
most_dangerous_n<-Neighbour[which.max(tabulate(match(dataset$Neighbourhood, Neighbour)))]
most_dangerous_n
```
Waterfront Communities-The Island insights
Assault is the most frequent type of crime in the most dangerous neighborhood
```{r}
neighbor_analysis<-dataset[dataset$Neighbourhood == most_dangerous_n,]
neighbor_crimes<-unique(na.omit(neighbor_analysis$mci_category))
counts<-tabulate(match(neighbor_analysis$mci_category, neighbor_crimes))
pie(c(counts),labels=paste(neighbor_crimes,round(100*counts/sum(counts),0),"%"),col=c("blue","red","yellow","green","pink","magenta","gray"),main=paste("Crime Analysis",most_dangerous_n))
```
Least Dangerous neighborhood analysis
```{r}
least_dangerous_n<-Neighbour[which.min(tabulate(match(dataset$Neighbourhood, Neighbour)))]
least_dangerous_n
```
```{r}
neighbor_analysis<-dataset[dataset$Neighbourhood == least_dangerous_n,]
neighbor_crimes<-unique(na.omit(neighbor_analysis$mci_category))
counts<-tabulate(match(neighbor_analysis$mci_category, neighbor_crimes))
typeof(counts)
pie(c(counts),labels=paste(neighbor_crimes,round(100*counts/sum(counts),0),"%"),col=c("blue","red","yellow","green","pink","magenta","gray"),main=paste("Crime Analysis",least_dangerous_n))
```
Crime Rate over Years 1966-2022
As the graph shows the crime rate kept increasing from 2014 till 2019
2019 was the peak
then the rate dropped until now, this means that the police station implemented better security precautions
```{r}
years <- unique(na.omit(dataset$occurrenceyear))
years[which.max(tabulate(match(dataset$occurrenceyear, years)))]
count<-tabulate(match(dataset$occurrenceyear, years))
library(ggplot2)
#ggplot(mapping= aes(x=months, y=count, group=0)) +geom_line()+geom_point()+geom_line()+ggtitle("Crimes across all months")
# Second variable
val<-data.frame(years,count)
val
theme_update(plot.title = element_text(hjust = 0.5))
ggplot(data=val, aes(x=years, y=count, group=2)) +geom_line()+ggtitle(label="Crime Rates over the years\n 1966-2022")
```
Crime rate analysis of months
```{r}
months <- unique(na.omit(dataset$occurrencemonth))
months
count<-tabulate(match(dataset$occurrencemonth, months))
count
#plot(y=count,type = "o", col = "red", xlab = "Years", ylab = "Rain fall", main = "Crimes rate across months of all years", x=months,xlim=c(1,12))
library(ggplot2)
#ggplot(mapping= aes(x=months, y=count, group=0)) +geom_line()+geom_point()+geom_line()+ggtitle("Crimes across all months")
# Second variable
levels(factor(dataset$occurrencemonth))
val<-data.frame(months,count)
theme_update(plot.title = element_text(hjust = 0.5))
ggplot(data=val, aes(x=months, y=count, group=1)) +geom_line()+
geom_point()+xlim(1, 12)+scale_x_discrete(limits=(1:12))+ggtitle(label="Total number of crimes per month")
```
Time difference between occurence and reporting
```{r}
s <-aggregate(x= difftime(dataset$reporteddate, dataset$occurrencedate, units = "days") ,by = list(dataset$mci_categor),FUN = mean,na.rm=TRUE)
mean_time_difference<-data.frame(s)
mean_time_difference
mean_num<-as.numeric(mean_time_difference$x)
pie(c(mean_num),labels=paste(mean_time_difference$Group.1,round((mean_num),0),"days"),col=c("blue","red","yellow","green","pink","magenta","gray"),main=paste("Time difference between occurence and reporting"))
```
insights on premises
We calculated the most frequent premises type in which each category of crime occurs
```{r}
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
t<-aggregate(x=dataset$premises_type ,by = list(dataset$mci_category),FUN = Mode)
t
```
Day of the week on which the most number of crimes occur
we calculated the most frequent day on which each type of crime occured
we also represented the frequency of crimes per days of the week using a bar chart
```{r}
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
day <- unique(na.omit(dataset$occurrencedayofweek))
day[1]
count<-tabulate(match(dataset$occurrencedayofweek, day))
count
library(ggplot2)
df<-data.frame(day,count)
theme_update(plot.title = element_text(hjust = 0.5))
p<-ggplot(data=df, aes(x=day, y=count,fill=count))+geom_bar(stat="identity") +geom_text(aes(label = count), vjust = 0)+ggtitle(label="Total number of crimes per days of the week")+xlab(label="days")+ylab("count")
p
days<-aggregate(x=dataset$occurrencedayofweek ,by = list(dataset$mci_category),FUN = Mode)
days
```