-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExploritoryAnalysis.Rmd
318 lines (218 loc) · 6.34 KB
/
ExploritoryAnalysis.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
---
title: "Data Exploration"
author: "Christina De Cesaris"
date: "3/8/2021"
output: html_document
---
```{r}
library(tidyverse)
library(AER)
library(ggplot2)
library(plotly)
library(lmerTest)
library(MASS)
library(grid)
library(gridExtra)
library(DT)
```
# Expolritiy Analysis
```{r}
data(Guns)
dim(Guns)
summary(Guns)
str(Guns)
which(is.na(Guns)) #data clean
head(Guns)
```
```{r warning=FALSE, include=FALSE}
av_st = Guns %>%
group_by(state) %>% # average all state
summarise_all(mean) %>%
mutate_if(is.numeric,funs(round(.,2)))
av_st
#av_st
av_st_r = Guns %>%
group_by(state) %>% # average all state
summarise(mean(violent),median(violent), sd(violent), max(violent), min(violent))%>%
mutate_if(is.numeric,funs(round(.,2)))
av_st_r
av_yr_r = Guns %>%
group_by(year) %>% # average all state
summarise(mean(violent),median(violent), sd(violent), max(violent), min(violent))%>%
mutate_if(is.numeric,funs(round(.,2)))
av_yr_r
av_styr = Guns %>% # average each year each state
group_by(year,state) %>%
summarise_all(mean)%>%
mutate_if(is.numeric,funs(round(.,2)))
av_styr
av_yr = Guns %>% # average all states each years
group_by(year)%>%
summarise_all(mean)%>%
mutate_if(is.numeric,funs(round(.,2)))
av_yr
#delete null cols
av_yr=dplyr::select(av_yr,-c(12,13))
av_st=dplyr::select(av_st,-c(2,7,8,13))
```
```{r Yearly Trends All States}
v=Guns %>%
ggplot(
aes(x=year,
y=violent,
group=state,
color=state)) +
geom_line() +
labs(
title="Yearly Trends in Violent Crime by State",
x="School ID",
y = "Violent Crime Rate per 100K Poeple") +
theme(
text = element_text(
size=8,
face = 'bold'),
axis.text.x = element_text(angle=75,hjust=1),
axis.ticks.x = element_line(size=0.5)) +
scale_x_discrete(
guide = guide_axis(n.dodge =1),
expand=c(0, 0)) +
scale_fill_discrete(name = "State")
ggplotly(v)
r = Guns %>%
ggplot(
aes(x=year,
y=robbery,
group=state,
color=state)) +
geom_line() +
labs(
title="Yearly Trends in Robbery Crime by State",
x="School ID",
y = "Robbery Crime Rate per 100K People") +
theme(
text = element_text(
size=8,
face = 'bold'),
axis.text.x = element_text(angle=75,hjust=1),
axis.ticks.x = element_line(size=0.5)) +
scale_x_discrete(
guide = guide_axis(n.dodge =1),
expand=c(0, 0)) +
scale_fill_discrete(name = "State")
ggplotly(r)
m = Guns %>%
ggplot(
aes(x=year,
y=(violent),
group=year,
col=year)) +
geom_boxplot()+
labs(
title="USA Violent Crime by Year",
x="Year",
y = "Violent Crime Rate per 100K People") +
theme(
text = element_text(
size=8,
face = 'bold'),
axis.text.x = element_text(angle=75,hjust=1),
axis.ticks.x = element_line(size=0.5)) +
scale_x_discrete(
guide = guide_axis(n.dodge =1),
expand=c(0, 0)) +
scale_fill_discrete(name = "year")
ggplotly(m) # DC is a gross outlier, consider removal
```
```{r Interactive Datatables, include=FALSE}
names_yr=c("Year","Violent Crime Rate","Murder Rate","Robbery Rate","Prisoners","Percent Male","Population", "Income","Density")
dt_av_yr=datatable(av_yr,
class = 'cell-border stripe',
caption = htmltools::tags$caption(style = 'caption-side: bottom; text-align: center;','Table 1: ',
htmltools::em(
'Overall Averages By State')),
colnames = c(
names_yr))
names_st=c("State","Violent Crime Rate","Murder Rate","Robbery Rate","Prisoners","Percent Male","Population", "Income","Density")
dt_av_st=datatable(av_st,
class = 'cell-border stripe',
caption = htmltools::tags$caption(style = 'caption-side: bottom; text-align: center;','Table 1: ',
htmltools::em(
'Overall Averages By State')),
colnames = c(
names_st))
#RESPONSE
names_av_st_r=c("State","Mean Violence Rate", "Median Violence Rate", "Standard Deviation Violence Rate","Max Violence Rate","Min Violence Rate" )
dt_av_st_r=datatable(av_st_r,
class = 'cell-border stripe',
caption = htmltools::tags$caption(style = 'caption-side: bottom; text-align: center;','Table 1: ',
htmltools::em(
'Overall Averages By State')),
colnames = c(
names_av_st_r))
names_av_yr_r=c("Year","Mean Violence Rate", "Median Violence Rate", "Standard Deviation Violence Rate","Max Violence Rate","Min Violence Rate" )
dt_av_yr_r=datatable(av_yr_r,
class = 'cell-border stripe',
caption = htmltools::tags$caption(style = 'caption-side: bottom; text-align: center;','Table 1: ',
htmltools::em(
'Overall Averages By State')),
colnames = c(
names_av_yr_r))
```
```{r Average Crime USA Plot, include=FALSE}
crime_fig <- plot_ly(av_yr, x= ~year) %>%
add_lines(
y= ~(violent),
name = "Violent Crime",
type = 'scatter',
mode = 'lines+markers') %>%
add_lines(
y= ~(robbery),
name = "Robberies",
visible = T) %>%
add_lines(
y= ~(murder),
name = "Murder",
visible = T) %>%
add_lines(
y= ~(prisoners),
name = "Prisoners",
visible = T) %>%
layout(
title = "Average Crime and Incarceration Rates in the USA",
xaxis = list(title = "Year",
tickangle = 75),
yaxis = list(title = "Average per 100k People",
margin = margin))
crime_fig
```
```{r}
av_yr$other= (100-(av_yr$afam+av_yr$cauc))
crime_fig <- plot_ly(av_yr, x= ~year) %>%
add_lines(
y= ~(male),
name = "Males: Ages 10-29",
type = 'scatter',
mode = 'lines+markers') %>%
add_lines(
y= ~(afam),
name = "African Americans: Ages 10-64",
visible = T) %>%
add_lines(
y= ~(cauc),
name = "Caucation Americans: Ages 10-64",
visible = T) %>%
add_lines(
y= ~(other),
name = "Other American Ethnicities: Ages 10-64",
visible = T) %>%
layout(
title = "Population Demographics",
xaxis = list(title = "Year",
tickangle = 75),
yaxis = list(title = "Average percent of Population",
margin = margin),
legend)
crime_fig
```
```{r ref.label=knitr::all_labels(), echo = T, eval = F}
```