Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I want to try and probe a question that was raised since Las Vegas and now revived due to the tragedy in Sutherland Springs,TX: Given the free trade between states, can a state unilaterally regulate firearms. This post will try to start to give an answer to this question using R.

Since there is not an open electronic federal database for firearm ownership and transactions, one important source of information is the Bureau of Alcohol, Tobacco and Firearms, (ATF). They publish the trace of firearms that were recovered every year, and when possible trace the state where the firearm originated. This creates a weighted adjaceny matrix that is similar to what in economics is called Direction of Trade.

In this matrix the rows depict the source and the columns depict the destination. This lets one get an idea where firearms that are confiscated by the ATF orginated from. From this we can also infer which states are net importers of firearms and which states are net exporters. I will explore this matrix in an attempt to better understand if firearms are more likely to flow between geographically adjacent states. In the end I will get to a shiny app that ties everything together, for those who want to stop here…

The shiny app can be run directly

pkgs <- c('reshape2','geojson','readxl','leaflet',
'httr','rgeolocate','shiny','ggplot2','sp',
'widyr','igraph','slickR','ggraph','svglite','dplyr')

check <- sapply(pkgs,require,warn.conflicts = TRUE,character.only = TRUE)

if( any(!check) ) {

pkgs.missing <- pkgs[!check]
install.packages(pkgs.missing)
invisible(sapply(pkgs.missing,
require,
warn.conflicts = TRUE,
character.only = TRUE)
)
}

shiny::runGitHub('yonicd/gunflow')

Or accessed through shinyapps.io.

Please deploy from R console as to not deplete the account on shinyapps.io. Thank you!

## Data sources

We read in the data sources

• us-states.geojson: for the leaflet,
• gun_mat: weighted adjency matrices from the ATF site
• atf_data: total firearm registration with adult state population to calculate ratios per 100 persons.
• gun_ranking: State firearm regulation ranking from the Law Center To Prevent Gun Violence.

All the raw data can be found in the project home repo https://github.com/yonicd/gunflow.

states <- geojsonio::geojson_read('https://github.com/yonicd/gunflow/blob/master/www/us-states.geojson?raw=true', what = "sp")
f <- tempfile()
for(FILE in c('gun_mat','atf_data','gun_ranking')){
}
unlink(f)

## Helper functions

To save some space we load helper functions from the project repo.

source('https://github.com/yonicd/gunflow/blob/master/funs.R?raw=true')

plot_size = 7

capitalize=function(x){
gsub("(^|[[:space:]])([[:alpha:]])", "\\1\\U\\2", x, perl=TRUE)
}

net_flow <- calc(side = 'from')%>%
left_join(calc(side = 'to'),by=c('year','state'))%>%
mutate(net=state_sum_from-state_sum_to,
ratio_net=ratio_from-ratio_to)%>%
arrange(desc(ratio_net))

network_dat <- net_dat(gun_mat)
tot <- scatter_fun(gun_mat)
tot <- tot%>%mutate(state=as.character(state))%>%left_join(gun_ranking,by=c('year','state'))

tot$state_grade <- gsub('NA','',paste(tot$state,tot$grade,tot$smart_law))
tot$grade_round <- gsub('[+-]','',tot$grade)

## Leaflet

The leafet is a great way to show interactively an adjacency matrix. To reproduce this map use the script here. In the following output, when hovering above a state you can see information regarding the total firearms recovered in Connecticut, the total amount of firearms imorted into Connecticut in 2016 and what percent each state is of the imported total.In the shiny app you can control what state is selected and if the direction of flow is importing or exporting, as well if the colour scale is on the state level of national level.

In the app it is easy to see that since the Sandy Hook mass shooting high regulation has caused a large net inflow of firearms into Connecticut. Since these are only the recovered firearms by authorities it is a proxy of the amount of weapons entering the state from other states.

## Net flow per 100 firearms between states

We move on to the adjacency matrices showing the firearms found by the ATF. The script the plot for 2016 can be found here. We calculate the outflow rate per 100 firearms per state and the inflow rate per 100 firearms and the subtract the two to get the net flow per 100 firearms. A high positive number depicts a net exporter of firearms and a low negative number depicts a net importer of firearms. The gif below animates how this metric changes from 2011-2016.

What we see:

• Washington DC and Puerto Rico have moved from strong net exporters to the top net importers of firearms.
• Since 2014 New England has become a strong net exporter of firearms.

## Firearms Flow relationship

Next we combine the firearms flow with the regulation grades for each state 2011-2016, and give a colour for each node as the number of firearms recovered within the state and it was registered to someone in the state. The script to recreate these plots can be found here.

Here we start to see that states with high regulation tend to be high net importers of firearms.

## State Firearm flow networks

To find clusters of states that have a high degree of correlation among each other and locate states that are conduits between geographical areas we use visualize the weighted adjaceny matrix as a network. The script to create these plots can be found here. We can see over time that the network has become more connected and clusters of states have been connected across geographical regions, with Tennesee and Michigan being having a central role in these connections.

## Power Centrality

Finally we try and see if we can locate states that have a strong centrality power with the networks. We use the Bonacichi centrality metric to rate both the states that have high centrality because they are connected to states with that have themselves many connections (a cooperative relationship). Inversely we also measure states that are have a high centrality measure because states that are connected to them lack connections and depened on the state for firearms (an antagonistic relationship). The script to create these plots can be found here

This exploratory analysis attempted to start to answer the question can states unilaterally regulate firearms effictively. My impression is that it is nearly impossible to do so when neighboring states do not share the same regulatory framework. This manifests itself in changes of interstate firearm flow to reach a new steady state of firearms regardless of within state regulation.

Hopefully this will be a good starting point for some modeling of firearm flow between states and developing more effective state and federal policy through the prediction of the flow of firearms between states.

Any thoughts on the analysis is always welcome, especially if you think I erred in any way, or want have any ideas on how to model such a problem.