-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_data.R
269 lines (245 loc) · 11.6 KB
/
read_data.R
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
# This file will read and transform the full historical
# data files needed for analysis and modeling
library(data.table)
library(plyr)
library(reshape2)
library(stringr)
library(zoo)
source("util_functions.R")
# read the csv data in
charges <- data.table(read.csv("data/charges.csv"))
murders <- data.table(read.csv("data/murders.csv"))
dp.status <- data.table(read.csv("data/death_penalty_legality_ny.csv"))
pleas <- data.table(read.csv("data/pleas.csv"))
#fill down the county name
pleas$region <- paste(pleas$region)
ReplaceValues( pleas, "", NA, "region")
pleas[ ,region := na.locf(region) ]
# read the .txt data
size.budget.01 <- rbindlist(lapply(readLines(
"data/budget_2001/DS0001/03418-0001-Data.txt"),function(row)
data.table(
state = substr(row,442,443),
district = substr(row,451,482),
full_time_staff_2001 = as.numeric(substr(row,65,68)),
part_time_staff_2001 = as.numeric(substr(row,69,71)),
total_budget_2001 = as.numeric(substr(row,121,129))
)
))[ state == "NY" ]
ny.pop.2001 <- data.table(read.csv("data/ny_county_pulation_2001.csv"))
size.budget.01[ ,district := str_replace_all(district," ",replacement = "") ]
ny.pop.2001[ ,district := str_replace_all(district," ",replacement = "") ]
size.budget.01 <- merge(size.budget.01, ny.pop.2001, by="district")
size.budget.01[total_budget_2001 == 999999999 ]$total_budget_2001 <- NaN
size.budget.05 <- rbindlist(lapply(readLines(
"data/budget_2005/DS0001/04600-0001-Data.txt"),function(row)
data.table(
state = substr(row,19,20),
district = substr(row,21,68),
population_2005 = as.numeric(substr(row,73,80)),
full_time_staff_2005 = as.numeric(substr(row,1848,1855)),
part_time_staff_2005 = as.numeric(substr(row,1856,1863)),
total_budget_2005 = as.numeric(substr(row,1912,1919))
)
))[ state == "NY" ]
size.budget.05[ ,district := str_replace_all(district," ",replacement = "") ]
size.budget.overlap <- merge(size.budget.05,
size.budget.01,
by=c("state","district"))
size.budget.overlap[,c("staff_2001","staff_2005") := list(
sum(full_time_staff_2001,part_time_staff_2001),
sum(full_time_staff_2005,part_time_staff_2005)
), by="district" ]
size.budget.overlap <- {
budget <- melt(size.budget.overlap,
id.vars = c("district"),
measure.vars = c("total_budget_2005","total_budget_2001"),
variable.name = "year",value.name = "budget")
budget[ ,year := as.numeric(substr(year,14,17)) ]
staff <- melt(size.budget.overlap,
id.vars = c("district"),
measure.vars = c("staff_2005","staff_2001"),
variable.name = "year",value.name = "staff")
staff[ ,year := as.numeric(substr(year,7,19)) ]
population <- melt(size.budget.overlap,
id.vars = c("district"),
measure.vars = c("population_2005","population_2001"),
variable.name = "year",value.name = "population")
population[ ,year := as.numeric(substr(year,12,15)) ]
MergeDataFrames(list(
budget, population, staff
), by = c("district","year"),
all = FALSE)
}
#show there's no difference between 2001 and 2005 in budget allocation
hist( (size.budget.overlap$budget )^.4 )
summary(aov( budget ~ year + (population * staff), data = size.budget.overlap))
# melt the charges frame so it's useful
charges <- melt(charges,
id.vars = c("charge","county","outcome"),
value.name = "count",
variable.name = "year")
charges[ ,year_num := as.numeric(substr(year,2,5)) ]
charges$year <- NULL; setnames(charges, "year_num", "year")
charges <- dcast.data.table(charges, county + year + charge ~ outcome, value.var = "count")
setnames(charges,
c("region","year","charge","acquitted",
"convicted_plea","convicted_verdict","dismissed",
"sentenced_jail","not_prosecuted","other_outcome",
"sentenced_other","sentenced_prison",
"sentenced_probation","total_dispositions"))
charges <- charges[ !region %in% c("New York State","Non-New York City","New York City") &
!charge == "total" ]
# ditto with the murders frame
murders <- melt(murders,
id.vars = "region",
value.name = "n_murders",
variable.name = "year")[
!region %in% c("New York City","Rest of State","New York State")
]
murders[ ,year_num := as.numeric(substr(year,2,5)) ]
murders$year <- NULL; setnames(murders, "year_num", "year")
#aaaand the pleas frame (get it? pleas frame doo do-do do. do. do. do...)
pleas <- melt(pleas,
id.vars = c("charge","region","conviction"),
value.name = "n_dispositions",
variable.name = "year")[
!region %in% c("New York City","Rest of State","New York State")
]
pleas[ ,year_num := as.numeric(substr(year,2,5)) ]
pleas$year <- NULL; setnames(pleas, "year_num", "year")
pleas <- dcast.data.table(pleas,
charge + region + year ~ conviction,
value.var = "n_dispositions" )
setnames(pleas,
c("other","same","total"),
c("pleas_other","pleas_same","total_pleas"))
#merge em together!
murders.charges <- merge(
merge(charges,
pleas,
by = c("region","year","charge")),
murders,
by=c("region","year"),
all = TRUE )
ReplaceValues(murders.charges, NA, 0,
columns = names(murders.charges)[4:ncol(murders.charges)])
#put in an indicator for which years were death penalty ones
murders.charges <- merge(
murders.charges,
rbind(dp.status,data.table(year = 2014,
death_penalty_illegal = 1,
death_penalty_moratorium = 1,
death_status = "illegal")),
by="year"
)
murders.charges[ convicted_verdict == 1 & charge == "murder_1", .N, by="year,death_status"]
murders.charges[ ,nyc := region %in% c("New York","Queens","Kings","Bronx","Richmond") ]
murders.charges$nyc <- factor(murders.charges$nyc, levels = c(TRUE,FALSE),
labels = c("New York City","Other Districts"))
library(ggplot2)
rect_dat <- data.table( start = 1995, end_1 = 2004, end_2 = 2007 )
murders.charges[ ,zone := if(year < 1995) "pre" else if(year < 2006) "during" else "post",
by = year]
ggplot( murders.charges[!is.na(charge),
list(n_murders = sum(n_murders),
n_pleas = sum(convicted_plea),
n_dispos = sum(total_dispositions)),
by="year,zone,nyc,charge"][
,per_pleas := n_pleas / n_dispos ],
aes(x = year, y = per_pleas, color = charge )) +
geom_smooth( aes(group = zone), size = 1.8, se = TRUE, method = "lm" ) +
geom_point( size = 2.6, alpha = .7) +
facet_grid( nyc ~ charge ) +
geom_rect(data=rect_dat, aes(xmin=start, xmax=end_1),
ymin=0, ymax=15, alpha=0.3, fill="grey60", inherit.aes = FALSE) +
geom_rect(data=rect_dat, aes(xmin=end_1, xmax=end_2),
ymin=0, ymax=15, alpha=0.3, fill="grey80", inherit.aes = FALSE) +
ylab("Percent of Dispositions Resulting in Plea") +
theme( legend.position = "none",
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_text(size = 12),
axis.title.y = element_text(size = 13),
axis.title.x = element_blank(),
legend.title = element_blank())
#add some variables
plea.data <-
murders.charges[ charge == "murder_1",
list(per_plea_m1 = sum(convicted_plea) / sum(total_dispositions)),
by="year,nyc,death_status"][ !is.nan(per_plea_m1) ]
hist(plea.data$per_plea_m1)
plea.model <- aov( per_plea_m1 ~ death_status * nyc, data = plea.data )
plot(plea.model)
summary(plea.model)
TukeyHSD(aov(per_plea_m1 ~ death_status * nyc, data = plea.data))
#total pleas that are for murder 1
total.pleas <-
merge(
murders.charges[,list(total_dispos = sum(total_dispositions)),
by="year,nyc,death_status"],
murders.charges[charge == "murder_1",
list(murder_1_pleas = sum(convicted_plea)),
by="year,nyc,death_status"],
by=c("year","nyc","death_status")
)[,per_murder_1_total := murder_1_pleas / total_dispos ]
total.model <- aov(per_murder_1_total ~ year * death_status,
data = total.pleas )
plot(total.model)
summary(total.model)
ggplot(total.pleas, aes(x = year, y = per_murder_1_total)) +
geom_point( size = 3, alpha = .8) +
geom_rect(data=rect_dat, aes(xmin=start, xmax=end_1),
ymin=0, ymax=15, alpha=0.3, fill="grey60", inherit.aes = FALSE) +
geom_rect(data=rect_dat, aes(xmin=end_1, xmax=end_2),
ymin=0, ymax=15, alpha=0.3, fill="grey80", inherit.aes = FALSE) +
theme_bw()
#now percentage of pleas that are for lesser offenses
pleas.down <- murders.charges[ charge == "murder_1",
list(dispositions = sum(total_dispositions),
pleas = sum(total_pleas),
pleas_same = sum(pleas_same),
pleas_other = sum(pleas_other)),
by="year,nyc,death_status" ][
,per_lesser := pleas_other / pleas
][ !is.na(per_lesser) ]
hist(pleas.down$per_lesser)
ggplot(pleas.down, aes(x = death_status, y = per_lesser)) +
geom_boxplot() +
geom_point() +
facet_wrap( ~ nyc )
ggplot(pleas.down, aes(x = nyc, y = per_lesser)) +
geom_boxplot() +
geom_point() +
facet_wrap( ~ death_status )
pleas.down[ ,log_per := log(per_lesser) ]
summary(aov(log_per ~ death_status * nyc, pleas.down[!is.infinite(log_per)]))
TukeyHSD(aov(log_per ~ death_status + nyc, pleas.down[!is.infinite(log_per)]))
#look at the percent of all murder charges that are murder 1 charges
charges.murder <-
murders.charges[!is.na(charge),
list(n_murders = sum(n_murders),
n_charges = sum(total_dispositions)),
by=c("nyc","charge","year","death_status") ]
charges.murder <- merge( dcast.data.table(charges.murder,
nyc + year + death_status ~ charge,
value.var = "n_charges"),
murders.charges[,list(n_murders = sum(n_murders)),by="death_status,nyc,year"],
by=c("year","nyc","death_status") )
charges.murder[,per_murder_1 := murder_1/sum(c(murder_1,murder_2)),by="year,nyc,death_status" ]
charges.murder[,log_per_murder_1 := log(per_murder_1) ]
shapiro.test(charges.murder[!death_status == "shady" & !year %in% c(1995) ]$per_murder_1)
shapiro.test(charges.murder[!death_status == "shady" & !year %in% c(1995) ]$log_per_murder_1)
charges.murder[,statute := factor(year < 1995, levels = c(TRUE,FALSE), labels = c("pre","post")) ]
charges.murder.aov <-
aov( log_per_murder_1 ~ nyc * factor(year),
data = charges.murder[ !death_status == "shady" & !year %in% c(1995) ] )
summary.aov(lm(charges.murder.aov))
summary(lm(log_per_murder_1 ~ nyc + statute + death_status,
charges.murder[ !death_status == "shady" & year != 1995]))
plot(charges.murder.aov)
TukeyHSD(aov(log_per_murder_1 ~ death_status + nyc + statute,
data = charges.murder[ !death_status == "shady" & year != 1995 ]))
summary(aov(log_per_murder_1 ~ death_status * n_murders + nyc + factor(year),
data = charges.murder[ !death_status == "shady" & year > 1995 ]))
charges.murder[ ,mean(per_murder_1), by="death_status"]