Library needed to start program

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Downloading all datasets

closureData <- read_csv("ClosureCases_clean.csv")
## Rows: 34 Columns: 25
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (20): caseNbr, lastName, firstName, Cty, DOB, Gender, Race, Program, dat...
## dbl  (3): Age, lengthRefClsd, Payment
## lgl  (2): restitutionPaid, Notes
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
intakeData <- read_csv("IntakeCases_clean.csv")
## Rows: 44 Columns: 25
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (23): caseNbr, lastName, firstName, Program, DOB, Gender, dateIncident, ...
## dbl  (2): Age, lengthRef_Intake
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
refData <- read_csv("ReferralsCases_clean.csv")
## Rows: 58 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (17): caseNbr, lastName, firstName, Cty, Program, DOB, Gender, dateIncid...
## dbl  (1): lengthIncidentRef
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ACRJ <- read_csv("ACRJ_clean.csv")
## Rows: 68 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (15): caseNbr, Location, caseCategory, caseType, Style, lastName, caseSt...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Renaming our variables

ACRJ <- ACRJ |>
  rename(Status = 7, offenseType = 9, docketNbr = 1, dateIncident = 10) 

Removing the case type variable as since its basically a dupe of our program variable

closureData <- closureData |>
  subset(select = -caseType)

Changing the class of referral variables to match other data sets for merging

refData <- refData |>
  mutate(Age = as.numeric(Age)) |>
  mutate(lengthIncidentRef = as.numeric(Age))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Age = as.numeric(Age)`.
## Caused by warning:
## ! NAs introduced by coercion

Intake data set

intakeData <- intakeData |>
  mutate(lengthIncidentRef = as.numeric(lengthIncidentRef))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `lengthIncidentRef = as.numeric(lengthIncidentRef)`.
## Caused by warning:
## ! NAs introduced by coercion

To merge all datasets together

mergedData <- intakeData |>
  full_join(closureData) |>
  full_join(refData) |>
  full_join(ACRJ)
## Joining with `by = join_by(caseNbr, lastName, firstName, Program, DOB, Gender,
## dateIncident, Age, dateRef, dateReRef, Race, docketNbr, Court, Notes)`
## Joining with `by = join_by(caseNbr, lastName, firstName, Program, DOB, Gender,
## dateIncident, Age, dateCharged, dateRef, dateReRef, lengthIncidentRef, Race,
## docketNbr, Court, Status, Notes, Cty)`
## Joining with `by = join_by(lastName, Program, dateIncident, docketNbr, Status,
## Outcome)`

To save merged datasets into a single dataframe

library(writexl)
write_xlsx(mergedData, "DSAD_DATA_MERGED")

Removing duplicates

dupsRemoved <- mergedData |>
  distinct(docketNbr, Program, lastName, .keep_all = TRUE)

Re-saving now that duplicates have been removed

library(writexl)
write_xlsx(dupsRemoved,"dupsRemoved_Data.xlsx")

Loading in the dataset after calculating lengthIncidentRef in excel and offense from 2000

dupsRemoved2 <- read_csv("dupsRemoved_Data.csv")
## Rows: 145 Columns: 44
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (38): caseNbr, lastName, firstName, Program, DOB, Gender, dateIncident, ...
## dbl  (5): Age, lengthIncidentRef, lengthRef_Intake, lengthRefClsd, Payment
## lgl  (1): restitutionPaid
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

To graph for program status

pieData <- dupsRemoved2 |>
  filter(!is.na(Outcome))

ggplot(pieData, aes(x = "", fill = factor(Outcome))) +
  geom_bar(width = 1) +
  coord_polar(theta = "y") +
  theme_minimal() +
  labs(fill = "Program Status", title = "
       Status of ACRJ Programs")

To graph the ages of program participants

barData <- dupsRemoved2 |>
  filter(!Age > 90) #Removes the supposed 123 y/o

ggplot(barData, aes(x = Age)) +
  geom_histogram(bins = 30, fill = "blue", color = "black") +
  theme_minimal() +
  labs(title = "Age Distribution of Participants", x = "Age", y = "Count")

Age distrubution by program

histoData <- dupsRemoved2|>
  filter(!Program =="Facility Assessment/Screening") 


ggplot(histoData, aes(x = factor(Program), y = Age)) +
  geom_boxplot() +
  theme_minimal() +
  labs(title = "Age Distribution by Program", x = "Program", y = "Age") +
    theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1))
## Warning: Removed 59 rows containing non-finite values (`stat_boxplot()`).

Time between incident and refferal

ggplot(dupsRemoved2, aes(x = lengthIncidentRef)) +
  geom_density(fill = "blue", alpha = 0.5) +
  labs(title = "Density of Time Between Incident and Referral", x = "Time Duration", y = "Density") +
  theme_minimal()
## Warning: Removed 91 rows containing non-finite values (`stat_density()`).

Time between incident and refferal by gender

violinData <-dupsRemoved2|>
  filter(Gender == "Man" | Gender == "Woman")
  
ggplot(violinData, aes(x = Gender, y = lengthIncidentRef)) +
  geom_violin(trim = FALSE) +
  labs(title = "Distribution of Time Between Incident and Referral by Gender", x = "Gender", y = "Time Duration") +
  theme_minimal()
## Warning: Removed 31 rows containing non-finite values (`stat_ydensity()`).

Program trends in 2023

dupsRemoved2$DateRefColumn <- as.Date(dupsRemoved2$dateRef, format = "%m/%d/%y")

dupsRemoved2$DateIncidentColumn <- as.Date(dupsRemoved2$dateIncident, format = "%m/%d/%y")

x_limits <- as.Date(c("2023-05-01", "2023-09-28"))

ggplot(dupsRemoved2, aes(x = DateRefColumn, group = Program, color = Program)) +
  geom_line(stat = "count") +
  labs(title = "Trends in Program Types 2023", x = "Date of Referral", y = "Count of Offenses") +
      xlim(x_limits) +
  theme_minimal()
## Warning: Removed 75 rows containing non-finite values (`stat_count()`).

Overlap between the date of incident and date of refferal

x_limitsLong <- as.Date(c("2020-05-20", "2023-06-22"))

# Reshape the data
long_df <- dupsRemoved2 |>
  gather(key = "Type", value = "Date", DateIncidentColumn, DateRefColumn)

ggplot(long_df, aes(x = Date, color = Type)) +
  geom_density() +
  labs(title = "Overlap of Date of Incident and Date of Referral", x = "Date", y = "Density") +
  xlim(x_limitsLong)+
  theme_minimal()
## Warning: Removed 147 rows containing non-finite values (`stat_density()`).