Project 2

Author

Andrew C Marshall

source: data.cityofchicago.org

#Introduction: This dataset contains information for the city of Chicago, IL on shootings, both fatal and non-fatal shootings. Specifically it covers homicides from 1991-present and non-fatal shootings from 2010-present. It has many variables including case number, date and time, address, primary victimization, gunshot, community, district, age, sex, race, location by gps coordinates and several others. This was sourced from the city of chicago.org via data.gov.

#This dataset shows individual victimizations, both fatal and non-fatal violence against persons in Chicago over the last couple of decades. “A victimization is considered a homicide victimization or non-fatal shooting victimization depending on its presence in CPD’s homicide victims data table or its shooting victims data table”1. “Each row represents a single victimization, i.e., a unique event when an individual became the victim of a homicide or non-fatal shooting. Each row does not represent a unique victim—if someone is victimized multiple times there will be multiple rows for each of those distinct events.2” This dataset is important because it represents the problem of gun violence in American and Chicago is one of the most prevalent cities in the country when it comes to homicides and worthy of study.

#load libaries and the dataset

library(tidyverse)
Warning: package 'tidyr' was built under R version 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ 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
setwd("C:/Users/Papad/Documents/MontCommunityCollege/Data110-Summer2024/DataSets")
chicago <- read_csv("Chicago-Violence.csv")
Rows: 59158 Columns: 38
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (26): CASE_NUMBER, DATE, BLOCK, VICTIMIZATION_PRIMARY, INCIDENT_PRIMARY,...
dbl (12): ZIP_CODE, WARD, AREA, DISTRICT, BEAT, MONTH, DAY_OF_WEEK, HOUR, ST...

ℹ 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.

#clean and filter data to intended variables for use study, via graphing, etc.

chicago2<- chicago|>
    select(INCIDENT_PRIMARY, GUNSHOT_INJURY_I, DISTRICT, RACE, LATITUDE, LONGITUDE)|>
    filter(INCIDENT_PRIMARY == "HOMICIDE" & GUNSHOT_INJURY_I == "NO")|>
    filter(DISTRICT == "11"); chicago2
# A tibble: 297 × 6
   INCIDENT_PRIMARY GUNSHOT_INJURY_I DISTRICT RACE  LATITUDE LONGITUDE
   <chr>            <chr>               <dbl> <chr>    <dbl>     <dbl>
 1 HOMICIDE         NO                     11 BLK       41.9     -87.7
 2 HOMICIDE         NO                     11 BLK       41.9     -87.7
 3 HOMICIDE         NO                     11 BLK       41.9     -87.7
 4 HOMICIDE         NO                     11 BLK       41.9     -87.7
 5 HOMICIDE         NO                     11 BLK       41.9     -87.7
 6 HOMICIDE         NO                     11 BLK       41.9     -87.7
 7 HOMICIDE         NO                     11 BLK       41.9     -87.7
 8 HOMICIDE         NO                     11 WHI       41.9     -87.7
 9 HOMICIDE         NO                     11 BLK       41.9     -87.7
10 HOMICIDE         NO                     11 BLK       41.9     -87.7
# ℹ 287 more rows

#First Graph of the chicago data frame:

graph1<- chicago|>
  ggplot(aes(x = DISTRICT))+
  geom_histogram(col = "blue")+
  labs(title = "Histogram of Non-Fatal Shootings by District",
       x = "District",
       y = "Number of Incidents",
       caption = "Source: City.Chicago.gov")
graph1
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 4 rows containing non-finite values (`stat_bin()`).

#This histogram shows the amount of incidents of fatal and non-fatal shootings, per District in Chicago, IL from 1991- present.

#Next lets map the location of all the incidents in just District 11.

library(leaflet)
Warning: package 'leaflet' was built under R version 4.3.3
library(plotly)
Warning: package 'plotly' was built under R version 4.3.3

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
chicago_lon <- -87.72
chicago_lat <- 41.88
  
  shootings <- paste0(
      "<b>DISTRICT: </b>", chicago2$DISTRICT, "<br>",
      "<b>GUNSHOT_INJURY_I: </b>", chicago2$GUNSHOT_INJURY_I, "<br>",
      "<b>RACE: </b>", chicago2$RACE, "<br>",
      "<chicago2>INCIDENT: </chicago2>", chicago2$INCIDENT_PRIMARY, "<br>"
      )

leaflet() |>
  setView(lng = chicago_lon, lat = chicago_lat, zoom = 13) |>
  addProviderTiles("Esri.WorldStreetMap") |>
  addCircles(
    data = chicago2,
    radius = chicago2$DISTRICT,
    color = "blue",
    fillColor = "yellow",
    fillOpacity = 0.25,
    popup = shootings)|>
    labs(title = "Map of Incidents in District 11, Chicago, IL",
       caption = "Source: City.Chicago.org")
Assuming "LONGITUDE" and "LATITUDE" are longitude and latitude, respectively
[[1]]

$title
[1] "Map of Incidents in District 11, Chicago, IL"

$caption
[1] "Source: City.Chicago.org"

attr(,"class")
[1] "labels"

#This map represents all non-fatal shootings in Chicago, IL from 2010-present. It shows the exact location of each incident, with a tooltip that tells the weather there was the following: primary incident, gunshot injury, race of victim, and that it was in District 11 in Chicago.

Statistical Analysis: We Will do a linear regresssion of some of variables from this dataset.

chicago_linear <- lm(ZIP_CODE ~ WARD, data = chicago)
summary(chicago_linear)

Call:
lm(formula = ZIP_CODE ~ WARD, data = chicago)

Residuals:
    Min      1Q  Median      3Q     Max 
-31.857  -9.924  -6.801   7.791 198.015 

Coefficients:
             Estimate Std. Error   t value Pr(>|t|)    
(Intercept) 6.063e+04  1.890e-01 320735.92   <2e-16 ***
WARD        1.173e-01  8.325e-03     14.09   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 22.16 on 59152 degrees of freedom
  (4 observations deleted due to missingness)
Multiple R-squared:  0.003346,  Adjusted R-squared:  0.003329 
F-statistic: 198.6 on 1 and 59152 DF,  p-value: < 2.2e-16

#The equation for this is 0.117(WARD) + 60630. THE p value is very small at 2x10^-16 and the r squared is very small also showing very little variabiltly at 0.00333.

library(GGally)
Warning: package 'GGally' was built under R version 4.3.3
Registered S3 method overwritten by 'GGally':
  method from   
  +.gg   ggplot2
ggpairs(chicago, columns = 8:9)
Warning: Removed 4 rows containing non-finite values (`stat_density()`).
Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
Removed 4 rows containing missing values
Warning: Removed 4 rows containing missing values (`geom_point()`).
Warning: Removed 4 rows containing non-finite values (`stat_density()`).

Bibliography: 1. Sourced from catalog.data.gov per data from city of Chicago. 2. Retrieved from catalog.data.gov per data from city of Chicago.