-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pct_crime.R
More file actions
372 lines (311 loc) · 14 KB
/
test_pct_crime.R
File metadata and controls
372 lines (311 loc) · 14 KB
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
#' from https://data.cityofnewyork.us/Public-Safety/NYPD-Complaint-Data-Historic/qgea-i56i/data_preview
#' but I actually got it from Kaggle bc the NYPD data would not download
#' https://data.cityofnewyork.us/Public-Safety/NYPD-Complaint-Data-Historic/qgea-i56i/data_preview
#'
#' if crime dataset doesn't already exist
if (!file.exists("data/nypd-crime/test_pct_crime.rds")) {
#' the data is split into two files,
#' one with data 2006 to 2019 and one with data 2020 to 2024
crime_data_historic_to2019 <- read_csv(
"data/nypd-crime/NYPD_Complaint_Data_Historic.csv",
col_types = cols(.default = col_character()) # treat all vars as chars for bind
)
crime_data_historic_to2024 <- read_csv(
"data/nypd-crime/NYPD_Complaint_Data_Historic_1.csv",
col_types = cols(.default = col_character()) # treat all vars as chars for bind
)
# bind complaint data together, 2006 to 2024
test_pct_crime <- bind_rows(crime_data_historic_to2019,
crime_data_historic_to2024)
}
saveRDS(test_pct_crime, file = "data/nypd-crime/test_pct_crime.rds")
test_pct_crime <- readRDS("data/nypd-crime/test_pct_crime.rds")
# list of required variables for OLS models
req_vars <- c("pct", "boro", "year", "month",
"felony", "misdemeanor", "violation",
"crime_Violent", "crime_Weapons", "crime_Property", "crime_Drug",
"crime_Trespass", "crime_QualityOfLife", "crime_Other")
# ---- time variables ----
#' create `year` and `month` variables
#' lubridate is doing something weird with the dates,
#' check to make sure the dates are just formatted weird
weird_dates <- test_pct_crime %>%
arrange(CMPLNT_FR_DT) %>%
head(150) %>%
select(CMPLNT_NUM, CMPLNT_FR_DT)
#' pull the complaint numbers for the weird dates so
#' I can check them in the original data frame
weird_dates_nums <- weird_dates %>% pull(CMPLNT_NUM)
#' check the original data frame for those complaint numbers to see if
#' the dates are just formatted weirdly or if they are actually wrong
test_pct_crime %>%
filter(CMPLNT_NUM %in% weird_dates_nums) %>%
select(CMPLNT_NUM, CMPLNT_FR_DT, CMPLNT_TO_DT) %>%
print(n = 150)
#' lubridate dates,
#' if `CMPLNT_FR_DT` is more than 3 days before `CMPLNT_TO_DT`,
#' then use `CMPLNT_TO_DT` as the year/month, otherwise use `CMPLNT_FR_DT`
test_pct_crime <- test_pct_crime %>%
mutate(
CMPLNT_FR_DT = mdy(CMPLNT_FR_DT),
CMPLNT_TO_DT = mdy(CMPLNT_TO_DT)
)
#' create `year` and `month` variables
test_pct_crime <- test_pct_crime %>%
mutate(
final_date = if_else(!is.na(CMPLNT_TO_DT) # if start is is not empty
& (CMPLNT_TO_DT - CMPLNT_FR_DT) > ddays(3), # and if end date is more than 3 days after start date
CMPLNT_TO_DT, # then use end date
CMPLNT_FR_DT), # else use start date
year = as.integer(year(final_date)), # lube year from final date
month = as.integer(month(final_date)) # lube month from final date
)
#' create `pct` and `boro` variables
test_pct_crime <- test_pct_crime %>%
mutate(pct = as.numeric(ADDR_PCT_CD),
boro = BORO_NM
)
# ---- offense categories ----
#' summary table of offenses
test_pct_crime %>%
group_by(LAW_CAT_CD) %>%
summarize(n = n()) %>%
mutate(
percent = n / sum(n) * 100
) %>%
arrange(desc(n))
#' create `felony`, `misdemeanor`, and `violation` variables
test_pct_crime <- test_pct_crime %>%
mutate(felony = ifelse(LAW_CAT_CD == "FELONY", 1, 0),
misdemeanor = ifelse(LAW_CAT_CD == "MISDEMEANOR", 1, 0),
violation = ifelse(LAW_CAT_CD == "VIOLATION", 1, 0))
# list all unique offense descriptions in the data
sort(unique(test_pct_crime$OFNS_DESC))
#' categorize crimes into broad offense categories
test_pct_crime <- test_pct_crime %>%
mutate(
off_cat_broad = case_when(
## ── Violent ──────────────────────────
str_detect(PD_DESC,
"ASSAULT|AGGRAVATED|ROBBERY|KIDNAPPING|MENACING|STRANGULATION|
RECKLESS ENDANGERMENT|COERCION|IMPRISONMENT|HARASSMENT|
OBSTR BREATH|TERRORISTIC|RIOT|HOMICIDE|RAPE|SEX|
TERRORISM|TORTURE") ~ "Violent",
## ── Sex crimes (kept in Other) ──────
str_detect(PD_DESC,
"SODOMY|INCEST|
LEWD|SEX TRAFFICKING") ~ "Other",
## ── Weapons ───────────────────────────────────────
str_detect(PD_DESC,
"WEAPON|FIREARM|PISTOL|RIFLE|KNIFE|
CRIMINAL POSSESSION WEAPON|
UNLAWFUL POSS. WEAPON|WEAP|
WEAPONS") ~ "Weapons",
## ── Drug ──────────────────────────────────────────
str_detect(PD_DESC,
"CONTROLLED SUBSTANCE|CANNABIS|MARIJUANA|
DRUG|PARAPHERNALIA|
SALE SCHOOL GROUNDS|
UNDER THE INFLUENCE") ~ "Drug",
## ── Property ──────────────────────────────────────
str_detect(PD_DESC,
"LARCENY|BURGLARS|BURGLARY|ARSON|MISCHIEF|STOLEN|
FORGERY|FRAUD|THEFT|TAMPERING|
GRAND|PETIT|JOSTLING|
UNAUTHORIZED USE VEHICLE|
THEFT OF SERVICES") ~ "Property",
## ── Trespass ──────────────────────────────────────
str_detect(PD_DESC, "TRESPASS") ~ "Trespass",
## ── Quality of Life / Disorder ────────────────────
str_detect(PD_DESC,
"ANARCHY|ASSEMBLY|CHILD|DISORDERLY|LOITERING|ALCOHOLIC|
EDUCATION|FIREWORKS|INTOXICATED|IMPAIRED DRIVING|
NOISE|GAMBLING|GRAFFITI|PEDDLING|NUISANCE|
ADM.CODE|HEALTH CODE|LOITERING|OBSCENITY|
PUBLIC ADMINISTRATION|
TRAFFIC|PARKR&R|PROSTITUTION|
FIREWORKS|GYPSY CAB|
SMOKING") ~ "QualityOfLife",
TRUE ~ "Other"
),
off_cat_broad = factor(
off_cat_broad,
levels = c(
"Murder", "Violent", "Weapons",
"Property", "Drug", "Trespass",
"QualityOfLife", "Other"
)
)
)
# offense category frequency table
test_pct_crime %>%
count(off_cat_broad) %>%
mutate(
percent = n / sum(n) * 100
) %>%
arrange(desc(n))
#' list offenses being categorized as "Other"
test_pct_crime %>%
filter(off_cat_broad == "Other") %>%
distinct(PD_DESC) %>%
arrange(PD_DESC) %>%
print(n = Inf)
#' summarize crime data for each `pct`, `year`, and `month.`
#' calculate the total number of `felony`, `misdemeanor`, and `violation` offenses,
#' as well as the count of each broad offense category for each month.
test_pct_crime_month <- test_pct_crime %>%
# Group the data by precinct, year, and month.
group_by(pct, year, month) %>%
# calculate the total number of felony, misdemeanor, and violation offenses.
summarize(
felony = sum(felony, na.rm = TRUE),
misdemeanor = sum(misdemeanor, na.rm = TRUE),
violation = sum(violation, na.rm = TRUE),
# calculate the count of each broad offense category.
crime_Violent = sum(off_cat_broad == "Violent", na.rm = TRUE),
crime_Weapons = sum(off_cat_broad == "Weapons", na.rm = TRUE),
crime_Property = sum(off_cat_broad == "Property", na.rm = TRUE),
crime_Drug = sum(off_cat_broad == "Drug", na.rm = TRUE),
crime_Trespass = sum(off_cat_broad == "Trespass", na.rm = TRUE),
crime_QualityOfLife = sum(off_cat_broad == "QualityOfLife", na.rm = TRUE),
crime_Other = sum(off_cat_broad == "Other", na.rm = TRUE)
) %>%
# remove the grouping variable
ungroup()
test_pct_crime_month <- test_pct_crime_month %>%
filter(year >= 2009 & year <= 2024,
pct >= 1 & pct <= 123)
# ----- finish prep for OLS models ----
#' divide yearly demographic data into monthly data
#' by uncounting each row 12 times and creating a `month` variable
if (!exists("pct_demo_expanded")) {
pct_demo_month <- pct_demo %>%
uncount(12) %>%
group_by(pct, year) %>%
mutate(month = row_number()) %>%
ungroup()
pct_demo_expanded <- TRUE
}
#' join total pop from pct_demo for density calculations
test_pct_crime_month <- test_pct_crime_month %>%
mutate(year = as.integer(year),
month = as.integer(month))
test_pct_crime_month <- test_pct_crime_month %>%
left_join(pct_demo_month, by = c("pct", "year", "month"))
# join unemployment data
test_pct_crime_month <- test_pct_crime_month %>%
left_join(nyc_unemp_month, by = c("year", "month", "BoroName")) %>%
rename(unemp_rate = unemployment_rate)
#
test_pct_crime_month <- test_pct_crime_month %>%
mutate(year_month = format(as.Date(paste0(year, "-", month, "-01")), "%Y-%m")) %>%
mutate(nonviolent_crime = crime_Weapons + crime_Property + crime_Drug +
crime_Trespass + crime_QualityOfLife + crime_Other) %>%
mutate(violent_rate = log((crime_Violent + 1) / total_pop * 1000),
nonviolent_rate = log((nonviolent_crime + 1) / total_pop * 1000)) %>%
mutate(date = as.Date(paste0(year_month, "-01")))
# shootings
shootings_historic <- read_csv("data/nypd-crime/NYPD_Shootings_Data__Historic.csv")
#' lubridate dates,
#' if `CMPLNT_FR_DT` is more than 3 days before `CMPLNT_TO_DT`,
#' then use `CMPLNT_TO_DT` as the year/month, otherwise use `CMPLNT_FR_DT`
shootings_historic <- shootings_historic %>%
mutate(
OCCUR_DATE = mdy(OCCUR_DATE),
year = as.integer(year(OCCUR_DATE)), # lube year from date
month = as.integer(month(OCCUR_DATE)) # lube month from date
)
#' create `pct` and `boro` variables
shootings_historic <- shootings_historic %>%
mutate(pct = as.numeric(PRECINCT),
BoroName = BORO
)
shootings_year_month <- shootings_historic %>%
group_by(pct, year, month) %>%
summarize(shootings = n()) %>%
ungroup()
test_pct_crime_month <- test_pct_crime_month %>%
left_join(shootings_year_month, by = c("pct", "year", "month"))
#' add precinct area (derived from sf geometries) and build the final monthly panel.
#'
#' Note: Areas are computed once per precinct; we join those scalar values into
#' the monthly panel to compute population density.
#' join with precinct geometry from nypd_sf - but don't convert to sf yet
test_pct_crime_month <- test_pct_crime_month %>%
left_join(st_drop_geometry(nypd_sf) %>% select(Precinct), by = c("pct" = "Precinct"))
# calculate area for each precinct separately (no sf joins)
pct_areas <- nypd_sf %>%
filter(st_is_valid(geometry)) %>% # Filter out invalid geometries
mutate(
area_sq_meters = as.numeric(st_area(geometry)),
area_sq_miles = area_sq_meters * 3.861e-7
) %>%
select(Precinct, area_sq_meters, area_sq_miles) %>%
st_drop_geometry()
cat("pct_month is sf:", inherits(pct_month, "sf"), "\n")
cat("test_pct_crime_month is sf:", inherits(test_pct_crime_month, "sf"), "\n")
cat("pct_demo is sf:", inherits(pct_demo, "sf"), "\n")
# join with stops data
test_pct_month_full <- st_drop_geometry(pct_month) %>%
left_join(test_pct_crime_month, by = c("pct", "year_month")) %>%
mutate(
pct_black = black_pop / total_pop,
pct_white = white_pop / total_pop,
pct_hisp = hisp_pop / total_pop,
pct_18_24 = age_18_24_pop / total_pop,
log_stops_rate = log((stops + 1) / total_pop * 1000),
lag_unemp = dplyr::lag(unemp_rate, 1)
) %>%
filter(!is.na(violent_rate), !is.na(pct_black)) %>%
left_join(pct_areas, by = c("pct" = "Precinct")) %>%
mutate(pop_density = total_pop / area_sq_miles)
test_pct_crime_month <- test_pct_crime_month %>%
filter(total_pop > 0) %>%
mutate(shooting_rate = log((shootings + 1) / total_pop * 1000))
summary(test_pct_crime_month$shooting_rate)
# Sort by precinct and date for proper lagging
test_pct_month_full <- test_pct_month_full %>%
mutate(date = date.x,
month = month.x,
year = year.x) %>%
select(-date.x, -date.y, -month.x, -month.y, -year.x, -year.y) %>%
arrange(pct, date)
# Create lagged variables (1-month lags, within precinct)
# Create one-month lagged crime rates by precinct
test_pct_month_full <- test_pct_month_full %>%
group_by(pct) %>%
arrange(date) %>%
mutate(
# One-month lagged crime rates
lag_violent_rate = dplyr::lag(violent_rate, 1),
lag_nonviolent_rate = dplyr::lag(nonviolent_rate, 1),
# Also create lagged stops to capture persistence in police allocation
lag_stops = dplyr::lag(stops, 1),
lag_log_stops = dplyr::lag(log_stops, 1)
) %>%
ungroup()
test_pct_month_full_lagged <- test_pct_month_full %>%
# Remove first month for each precinct due to missing lag
filter(!is.na(lag_violent_rate))
# Summary statistics
cat("Monthly observations with lagged variables:", nrow(test_pct_month_full_lagged), "\n")
cat("Number of precincts:", length(unique(test_pct_month_full_lagged$pct)), "\n")
cat("Date range:", min(test_pct_month_full_lagged$year_month), "to", max(test_pct_month_full_lagged$year_month), "\n")
saveRDS(test_pct_month_full_lagged, file = "data/test_pct_month_full_lagged.rds")
race_monthly_counts <- sqf_ols %>%
group_by(year, month, pct) %>%
summarize(
stops_black = sum(race == "BLACK", na.rm = TRUE),
stops_hisp = sum(race == "HISPANIC", na.rm = TRUE),
stops_white = sum(race == "WHITE", na.rm = TRUE)
) %>%
ungroup()
# Create logged versions of these counts (log(count + 1) to avoid log(0))
race_monthly_counts <- race_monthly_counts %>%
mutate(
log_stops_black = log(stops_black + 1),
log_stops_hisp = log(stops_hisp + 1),
log_stops_white = log(stops_white + 1)
)
test_pct_month_full_lagged <- test_pct_month_full_lagged %>%
left_join(race_monthly_counts, by = c("year", "month", "pct"))