library(tidyverse)
library(readxl)
library(psych)
countyjail_data <- read_excel(
  "~/Desktop/capstone bexar county jail/excel work sheets and r code/TXJail_DeathsCounties2015_2025 Capstone analysis.xlsx",
  sheet = "All_Data")
# Figure 6: Housing Correlation
library(ggplot2)

# County data
county_housing <- data.frame(
  County = c("Travis", "Bexar", "Dallas", "Harris"),
  Single_Cell_Pct = c(62.8, 21.8, 20.8, 19.9),
  Preventable_Rate = c(27.9, 27.7, 7.8, 8.6)
)

# Create scatter plot
housing_plot <- ggplot(county_housing, aes(x = Single_Cell_Pct, y = Preventable_Rate)) +
  geom_point(size = 5, color = "darkred") +
  geom_smooth(method = "lm", se = TRUE, color = "steelblue", fill = "lightblue") +
  geom_text(aes(label = County), vjust = -1.2, size = 4.5, fontface = "bold") +
  labs(title = "Single-Cell Housing and Preventable Death Rates",
       subtitle = "Correlation r = .54, p < .05 (N = 4 counties)",
       x = "Single-Cell Housing (%)",
       y = "Preventable Death Rate (%)") +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
    plot.subtitle = element_text(size = 12, hjust = 0.5),
    axis.title = element_text(size = 13, face = "bold"),
    axis.text = element_text(size = 11),
    panel.grid.minor = element_blank()
  ) +
  ylim(0, 35) +
  xlim(15, 70)

# Save it
ggsave("Figure6_Housing_Correlation.png", plot = housing_plot, 
       width = 8, height = 6, dpi = 300)

# Display it
print(housing_plot)

cat("\nāœ“ Figure 6 saved as: Figure6_Housing_Correlation.png\n")
## 
## āœ“ Figure 6 saved as: Figure6_Housing_Correlation.png
The Screening Paradox: Documentation Quality Does Not Predict Prevention Success

The Screening Paradox: Documentation Quality Does Not Predict Prevention Success

# Figure 8: Odds Ratios from Model 3
library(ggplot2)

# Model 3 results
model_results <- data.frame(
  Variable = c(
    "Age (per year)",
    "Male Sex",
    "Black Race",
    "Hispanic Race", 
    "MH Problems Documented",
    "Suicidal Statements",
    "Days in Custody",
    "Dallas County",
    "Harris County",
    "Travis County"
  ),
  OR = c(0.927, 0.852, 0.229, 0.640, 0.926, 5.498, 1.000, 0.209, 0.317, 0.659),
  Lower_CI = c(0.902, 0.352, 0.098, 0.296, 0.330, 1.651, 0.999, 0.065, 0.138, 0.248),
  Upper_CI = c(0.950, 2.226, 0.503, 1.349, 2.391, 19.064, 1.002, 0.577, 0.700, 1.662),
  P_Value = c(0.000, 0.732, 0.000, 0.246, 0.878, 0.006, 0.691, 0.004, 0.005, 0.388),
  Category = c(
    "Demographic", "Demographic", "Demographic", "Demographic",
    "Mental Health", "Mental Health", "Timing",
    "County", "County", "County"
  )
)

# Add significance indicator
model_results$Significant <- model_results$P_Value < 0.05

# Reorder for better visualization
model_results$Variable <- factor(model_results$Variable,
                                  levels = rev(model_results$Variable))

# Create forest plot
odds_plot <- ggplot(model_results, aes(x = OR, y = Variable, color = Significant)) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray40", size = 0.8) +
  geom_errorbarh(aes(xmin = Lower_CI, xmax = Upper_CI), height = 0.3, size = 1) +
  geom_point(size = 4, shape = 18) +
  scale_color_manual(values = c("gray60", "darkred"), guide = "none") +
  scale_x_log10(
    breaks = c(0.1, 0.2, 0.5, 1, 2, 5, 10, 20),
    labels = c("0.1", "0.2", "0.5", "1", "2", "5", "10", "20")
  ) +
  labs(
    title = "Odds Ratios for Preventable Death (Model 3)",
    subtitle = "Reference: Bexar County, Other Race, Female, No MH Problems, No Suicidal Statements",
    x = "Odds Ratio (log scale)",
    y = "",
    caption = "Red points: p < .05 (significant) | Error bars: 95% CI"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", size = 15, hjust = 0.5),
    plot.subtitle = element_text(size = 10, hjust = 0.5, color = "gray30"),
    plot.caption = element_text(size = 9, hjust = 1, color = "gray40"),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text.y = element_text(size = 11),
    axis.text.x = element_text(size = 10),
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_blank()
  ) +
  annotate("text", x = 0.15, y = 10.5, label = "Protective\n(Lower Odds)", 
           color = "darkblue", size = 3, fontface = "italic") +
  annotate("text", x = 7, y = 10.5, label = "Risk Factor\n(Higher Odds)", 
           color = "darkred", size = 3, fontface = "italic")

# Save it
ggsave("Figure8_Odds_Ratios.png", plot = odds_plot, 
       width = 10, height = 7, dpi = 300)

# Display it
print(odds_plot)

cat("\nāœ“ Figure 8 saved as: Figure8_Odds_Ratios.png\n")
## 
## āœ“ Figure 8 saved as: Figure8_Odds_Ratios.png