library(readxl)
# Read data from Excel
options(repos = c(CRAN = "http://cran.rstudio.com"))
install.packages("janitor")
## Installing package into 'C:/Users/ediso/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'janitor' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\ediso\AppData\Local\Temp\Rtmp4Ca4uR\downloaded_packages
#CENSUS <- read_excel("C:/Users/ediso/Downloads/M.S/CENSUs.xlsx", sheet = "Sheet1")
ONYC <- read_excel("C:/Users/ediso/Downloads/M.S/ONYC2.xlsx")
NYC <-read_excel("C:/Users/ediso/Downloads/M.S/NYC3.xlsx")
## New names:
## • `Known_Days_in_Custody` -> `Known_Days_in_Custody...2`
## • `Top_Severity_at_Arraign` -> `Top_Severity_at_Arraign...3`
## • `Top_Severity_at_Arraign` -> `Top_Severity_at_Arraign...24`
## • `Known_Days_in_Custody` -> `Known_Days_in_Custody...83`
colnames(ONYC)[23] <- "Arraign_Charge_Category"
colnames(NYC)[3] <- "Top_Severity_at_Arraign"
colnames(NYC)[83] <- "Known_Days_in_Custody"
colnames(NYC)[37] <- "Release_Decision_at_Arraign"
colnames(NYC)[14] <- "Offense_Year"
colnames(ONYC)[12] <- "Offense_Year"
colnames(NYC)[25] <- "Arraign_Charge_Category"
colnames(NYC)[37] <- "Release_Decision_at_Arraign"
install.packages("janitor")
## Installing package into 'C:/Users/ediso/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'janitor' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\ediso\AppData\Local\Temp\Rtmp4Ca4uR\downloaded_packages
# ONYC Unknownn Race PERCENTAGE
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(janitor)
## Warning: package 'janitor' was built under R version 4.3.3
##
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
percent_unknown_race <- ONYC %>%
summarise(Percent = sum(Race == "Unknown") / n() * 100) %>%
pull(Percent)
print(paste("ONYC Percentage of Race as 'Unknown':", percent_unknown_race, "%"))
## [1] "ONYC Percentage of Race as 'Unknown': 34.9874596098749 %"
#NYC Unknown Race PERCENTAGE
library(dplyr)
percent_unknown_race <- NYC %>%
summarise(Percent = sum(Race == "Unknown") / n() * 100) %>%
pull(Percent)
print(paste("NYC Percentage of Race as 'Unknown':", percent_unknown_race, "%"))
## [1] "NYC Percentage of Race as 'Unknown': 3.97729523675372 %"
filtered_data <- NYC %>%
filter(Offense_Year >= 2022, Top_Severity_at_Arraign == "Misdemeanor", Known_Days_in_Custody< 500, Electronic_Monitoring == "N")
filtered_data2 <- ONYC %>%
filter(Offense_Year >= 2022, Top_Severity_at_Arraign == "Misdemeanor", Known_Days_in_Custody< 500,
Electronic_Monitoring == "N")
nrow(filtered_data2)
## [1] 113534
nrow(filtered_data)
## [1] 148085
filtered_data3 <- NYC %>%
filter(Offense_Year >= 2022, Top_Severity_at_Arraign == "Misdemeanor", (Known_Days_in_Custody > 500 ))
filtered_data4 <- ONYC %>%
filter(Offense_Year >= 2022, Top_Severity_at_Arraign == "Misdemeanor", (Known_Days_in_Custody > 500))
print(paste("NYC Cases KDC removed :", nrow(filtered_data3)))
## [1] "NYC Cases KDC removed : 11"
print(paste("ONYC Cases KDC removed :", nrow(filtered_data4)))
## [1] "ONYC Cases KDC removed : 16"
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
# Assuming filtered_data is your dataframe and 'Arraign Charge Category' is the column of interest
# First, create a summary of the counts
category_counts <- table(filtered_data$Arraign_Charge_Category)
# Convert to a dataframe for ggplot
df <- as.data.frame(category_counts)
# Name the columns appropriately
names(df) <- c("Category", "Count")
# Calculate the percentages
df$Percentage <- df$Count / sum(df$Count) * 100
# Create a new label combining the category and its percentage
df$Label <- paste(df$Category, sprintf("(%.1f%%)", df$Percentage))
# Define a custom color palette
num_categories <- length(unique(df$Category))
start_colors <- c("#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02", "#a6761d", "#666666")
custom_palette <- colorRampPalette(start_colors)(num_categories)
# Generate the pie chart using the new labels for the legend and custom colors
ggplot(df, aes(x = "", y = Count, fill = Label)) +
geom_bar(width = 1, stat = "identity", colour = "white") +
coord_polar("y", start = 0) +
theme_void() +
scale_fill_manual(values = custom_palette, name = "Charge Category") +
theme(legend.title = element_blank()) +
labs(title = "Distribution of Arraign Charge Categories")

library(ggplot2)
# Assuming filtered_data is your dataframe and 'Arraign Charge Category' is the column of interest
# First, create a summary of the counts
category_counts <- table(filtered_data$Arraign_Charge_Category)
# Convert to a dataframe for ggplot
df <- as.data.frame(category_counts)
# Name the columns appropriately
names(df) <- c("Category", "Count")
# Calculate the percentages
df$Percentage <- df$Count / sum(df$Count) * 100
# Create a new label combining the category and its percentage with two decimal places
df$Label <- paste(df$Category, sprintf("(%.2f%%)", df$Percentage))
# Define a custom color palette
num_categories <- length(unique(df$Category))
start_colors <- c("#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02", "#a6761d", "#666666")
custom_palette <- colorRampPalette(start_colors)(num_categories)
# Generate the pie chart using the new labels for the legend and custom colors
ggplot(df, aes(x = "", y = Count, fill = Label)) +
geom_bar(width = 1, stat = "identity", colour = "white") +
coord_polar("y", start = 0) +
theme_void() +
scale_fill_manual(values = custom_palette, name = "Charge Category") +
theme(legend.title = element_blank()) +
labs(title = "NYC Misdemeanors - Distribution of Arraign Charge Categories.
148085 Cases Included")

# Creating dummy variables
Black_Race <- ifelse(filtered_data$Race == "Black", 1, 0)
Asian <- ifelse(filtered_data$Race == "Asian", 1, 0)
Hispanic <- ifelse(filtered_data$Race == "White" & filtered_data$Ethnicity == "Hispanic", 1, 0)
Misdemeanor <- ifelse(filtered_data$Top_Severity_at_Arraign == "Misdemeanor", 1, 0)
DAT <- ifelse(filtered_data$Arrest_Type == "DAT", 1, 0)
Felony <- ifelse(filtered_data$Top_Severity_at_Arraign == "Felony", 1, 0)
Days_in_Custody <- ifelse(filtered_data$Known_Days_in_Custody > 3, filtered_data$Known_Days_in_Custody, 0)
GenderFemale <- ifelse(filtered_data$Gender == "Female", 1, 0)
RetainedAttorn <- ifelse(filtered_data$Representation_Type == "Retained Attorney", 1, 0)
pend_mis <- ifelse(filtered_data$pend_misd >= 1, 1, 0)
pend_nonvfo <- ifelse(filtered_data$pend_nonvfo >= 1, 1, 0)
pend_vfo <- ifelse(filtered_data$pend_vfo >= 1, 1, 0)
prior_misd <- ifelse(filtered_data$prior_misd_cnt >= 1, 1, 0)
prior_nonvfo <- ifelse(filtered_data$prior_nonvfo_cnt >= 1, 1, 0)
prior_vfo <- ifelse(filtered_data$prior_vfo_cnt >= 1, 1, 0)
pled_guilty <- ifelse(filtered_data$Disposition_Detail == "Pled Guilty", 1, 0)
family_offense <- ifelse(filtered_data$Order_of_Protection == "Family Offense", 1, 0)
Age_at_Arrest <- filtered_data$Age_at_Arrest
Superior_Court <- ifelse(filtered_data$Court_Type == "Superior", 1, 0)
Rearrest <- ifelse(filtered_data$rearrest != "No Arrest", 1, 0)
BSNP <- ifelse(filtered_data$Bail_Set_and_Not_Posted_at_Arraign == "Y", 1, 0)
NMR <- ifelse(filtered_data$NMR_at_Arraign == "Y",1,0)
ROR <- ifelse(filtered_data$ROR_at_Arraign == "Y",1,0)
Disposed = ifelse(filtered_data$Release_Decision_at_Arraign == "Disposed at arraign",1,0)
UnknownRelease = ifelse(filtered_data$Release_Decision_at_Arraign == "Unknown",1,0)
Remanded <- ifelse(filtered_data$Remanded_to_Jail_at_Arraign == "Y",1,0)
ElectronicMon <- ifelse(filtered_data$Electronic_Monitoring == "Y", 1, 0)
RepAtArraign <- ifelse(filtered_data$Representation_at_Securing_Order == "Y", 1, 0)
NMR <- ifelse(filtered_data$NMR_at_Arraign == "Y", 1, 0)
NewYork <- ifelse(filtered_data$County_Name == "New York", 1, 0)
Richmond <- ifelse(filtered_data$County_Name == "Richmond", 1, 0) # Richmond County is Staten Island
Kings <- ifelse(filtered_data$County_Name == "Kings", 1, 0) # Kings County is Brooklyn
Queens <- ifelse(filtered_data$County_Name == "Queens", 1, 0)
Bronx <- ifelse(filtered_data$County_Name == "Bronx", 1, 0)
Bail_Set <- ifelse(filtered_data$`Release Decision at Arraign` == "Bail-set",1,0)
## Warning: Unknown or uninitialised column: `Release Decision at Arraign`.
#Charge Category
Assault <- ifelse(filtered_data$Arraign_Charge_Category == "Assault",1,0)
AggAssault <- ifelse(filtered_data$Arraign_Charge_Category == "Aggravated Assault",1,0)
Burglary <- ifelse(filtered_data$Arraign_Charge_Category == "Burglary",1,0)
CrimContempt <- ifelse(filtered_data$Arraign_Charge_Category == "Criminal Contempt",1,0)
CriminalTrespass <- ifelse(filtered_data$Arraign_Charge_Category == "Criminal Trespass",1,0)
Drug <- ifelse(filtered_data$Arraign_Charge_Category == "Drug",1,0)
DWI <- ifelse(filtered_data$Arraign_Charge_Category == "DWI",1,0)
EndagWelfare <- ifelse(filtered_data$Arraign_Charge_Category == "Endagering Welfare",1,0)
Larceny <- ifelse(filtered_data$Arraign_Charge_Category == "Larceny",1,0)
Obstruct <- ifelse(filtered_data$Arraign_Charge_Category == "Obstruction",1,0)
Other <- ifelse(filtered_data$Arraign_Charge_Category == "Other",1,0)
OtherSex <- ifelse(filtered_data$Arraign_Charge_Category == "Other Sex Offense",1,0)
OtherVTL <- ifelse(filtered_data$Arraign_Charge_Category == "Other VTL",1,0)
Property <- ifelse(filtered_data$Arraign_Charge_Category == "Property",1,0)
Rape <- ifelse(filtered_data$Arraign_Charge_Category == "Rape",1,0)
Strangu <- ifelse(filtered_data$Arraign_Charge_Category == "Strangulation",1,0)
UnlicensedOp <- ifelse(filtered_data$Arraign_Charge_Category == "Unlicensed Operation",1,0)
#rm(NYC_dummy_vars)
NYC_dummy_vars <- data.frame(
Days_in_Custody,
Age_at_Arrest,
Black_Race,
Hispanic,
BSNP,
# Bail_Set,
NMR, ROR,
Remanded,
#UnknownRelease,
Disposed,
pend_mis,
pend_nonvfo,
# pend_vfo,
# prior_misd,
# prior_nonvfo,prior_vfo,
DAT,
GenderFemale,
# RetainedAttorn,
pled_guilty, family_offense,
Superior_Court,
Rearrest,
RepAtArraign,
NewYork,
Richmond,
Kings,
Queens,
Bronx,
Assault,AggAssault, Burglary, CrimContempt, CriminalTrespass, Drug,DWI,
EndagWelfare, Larceny, Obstruct, Other, OtherSex, OtherVTL, Property, Rape, Strangu, UnlicensedOp)
# View the new data frame
NYC_dummy_vars <- NYC_dummy_vars %>%
mutate_if(is.numeric, ~ ifelse(is.na(.), mean(., na.rm = TRUE), .))
reg <- lm(Days_in_Custody ~ ., data = NYC_dummy_vars, na.action = na.exclude)
# hrm(stepwise)
# rm(reg)
library(broom)
library(dplyr)
# Assuming 'reg' is your initial regression model
# Stepwise model selection
stepwise <- step(reg, direction = "both") # 'both' allows for both forward and backward stepwise regression
## Start: AIC=898387.4
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + ROR + Remanded + Disposed + pend_mis + pend_nonvfo +
## DAT + GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + NewYork + Richmond + Kings + Queens +
## Bronx + Assault + AggAssault + Burglary + CrimContempt +
## CriminalTrespass + Drug + DWI + EndagWelfare + Larceny +
## Obstruct + Other + OtherSex + OtherVTL + Property + Rape +
## Strangu + UnlicensedOp
##
##
## Step: AIC=898387.4
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + ROR + Remanded + Disposed + pend_mis + pend_nonvfo +
## DAT + GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + NewYork + Richmond + Kings + Queens +
## Bronx + Assault + AggAssault + Burglary + CrimContempt +
## CriminalTrespass + Drug + DWI + Larceny + Obstruct + Other +
## OtherSex + OtherVTL + Property + Rape + Strangu + UnlicensedOp
##
##
## Step: AIC=898387.4
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + ROR + Remanded + Disposed + pend_mis + pend_nonvfo +
## DAT + GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + NewYork + Richmond + Kings + Queens +
## Bronx + Assault + Burglary + CrimContempt + CriminalTrespass +
## Drug + DWI + Larceny + Obstruct + Other + OtherSex + OtherVTL +
## Property + Rape + Strangu + UnlicensedOp
##
##
## Step: AIC=898387.4
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + ROR + Remanded + Disposed + pend_mis + pend_nonvfo +
## DAT + GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + NewYork + Richmond + Kings + Queens +
## Assault + Burglary + CrimContempt + CriminalTrespass + Drug +
## DWI + Larceny + Obstruct + Other + OtherSex + OtherVTL +
## Property + Rape + Strangu + UnlicensedOp
##
## Df Sum of Sq RSS AIC
## - Assault 1 59 63830655 898386
## - NewYork 1 83 63830679 898386
## - DAT 1 177 63830772 898386
## - ROR 1 579 63831175 898387
## - Strangu 1 666 63831262 898387
## - OtherVTL 1 792 63831388 898387
## <none> 63830596 898387
## - Rape 1 951 63831547 898388
## - DWI 1 1281 63831876 898388
## - Property 1 2010 63832605 898390
## - Other 1 2172 63832768 898390
## - Hispanic 1 2307 63832902 898391
## - pend_mis 1 2417 63833013 898391
## - Age_at_Arrest 1 3140 63833735 898393
## - Burglary 1 4098 63834694 898395
## - Superior_Court 1 4208 63834804 898395
## - Disposed 1 4913 63835508 898397
## - CriminalTrespass 1 5106 63835702 898397
## - Obstruct 1 5952 63836548 898399
## - GenderFemale 1 7148 63837743 898402
## - Richmond 1 7657 63838253 898403
## - Queens 1 7895 63838490 898404
## - Black_Race 1 8107 63838703 898404
## - Drug 1 9034 63839630 898406
## - pend_nonvfo 1 16147 63846743 898423
## - Kings 1 17476 63848071 898426
## - CrimContempt 1 17803 63848398 898427
## - UnlicensedOp 1 23897 63854493 898441
## - pled_guilty 1 25661 63856257 898445
## - Larceny 1 41665 63872261 898482
## - OtherSex 1 49242 63879837 898500
## - family_offense 1 51683 63882278 898505
## - RepAtArraign 1 128049 63958645 898682
## - NMR 1 137405 63968001 898704
## - Remanded 1 178815 64009410 898800
## - Rearrest 1 1835972 65666568 902585
## - BSNP 1 11470940 75301536 922859
##
## Step: AIC=898385.6
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + ROR + Remanded + Disposed + pend_mis + pend_nonvfo +
## DAT + GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + NewYork + Richmond + Kings + Queens +
## Burglary + CrimContempt + CriminalTrespass + Drug + DWI +
## Larceny + Obstruct + Other + OtherSex + OtherVTL + Property +
## Rape + Strangu + UnlicensedOp
##
## Df Sum of Sq RSS AIC
## - NewYork 1 84 63830739 898384
## - DAT 1 187 63830842 898384
## - ROR 1 577 63831232 898385
## <none> 63830655 898386
## - Strangu 1 921 63831576 898386
## - OtherVTL 1 948 63831602 898386
## - Rape 1 968 63831623 898386
## - DWI 1 1393 63832048 898387
## + Assault 1 59 63830596 898387
## - Hispanic 1 2318 63832972 898389
## - pend_mis 1 2406 63833060 898389
## - Other 1 2962 63833617 898390
## - Age_at_Arrest 1 3120 63833774 898391
## - Property 1 3255 63833910 898391
## - Superior_Court 1 4252 63834907 898393
## - Burglary 1 4311 63834966 898394
## - Disposed 1 4988 63835642 898395
## - CriminalTrespass 1 5291 63835945 898396
## - Obstruct 1 6625 63837280 898399
## - GenderFemale 1 7114 63837769 898400
## - Richmond 1 7673 63838327 898401
## - Queens 1 7930 63838585 898402
## - Black_Race 1 8110 63838765 898402
## - Drug 1 14194 63844849 898417
## - pend_nonvfo 1 16118 63846773 898421
## - Kings 1 17542 63848197 898424
## - pled_guilty 1 25730 63856385 898443
## - CrimContempt 1 28850 63859505 898450
## - UnlicensedOp 1 41770 63872424 898480
## - family_offense 1 52345 63882999 898505
## - OtherSex 1 54672 63885327 898510
## - Larceny 1 85689 63916343 898582
## - RepAtArraign 1 128009 63958664 898680
## - NMR 1 137350 63968005 898702
## - Remanded 1 178817 64009472 898798
## - Rearrest 1 1835924 65666578 902583
## - BSNP 1 11471478 75302133 922858
##
## Step: AIC=898383.8
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + ROR + Remanded + Disposed + pend_mis + pend_nonvfo +
## DAT + GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + Richmond + Kings + Queens + Burglary +
## CrimContempt + CriminalTrespass + Drug + DWI + Larceny +
## Obstruct + Other + OtherSex + OtherVTL + Property + Rape +
## Strangu + UnlicensedOp
##
## Df Sum of Sq RSS AIC
## - DAT 1 183 63830921 898382
## - ROR 1 529 63831268 898383
## <none> 63830739 898384
## - Strangu 1 912 63831651 898384
## - OtherVTL 1 945 63831684 898384
## - Rape 1 966 63831704 898384
## - DWI 1 1399 63832138 898385
## + NewYork 1 84 63830655 898386
## + Bronx 1 84 63830655 898386
## + Assault 1 60 63830679 898386
## - Hispanic 1 2411 63833150 898387
## - pend_mis 1 2412 63833151 898387
## - Other 1 2932 63833671 898389
## - Age_at_Arrest 1 3114 63833852 898389
## - Property 1 3230 63833969 898389
## - Superior_Court 1 4273 63835012 898392
## - Burglary 1 4289 63835028 898392
## - Disposed 1 4918 63835656 898393
## - CriminalTrespass 1 5278 63836017 898394
## - Obstruct 1 6631 63837370 898397
## - GenderFemale 1 7101 63837840 898398
## - Richmond 1 8060 63838798 898400
## - Black_Race 1 8289 63839028 898401
## - Queens 1 10476 63841215 898406
## - Drug 1 14235 63844973 898415
## - pend_nonvfo 1 16121 63846860 898419
## - Kings 1 24299 63855038 898438
## - pled_guilty 1 25764 63856503 898442
## - CrimContempt 1 28815 63859554 898449
## - UnlicensedOp 1 41700 63872439 898478
## - family_offense 1 52613 63883351 898504
## - OtherSex 1 54637 63885375 898508
## - Larceny 1 85836 63916575 898581
## - RepAtArraign 1 128478 63959216 898680
## - NMR 1 138239 63968978 898702
## - Remanded 1 178917 64009655 898796
## - Rearrest 1 1836569 65667308 902582
## - BSNP 1 11516349 75347087 922945
##
## Step: AIC=898382.2
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + ROR + Remanded + Disposed + pend_mis + pend_nonvfo +
## GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + Richmond + Kings + Queens + Burglary +
## CrimContempt + CriminalTrespass + Drug + DWI + Larceny +
## Obstruct + Other + OtherSex + OtherVTL + Property + Rape +
## Strangu + UnlicensedOp
##
## Df Sum of Sq RSS AIC
## - ROR 1 484 63831406 898381
## <none> 63830921 898382
## - Strangu 1 900 63831822 898382
## - Rape 1 965 63831886 898382
## - OtherVTL 1 979 63831900 898382
## - DWI 1 1440 63832361 898384
## + DAT 1 183 63830739 898384
## + NewYork 1 80 63830842 898384
## + Bronx 1 80 63830842 898384
## + Assault 1 70 63830852 898384
## - Hispanic 1 2447 63833369 898386
## - pend_mis 1 2577 63833498 898386
## - Other 1 2774 63833695 898387
## - Age_at_Arrest 1 3113 63834034 898387
## - Property 1 3134 63834055 898387
## - Superior_Court 1 4211 63835132 898390
## - Burglary 1 4250 63835172 898390
## - Disposed 1 5101 63836023 898392
## - CriminalTrespass 1 5265 63836187 898392
## - Obstruct 1 6630 63837551 898396
## - GenderFemale 1 7183 63838104 898397
## - Richmond 1 8073 63838995 898399
## - Black_Race 1 8444 63839366 898400
## - Queens 1 10437 63841358 898404
## - Drug 1 14124 63845045 898413
## - pend_nonvfo 1 16258 63847179 898418
## - Kings 1 24123 63855044 898436
## - pled_guilty 1 25584 63856505 898440
## - CrimContempt 1 28806 63859727 898447
## - UnlicensedOp 1 48067 63878988 898492
## - family_offense 1 53065 63883986 898503
## - OtherSex 1 54890 63885812 898507
## - Larceny 1 85769 63916691 898579
## - RepAtArraign 1 128298 63959219 898678
## - NMR 1 140713 63971634 898706
## - Remanded 1 179020 64009942 898795
## - Rearrest 1 1836685 65667607 902581
## - BSNP 1 11569326 75400247 923047
##
## Step: AIC=898381.3
## Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic + BSNP +
## NMR + Remanded + Disposed + pend_mis + pend_nonvfo + GenderFemale +
## pled_guilty + family_offense + Superior_Court + Rearrest +
## RepAtArraign + Richmond + Kings + Queens + Burglary + CrimContempt +
## CriminalTrespass + Drug + DWI + Larceny + Obstruct + Other +
## OtherSex + OtherVTL + Property + Rape + Strangu + UnlicensedOp
##
## Df Sum of Sq RSS AIC
## <none> 63831406 898381
## - Strangu 1 902 63832308 898381
## - Rape 1 964 63832370 898382
## - OtherVTL 1 977 63832383 898382
## + ROR 1 484 63830921 898382
## - DWI 1 1445 63832851 898383
## + DAT 1 138 63831268 898383
## + Assault 1 66 63831340 898383
## + NewYork 1 35 63831371 898383
## + Bronx 1 35 63831371 898383
## - Hispanic 1 2407 63833812 898385
## - pend_mis 1 2604 63834010 898385
## - Other 1 2867 63834273 898386
## - Age_at_Arrest 1 3086 63834492 898386
## - Property 1 3141 63834546 898387
## - Superior_Court 1 4130 63835536 898389
## - Burglary 1 4278 63835684 898389
## - CriminalTrespass 1 5272 63836678 898392
## - Obstruct 1 6589 63837995 898395
## - Disposed 1 6680 63838086 898395
## - GenderFemale 1 7193 63838599 898396
## - Black_Race 1 8373 63839779 898399
## - Richmond 1 8541 63839946 898399
## - Queens 1 10340 63841745 898403
## - Drug 1 14068 63845473 898412
## - pend_nonvfo 1 16323 63847729 898417
## - Kings 1 24941 63856346 898437
## - pled_guilty 1 25960 63857366 898440
## - CrimContempt 1 28757 63860162 898446
## - UnlicensedOp 1 48427 63879832 898492
## - family_offense 1 53083 63884489 898502
## - OtherSex 1 55013 63886419 898507
## - Larceny 1 86119 63917525 898579
## - RepAtArraign 1 128672 63960077 898678
## - Remanded 1 182149 64013555 898801
## - NMR 1 395841 64227247 899295
## - Rearrest 1 1836636 65668042 902580
## - BSNP 1 19671468 83502874 938160
# Tidy summary of the stepwise regression model
tidy_stepwise <- tidy(stepwise) %>%
mutate(across(where(is.numeric), round, 3))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `across(where(is.numeric), round, 3)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
##
## # Previously
## across(a:b, mean, na.rm = TRUE)
##
## # Now
## across(a:b, \(x) mean(x, na.rm = TRUE))
# View the tidy summary with 3 decimal places
tidy_stepwise
## # A tibble: 33 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 8.3 0.49 16.9 0
## 2 Age_at_Arrest -0.012 0.005 -2.68 0.007
## 3 Black_Race 0.652 0.148 4.41 0
## 4 Hispanic 0.393 0.166 2.36 0.018
## 5 BSNP 59.8 0.28 214. 0
## 6 NMR 5.66 0.187 30.3 0
## 7 Remanded 49.1 2.39 20.6 0
## 8 Disposed -0.621 0.158 -3.94 0
## 9 pend_mis 0.369 0.15 2.46 0.014
## 10 pend_nonvfo 1.03 0.167 6.15 0
## # ℹ 23 more rows
summary(stepwise)
##
## Call:
## lm(formula = Days_in_Custody ~ Age_at_Arrest + Black_Race + Hispanic +
## BSNP + NMR + Remanded + Disposed + pend_mis + pend_nonvfo +
## GenderFemale + pled_guilty + family_offense + Superior_Court +
## Rearrest + RepAtArraign + Richmond + Kings + Queens + Burglary +
## CrimContempt + CriminalTrespass + Drug + DWI + Larceny +
## Obstruct + Other + OtherSex + OtherVTL + Property + Rape +
## Strangu + UnlicensedOp, data = NYC_dummy_vars, na.action = na.exclude)
##
## Residuals:
## Min 1Q Median 3Q Max
## -77.99 -3.41 -0.12 1.35 479.32
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.299534 0.490389 16.924 < 2e-16 ***
## Age_at_Arrest -0.012317 0.004604 -2.675 0.007465 **
## Black_Race 0.651623 0.147867 4.407 1.05e-05 ***
## Hispanic 0.392914 0.166305 2.363 0.018148 *
## BSNP 59.814778 0.280027 213.603 < 2e-16 ***
## NMR 5.665151 0.186965 30.301 < 2e-16 ***
## Remanded 49.053336 2.386519 20.554 < 2e-16 ***
## Disposed -0.621356 0.157858 -3.936 8.28e-05 ***
## pend_mis 0.369197 0.150229 2.458 0.013989 *
## pend_nonvfo 1.026133 0.166767 6.153 7.62e-10 ***
## GenderFemale -0.574533 0.140657 -4.085 4.42e-05 ***
## pled_guilty -1.131183 0.145776 -7.760 8.57e-15 ***
## family_offense -1.808475 0.162983 -11.096 < 2e-16 ***
## Superior_Court -2.227845 0.719800 -3.095 0.001968 **
## Rearrest 10.070830 0.154299 65.268 < 2e-16 ***
## RepAtArraign -7.163591 0.414667 -17.276 < 2e-16 ***
## Richmond -1.163361 0.261384 -4.451 8.56e-06 ***
## Kings -1.025690 0.134857 -7.606 2.85e-14 ***
## Queens -0.695439 0.142009 -4.897 9.73e-07 ***
## Burglary 1.659916 0.526973 3.150 0.001634 **
## CrimContempt -2.332697 0.285627 -8.167 3.19e-16 ***
## CriminalTrespass 2.185505 0.624980 3.497 0.000471 ***
## Drug 1.338636 0.234349 5.712 1.12e-08 ***
## DWI 0.630967 0.344598 1.831 0.067099 .
## Larceny 2.636435 0.186542 14.133 < 2e-16 ***
## Obstruct 1.748638 0.447308 3.909 9.26e-05 ***
## Other 0.610615 0.236783 2.579 0.009915 **
## OtherSex 6.563695 0.581067 11.296 < 2e-16 ***
## OtherVTL -1.305225 0.866855 -1.506 0.132146
## Property 0.526495 0.195076 2.699 0.006957 **
## Rape -9.365618 6.261759 -1.496 0.134738
## Strangu -0.816300 0.564316 -1.447 0.148031
## UnlicensedOp -2.482343 0.234223 -10.598 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 20.76 on 148052 degrees of freedom
## Multiple R-squared: 0.3101, Adjusted R-squared: 0.31
## F-statistic: 2080 on 32 and 148052 DF, p-value: < 2.2e-16
number_of_rows <- nrow(NYC_dummy_vars)
print(paste("Number of cases included in regression:", number_of_rows))
## [1] "Number of cases included in regression: 148085"
# Creating dummy variables
Misdemeanor <- ifelse(filtered_data2$Top_Severity_at_Arraign == "Misdemeanor", 1, 0)
DAT <- ifelse(filtered_data2$Arrest_Type == "DAT", 1, 0)
Felony <- ifelse(filtered_data2$Top_Severity_at_Arraign == "Felony", 1, 0)
Days_in_Custody <- ifelse(filtered_data2$Known_Days_in_Custody > 3, filtered_data2$Known_Days_in_Custody, 0)
GenderFemale <- ifelse(filtered_data2$Gender == "Female", 1, 0)
RetainedAttorn <- ifelse(filtered_data2$Representation_Type == "Retained Attorney", 1, 0)
pend_mis <- ifelse(filtered_data2$pend_misd >= 1, 1, 0)
pend_nonvfo <- ifelse(filtered_data2$pend_nonvfo >= 1, 1, 0)
pend_vfo <- ifelse(filtered_data2$pend_vfo >= 1, 1, 0)
prior_misd <- ifelse(filtered_data2$prior_misd_cnt >= 1, 1, 0)
prior_nonvfo <- ifelse(filtered_data2$prior_nonvfo_cnt >= 1, 1, 0)
prior_vfo <- ifelse(filtered_data2$prior_vfo_cnt >= 1, 1, 0)
pled_guilty <- ifelse(filtered_data2$Disposition_Detail == "Pled Guilty", 1, 0)
family_offense <- ifelse(filtered_data2$Order_of_Protection == "Family Offense", 1, 0)
Age_at_Arrest <- filtered_data2$Age_at_Arrest
Superior_Court <- ifelse(filtered_data2$Court_Type == "Superior", 1, 0)
Rearrest <- ifelse(filtered_data2$rearrest != "No Arrest", 1, 0)
BSNP <- ifelse(filtered_data2$Bail_Set_and_Not_Posted_at_Arraign == "Y", 1, 0)
BailSet <- ifelse(filtered_data2$`Release Decision at Arraign` == "Bail-set", 1, 0)
NMR <- ifelse(filtered_data2$NMR_at_Arraign == "Y", 1, 0)
ROR <- ifelse(filtered_data2$ROR_at_Arraign == "Y", 1, 0)
Remanded <- ifelse(filtered_data2$Remanded_to_Jail_at_Arraign == "Y", 1, 0)
ElectronicMon <- ifelse(filtered_data2$Electronic_Monitoring == "Y", 1, 0)
RepAtArraign <- ifelse(filtered_data2$Representation_at_Securing_Order == "Y", 1, 0)
# Create data frame with dummy variables
ONYC_dummy_vars <- data.frame(
Days_in_Custody, BSNP, NMR, ROR, Remanded,
# pend_mis, pend_nonvfo, pend_vfo, prior_misd, prior_nonvfo, prior_vfo,
DAT, pled_guilty, Rearrest,family_offense, RepAtArraign
)
# Handle NAs
ONYC_dummy_vars <- ONYC_dummy_vars %>%
mutate(across(everything(), ~ifelse(is.na(.), 0, .)))
# Linear regression
reg <- lm(Days_in_Custody ~ ., data = ONYC_dummy_vars, na.action = na.exclude)
# Stepwise regression
stepwise <- step(reg, direction = "both") # Both forward and backward stepwise regression
## Start: AIC=750426.8
## Days_in_Custody ~ BSNP + NMR + ROR + Remanded + DAT + pled_guilty +
## Rearrest + family_offense + RepAtArraign
##
## Df Sum of Sq RSS AIC
## <none> 84257787 750427
## - RepAtArraign 1 6699 84264487 750434
## - ROR 1 32097 84289884 750468
## - family_offense 1 218793 84476581 750719
## - NMR 1 334195 84591983 750874
## - DAT 1 406321 84664108 750971
## - Remanded 1 428378 84686166 751001
## - pled_guilty 1 483409 84741197 751074
## - Rearrest 1 972291 85230078 751727
## - BSNP 1 16776032 101033819 771040
# Load broom library for tidy summary
library(broom)
# Tidy summary of stepwise regression model
tidy_stepwise <- tidy(stepwise)
# Tidy summary of the stepwise regression model
tidy_stepwise <- tidy(stepwise) %>%
mutate(across(where(is.numeric), round, 3))
tidy_stepwise
## # A tibble: 10 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.35 0.343 9.77 0
## 2 BSNP 58.5 0.389 150. 0
## 3 NMR 8.33 0.393 21.2 0
## 4 ROR 1.67 0.254 6.58 0
## 5 Remanded 22.0 0.916 24.0 0
## 6 DAT -4.74 0.202 -23.4 0
## 7 pled_guilty -4.26 0.167 -25.5 0
## 8 Rearrest 6.38 0.176 36.2 0
## 9 family_offense -4.61 0.269 -17.2 0
## 10 RepAtArraign 0.582 0.194 3.00 0.003
summary(stepwise)
##
## Call:
## lm(formula = Days_in_Custody ~ BSNP + NMR + ROR + Remanded +
## DAT + pled_guilty + Rearrest + family_offense + RepAtArraign,
## data = ONYC_dummy_vars, na.action = na.exclude)
##
## Residuals:
## Min 1Q Median 3Q Max
## -86.19 -6.65 -2.39 -0.76 492.22
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.3467 0.3426 9.768 < 2e-16 ***
## BSNP 58.4966 0.3891 150.343 < 2e-16 ***
## NMR 8.3291 0.3925 21.220 < 2e-16 ***
## ROR 1.6699 0.2539 6.576 4.85e-11 ***
## Remanded 22.0058 0.9160 24.024 < 2e-16 ***
## DAT -4.7378 0.2025 -23.398 < 2e-16 ***
## pled_guilty -4.2598 0.1669 -25.521 < 2e-16 ***
## Rearrest 6.3748 0.1761 36.194 < 2e-16 ***
## family_offense -4.6118 0.2686 -17.169 < 2e-16 ***
## RepAtArraign 0.5821 0.1938 3.004 0.00266 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 27.24 on 113524 degrees of freedom
## Multiple R-squared: 0.251, Adjusted R-squared: 0.251
## F-statistic: 4228 on 9 and 113524 DF, p-value: < 2.2e-16
newyork_data <- NYC_dummy_vars %>%
filter(NewYork == 1) %>%
summarise(
Total_Cases = n(),
Avg_Days_in_custody = mean(Days_in_Custody, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody, na.rm = TRUE),
)
# Richmond Data
richmond_data <- NYC_dummy_vars %>%
filter(Richmond == 1) %>%
summarise(
Total_Cases = n(),
Avg_Days_in_custody = mean(Days_in_Custody, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody, na.rm = TRUE),
)
# Kings Data
kings_data <- NYC_dummy_vars %>%
filter(Kings == 1) %>%
summarise(
Total_Cases = n(),
Avg_Days_in_custody = mean(Days_in_Custody, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody, na.rm = TRUE),
)
# Queens Data
queens_data <- NYC_dummy_vars %>%
filter(Queens == 1) %>%
summarise(
Total_Cases = n(),
Avg_Days_in_custody = mean(Days_in_Custody, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody, na.rm = TRUE),
)
# Bronx Data
bronx_data <- NYC_dummy_vars %>%
filter(Bronx == 1) %>%
summarise(
Total_Cases = n(),
Avg_Days_in_custody = mean(Days_in_Custody, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody, na.rm = TRUE),
)
library(dplyr)
library(tidyr)
# Add the County column to each dataframe
bronx_data$County <- "Bronx"
kings_data$County <- "Kings"
newyork_data$County <- "NewYork"
queens_data$County <- "Queens"
richmond_data$County <- "Richmond"
# Combine the dataframes
combined_data <- bind_rows(bronx_data, kings_data, newyork_data, queens_data, richmond_data)
# Now pivot the data
final_data <- combined_data %>%
pivot_longer(cols = -County, names_to = "Variable", values_to = "Value") %>%
pivot_wider(names_from = County, values_from = Value)
# View the final data
final_data <- final_data %>%
mutate(across(where(is.numeric), ~ round(., 2)))
# Now, print final_data to see the formatted output
print(final_data)
## # A tibble: 5 × 6
## Variable Bronx Kings NewYork Queens Richmond
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Total_Cases 26816 42071 36193 35465 7540
## 2 Avg_Days_in_custody 5.07 4.63 6.88 4.43 4.24
## 3 Max_Days_in_Custody 497 482 493 436 406
## 4 Median_Days_in_Custody 0 0 0 0 0
## 5 Min_Days_in_Custody 0 0 0 0 0
# Create a new data frame from filtered_data
summary_dummy <- data.frame(
Black_Race = ifelse(filtered_data$Race == "Black", 1, 0),
Hispanic <- ifelse(filtered_data$Race == "White" & filtered_data$Ethnicity == "Hispanic", 1, 0),
GenderFemale = ifelse(filtered_data$Gender == "Female", 1, 0),
BSNP = ifelse(filtered_data$Bail_Set_and_Not_Posted_at_Arraign == "Y", 1, 0),
NMR = ifelse(filtered_data$NMR_at_Arraign == "Y", 1, 0),
ROR = ifelse(filtered_data$Release_Decision_at_Arraign == "ROR",1,0),
Disposed = ifelse(filtered_data$Release_Decision_at_Arraign == "Disposed at arraign",1,0),
UnknownRelease = ifelse(filtered_data$Release_Decision_at_Arraign == "Unknown",1,0),
Remanded = ifelse(filtered_data$Remanded_to_Jail_at_Arraign == "Y", 1, 0),
pend_mis = ifelse(filtered_data$pend_misd >= 1, 1, 0),
pend_nonvfo = ifelse(filtered_data$pend_nonvfo >= 1, 1, 0),
BailSet_ = ifelse(filtered_data$Release_Decision_at_Arraign == "Bail-set", 1, 0),
DAT = ifelse(filtered_data$Arrest_Type == "DAT", 1, 0),
family_offense = ifelse(filtered_data$Order_of_Protection == "Family Offense", 1, 0),
Superior_Court = ifelse(filtered_data$Court_Type == "Superior", 1, 0),
Rearrest = ifelse(filtered_data$rearrest != "No Arrest", 1, 0),
RepAtArraign = ifelse(filtered_data$Representation_at_Securing_Order == "Y", 1, 0),
County = filtered_data$County_Name
)
# Sort the dataframe by County
sorted_summary_dummy <- summary_dummy %>%
arrange(County)
library(dplyr)
library(tidyr)
# Calculate the mean (percentage) by County
percentage_by_county <- sorted_summary_dummy %>%
group_by(County) %>%
summarise(across(everything(), ~ mean(., na.rm = TRUE) * 100, .names = "percent_{.col}")) %>%
ungroup()
# Reshape the data for a summary table
summary_table <- percentage_by_county %>%
pivot_longer(cols = -County, names_to = "Variable", values_to = "Percentage") %>%
pivot_wider(names_from = County, values_from = Percentage)
# View the final data
summary_table <- summary_table %>%
mutate(across(where(is.numeric), ~ round(., 2)))
# Now, print final_data to see the formatted output
print(summary_table)
## # A tibble: 17 × 6
## Variable Bronx Kings `New York` Queens Richmond
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 percent_Black_Race 62.6 63.2 58.3 41.0 44.3
## 2 percent_Hispanic....ifelse.filtered_d… 30.7 18.9 24.3 32.0 21.0
## 3 percent_GenderFemale 20.5 18.3 18.6 18.8 20.2
## 4 percent_BSNP 3.77 4.22 5.78 4.22 3.5
## 5 percent_NMR 6.39 11.5 13.8 12.7 17.0
## 6 percent_ROR 63.0 65.8 48.4 57.5 56.2
## 7 percent_Disposed 26.5 18.0 31.5 25.3 22.8
## 8 percent_UnknownRelease 0.18 0.23 0.31 0.1 0.24
## 9 percent_Remanded 0.04 0.03 0.05 0.08 0.11
## 10 percent_pend_mis 24.5 21.7 28.4 18.2 24.1
## 11 percent_pend_nonvfo 15.4 17.3 17.5 14.3 18.7
## 12 percent_BailSet_ 3.89 4.36 5.91 4.34 3.63
## 13 percent_DAT 25.1 14.7 23.2 25.1 28.5
## 14 percent_family_offense 54.2 59.0 61.2 57.9 60.9
## 15 percent_Superior_Court 0.28 1.7 0.34 0.65 1.43
## 16 percent_Rearrest 19.6 21.1 19.3 18.1 22.7
## 17 percent_RepAtArraign 97 97.9 98.3 97.7 90.2
# Recreating the dummy variables and including County_Name
ONYC_dummy_vars_summary <- data.frame(
County_Name_ = filtered_data2$County_Name,
BSNP_ = ifelse(filtered_data2$Bail_Set_and_Not_Posted_at_Arraign == "Y", 1, 0),
NMR_ = ifelse(filtered_data2$NMR_at_Arraign == "Y", 1, 0),
ROR_ = ifelse(filtered_data2$`Release Decision at Arraign`== "ROR",1,0),
Remanded_ = ifelse(filtered_data2$Remanded_to_Jail_at_Arraign == "Y", 1, 0),
DAT_ = ifelse(filtered_data2$Arrest_Type == "DAT", 1, 0),
BailSet_ = ifelse(filtered_data2$`Release Decision at Arraign` == "Bail-set", 1, 0),
pled_guilty_ = ifelse(filtered_data2$Disposition_Detail == "Pled Guilty", 1, 0),
Rearrest_ = ifelse(filtered_data2$rearrest != "No Arrest", 1, 0),
family_offense_ = ifelse(filtered_data2$Order_of_Protection == "Family Offense", 1, 0),
RepAtArraign_ = ifelse(filtered_data2$Representation_at_Securing_Order == "Y", 1, 0),
Days_in_Custody_ = ifelse(filtered_data2$Known_Days_in_Custody > 3, filtered_data2$Known_Days_in_Custody, 0)
)
# Handling NA values by replacing them with 0 across all columns
ONYC_dummy_vars_summary <- ONYC_dummy_vars_summary %>%
mutate(across(everything(), ~ifelse(is.na(.), 0, .)))
# Group by County_Name and summarize the statistics
county_summary <- ONYC_dummy_vars_summary %>%
group_by(County_Name_) %>%
summarize(
Number_of_Cases = n(),
Mean_Days_in_Custody = mean(Days_in_Custody_, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody_, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody_, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody_, na.rm = TRUE),
Mean_BSNP = mean(BSNP_, na.rm = TRUE),
Mean_NMR = mean(NMR_, na.rm = TRUE),
Mean_ROR = mean(ROR_, na.rm = TRUE),
Mean_Remanded = mean(Remanded_, na.rm = TRUE),
Mean_DAT = mean(DAT_, na.rm = TRUE),
Mean_Pled_Guilty = mean(pled_guilty_, na.rm = TRUE),
Mean_Rearrest = mean(Rearrest_, na.rm = TRUE),
Mean_Family_Offense = mean(family_offense_, na.rm = TRUE),
Mean_RepAtArraign = mean(RepAtArraign_, na.rm = TRUE)
) %>%
arrange(County_Name_)
# Round the numeric columns to 3 decimal places
county_summary <- county_summary %>%
mutate(across(where(is.numeric), round, 3))
# View the summary table
print(county_summary)
## # A tibble: 52 × 15
## County_Name_ Number_of_Cases Mean_Days_in_Custody Median_Days_in_Custody
## <chr> <dbl> <dbl> <dbl>
## 1 Albany 3149 6.63 0
## 2 Allegany 33 22.9 0
## 3 Broome 1755 11.0 0
## 4 Cattaraugus 1067 16.6 0
## 5 Cayuga 1179 9.62 0
## 6 Chautauqua 3042 6.65 0
## 7 Chemung 1364 7.90 0
## 8 Chenango 417 10.5 0
## 9 Clinton 633 7.28 0
## 10 Columbia 347 2.28 0
## # ℹ 42 more rows
## # ℹ 11 more variables: Max_Days_in_Custody <dbl>, Min_Days_in_Custody <dbl>,
## # Mean_BSNP <dbl>, Mean_NMR <dbl>, Mean_ROR <dbl>, Mean_Remanded <dbl>,
## # Mean_DAT <dbl>, Mean_Pled_Guilty <dbl>, Mean_Rearrest <dbl>,
## # Mean_Family_Offense <dbl>, Mean_RepAtArraign <dbl>
# Selecting top 11 counties based on Mean_Days_in_Custody
top_11_counties <- county_summary %>%
arrange(desc(Mean_Days_in_Custody)) %>%
head(11)
# Print the top 11 counties with rounded values
print(top_11_counties)
## # A tibble: 11 × 15
## County_Name_ Number_of_Cases Mean_Days_in_Custody Median_Days_in_Custody
## <chr> <dbl> <dbl> <dbl>
## 1 Washington 2 64 64
## 2 Allegany 33 22.9 0
## 3 Dutchess 767 17.8 0
## 4 Cattaraugus 1067 16.6 0
## 5 Onondaga 4660 16.4 0
## 6 Tompkins 581 15.4 0
## 7 Genesee 613 13.9 0
## 8 Jefferson 1148 12.2 0
## 9 Westchester 7810 12.2 0
## 10 Cortland 910 12.0 0
## 11 Erie 6053 11.7 0
## # ℹ 11 more variables: Max_Days_in_Custody <dbl>, Min_Days_in_Custody <dbl>,
## # Mean_BSNP <dbl>, Mean_NMR <dbl>, Mean_ROR <dbl>, Mean_Remanded <dbl>,
## # Mean_DAT <dbl>, Mean_Pled_Guilty <dbl>, Mean_Rearrest <dbl>,
## # Mean_Family_Offense <dbl>, Mean_RepAtArraign <dbl>
# Get the number of rows in county_summary
number_of_rows <- nrow(county_summary)
print(number_of_rows)
## [1] 52
filtered_county_summary222 <- county_summary %>%
filter(County_Name_ == "Washington")
filtered_county_summary222
## # A tibble: 1 × 15
## County_Name_ Number_of_Cases Mean_Days_in_Custody Median_Days_in_Custody
## <chr> <dbl> <dbl> <dbl>
## 1 Washington 2 64 64
## # ℹ 11 more variables: Max_Days_in_Custody <dbl>, Min_Days_in_Custody <dbl>,
## # Mean_BSNP <dbl>, Mean_NMR <dbl>, Mean_ROR <dbl>, Mean_Remanded <dbl>,
## # Mean_DAT <dbl>, Mean_Pled_Guilty <dbl>, Mean_Rearrest <dbl>,
## # Mean_Family_Offense <dbl>, Mean_RepAtArraign <dbl>
library(tigris)
## Warning: package 'tigris' was built under R version 4.3.3
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(ggplot2)
library(dplyr)
MAP_measure <-
# Load spatial data for New York counties
ny_counties <- counties(state = "NY", class = "sf")
## Retrieving data for the year 2022
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========= | 14%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 16%
|
|============ | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 21%
|
|================ | 23%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================= | 34%
|
|======================== | 34%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|============================== | 44%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|===================================== | 54%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|========================================== | 61%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================ | 64%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|=================================================== | 74%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================= | 94%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|======================================================================| 100%
# Assuming county_summary is your main data frame
# Remove rows where County_Name is "Washington"
filtered_county_summary <- county_summary %>%
filter(County_Name_ != "Washington")
#filter(Median_Days_in_Custody >= 0)
# Assuming County_Name_ contains the names of the boroughs
nyc_boroughs <- c("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island")
# Filter for NYC boroughs
nyc_boroughs_summary <- ONYC_dummy_vars_summary %>%
filter(County_Name_ %in% nyc_boroughs) %>%
group_by(County_Name_) %>%
summarize(
Mean_Days_in_Custody = mean(Days_in_Custody_, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody_, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody_, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody_, na.rm = TRUE),
Mean_BSNP = mean(BSNP_, na.rm = TRUE),
Mean_NMR = mean(NMR_, na.rm = TRUE),
Mean_Remanded = mean(Remanded_, na.rm = TRUE),
Mean_DAT = mean(DAT_, na.rm = TRUE),
Mean_Pled_Guilty = mean(pled_guilty_, na.rm = TRUE),
Mean_Rearrest = mean(Rearrest_, na.rm = TRUE),
Mean_Family_Offense = mean(family_offense_, na.rm = TRUE),
Mean_RepAtArraign = mean(RepAtArraign_, na.rm = TRUE)
) %>%
arrange(County_Name_)
## Warning: There were 2 warnings in `summarize()`.
## The first warning was:
## ℹ In argument: `Max_Days_in_Custody = max(Days_in_Custody_, na.rm = TRUE)`.
## Caused by warning in `max()`:
## ! no non-missing arguments to max; returning -Inf
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
# View the NYC boroughs summary
print(nyc_boroughs_summary)
## # A tibble: 0 × 13
## # ℹ 13 variables: County_Name_ <chr>, Mean_Days_in_Custody <dbl>,
## # Median_Days_in_Custody <dbl>, Max_Days_in_Custody <dbl>,
## # Min_Days_in_Custody <dbl>, Mean_BSNP <dbl>, Mean_NMR <dbl>,
## # Mean_Remanded <dbl>, Mean_DAT <dbl>, Mean_Pled_Guilty <dbl>,
## # Mean_Rearrest <dbl>, Mean_Family_Offense <dbl>, Mean_RepAtArraign <dbl>
# Joining the datasets
merged_data <- ny_counties %>%
left_join(filtered_county_summary, by = c("NAME" = "County_Name_"))
# Create the plot
ggplot(data = merged_data) +
geom_sf(aes(fill = Mean_Days_in_Custody), color = "white") +
scale_fill_gradient(low = "lightgrey", high = "darkblue") +
theme_minimal() +
labs(fill = "Mean_Days_in_Custody",
title = "Mean_Days_in_Custody by County in New York")

install.packages("sf")
## Warning: package 'sf' is in use and will not be installed
install.packages("ggplot2")
## Warning: package 'ggplot2' is in use and will not be installed
install.packages("dplyr")
## Warning: package 'dplyr' is in use and will not be installed
install.packages("tigris")
## Warning: package 'tigris' is in use and will not be installed
library(tigris)
library(sf)
library(ggplot2)
library(dplyr)
MAP_measure <-
# Load spatial data for New York counties
ny_counties <- counties(state = "NY", class = "sf")
## Retrieving data for the year 2022
# Assuming county_summary is your main data frame
# Remove rows where County_Name is "Washington"
filtered_county_summary <- county_summary %>%
filter(County_Name_ != "Washington")
#filter(Median_Days_in_Custody >= 0)
# Assuming County_Name_ contains the names of the boroughs
nyc_boroughs <- c("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island")
# Filter for NYC boroughs
nyc_boroughs_summary <- ONYC_dummy_vars_summary %>%
filter(County_Name_ %in% nyc_boroughs) %>%
group_by(County_Name_) %>%
summarize(
Mean_Days_in_Custody = mean(Days_in_Custody_, na.rm = TRUE),
Median_Days_in_Custody = median(Days_in_Custody_, na.rm = TRUE),
Max_Days_in_Custody = max(Days_in_Custody_, na.rm = TRUE),
Min_Days_in_Custody = min(Days_in_Custody_, na.rm = TRUE),
Mean_BSNP = mean(BSNP_, na.rm = TRUE),
Mean_NMR = mean(NMR_, na.rm = TRUE),
Mean_Remanded = mean(Remanded_, na.rm = TRUE),
Mean_DAT = mean(DAT_, na.rm = TRUE),
Mean_Pled_Guilty = mean(pled_guilty_, na.rm = TRUE),
Mean_Rearrest = mean(Rearrest_, na.rm = TRUE),
Mean_Family_Offense = mean(family_offense_, na.rm = TRUE),
Mean_RepAtArraign = mean(RepAtArraign_, na.rm = TRUE)
) %>%
arrange(County_Name_)
## Warning: There were 2 warnings in `summarize()`.
## The first warning was:
## ℹ In argument: `Max_Days_in_Custody = max(Days_in_Custody_, na.rm = TRUE)`.
## Caused by warning in `max()`:
## ! no non-missing arguments to max; returning -Inf
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
# View the NYC boroughs summary
print(nyc_boroughs_summary)
## # A tibble: 0 × 13
## # ℹ 13 variables: County_Name_ <chr>, Mean_Days_in_Custody <dbl>,
## # Median_Days_in_Custody <dbl>, Max_Days_in_Custody <dbl>,
## # Min_Days_in_Custody <dbl>, Mean_BSNP <dbl>, Mean_NMR <dbl>,
## # Mean_Remanded <dbl>, Mean_DAT <dbl>, Mean_Pled_Guilty <dbl>,
## # Mean_Rearrest <dbl>, Mean_Family_Offense <dbl>, Mean_RepAtArraign <dbl>
# Joining the datasets
merged_data <- ny_counties %>%
left_join(filtered_county_summary, by = c("NAME" = "County_Name_"))
# Create the plot
ggplot(data = merged_data) +
geom_sf(aes(fill = Mean_BSNP), color = "white") +
scale_fill_gradient(low = "lightgrey", high = "darkblue") +
theme_minimal() +
labs(fill = "Mean_BSNP",
title = "Mean_BSNP by County in New York")

library(dplyr)
filtered_data_BSNP <- filtered_data %>%
filter(Bail_Set_and_Not_Posted_at_Arraign == "Y")
category_counts <- table(filtered_data_BSNP$Race)
# Convert to a dataframe for ggplot
df <- as.data.frame(category_counts)
# Name the columns appropriately
names(df) <- c("Category", "Count")
# Calculate the percentages
df$Percentage <- df$Count / sum(df$Count) * 100
# Create a new label combining the category and its percentage with two decimal places
df$Label <- paste(df$Category, sprintf("(%.2f%%)", df$Percentage))
# Define a custom color palette
num_categories <- length(unique(df$Category))
start_colors <- c("#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02", "#a6761d", "#666666")
custom_palette <- colorRampPalette(start_colors)(num_categories)
# Generate the pie chart using the new labels for the legend and custom colors
ggplot(df, aes(x = "", y = Count, fill = Label)) +
geom_bar(width = 1, stat = "identity", colour = "white") +
coord_polar("y", start = 0) +
theme_void() +
scale_fill_manual(values = custom_palette, name = "Charge Category") +
theme(legend.title = element_blank()) +
labs(title = "NYC Misdemeanors - BSNP Subset - Race
6,637 Cases for 423,913 Days")

number_of_rowsBSNP <- nrow(filtered_data_BSNP) #423,913 "Number of cases included in pie chart: 6637"
print(paste("Number of cases included in pie chart:", number_of_rowsBSNP))
## [1] "Number of cases included in pie chart: 6637"
sum(filtered_data_BSNP$Known_Days_in_Custody)
## [1] 423913
filtered_data_Remanded <- filtered_data %>%
filter(Release_Decision_at_Arraign == "Remanded")
nrow(filtered_data_Remanded)
## [1] 71
category_counts <- table(filtered_data_Remanded$Arraign_Charge_Category)
# Convert to a dataframe for ggplot
df <- as.data.frame(category_counts)
# Name the columns appropriately
names(df) <- c("Category", "Count")
# Calculate the percentages
df$Percentage <- df$Count / sum(df$Count) * 100
# Create a new label combining the category and its percentage with two decimal places
df$Label <- paste(df$Category, sprintf("(%.2f%%)", df$Percentage))
# Define a custom color palette
num_categories <- length(unique(df$Category))
start_colors <- c("#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02", "#a6761d", "#666666")
custom_palette <- colorRampPalette(start_colors)(num_categories)
# Generate the pie chart using the new labels for the legend and custom colors
ggplot(df, aes(x = "", y = Count, fill = Label)) +
geom_bar(width = 1, stat = "identity", colour = "white") +
coord_polar("y", start = 0) +
theme_void() +
scale_fill_manual(values = custom_palette, name = "Charge Category") +
theme(legend.title = element_blank()) +
labs(title = "Distribution of Arraign Charge Categories for Remanded")

number_of_rows_Remanded <- nrow(filtered_data_Remanded)
print(paste("Number of cases included in regression:", number_of_rows_Remanded ))
## [1] "Number of cases included in regression: 71"
sum(filtered_data_BSNP$Known_Days_in_Custody)
## [1] 423913
# Load the necessary library
# install.packages("Hmisc") # Uncomment if you don't have the Hmisc package
# library(Hmisc)
# Assuming NYC_dummy_vars is your data frame
# If you need to read data from a file, use something like
# NYC_dummy_vars <- read.csv("path_to_your_file.csv")
# Assuming NYC_dummy_vars is your data frame
# Use na.omit to remove rows with NA
NYC_dummy_vars_no_NA <- na.omit(ONYC_dummy_vars)
# Calculate the correlation matrix again
correlation_matrix <- cor(NYC_dummy_vars_no_NA)
# Round to two decimal places
correlation_matrix_rounded <- round(correlation_matrix, 2)
# Print the matrix
print(correlation_matrix_rounded)
## Days_in_Custody BSNP NMR ROR Remanded DAT pled_guilty
## Days_in_Custody 1.00 0.48 0.02 -0.25 0.07 -0.14 -0.12
## BSNP 0.48 1.00 -0.08 -0.46 0.03 -0.20 -0.10
## NMR 0.02 -0.08 1.00 -0.46 -0.02 -0.19 -0.09
## ROR -0.25 -0.46 -0.46 1.00 -0.07 0.15 0.03
## Remanded 0.07 0.03 -0.02 -0.07 1.00 -0.09 -0.04
## DAT -0.14 -0.20 -0.19 0.15 -0.09 1.00 0.20
## pled_guilty -0.12 -0.10 -0.09 0.03 -0.04 0.20 1.00
## Rearrest 0.00 -0.15 -0.08 0.01 -0.06 0.36 0.11
## family_offense 0.01 0.05 0.15 -0.04 0.10 -0.47 -0.13
## RepAtArraign 0.03 0.04 0.02 -0.03 0.00 -0.01 -0.08
## Rearrest family_offense RepAtArraign
## Days_in_Custody 0.00 0.01 0.03
## BSNP -0.15 0.05 0.04
## NMR -0.08 0.15 0.02
## ROR 0.01 -0.04 -0.03
## Remanded -0.06 0.10 0.00
## DAT 0.36 -0.47 -0.01
## pled_guilty 0.11 -0.13 -0.08
## Rearrest 1.00 -0.24 -0.06
## family_offense -0.24 1.00 0.02
## RepAtArraign -0.06 0.02 1.00