GitHub: https://github.com/CUNY-SPS-Data-Science-Program/story-3-seung-m1nsong
rpubs: https://rpubs.com/seungm1nsong/1094858

Assignment

The CDC publishes firearm mortality for each State per 100,000 persons https://www.cdc.gov/nchs/pressroom/sosmap/firearm_mortality/firearm.htm. Each State’ firearm control laws can be categorized as very strict to very lax. The purpose of this Story is to answer the question, ” Do stricter firearm control laws help reduce firearm mortality?”

For this assignment you will need to:

  • Access the firearm mortality data from the CDC using an available API (https://open.cdc.gov/apis.html)

  • Create a 5 point Likert scale categorizing gun control laws from most lax to strictest and assign each state to the most appropriate Likert bin.

  • Determine wether stricter gun control laws result in reduced gun violence deaths

  • Present your story using heat maps

API

Access https://socrataapikeys.docs.apiary.io/#introduction/key-id-and-key-secret to request an API Key.

url <- "https://data.cdc.gov/resource/489q-934x.json"

response <- GET(url)

if (status_code(response) == 200) {

  data <- fromJSON(content(response, "text", encoding = "UTF-8"))
  
  df_mortality <- as.data.frame(data)
  
  
} else {
  cat("Error: Unable to retrieve data from the URL.\n")
}

head(df_mortality)
##   year_and_quarter                   time_period
## 1          2020 Q1 12 months ending with quarter
## 2          2020 Q1 12 months ending with quarter
## 3          2020 Q1 12 months ending with quarter
## 4          2020 Q1 12 months ending with quarter
## 5          2020 Q1 12 months ending with quarter
## 6          2020 Q1 12 months ending with quarter
##                        cause_of_death    rate_type               unit
## 1                          All causes Age-adjusted Deaths per 100,000
## 2                   Alzheimer disease Age-adjusted Deaths per 100,000
## 3                            COVID-19 Age-adjusted Deaths per 100,000
## 4                              Cancer Age-adjusted Deaths per 100,000
## 5 Chronic liver disease and cirrhosis Age-adjusted Deaths per 100,000
## 6  Chronic lower respiratory diseases Age-adjusted Deaths per 100,000
##   rate_overall rate_sex_female rate_sex_male rate_alaska rate_alabama
## 1        718.6           605.1           851       691.2        898.2
## 2         29.8            33.6          23.8        24.1         44.6
## 3          1.7             1.2           2.3        <NA>          0.7
## 4        145.7             126         172.2       141.3        159.9
## 5         11.5             8.1          15.2          16         13.7
## 6           38            35.5          41.4        29.2         54.5
##   rate_arkansas rate_arizona rate_california rate_colorado rate_connecticut
## 1         870.9        664.5           602.7         651.6            646.6
## 2          41.1         32.2            37.2          33.5             17.5
## 3          <NA>          0.6             0.6           1.6              2.3
## 4         165.8        130.6           131.4           128            132.3
## 5          12.9         14.5            12.4            15              9.4
## 6          59.4         37.4            28.9          40.7               27
##   rate_district_of_columbia rate_delaware rate_florida rate_georgia rate_hawaii
## 1                     715.4         732.1        652.9        776.6       570.9
## 2                      12.1          26.9         18.2         41.8        20.1
## 3                      <NA>          <NA>          0.5          1.8        <NA>
## 4                     147.9         150.5        138.1        150.8       126.4
## 5                       9.2           9.3         11.2         10.2         6.7
## 6                      19.8          34.8         35.1         41.9        17.6
##   rate_iowa rate_idaho rate_illinois rate_indiana rate_kansas rate_kentucky
## 1     735.5      709.1         711.1          832       767.1         910.7
## 2      30.4       32.5          24.7         32.4        22.4          31.2
## 3      <NA>       <NA>           1.3          1.5        <NA>           0.6
## 4     150.9      138.4         151.7        164.1       152.1         175.5
## 5       9.7       12.8          10.2         12.9        11.2          13.9
## 6      43.7       44.6          35.1           56        46.6          61.6
##   rate_louisiana rate_massachusetts rate_maryland rate_maine rate_michigan
## 1            870              656.9         704.7      756.7         782.9
## 2           42.1               17.4          14.1       25.9          33.5
## 3            7.1                1.3           0.6       <NA>           3.8
## 4          165.1              139.3         142.9      162.8         157.9
## 5            9.7                9.2           8.2         11          11.7
## 6           39.5               30.6          28.8       47.5          44.9
##   rate_minnesota rate_missouri rate_mississippi rate_montana
## 1          652.9         804.1            945.4        736.2
## 2           34.7          34.1             48.6         22.2
## 3           <NA>           0.3                1         <NA>
## 4          141.4         157.4            178.4        138.3
## 5           11.3          10.4             13.6         15.8
## 6           31.8          47.6             57.4         49.3
##   rate_north_carolina rate_north_dakota rate_nebraska rate_new_hampshire
## 1                 767               728         706.1                691
## 2                36.9              38.6          30.3               27.5
## 3                 0.2              <NA>          <NA>               <NA>
## 4               150.9             144.3         148.2              145.4
## 5                11.6              15.3          11.4               11.3
## 6                40.8              35.8          44.4               39.2
##   rate_new_jersey rate_new_mexico rate_nevada rate_new_york rate_ohio
## 1           666.8           758.9       738.4         635.5       827
## 2              21              21          23            14        33
## 3             5.7            <NA>         0.9          10.9       0.6
## 4           135.9           129.6       145.3         133.3     161.4
## 5             7.9            26.4        13.2           7.2      11.3
## 6            25.8            39.8        47.3          27.5      46.2
##   rate_oklahoma rate_oregon rate_pennsylvania rate_rhode_island
## 1         876.7       695.4             750.4             704.3
## 2          37.8        37.4              20.7                29
## 3           0.7         0.5               0.6              <NA>
## 4         171.3       145.1             154.3             151.7
## 5          15.6        12.9               8.7              11.1
## 6          61.8        37.4              34.4              34.5
##   rate_south_carolina rate_south_dakota rate_tennessee rate_texas rate_utah
## 1               809.9             738.5          889.6      717.5     687.6
## 2                38.2              39.1           40.8       38.5      40.3
## 3                 0.5              <NA>            0.4        0.3      <NA>
## 4               154.6             155.4          165.3      141.8     116.5
## 5                13.2              18.6           14.1       14.4       8.6
## 6                42.8              45.8           53.4       37.8      31.3
##   rate_virginia rate_vermont rate_washington rate_wisconsin rate_west_virginia
## 1         709.4        685.2           663.9          729.6              950.4
## 2            27         32.9            41.2           31.1               32.8
## 3           0.6         <NA>             3.1            0.6               <NA>
## 4         147.1        148.6           140.8          149.7              175.4
## 5          10.3          8.9            12.2           10.3               16.4
## 6            36         38.4            32.6           37.3               62.1
##   rate_wyoming rate_age_1_4 rate_age_5_14 rate_age_15_24 rate_age_25_34
## 1        739.4         <NA>          <NA>           <NA>           <NA>
## 2         34.6         <NA>          <NA>           <NA>           <NA>
## 3         <NA>         <NA>          <NA>           <NA>           <NA>
## 4        137.9         <NA>          <NA>           <NA>           <NA>
## 5           21         <NA>          <NA>           <NA>           <NA>
## 6         53.8         <NA>          <NA>           <NA>           <NA>
##   rate_age_35_44 rate_age_45_54 rate_age_55_64 rate_65_74 rate_age_75_84
## 1           <NA>           <NA>           <NA>       <NA>           <NA>
## 2           <NA>           <NA>           <NA>       <NA>           <NA>
## 3           <NA>           <NA>           <NA>       <NA>           <NA>
## 4           <NA>           <NA>           <NA>       <NA>           <NA>
## 5           <NA>           <NA>           <NA>       <NA>           <NA>
## 6           <NA>           <NA>           <NA>       <NA>           <NA>
##   rate_age_85_plus
## 1             <NA>
## 2             <NA>
## 3             <NA>
## 4             <NA>
## 5             <NA>
## 6             <NA>

Data Preparation

# Renaming the 'state' column to its corresponding abbreviation.
# Filter Firearm-related injury only
df_gun <- df_mortality[df_mortality$cause_of_death == "Firearm-related injury", ]
df_gun <- df_gun[df_gun$rate_type == "Crude", ]
df_gun <- df_gun[df_gun$time_period == "12 months ending with quarter", ]
df_gun <- data.frame(df_gun, row.names = NULL)

colnames(df_gun)[colnames(df_gun) == "rate_alaska"] <- "AK"
colnames(df_gun)[colnames(df_gun) == "rate_alabama"] <- "AL"
colnames(df_gun)[colnames(df_gun) == "rate_arkansas"] <- "AR"
colnames(df_gun)[colnames(df_gun) == "rate_arizona"] <- "AZ"
colnames(df_gun)[colnames(df_gun) == "rate_california"] <- "CA"
colnames(df_gun)[colnames(df_gun) == "rate_colorado"] <- "CO"
colnames(df_gun)[colnames(df_gun) == "rate_connecticut"] <- "CT"
colnames(df_gun)[colnames(df_gun) == "rate_district_of_columbia"] <- "DC"
colnames(df_gun)[colnames(df_gun) == "rate_delaware"] <- "DE"
colnames(df_gun)[colnames(df_gun) == "rate_florida"] <- "FL"
colnames(df_gun)[colnames(df_gun) == "rate_georgia"] <- "GA"
colnames(df_gun)[colnames(df_gun) == "rate_hawaii"] <- "HI"
colnames(df_gun)[colnames(df_gun) == "rate_iowa"] <- "IA"
colnames(df_gun)[colnames(df_gun) == "rate_idaho"] <- "ID"
colnames(df_gun)[colnames(df_gun) == "rate_illinois"] <- "IL"
colnames(df_gun)[colnames(df_gun) == "rate_indiana"] <- "IN"
colnames(df_gun)[colnames(df_gun) == "rate_kansas"] <- "KS"
colnames(df_gun)[colnames(df_gun) == "rate_kentucky"] <- "KY"
colnames(df_gun)[colnames(df_gun) == "rate_louisiana"] <- "LA"
colnames(df_gun)[colnames(df_gun) == "rate_massachusetts"] <- "MA"
colnames(df_gun)[colnames(df_gun) == "rate_maryland"] <- "MD"
colnames(df_gun)[colnames(df_gun) == "rate_maine"] <- "ME"
colnames(df_gun)[colnames(df_gun) == "rate_michigan"] <- "MI"
colnames(df_gun)[colnames(df_gun) == "rate_minnesota"] <- "MN"
colnames(df_gun)[colnames(df_gun) == "rate_missouri"] <- "MO"
colnames(df_gun)[colnames(df_gun) == "rate_mississippi"] <- "MS"
colnames(df_gun)[colnames(df_gun) == "rate_montana"] <- "MT"
colnames(df_gun)[colnames(df_gun) == "rate_north_carolina"] <- "NC"
colnames(df_gun)[colnames(df_gun) == "rate_north_dakota"] <- "ND"
colnames(df_gun)[colnames(df_gun) == "rate_nebraska"] <- "NE"
colnames(df_gun)[colnames(df_gun) == "rate_new_hampshire"] <- "NH"
colnames(df_gun)[colnames(df_gun) == "rate_new_jersey"] <- "NJ"
colnames(df_gun)[colnames(df_gun) == "rate_new_mexico"] <- "NM"
colnames(df_gun)[colnames(df_gun) == "rate_nevada"] <- "NV"
colnames(df_gun)[colnames(df_gun) == "rate_new_york"] <- "NY"
colnames(df_gun)[colnames(df_gun) == "rate_ohio"] <- "OH"
colnames(df_gun)[colnames(df_gun) == "rate_oklahoma"] <- "OK"
colnames(df_gun)[colnames(df_gun) == "rate_oregon"] <- "OR"
colnames(df_gun)[colnames(df_gun) == "rate_pennsylvania"] <- "PA"
colnames(df_gun)[colnames(df_gun) == "rate_rhode_island"] <- "RI"
colnames(df_gun)[colnames(df_gun) == "rate_south_carolina"] <- "SC"
colnames(df_gun)[colnames(df_gun) == "rate_south_dakota"] <- "SD"
colnames(df_gun)[colnames(df_gun) == "rate_tennessee"] <- "TN"
colnames(df_gun)[colnames(df_gun) == "rate_texas"] <- "TX"
colnames(df_gun)[colnames(df_gun) == "rate_utah"] <- "UT"
colnames(df_gun)[colnames(df_gun) == "rate_virginia"] <- "VA"
colnames(df_gun)[colnames(df_gun) == "rate_vermont"] <- "VT"
colnames(df_gun)[colnames(df_gun) == "rate_washington"] <- "WA"
colnames(df_gun)[colnames(df_gun) == "rate_wisconsin"] <- "WI"
colnames(df_gun)[colnames(df_gun) == "rate_west_virginia"] <- "WV"
colnames(df_gun)[colnames(df_gun) == "rate_wyoming"] <- "WY"
#print(df_gun)
#Save to local computer to check the data
#write.csv(df_gun, file = "C:/Users/SeungminSong/Downloads/608R/df_gun.csv", row.names = FALSE)
# CHeck Data type
#str(df_gun)
# Data type conversion: Columns 2 to 5 are kept as character, columns 6 to 69 are converted to double.
df_gun <- df_gun %>%
  mutate(across(.cols = 6:69, .fns = as.double))

# Extract year information and group it
gun_grouped_by_year <- df_gun %>%
  mutate(year = substr(year_and_quarter, 1, 4)) %>%  # Extract year
  group_by(year)  # Group by year

gun_grouped_by_year <- gun_grouped_by_year%>%
  filter(grepl("2022 Q3|Q4", year_and_quarter))


#head(gun_grouped_by_year)
# Piving Long
gun_grouped_by_year_long <- gun_grouped_by_year %>%
                          pivot_longer(
                            cols = c(AK:WY),  # Specify the range of columns to pivot
                            names_to = "state",  # New column name
                            values_to = "rate"  # New column for values
                          )

#gun_grouped_by_year_long
final_df <- gun_grouped_by_year_long %>%
          select(year, state, rate)

head(final_df)
## # A tibble: 6 × 3
## # Groups:   year [1]
##   year  state  rate
##   <chr> <chr> <dbl>
## 1 2020  AK     23.9
## 2 2020  AL     23.2
## 3 2020  AR     22.2
## 4 2020  AZ     17  
## 5 2020  CA      8.8
## 6 2020  CO     15.9

Reference: https://giffords.org/lawcenter/gun-laws/browse-gun-laws-by-state/

# Add gun control rating to the database.
# gun law rates
final_df <- final_df %>%
  mutate(
    gun_laws = case_when(
      state == "AK" ~ "1",
      state == "AL" ~ "1",
      state == "AR" ~ "1",
      state == "AZ" ~ "1",
      state == "CA" ~ "5",
      state == "CO" ~ "4",
      state == "CT" ~ "5",
      state == "DC" ~ "5",
      state == "DE" ~ "4",
      state == "FL" ~ "3",
      state == "GA" ~ "1",
      state == "HI" ~ "5",
      state == "IA" ~ "1",
      state == "ID" ~ "1",
      state == "IL" ~ "5",
      state == "IN" ~ "1",
      state == "KS" ~ "1",
      state == "KY" ~ "1",
      state == "LA" ~ "1",
      state == "MA" ~ "5",
      state == "MD" ~ "5",
      state == "ME" ~ "1",
      state == "MI" ~ "3",
      state == "MN" ~ "3",
      state == "MO" ~ "1",
      state == "MS" ~ "1",
      state == "MT" ~ "1",
      state == "NC" ~ "3",
      state == "ND" ~ "1",
      state == "NE" ~ "3",
      state == "NH" ~ "1",
      state == "NJ" ~ "5",
      state == "NM" ~ "3",
      state == "NV" ~ "3",
      state == "NY" ~ "5",
      state == "OH" ~ "2",
      state == "OK" ~ "1",
      state == "OR" ~ "4",
      state == "PA" ~ "4",
      state == "RI" ~ "4",
      state == "SC" ~ "1",
      state == "SD" ~ "1",
      state == "TN" ~ "1",
      state == "TX" ~ "1",
      state == "UT" ~ "1",
      state == "VA" ~ "4",
      state == "VT" ~ "3",
      state == "WA" ~ "2",
      state == "WI" ~ "3",
      state == "WV" ~ "1",
      state == "WY" ~ "1",
      TRUE ~ NA_character_
    )
  )

final_df$gun_laws <- as.numeric(final_df$gun_laws)
final_df$year <- as.numeric(final_df$year)
#final_df
#str(final_df)

US heatmap

As the most up-to-date data available spans from Q1 to Q4 of 2021, we have utilized the mortality data from that year.

It does seem evident that states with less stringent gun control measures tend to experience higher rates of gun-related deaths. However, there are noteworthy exceptions to this trend. For instance, consider Illinois, where gun control regulations are relatively robust, yet the incidence of shooting incidents remains lower compared to states with similar levels of gun control. Similarly, California stands out as a state with favorable outcomes relative to others.

Taking a broader view across all United States, the data strongly supports the notion that effective gun control measures contribute to a reduction in gun-related fatalities