R Markdown

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(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.1     ✔ readr     2.2.0
## ✔ ggplot2   4.0.3     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.2     ✔ tidyr     1.3.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
library(dplyr)
ice_arrests_results4capstone <- readRDS("~/Desktop/QuantMethods/Research Project/cleaned_ice_dataset.RData")
tx_data <- ice_arrests_results4capstone
names(tx_data)
##  [1] "apprehension_aor"                "apprehension_criminality"       
##  [3] "apprehension_date"               "apprehension_state_original"    
##  [5] "apprehension_city_original"      "case_status"                    
##  [7] "case_threat_level"               "citizenship_country"            
##  [9] "gender"                          "age_at_apprehension_approximate"
## [11] "case_threat_level2"
tx_data <- tx_data %>%
  mutate(status = case_when(
    case_threat_level2 == 0 ~ 0,
    case_threat_level2 %in% c(1, 2, 3) ~ 1,
    TRUE ~ NA_real_
  ))
tx_data <- tx_data %>%
  rename(tx_aors = apprehension_aor)
tx_aors <- c("San Antonio Area of Responsibility", "El Paso Area of Responsibility", "Dallas Area of Responsibility", "Harlingen Area of Responsibility", "Houston Area of Responsibility")
total_tx <- nrow(tx_data)
no_conv_count <- sum(tx_data$case == "No Conviction")
## Warning: Unknown or uninitialised column: `case`.
h1_test <- prop.test (x = no_conv_count, n = total_tx, p = 0.50, alternative = "greater")
h1_test
## 
##  1-sample proportions test with continuity correction
## 
## data:  no_conv_count out of total_tx, null probability 0.5
## X-squared = 142904, df = 1, p-value = 1
## alternative hypothesis: true p is greater than 0.5
## 95 percent confidence interval:
##  0 1
## sample estimates:
## p 
## 0
tx_data %>%
  count(status) %>%
  mutate(pct = n / sum(n)) %>%
  ggplot(aes(x = status, y = pct, fill = status)) +
  geom_col(width = 0.5, show.legend = FALSE) +
  # Add a dashed reference line at 50% (the majority mark)
  geom_hline(yintercept = 0.50, linetype = "dashed", color = "red", linewidth = 1) +
  geom_text(aes(label = scales::percent(pct, accuracy = 0.1)), vjust = -0.5) +
  scale_y_continuous(labels = scales::percent_format(), limits = c(0, 1)) +
  labs(
    title = "Share of ICE Arrests in Texas by Conviction Status",
    subtitle = "Red dashed line marks the 50% majority threshold",
    x = NULL,
    y = "Percentage of Arrests"
  ) +
  theme_minimal()

aor_tbl <- table(tx_data$tx_aors,tx_data$status)
aor_tbl
##                                     
##                                          0     1
##   Dallas Area of Responsibility      21545  9608
##   El Paso Area of Responsibility      6054  3554
##   Harlingen Area of Responsibility   23346  7463
##   Houston Area of Responsibility     23438 14504
##   San Antonio Area of Responsibility 21195 12199
round(prop.table(aor_tbl, margin = 1) * 100, 1)
##                                     
##                                         0    1
##   Dallas Area of Responsibility      69.2 30.8
##   El Paso Area of Responsibility     63.0 37.0
##   Harlingen Area of Responsibility   75.8 24.2
##   Houston Area of Responsibility     61.8 38.2
##   San Antonio Area of Responsibility 63.5 36.5
h2_chisq <- chisq.test(aor_tbl)
h2_chisq
## 
##  Pearson's Chi-squared test
## 
## data:  aor_tbl
## X-squared = 1861, df = 4, p-value < 2.2e-16
tx_data %>%
  count(tx_aors, status) %>%
  group_by(tx_aors) %>%
  mutate(pct = n / sum(n)) %>%
  ggplot(aes(x = tx_aors, y = pct, fill = status)) +
  geom_col(width = 0.6) +
  # Add percentage labels inside the stacked bars
  geom_text(
    aes(label = scales::percent(pct, accuracy = 0.1)),
    position = position_stack(vjust = 0.5),
    color = "white",
    fontface = "bold",
    size = 3.5
  ) +
  geom_hline(yintercept = 0.50, linetype = "dashed", color = "black", linewidth = 0.8) +
  scale_y_continuous(labels = scales::percent_format()) +
  labs(
    title = "ICE Arrests by Conviction Status Across Texas AORs",
    subtitle = "Dashed line indicates 50% threshold",
    x = "Area of Responsibility (AOR)",
    y = "Percentage of Arrests",
    fill = "Status"
  ) +
  theme_minimal() + scale_x_discrete(labels = c("San Antonio", "El Paso", "Dallas", "Harlingen", "Houston"))

sqrt(chisq.test(aor_tbl)$statistic / sum(aor_tbl))
## X-squared 
## 0.1141155