<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/a-call-to-armslist-data-analysis/"></div></div><div><a data-url="http://www.r-bloggers.com/a-call-to-armslist-data-analysis/" data-counturl="http://www.r-bloggers.com/a-call-to-armslist-data-analysis/" data-count="horizontal" data-text="A Call to Arms[list] Data Analysis!" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rud.is/b/2016/06/19/a-call-to-armslist-data-analysis/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The NPR vis team contributed to a recent <a target="_blank" href="http://n.pr/1USSliN">story</a> about Armslist, a “craigslist for guns”. Now, I’m neither pro-“gun” or anti-“gun” since this subject, like most heated ones, has more than two sides. What I <em>am</em> is pro-<em>data</em>, and the U.S. Congress is so <a target="_blank" href="http://abcnews.go.com/Health/cdc-launched-comprehensive-gun-study-15-years/story?id=39873289">deep in the pockets of the NRA</a> that there’s no way for there to be any Federally-supported, data-driven research on gun injuries/deaths. Thankfully, California is going to <a target="_blank" href="http://www.wired.com/2016/06/congress-refuses-california-funds-gun-violence-research-center/">start funding research</a>, so we may see some evidence-based papers in the (hopefully) not-too-distant future.</p>
<p>When I read the NPR story I couldn’t believe it was easier to get a gun than it is get [pick your vice or other bit of dangerous contraband]. The team at NPR ended up <a target="_blank" href="http://blog.apps.npr.org/2016/06/17/scraping-tips.html">scraping the Armslist site</a> and provided a <a target="_blank" href="http://apps.npr.org/armslist-analysis/armslist-listings-2016-06-16.csv">CSV of the data</a>. Their own blog post admirably started off with a “Can you scrape?” section. This is an area I see so many python, R and other folks totally ignore since they seem to feel that just because you <em>can</em> do something also gives you license to do so. </p>
<p>I’m glad the NPR team provided the CSV of their results since I suspect that Armslist will be adding some “no scraping” language to their Terms of Service. Interestingly enough, the Armslist site owners spend a great deal of verbiage across their site indemnifying themselves (that’s how proud of their service they are).</p>
<p>Since they provided the CSV, I poked at it a bit and produced some alternate views of the data. One bit of info I was interested in is how much the ask price was for the firearms. Since this is a craigslist-like site, some of the prices are missing and others are obviously either “filler” like <code>12345678</code> or are legitimately large (i.e. the price for a rare antique). Given the huge right-skew, I limited the initial view to “affordable” ones (which I defined as between $0.00 &amp; $2,500 USD and if you look at the data yourself you’ll see why). I then computed the bandwidth for the density estimate and did some other basic maths to see what price range of the offers made up at least 50% of the overall listings. I probably should have excluded the $1 offers but the data is there for you to use to augment anything I’ve done here. </p>
<p><a target="_blank" href="https://rud.is/b/wp-content/uploads/2016/06/ask-prices-1.png"><img height="768" alt="ask-prices-1" width="1536" class="wp-image-4465" src="https://rud.is/b/wp-content/uploads/2016/06/ask-prices-1.png"></a></p>
<p>Most of these firearms are quite affordable (even if you ignore the $1.00 USD offers).</p>
<p>One other view I wanted to see was that of the listings-per-day.</p>
<p><a target="_blank" href="https://rud.is/b/wp-content/uploads/2016/06/per-day-1.png"><img height="960" alt="per-day-1" width="1536" class="wp-image-4464" src="https://rud.is/b/wp-content/uploads/2016/06/per-day-1.png"></a></p>
<p>Info from the NPR vis team suggests this is not a 100% accurate view since the listings “age out” and they did a point-in-time scrape. It would be interesting to start a daily scraper for this site or ask to work with the raw data from the site itself (but it’s unlikely Armslist would have the courage to make said data available to news organizations or researchers). Also, the value for the last segment-bar does not appear to be from a fully day’s scrape. Nothing says ‘Murica like selling guns in a sketchy way for Memorial Day.</p>
<p>Finally, I wanted a different view of the ranked states. </p>
<p><a target="_blank" href="https://rud.is/b/wp-content/uploads/2016/06/by-state-1.png"><img height="1152" alt="by-state-1" width="1536" class="wp-image-4463" src="https://rud.is/b/wp-content/uploads/2016/06/by-state-1.png"></a></p>
<p>(The <code>ggplot2</code> code for this one is kinda interesting for any R folk who are curious). This segment-bar chart is a bit of an eye strain (click on it to make it larger) but the main thing I wanted to see was if Ohio was as gun-nutty for the three less-than-valid (IMO) types of firearms sales (which is a different view than automatic vs semi-automatic). Sure enough, Ohio leads the pack (in other news, the same states are in the top 5 across these three categories).</p>
<p>“Spinnable” R code for these charts is below, so go forth and see if you can tease out any other views from the data. There is a free-text listing description field which may be interesting to mine, and the R code has sorted lists by manufacturer and caliber you can view if you run/spin it. It might be interesting to get data <a target="_blank" href="https://en.wikipedia.org/wiki/Gun_laws_in_Ohio">like this for Ohio</a> for other states and do some clustering based on the legal categories outlined in the table.</p>
<pre><code>#' ---
#' output:
#'   html_document:
#'     keep_md: true
#' ---

#+ message=FALSE, echo=FALSE, warning=FALSE
library(dplyr)
library(readr)
library(ggalt)
library(hrbrmisc)
library(KernSmooth)
library(scales)
library(stringi)
library(extrafont)
library(DT)

loadfonts()

arms &lt;- read_csv(&quot;armslist-listings-2016-06-16.csv&quot;)
arms &lt;- mutate(arms,
               price=ifelse(price==&quot;FREE&quot;, 0, price),
               price=ifelse(price==&quot;Offer&quot;, NA, price),
               price=make_numeric(price))
arms &lt;- mutate(arms,
               listed_date=gsub(&quot;^.*y, &quot;, &quot;&quot;, listed_date),
               listed_date=as.Date(listed_date, &quot;%B %d, %Y&quot;))

affordable &lt;- filter(arms, price>0 &amp; price&lt;2500)

bw &lt;- dpik(affordable$price, scalest=&quot;stdev&quot;)

a_dens &lt;- bkde(affordable$price, bandwidth=bw,
               range.x=range(affordable$price),
               truncate=TRUE)

peaks &lt;- data_frame(
  pk=which(diff(sign(diff(c(0, a_dens$y)))) == -2),
  x=a_dens$x[pk],
  y=a_dens$y[pk]
)

ann &lt;- sprintf('%s (%s of all listings) firearms are\noffered between $1 &amp; $600 USD',
               comma(nrow(filter(affordable, between(price, 1, 600)))),
               percent(nrow(filter(affordable, between(price, 1, 600)))/nrow(arms)))

grps &lt;- setNames(1:6, unique(arms$category))

ggplot() +
  geom_segment(data=cbind.data.frame(a_dens), aes(x, xend=x, 0, yend=y),
               color=&quot;#2b2b2b&quot;, size=0.15) +
  geom_vline(data=peaks, aes(xintercept=x), size=0.5,
             linetype=&quot;dotted&quot;, color=&quot;#b2182b&quot;) +
  geom_label(data=peaks, label.size=0,
            aes(x, y, label=dollar(floor(x)), hjust=c(0, 0)),
            nudge_x=c(10, 10), vjust=0, size=3,
            family=&quot;Arial Narrow&quot;) +
  geom_label(data=data.frame(), hjust=0, label.size=0, size=3,
             aes(label=ann, x=800, y=min(a_dens$y) + sum(range(a_dens$y))*0.7),
             family=&quot;Arial Narrow&quot;) +
  scale_x_continuous(expand=c(0,0), breaks=seq(0, 2500, 500), label=dollar, limits=c(0, 2500)) +
  scale_y_continuous(expand=c(0,0), limits=c(0, max(a_dens$y*1.05))) +
  labs(x=NULL, y=&quot;density&quot;,
       title=&quot;Distribution of firearm ask prices on Armslist&quot;,
       subtitle=sprintf(&quot;Counts are across all firearm types (%s)&quot;,
                        stri_replace_last_regex(paste0(names(grps), collapse=&quot;, &quot;), &quot;,&quot;, &quot; &amp;&quot;)),
       caption=&quot;Source: NPR http://n.pr/1USSliN&quot;) +
  theme_hrbrmstr_an(grid=&quot;X=Y&quot;, subtitle_size=10) +
  theme(axis.text.x=element_text(hjust=c(0, rep(0.5, 4), 1))) +
  theme(axis.text.y=element_blank()) +
  theme(plot.margin=margin(10,10,10,10)) -> gg

#+ ask-prices, dev=&quot;png&quot;, fig.width=8, fig.height=4, fig.retina=2, message=FALSE, echo=FALSE, warning=FALSE
gg

count(arms, state, category) %>%
  group_by(category) %>%
  mutate(f=paste0(paste0(rep(&quot; &quot;, grps[category[1]]), collapse=&quot;&quot;), state, collaspe=&quot;&quot;)) %>%
  ungroup() %>%
  arrange(desc(n)) %>%
  mutate(f=factor(f, levels=rev(f))) %>%
  filter(category %in% c(&quot;Handguns&quot;, &quot;Rifles&quot;, &quot;Shotguns&quot;)) %>%
  ggplot(aes(x=n, y=f)) +
  geom_segment(aes(yend=f, xend=0), size=0.5) +
  scale_x_continuous(expand=c(0,0), label=comma) +
  facet_wrap(~category, scales=&quot;free&quot;) +
  labs(x=&quot;Note: free x-axis scale&quot;, y=NULL,
       title=&quot;Distribution of firearm listing by state&quot;,
       subtitle=&quot;Listings of Antique Firearms, Muzzle Loaders &amp; NFA Firearms are not included in this view&quot;,
       caption=&quot;Source: NPR http://n.pr/1USSliN&quot;) +
  theme_hrbrmstr_an(grid=&quot;X&quot;, subtitle_size=10) +
  theme(axis.text.y=element_text(size=6)) -> gg

#+ by-state, dev=&quot;png&quot;, fig.width=8, fig.height=6, fig.retina=2, message=FALSE, echo=FALSE, warning=FALSE
gg

count(arms, listed_date) %>%
  ggplot(aes(listed_date, n)) +
  geom_segment(aes(xend=listed_date, yend=0)) +
  geom_vline(xintercept=c(as.numeric(c(as.Date(&quot;2016-05-26&quot;),
                                       as.Date(&quot;2016-05-28&quot;),
                                       as.Date(&quot;2016-06-02&quot;)))), color=&quot;#b2182b&quot;, size=0.5, linetype=&quot;dotted&quot;) +
  geom_label(data=data.frame(), hjust=1, vjust=1, nudge_x=-0.5, label.size=0, size=3,
             aes(x=as.Date(&quot;2016-05-26&quot;), y=1800, label=&quot;NYT &amp; CNN Gun Editorials&quot;),
             family=&quot;Arial Narrow&quot;, color=&quot;#b2182b&quot;) +
  geom_label(data=data.frame(), hjust=1, vjust=1, nudge_x=-0.5, label.size=0, size=3,
             aes(x=as.Date(&quot;2016-05-28&quot;), y=8500, label=&quot;Memorial Day&quot;),
             family=&quot;Arial Narrow&quot;, color=&quot;#b2182b&quot;) +
  geom_label(data=data.frame(), hjust=0, vjust=1, nudge_x=0.5,
             label.size=0, size=3, lineheight=0.9,
             aes(x=as.Date(&quot;2016-06-02&quot;), y=7000,
                 label=&quot;National Gun\nViolence\nAwareness Day&quot;),
             family=&quot;Arial Narrow&quot;, color=&quot;#b2182b&quot;) +
  scale_x_date(expand=c(0,1), label=date_format(&quot;%B 2016&quot;)) +
  scale_y_continuous(expand=c(0,0), label=comma, limit=c(0, 9000)) +
  labs(x=NULL, y=NULL,
       title=&quot;Armslist firearm new listings per day&quot;,
       subtitle=&quot;Period range: March 16, 2016 to June 16, 2016&quot;,
       caption=&quot;Source: NPR http://n.pr/1USSliN&quot;) +
  theme_hrbrmstr_an(grid=&quot;XY&quot;) +
  theme(plot.margin=margin(10,10,10,10)) -> gg

#+ per-day, dev=&quot;png&quot;, fig.width=8, fig.height=5, fig.retina=2, message=FALSE, echo=FALSE, warning=FALSE
gg

count(arms, manufacturer) %>%
  filter(!is.na(manufacturer)) %>%
  arrange(desc(n)) %>%
  select(Manufacturer=manufacturer, Count=n) %>%
  datatable() %>%
  formatCurrency(columns=&quot;Count&quot;, currency=&quot;&quot;)

count(arms, caliber) %>%
  filter(!is.na(caliber)) %>%
  arrange(desc(n)) %>%
  select(Caliber=caliber, Count=n) %>%
  datatable() %>%
  formatCurrency(columns=&quot;Count&quot;, currency=&quot;&quot;)</code></pre>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/a-call-to-armslist-data-analysis/"></div></div><div><a data-url="http://www.r-bloggers.com/a-call-to-armslist-data-analysis/" data-counturl="http://www.r-bloggers.com/a-call-to-armslist-data-analysis/" data-count="vertical" data-text="A Call to Arms[list] Data Analysis!" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rud.is/b/2016/06/19/a-call-to-armslist-data-analysis/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ml8osuWQGo8"></a></div>