Gun Laws

October 07, 2017

I have recently seen quite a bit of outrage on social media aimed towards gun control in the USA. I don’t know why facebook’s algorithms decided that I should care about a topic that bears no relation to my interests, but seeing every other item in my news feed and on reddit trying to convince me to think a certain way without reference to data rings my internal “propaganda” alarms.

Normally, this would not bother me, but this time it struck a nerve. You see, many of the people sharing/liking these emotional articles and “hilarious” twitter beatdowns, none of which reference data or actual experiments, are the same people that were pointing fingers at how climate change deniers ignore science!

To be clear, it is likely that they are right! Perhaps stronger gun laws would make gun-related violence drop. I simply can’t tell from the media. That is what bothers me - a witty reply to a tweet certainly makes one feel great, but it has nothing to do with the truth of the matter. If you are inundated with such appeals, it becomes a classic example of propaganda, and I really hate it when I feel that I am being manipulated for someone’s agenda.

With that in mind, I decided to do a basic sanity check. Something stupidly simple: Take the amount of gun restrictions in each state, and plot it against the states’ firearm death rate.

This would be easy to do in excel, but python is much more fun than messing with data by hand, so I have performed the data cleaning in a jupyter notebook, which you can download here.

Download

Gun Laws vs Death Rates

It turns out that Wikipedia has a page on firearm death rates by state. That page has a nice little table for death rates per 100,000 population in 2013. Furthermore, there is also a page on gun laws per state.

Let's do something stupidly simple: Let's count up the gun laws in each state, and compare it to the firearm death rates.

This is obviously a very flawed metric, but it serves as a good initial sanity check. I expect there to be a pretty strong negative relation - the more gun control laws, the less gun-related deaths. If that is found, then sanity check passed, and barring more in-depth research showing otherwise, I can accept that adding gun control laws could plausibly decrease the gun death rate.

Scraping the wikipedia articles turns out to be surprisingly easy using pandas:

import pandas as pd

# Read the tables from the wikipedia page, and return the second one (index 1), which corresponds to the
# gun death rate per state.
gun_death_rate = pd.read_html("https://en.wikipedia.org/wiki/Firearm_death_rates_in_the_United_States_by_state",
             header=0,
             attrs={"class": ["sortable","wikitable", "plainrowheaders"]})[1]

# Read the tables from the gun laws wikipedia page, and return the 51 tables corresponding to the gun laws
# in each state, plus the district of columbia
per_state_gun_laws = pd.read_html("https://en.wikipedia.org/wiki/Gun_laws_in_the_United_States_by_state",header=0)[1:-7]

Now we combine the two datasets by just counting the laws per state:


# Let's extract the number of restrictions (ie, count the number of laws) for each state
number_of_restrictions_per_state = []
for state_laws in per_state_gun_laws:
    # Everywhere there is a "Yes", we say that there is an additional restriction,
    # except for the "open carry" row, which is reversed, disallowing open carry is
    # a restriction
    open_carry_row = (state_laws[state_laws["Subject/Law"].str.contains("Open carry|Open Carry")].index[0])

    handgun_restrictions = (state_laws["Handguns"].str.contains("Yes") 
                            + 0.5*state_laws["Handguns"].str.contains("Partial"))
    handgun_restrictions[open_carry_row] = 1 - handgun_restrictions[open_carry_row]

    # Annoyingly, wikipedia uses two different table headings
    try:
        longgun_restrictions = (state_laws["Long Guns"].str.contains("Yes")
                                + 0.5*state_laws["Long Guns"].str.contains("Partial"))
    except:
        longgun_restrictions = (state_laws["Long guns"].str.contains("Yes")
                                + 0.5*state_laws["Long guns"].str.contains("Partial"))

    longgun_restrictions[open_carry_row] = 1 - longgun_restrictions[open_carry_row]

    number_of_restrictions_per_state.append(handgun_restrictions.sum() + longgun_restrictions.sum())

# Thankfully the states are alphabetical in both wikipedia pages, so they match up without any more work.
# We just need to remove the district of columbia (8th element, 0-indexed)
del number_of_restrictions_per_state[8]
full_dataset = gun_death_rate.assign(Laws=number_of_restrictions_per_state)

# Now a bit of cleanup:
# Winsconsin has a weird table, which we need to fix manually
full_dataset.loc[full_dataset["State"]=="Wisconsin","Laws"] += 3

full_dataset
StateRateLaws
0Alabama17.793.0
1Alaska19.594.0
2Arizona14.203.0
3Arkansas16.936.0
4California7.8921.0
5Colorado11.7513.0
6Connecticut4.4816.0
7Delaware10.808.0
8Florida12.495.0
9Georgia12.633.0
10Hawaii2.7113.0
11Idaho14.082.0
12Illinois8.6716.0
13Indiana13.049.0
14Iowa8.198.0
15Kansas11.442.0
16Kentucky14.155.0
17Louisiana19.153.0
18Maine11.894.0
19Maryland9.7510.0
20Massachusetts3.1814.0
21Michigan12.035.0
22Minnesota7.887.0
23Mississippi17.554.0
24Missouri14.564.0
25Montana16.945.0
26Nebraska8.995.0
27Nevada14.165.0
28New Hampshire7.032.0
29New Jersey5.6912.0
30New Mexico15.637.5
31New York4.3917.0
32North Carolina12.427.0
33North Dakota11.894.0
34Ohio11.146.0
35Oklahoma16.416.0
36Oregon11.768.0
37Pennsylvania11.369.0
38Rhode Island5.3312.0
39South Carolina15.604.0
40South Dakota9.473.0
41Tennessee15.867.0
42Texas10.508.0
43Utah11.6912.0
44Vermont10.372.0
45Virginia10.465.0
46Washington9.078.5
47West Virginia15.104.0
48Wisconsin9.937.0
49Wyoming17.512.0

So, let's plot it!

%matplotlib inline
import seaborn as sb

sb.regplot(x="Laws",y="Rate",data=full_dataset)
<matplotlib.axes._subplots.AxesSubplot at 0x7f15b06cc978>

Given the large negative correlation, unless research is sent to me with a more in-depth analysis of the issue, I can accept that it is very plausible[1] that adding gun control laws would decrease the firearm death rate.


  1. Correlation does not imply causation. ↩︎

Click here to download the jupyter notebook

Finally, it is important to understand that even my simple analysis could lead to the wrong conclusion. Articles which do a much more in-depth analysis can easily be found online, like here. There is also peer-reviewed research which shows similar results: yes there is a correlation between guns and deaths, but no, causation cannot be concluded. With a more in-depth analysis, even causation might be determined.

To be clear, I am not advocating for anything here. I simply felt like I was being manipulated, because there was a lot of yelling on social media, but a distinct lack of facts.