-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 23.8 KB
/
.Rhistory
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
cor_matrix <- cor(data_subset)
# Convert the correlation matrix to a data frame
cor_df <- reshape2::melt(cor_matrix)
# Filter the data to only include the top 10 correlated variables to officer_years_on_force
cor_top_10 <- cor_df %>%
filter(Var1 == "OFFICER_YEARS_ON_FORC" & value != 1) %>%
arrange(desc(value)) %>%
head(10)
# Extract the names of the top 10 correlated variables
top_10_vars <- cor_top_10$Var2
# Create a subset of the data with only the top 10 correlated variables and officer_years_on_force
data_subset_top_10 <- data_subset %>%
select(one_of(c("OFFICER_YEARS_ON_FORC", top_10_vars)))
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_ID,OFFICER_YEARS_ON_FORCE,SUBJECT_ID,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# Select the relevant variables
data_subset <- subdata[, c("OFFICER_YEARS_ON_FORCE", "REPORTING_AREA", "BEAT", "SECTOR", "Officerrace", "officergender", "subjectrace", "subjectgender", "subjectinjury", "officerinjury")]
# Calculate the correlation matrix
cor_matrix <- cor(data_subset)
data_subset
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
Officerrace
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_ID,OFFICER_YEARS_ON_FORCE,SUBJECT_ID,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# Select the relevant variables
data_subset <- subdata[, c("OFFICER_YEARS_ON_FORCE", "REPORTING_AREA", "BEAT", "SECTOR", "Officerrace", "officergender", "subjectrace", "subjectgender", "subjectinjury", "officerinjury")]
# convert the data frame to numeric
data_subset <- apply(data_subset, 2, as.numeric)
# Calculate the correlation matrix
cor_matrix <- cor(data_subset)
# Identify the variables with the highest correlation with OFFICER_YEARS_ON_FORCE
cor_with_officer <- cor_matrix[, "OFFICER_YEARS_ON_FORCE"]
top_vars <- names(sort(cor_with_officer, decreasing = TRUE)[2:11])
# Create a subset of the data with only the top 10 correlated variables
data_subset_top10 <- data_subset[, c("OFFICER_YEARS_ON_FORCE", top_vars)]
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_ID,OFFICER_YEARS_ON_FORCE,SUBJECT_ID,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# Select the relevant variables
data_subset <- subdata[, c("OFFICER_YEARS_ON_FORCE", "REPORTING_AREA", "BEAT", "SECTOR", "Officerrace", "officergender", "subjectrace", "subjectgender", "subjectinjury", "officerinjury")]
# convert the data frame to numeric
data_subset <- apply(data_subset, 2, as.numeric)
# Calculate the correlation matrix
cor_matrix <- cor(data_subset)
# Identify the variables with the highest correlation with OFFICER_YEARS_ON_FORCE
cor_with_officer <- cor_matrix[, "OFFICER_YEARS_ON_FORCE"]
top_vars <- names(sort(cor_with_officer, decreasing = TRUE)[2:11])
# Create a subset of the data with only the top 10 correlated variables
data_subset_top10 <- data_subset[,c("OFFICER_YEARS_ON_FORCE", top_vars)]
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_ID,OFFICER_YEARS_ON_FORCE,SUBJECT_ID,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_numVar <- cor(subdata, use="pairwise.complete.obs") #correlations of all numeric variables
#sort on decreasing correlations with SalePrice
cor_sorted <- as.matrix(sort(cor_numVar[,'SalePrice'], decreasing = TRUE))
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_ID,OFFICER_YEARS_ON_FORCE,SUBJECT_ID,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_numVar <- cor(subdata, use="pairwise.complete.obs") #correlations of all variables
#sort on decreasing correlations with OFFICER_YEARS_ON_FORCE
cor_sorted <- as.matrix(sort(cor_numVar[,'OFFICER_YEARS_ON_FORCE'], decreasing = TRUE))
#select only high correlations
CorHigh <- names(which(apply(cor_sorted, 1, function(x) abs(x)>0.3| x< -0.3)))
cor_numVar <- cor_numVar[CorHigh, CorHigh]
corrplot.mixed(cor_numVar, tl.col="black", tl.pos = "lt")
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,SUBJECT_ID,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_numVar <- cor(subdata, use="pairwise.complete.obs") #correlations of all variables
#sort on decreasing correlations with OFFICER_YEARS_ON_FORCE
cor_sorted <- as.matrix(sort(cor_numVar[,'OFFICER_YEARS_ON_FORCE'], decreasing = TRUE))
#select only high correlations
CorHigh <- names(which(apply(cor_sorted, 1, function(x) abs(x)>0.05| x< -0.05)))
cor_numVar <- cor_numVar[CorHigh, CorHigh]
corrplot.mixed(cor_numVar, tl.col="black", tl.pos = "lt")
names(data)
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,SUBJECT_ID,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_numVar <- cor(subdata, use="pairwise.complete.obs") #correlations of all variables
#sort on decreasing correlations with OFFICER_YEARS_ON_FORCE
cor_sorted <- as.matrix(sort(cor_numVar[,'OFFICER_YEARS_ON_FORCE'], decreasing = TRUE))
#select only high correlations
CorHigh <- names(which(apply(cor_sorted, 1, function(x) abs(x)>0.3| x< -0.3)))
cor_numVar <- cor_numVar[CorHigh, CorHigh]
corrplot.mixed(cor_numVar, tl.col="black", tl.pos = "lt")
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_numVar <- cor(subdata, use="pairwise.complete.obs") #correlations of all variables
Creating a correlation matrix for the selected variables
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_matrix <- cor(subdata)
# Creating a matrix of the same dimensions as cor_matrix, but with all entries set to 0
mask <- matrix(0, nrow(cor_matrix), ncol(cor_matrix))
# Setting entries in the mask matrix to 1 for correlations that are above the threshold (0.7)
mask[abs(cor_matrix) > 0.7] <- 1
# Creating a heat map of the correlation matrix, with high correlations highlighted in red
library(ggplot2)
ggplot2::ggplot(data = reshape2::melt(cor_matrix)) +
ggplot2::geom_tile(ggplot2::aes(x = Var1, y = Var2, fill = value)) +
ggplot2::geom_text(data = reshape2::melt(cor_matrix * mask),
ggplot2::aes(x = Var1, y = Var2, label = round(value, 2)),
color = "red") +
ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Correlation\nCoefficient") +
ggplot2::theme_minimal() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 1,
size = 10, hjust = 1)) +
ggplot2::coord_fixed()
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,REPORTING_AREA,BEAT,SECTOR,STREET_NUMBER,INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_matrix <- cor(subdata)
# Creating a matrix of the same dimensions as cor_matrix, but with all entries set to 0
mask <- matrix(0, nrow(cor_matrix), ncol(cor_matrix))
# Setting entries in the mask matrix to 1 for correlations that are above the threshold (0.7)
mask[abs(cor_matrix) > 0.5] <- 1
# Creating a heat map of the correlation matrix, with high correlations highlighted in red
library(ggplot2)
ggplot2::ggplot(data = reshape2::melt(cor_matrix)) +
ggplot2::geom_tile(ggplot2::aes(x = Var1, y = Var2, fill = value)) +
ggplot2::geom_text(data = reshape2::melt(cor_matrix * mask),
ggplot2::aes(x = Var1, y = Var2, label = round(value, 2)),
color = "red") +
ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Correlation\nCoefficient") +
ggplot2::theme_minimal() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 1,
size = 10, hjust = 1)) +
ggplot2::coord_fixed()
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,BEAT, INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_matrix <- cor(subdata)
# Creating a matrix of the same dimensions as cor_matrix, but with all entries set to 0
mask <- matrix(0, nrow(cor_matrix), ncol(cor_matrix))
# Setting entries in the mask matrix to 1 for correlations that are above the threshold (0.7)
mask[abs(cor_matrix) > 0.5] <- 1
# Creating a heat map of the correlation matrix, with high correlations highlighted in red
library(ggplot2)
ggplot2::ggplot(data = reshape2::melt(cor_matrix)) +
ggplot2::geom_tile(ggplot2::aes(x = Var1, y = Var2, fill = value)) +
ggplot2::geom_text(data = reshape2::melt(cor_matrix * mask),
ggplot2::aes(x = Var1, y = Var2, label = round(value, 2)),
color = "red") +
ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Correlation\nCoefficient") +
ggplot2::theme_minimal() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 1,
size = 10, hjust = 1)) +
ggplot2::coord_fixed()
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,BEAT, INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_matrix <- cor(subdata)
# Creating a matrix of the same dimensions as cor_matrix, but with all entries set to 0
mask <- matrix(0, nrow(cor_matrix), ncol(cor_matrix))
# Setting entries in the mask matrix to 1 for correlations that are above the threshold (0.7)
mask[abs(cor_matrix) > 0.3] <- 1
# Creating a heat map of the correlation matrix, with high correlations highlighted in red
library(ggplot2)
ggplot2::ggplot(data = reshape2::melt(cor_matrix)) +
ggplot2::geom_tile(ggplot2::aes(x = Var1, y = Var2, fill = value)) +
ggplot2::geom_text(data = reshape2::melt(cor_matrix * mask),
ggplot2::aes(x = Var1, y = Var2, label = round(value, 2)),
color = "red") +
ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Correlation\nCoefficient") +
ggplot2::theme_minimal() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 1,
size = 10, hjust = 1)) +
ggplot2::coord_fixed()
library(corrplot)
library(ggcorrplot)
library(reshape2)
attach(data)
# Convert the vector to a factor
Officerrace<- factor(OFFICER_RACE)
subjectarrest <- factor(SUBJECT_WAS_ARRESTED)
officergender <- factor(OFFICER_GENDER)
subjectrace <- factor(SUBJECT_RACE)
subjectgender<- factor(SUBJECT_GENDER)
subjectinjury <- factor(SUBJECT_INJURY)
officerinjury <- factor(OFFICER_INJURY)
# Subset the dataset to numerical variables only
subdata <- data.frame(UOF_NUMBER,OFFICER_YEARS_ON_FORCE,BEAT, INCIDENT_DATE_AND_TIME, OFFICER_HIRE_DATE, Officerrace, subjectarrest, officergender, subjectrace, subjectgender, subjectinjury, officerinjury )
# convert the data frame to numeric
subdata <- apply(subdata, 2, as.numeric)
cor_matrix <- cor(subdata)
# Creating a matrix of the same dimensions as cor_matrix, but with all entries set to 0
mask <- matrix(0, nrow(cor_matrix), ncol(cor_matrix))
# Setting entries in the mask matrix to 1 for correlations that are above the threshold (0.7)
mask[abs(cor_matrix) > 0] <- 1
# Creating a heat map of the correlation matrix, with high correlations highlighted in red
library(ggplot2)
ggplot2::ggplot(data = reshape2::melt(cor_matrix)) +
ggplot2::geom_tile(ggplot2::aes(x = Var1, y = Var2, fill = value)) +
ggplot2::geom_text(data = reshape2::melt(cor_matrix * mask),
ggplot2::aes(x = Var1, y = Var2, label = round(value, 2)),
color = "red") +
ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limit = c(-1,1), space = "Lab",
name="Correlation\nCoefficient") +
ggplot2::theme_minimal() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90, vjust = 1,
size = 10, hjust = 1)) +
ggplot2::coord_fixed()
library(tidyverse)
library(plotly)
# Group data by day and calculate total incidents
daily_incidents <- data %>%
group_by(INCIDENT_DATE) %>%
summarise(total_incidents = n())
# Create time series plot with plotly
plot_ly(daily_incidents, x = ~INCIDENT_DATE, y = ~total_incidents, type = "scatter", mode = "lines") %>%
layout(title = "Total Incidents per Day",
xaxis = list(title = "Date"),
yaxis = list(title = "Total Incidents")) %>%
highlight(on = "plotly_hover", off = "plotly_unhover", dynamic = TRUE)
library(tidyverse)
library(plotly)
# Load data
data <- read.csv("your_data_file.csv")
library(tidyverse)
library(plotly)
# Group data by day and calculate total incidents
daily_incidents <- data %>%
group_by(INCIDENT_DATE) %>%
summarise(total_incidents = n())
# Create time series plot with plotly
plot_ly(daily_incidents, x = ~INCIDENT_DATE, y = ~total_incidents, type = "scatter", mode = "lines") %>%
layout(title = "Total Incidents per Day",
xaxis = list(title = "Date"),
yaxis = list(title = "Total Incidents"),
colorway = c("blue", "red", "green", "orange", "purple")) %>%
highlight(on = "plotly_hover", dynamic = TRUE)
library(tidyverse)
library(plotly)
# Group data by day and calculate total incidents
daily_incidents <- data %>%
group_by(INCIDENT_DATE) %>%
summarise(total_incidents = n())
# Create time series plot with plotly
plot_ly(daily_incidents, x = ~INCIDENT_DATE, y = ~total_incidents, type = "scatter", mode = "lines") %>%
layout(title = "Total Incidents per Day",
xaxis = list(title = "Date"),
yaxis = list(title = "Total Incidents"),
colorway = c("blue", "red", "green", "orange", "purple")) %>%
highlight(on = "plotly_hover", dynamic = TRUE)
# Calculate the number of officers who are involved in subject injury
num_subject_injured <- data %>%
filter(SUBJECT_INJURY == "Yes") %>%
group_by(OFFICER_ID) %>%
summarize(num_injuries = n())
num_subject_injured
sum(num_subject_injured$num_injuries)
library(ggplot2)
library(plotly)
p <- ggplot(num_subject_injured, aes(x = OFFICER_ID, y = num_injuries)) +
geom_point(color = "red", alpha = 0.7) +
stat_smooth(method = "loess", se = FALSE) + # add smoothing line
labs(title = "Scatter Plot of Number of subject injuries involved by an officer", x = "Officer ID", y = "Number of Injuries") +
theme_minimal()
ggplotly(p)
# Create a scatter plot with a loess smoothing line
ggplot(data, aes(x = OFFICER_YEARS_ON_FORCE, y = INCIDENT_DATE)) +
geom_point() +
geom_smooth(method = "loess", se = FALSE) +
labs(x = "Officer Years on Force", y = "Number of Incidents",
title = "Relationship between Officer Experience and Incidents")
# Load the required packages
library(leaflet)
# Create a leaflet map
leaflet(data ) %>%
addTiles() %>%
addMarkers(~LOCATION_LONGITUDE, ~LOCATION_LATITUDE, popup = ~REPORTING_AREA)
library(leaflet)
leaflet(data) %>%
addTiles() %>%
addMarkers(lng = ~LOCATION_LONGITUDE, lat = ~LOCATION_LATITUDE,
popup = ~paste("Incident Reason: ", INCIDENT_REASON,
"<br>Subject Race: ", SUBJECT_RACE,
"<br>Subject Offense: ", SUBJECT_OFFENSE))
library(leaflet)
leaflet(data) %>%
addTiles() %>%
addMarkers(lng = ~LOCATION_LONGITUDE, lat = ~LOCATION_LATITUDE,
popup = ~paste("Incident Reason: ", INCIDENT_REASON,
"<br>Officer Race: ", OFFICER_RACE,
"<br>Subject Race: ", SUBJECT_RACE,
"<br>Subject Offense: ", SUBJECT_OFFENSE
))
names(data)
library(leaflet)
leaflet(data) %>%
addTiles() %>%
addMarkers(lng = ~LOCATION_LONGITUDE, lat = ~LOCATION_LATITUDE,
popup = ~paste("Incident Reason: ", INCIDENT_REASON,
"<br>Subject Race: ", SUBJECT_RACE,
"<br>Subject Offense: ", SUBJECT_OFFENSE
))
# Calculate the number of officers who are involved in subject injury
num_subject_injured <- data %>%
filter(SUBJECT_INJURY == "Yes") %>%
group_by(OFFICER_ID) %>%
summarize(num_injuries = n())
num_subject_injured
sum(num_subject_injured$num_injuries)
library(ggplot2)
library(plotly)
p <- ggplot(num_subject_injured, aes(x = OFFICER_ID, y = num_injuries)) +
geom_point(color = "red", alpha = 0.7) +
labs(title = "Scatter Plot of Number of subject injuries involved by an officer", x = "Officer ID", y = "Number of Injuries") +
theme_minimal()
ggplotly(p)
library(plotly)
plot_ly(data , x = ~OFFICER_YEARS_ON_FORCE, y = ~INCIDENT_DATE, color = ~OFFICER_RACE, size = ~UOF_NUMBER,
mode = "markers", type = "scatter") %>%
layout(title = "Number of Incidents vs Officer Years on Force",
xaxis = list(title = "Officer Years on Force"),
yaxis = list(title = "Incident Date"))
table(data$OFFICER_RACE)
aggr_plot <- aggr(data, col=c('navyblue','red'),numbers=TRUE,sortVars=TRUE,
labels=names(data),cex.axis=.7,gap=3,
ylab=c("Histogram of Missing data","Pattern of Missing data"))
library(VIM)
aggr_plot <- aggr(data, col=c('navyblue','red'),numbers=TRUE,sortVars=TRUE,
labels=names(data),cex.axis=.7,gap=3,
ylab=c("Histogram of Missing data","Pattern of Missing data"))
library(ggplot2)
# Calculate the number of officers who are injured
num_officers_injured <- data %>%
filter(OFFICER_INJURY == "Yes") %>%
group_by(OFFICER_ID) %>%
summarize(num_injuries = n())
num_officers_injured
sum(num_officers_injured$num_injuries)
# Create a histogram of the number of officers who are injured
ggplot(num_officers_injured, aes(x = num_injuries)) +
geom_histogram(aes(fill = ..count..), binwidth = 1, color = "black", alpha = 0.7) +
scale_fill_gradient(low = "lightblue", high = "darkblue") +
stat_bin(aes(label = ..count..), binwidth = 1, color = "black", size = 2, vjust = -0.5) +
labs(title = "Number of officers who are injured", x = "Number of Injuries", y = "Count") +
theme_minimal()
setwd("C:/Users/mellogwayo/Downloads/3D Maps Tutorial")
getwd()