R Project

Author

Dila Kargi

Published

October 15, 2001

1. Loading Data and Libraries

library("devtools")
Loading required package: usethis
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("descr")
library("ggplot2")
options(scipen=999)

2. Data Basics

Summary of Data Basics: The Texas court dataset consists of 23,658 observations and 25 variables. Each observation represents an individual court case (or defendant) processed through the court system, recording information on defendant demographics (such as sex, race/ethnicity, and age), case timing, and procedural outcomes including initial and final court dispositions. The dataset is covering cases from 1996 through 2022

there are 23658 observations in the data

dim(court)
[1] 23658    25

Each observation represents an individual court case (or defendant) processed through the court system.

‘female’ (sex/gender), ‘race_eth’ (race and ethnicity), and ‘age’ describe the defendant(individual demographics).’caseyear’ indicates the calendar year the court case took place(timing).’first_d’ and ‘final_d’ record the initial and final court dispositions(outcomes).

head(court)
# A tibble: 6 × 25
  female race_eth     age caseyear first_d  final_d reptype off_level3 off_type4
   <dbl> <fct>      <dbl>    <dbl> <fct>    <fct>   <fct>   <fct>      <fct>    
1      1 2 Hispanic    29     1998 3 Diver… 2 Conv… 2 Hired 3 Felony … 3 Drugs  
2      0 2 Hispanic    18     2013 3 Diver… 2 Conv… 2 Hired 3 Felony … 1 Violent
3      0 3 White       21     2005 3 Diver… 2 Conv… 1 Appo… 2 Felony … 2 Proper…
4      0 3 White       20     1997 3 Diver… 2 Conv… 1 Appo… 1 Misdeme… 1 Violent
5      0 3 White       22     2017 3 Diver… 1 Dimi… 1 Appo… 1 Misdeme… 3 Drugs  
6      0 1 Black       22     2012 3 Diver… 1 Dimi… 1 Appo… 1 Misdeme… 3 Drugs  
# ℹ 16 more variables: offyear <dbl>, sent_finefee <dbl>, sent_jail <dbl>,
#   sent_prison <dbl>, sent_prob <dbl>, finefee <dbl>, prison_years <dbl>,
#   jail_days <dbl>, prob_days <dbl>, off_level <fct>, off_type9 <fct>,
#   offdesc <chr>, dispdesc <chr>, judgedesc <chr>, sentencedesc <chr>,
#   personid <dbl>

There are 25 variables in the data. Alternatively ncol(court) can be used to learn only the # of variables in the data

colnames(court)
 [1] "female"       "race_eth"     "age"          "caseyear"     "first_d"     
 [6] "final_d"      "reptype"      "off_level3"   "off_type4"    "offyear"     
[11] "sent_finefee" "sent_jail"    "sent_prison"  "sent_prob"    "finefee"     
[16] "prison_years" "jail_days"    "prob_days"    "off_level"    "off_type9"   
[21] "offdesc"      "dispdesc"     "judgedesc"    "sentencedesc" "personid"    

Data covers 1996 - 2022 (min - max)

summary(court$caseyear)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1996    2002    2008    2008    2013    2022 

3. Univariate Descriptive Analysis

I will use Summary statistics+histogram for Continuous/Numerical Variables, and freq. table + bar chart for categorical/binary variables

“reptype” shows what kind of lawyer the defendant had, such as a court-appointed public defender or a privately hired attorney. This variable is relevant because having a private attorney might lead to better case outcomes than an appointed attorney, or vice versa, depending on resources and time. Court-appointed counsel represents the clear majority at 64.85% (15343 cases), while hired counsel accounts for 35.15% (8315 cases), which likely reflects that most defendants entering the criminal court system cannot afford private legal representation.

freq(court$reptype, plot = TRUE)

Type of legal counsel 
            Frequency Percent
1 Appointed     15343   64.85
2 Hired          8315   35.15
Total           23658  100.00

“off_level3” measures how serious the alleged offense was, grouped into general categories like misdemeanors and felonies. This variable is relevant because we would expect the severity of the charge to play a big role in whether someone ends up convicted or is offered a diversion program instead. Misdemeanors make up the largest share of cases at 46.40% (10977 cases), followed by minor felonies at 37.37% (8842 cases), and major felonies at 16.23% (3839 cases). At this dataset, lower-level offenses and minor felonies comprise the vast majority of the court’s caseload, while major felonies appear much less frequently.

freq(court$off_level3, plot = TRUE)

3 levels of offense seriousness 
                 Frequency Percent
1 Misdemeanor        10977   46.40
2 Felony - minor      8842   37.37
3 Felony - major      3839   16.23
Total                23658  100.00

“off_type4” sorts the crimes into four general categories; property, drug, violent, or public order offenses. It is relevant because courts might handle violent crimes very differently and more strictly than non-violent drug or property offenses, regardless of other factors. Drug offenses make up the largest share of cases at 29.19% (6905 cases), followed by property offenses at 25.81% (6106 cases), other offenses at 23.33% (5519 cases), and violent offenses at 21.68% (5128 cases). Non-violent charges (drugs, property, and other) make up roughly 78% of the cases combined, while violent offenses represent the smallest category.

freq(court$off_type4, plot = TRUE)

4 category offense type 
           Frequency Percent
1 Violent       5128   21.68
2 Property      6106   25.81
3 Drugs         6905   29.19
4 Other         5519   23.33
Total          23658  100.00

“female” is a binary variable that indicates the defendant’s sex, where 1 means female and 0 means male. This variable is relevant because it helps us check whether male and female defendants face different treatment or sentencing outcomes in court. Looking at the frequency table, males make up the overwhelming majority at 75.43% (17846 cases), while females make up only 24.57% (5812 cases), showing a substantial gender imbalance in the court dataset.

freq(court$female, plot = TRUE)

Sex: Male=0 Female=1 
      Frequency Percent
0         17846   75.43
1          5812   24.57
Total     23658  100.00

“race_eth” records the racial and ethnic background of each defendant (Black, Hispanic, White or Other). This variable is relevant to indicate if racial or ethnic disparities exist in court decisions and final dispositions. Looking at the frequency table, Hispanic defendants make up the largest group at about 44.4%, followed by White defendants at 39.8%, Black defendants at 14.9%, and Other at just under 1%. Together, Black and Hispanic defendants account for nearly 60% of the sample, suggesting that minority groups are heavily represented in these court cases.

freq(court$race_eth, plot = TRUE)

Defendant race/ethnicity 
           Frequency  Percent
1 Black         3523  14.8914
2 Hispanic     10504  44.3994
3 White         9417  39.8047
4 Other          214   0.9046
Total          23658 100.0000

“final_d” captures the final outcome of the court case, showing whether it ended in a conviction or dismissal. This variable is relevant because it serves as the main outcome to see who ends up with a criminal conviction and who avoided. Convictions are the most common outcome at 60.75% (14373 cases), while dismissals and acquittals make up the remaining 39.25% (9285 cases), indicating that over six in ten defendants in this sample were convicted.

freq(court$final_d, plot = TRUE)

Final case outcome 
                Frequency Percent
1 Dimiss/Acquit      9285   39.25
2 Convict           14373   60.75
Total               23658  100.00

“finefee” measures the total dollar amount of fines and court fees charged to each defendant. This is relevant because monetary penalties can create a heavy financial burden on defendants, especially if they are already struggling financially. Looking at the summary statistics, the fees range from $149.20 to $7,550.00, with a median of $1,634.70 and a mean of $1,841.40. The mean being higher than the median, along with a standard deviation of about $1,063.29, suggests that higher fee amounts pull the average up. There are also 6,016 missing values (NAs), which might mean that those defendants were not assessed fees, or that that information wasn’t recorded.

# Summary statistics
summary(court$finefee)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.     NAs 
  149.2  1018.4  1634.7  1841.4  2511.2  7550.0    6016 
# Standard deviation
sd(court$finefee, na.rm = TRUE)
[1] 1063.294
# Histogram
hist(court$finefee, 
     main = "Distribution of Fines and Fees",
    
     xlab = "Fine and Fee Amount ($)", 
     ylab = "Frequency")

# Bivariate Analysis

4.a Bivariate frequency table (counts)

Out of 15343 defendants with court-appointed counsel, 66.6% were convicted, while only 33.4% (5128 individuals) had their cases dismissed or were acquitted. Out of 8315 defendants with hired counsel, exactly 50.0% were convicted, and 50.0% (4157 individuals) had their cases dismissed or were acquitted.

# Step 1: the two-way frequency table of raw counts (crosstab)
rep_disp <- table(court$reptype, court$final_d)
rep_disp
             
              1 Dimiss/Acquit 2 Convict
  1 Appointed            5128     10215
  2 Hired                4157      4158
# Step 2: Two-way table showing row proportions (each row sums to 1.0)
prop.table(rep_disp, margin = 1)
             
              1 Dimiss/Acquit 2 Convict
  1 Appointed       0.3342241 0.6657759
  2 Hired           0.4999399 0.5000601
# Step 3: Two-way table showing row percentages rounded to 1 decimal place
rep_disp_pct <- round(prop.table(rep_disp, margin = 1) * 100, 1)
rep_disp_pct
             
              1 Dimiss/Acquit 2 Convict
  1 Appointed            33.4      66.6
  2 Hired                50.0      50.0

4.b Percentage table showing conviction rates across rows

# Step 1: the raw count crosstab
rep_disp <- table(court$reptype, court$final_d)
rep_disp
             
              1 Dimiss/Acquit 2 Convict
  1 Appointed            5128     10215
  2 Hired                4157      4158
# Step 2: row to percentages convert, (margin = 1), each counsel type equals 100%
rep_disp_pct <- round(prop.table(rep_disp, margin = 1) * 100, 1)
rep_disp_pct
             
              1 Dimiss/Acquit 2 Convict
  1 Appointed            33.4      66.6
  2 Hired                50.0      50.0

4.c Barplot showing differences in conviction by representation type

barplot(rep_disp_pct,
        beside = TRUE,
        legend = rownames(rep_disp_pct),
        main = "Final Disposition by Representation Type",
        xlab = "Final Disposition",
        ylab = "Percentage (%)",
        col = c("steelblue", "coral"))

4.d Chi-Square Test of Independence

chi2_result <- chisq.test(rep_disp)
chi2_result

    Pearson's Chi-squared test with Yates' continuity correction

data:  rep_disp
X-squared = 620.39, df = 1, p-value < 0.00000000000000022