<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/new-zealand-fatal-traffic-crashes-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/10/15/traffic-crashes"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Traffic fatalities in the news</h2>
<p>Fatalities from traffic crashes have been in the news in New Zealand recently, for <a rel="nofollow" href="http://www.nzherald.co.nz/tags/road-accidents/663/" target="_blank">tragic reasons</a>. For once, data beyond the short term have come into the debate. Sam Warburton has written <a rel="nofollow" href="http://www.interest.co.nz/opinion/90323/sam-warburton-welcomes-public-debate-over-road-toll-bemoans-it-has-taken-horrific" target="_blank">two good articles</a> on the topic, with good consideration of the data. He has also, via Twitter, <a rel="nofollow" href="https://twitter.com/Economissive/status/918690224867196928" target="_blank">made his compilation of NZTA and other data and his considerable workings available</a>.</p>
<p>Traffic, and crashes in particular, are characterised by large volumes of high quality data. Sam Warburton’s workings can be supplemented with a range of data from the NZTA. I know little about the subject and thought it seemed a good one to explore. This is a long blog post but I still feel I’ve barely touched the surface. I nearly split this into a series of posts but decided I won’t get back to this for months at least, so best to get down what I fiddled around with straight away.</p>
<h2>Long term</h2>
<p>I always like to see as long term a picture as I can in a new area. In one of the tabs in Sam Warburton’s Excel book is a table with annual data back to 1950. Check this out:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-longterm.svg"></p>
<p>Interesting!</p>
<p>Here’s the R code that draws that graph, plus what’s needed in terms of R packages for the rest of the post:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>mapproj</span><span>)</span><span>
</span><span>library</span><span>(</span><span>proj4</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggmap</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggthemes</span><span>)</span><span> </span><span># for theme_map
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forcats</span><span>)</span><span>
</span><span>library</span><span>(</span><span>viridis</span><span>)</span><span>
</span><span>library</span><span>(</span><span>leaflet</span><span>)</span><span>
</span><span>library</span><span>(</span><span>htmltools</span><span>)</span><span>
</span><span>library</span><span>(</span><span>broom</span><span>)</span><span>
</span><span>library</span><span>(</span><span>openxlsx</span><span>)</span><span>
</span><span>library</span><span>(</span><span>forecastHybrid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>rstan</span><span>)</span><span>
</span><span>#=================long term picture========================
</span><span>longterm</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>&quot;../data/vkt2.1.xlsx&quot;</span><span>,</span><span> </span><span>sheet</span><span> </span><span>=</span><span> </span><span>&quot;safety (2)&quot;</span><span>,</span><span> </span><span>cols</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>3</span><span>),</span><span> </span><span>rows</span><span> </span><span>=</span><span> </span><span>2</span><span>:</span><span>69</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>longterm</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>Year</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Deaths</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Road deaths in New Zealand 1950 - 2016&quot;</span><span>,</span><span>
</span><span>&quot;Not adjusted for population or vehicle kilometres travelled&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: NZTA data compiled by Sam Warburton&quot;</span><span>)</span></code></pre>
</figure>
<h2>Spatial elements</h2>
<p>There have been a few charts going around of crashes and fatalities over time aggregated by region, but I was curious to see a more granular picture. It turns out the NZTA <a rel="nofollow" href="https://www.nzta.govt.nz/safety/safety-resources/road-safety-information-and-tools/disaggregated-crash-data/" target="_blank">publish a CSV of all half a million recorded crashes (fatal and non-fatal) back to 2000</a>. The data is cut back a bit – it doesn’t have comments on what may have caused the crash for example, or the exact date – but it does have the exact NZTM coordinates of the crash location, which we can convert to latitude and longitude and use for maps.</p>
<p>So I started as always with the big picture – where have all the fatal crashes been since 2000?</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-all-nz.svg"></p>
<p>Unsurprisingly, they are heavily concentrated in the population centres and along roads. What happens when we look at this view over time?</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-map-over-time.svg"></p>
<p>So, turns out a map isn’t a great way to see subtle trends. In fact, I doubt there’s an effective simple visualisation that would show changing spatial patterns here; it’s definitely a job for a sophisticated model first, followed by a visualisation of its results. Outside today’s scope.</p>
<p>A map <em>is</em> a good way to just get to understand what is in some data though. Here are another couple of variables the NZTA provide. First, whether the accidents happened on a public holiday:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-map-by-holiday.svg"></p>
<p>… and combination of vehicles they involved:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-map-by-multi.svg"></p>
<p>Here’s the code for downloading the individual crash data, converting coordinates from NZTM to latitude and longitude, and building those maps. This is a nice example of the power of the <code>ggplot2</code> universe. Once I’ve defined my basic map and its theme, redrawing it with new faceting variables is just one line of code in each case.</p>
<figure>
<pre><code data-lang="r"><span>#============download data and tidy up a bit======================
# The individual crash data come from # https://www.nzta.govt.nz/safety/safety-resources/road-safety-information-and-tools/disaggregated-crash-data/
</span><span>
</span><span># caution, largish file - 27 MB
</span><span>download.file</span><span>(</span><span>&quot;https://www.nzta.govt.nz/assets/Safety/docs/disaggregated-crash-data.zip&quot;</span><span>,</span><span>
</span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;disaggregated-crash-data.zip&quot;</span><span>,</span><span> </span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span>unzip</span><span>(</span><span>&quot;disaggregated-crash-data.zip&quot;</span><span>)</span><span>
</span><span>crash</span><span> </span><span>&lt;-</span><span> </span><span>read.csv</span><span>(</span><span>&quot;disaggregated-crash-data.csv&quot;</span><span>,</span><span> </span><span>check.names</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>dim</span><span>(</span><span>crash</span><span>)</span><span> </span><span># 586,189 rows
</span><span>
</span><span># I prefer lower case names, and underscores rather than dots:
</span><span>names</span><span>(</span><span>crash</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot; &quot;</span><span>,</span><span> </span><span>&quot;_&quot;</span><span>,</span><span> </span><span>str_trim</span><span>(</span><span>tolower</span><span>(</span><span>names</span><span>(</span><span>crash</span><span>))),</span><span> </span><span>fixed</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span># Convert the NZTM coordinates to latitude and longitude:
# see https://gis.stackexchange.com/questions/20389/converting-nzmg-or-nztm-to-latitude-longitude-for-use-with-r-map-library
</span><span>p</span><span>4</span><span>str</span><span> </span><span>&lt;-</span><span> </span><span>&quot;+proj=tmerc +lat_0=0.0 +lon_0=173.0 +k=0.9996 +x_0=1600000.0 +y_0=10000000.0 +datum=WGS84 +units=m&quot;</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>proj4</span><span>::</span><span>project</span><span>(</span><span>crash</span><span>[</span><span> </span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;easting&quot;</span><span>,</span><span> </span><span>&quot;northing&quot;</span><span>)],</span><span> </span><span>proj</span><span> </span><span>=</span><span> </span><span>p</span><span>4</span><span>str</span><span>,</span><span> </span><span>inverse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>crash</span><span>$</span><span>longitude</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>$</span><span>x</span><span>
</span><span>crash</span><span>$</span><span>latitude</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>$</span><span>y</span><span>
</span><span>#====================NZ maps========================
</span><span>my_map_theme</span><span> </span><span>&lt;-</span><span> </span><span>theme_map</span><span>(</span><span>base_family</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>strip.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>rel</span><span>(</span><span>1</span><span>),</span><span> </span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>),</span><span>
</span><span>plot.caption</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>))</span><span> </span><span>m</span><span> </span><span>&lt;-</span><span> </span><span>crash</span><span> </span><span>%>%</span><span>
</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>fatal_count</span><span> </span><span>></span><span> </span><span>0</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>longitude</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>latitude</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>fatal_count</span><span>))</span><span> </span><span>+</span><span>
</span><span>borders</span><span>(</span><span>&quot;nz&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>NA</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_map</span><span>()</span><span> </span><span>+</span><span>
</span><span>my_map_theme</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.9</span><span>,</span><span> </span><span>0.05</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>&quot;Fatalities&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: NZTA Disaggregated Crash Data&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Fatal crashes in New Zealand, 2000 - 2017&quot;</span><span>)</span><span>
</span><span>m</span><span>
</span><span>m</span><span> </span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span>~</span><span>crash_year</span><span>)</span><span>
</span><span>m</span><span> </span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span>~</span><span>multi_veh</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span>
</span><span>m</span><span> </span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span>~</span><span>holiday</span><span>)</span><span> </span></code></pre>
</figure>
<h2>Closer up maps</h2>
<p>I live in New Zealand’s capital, Wellington, and was interested in a closer view of accidents in there. <code>ggmap</code> makes it easy to do this with a street map in the background:</p>
<p><img src="https://i0.wp.com/ellisp.github.io/img/0113-wellington.png?w=100%25" data-recalc-dims="1"></p>
<figure>
<pre><code data-lang="r"><span>wtn</span><span> </span><span>&lt;-</span><span> </span><span>get_map</span><span>(</span><span>&quot;Wellington, New Zealand&quot;</span><span>,</span><span> </span><span>maptype</span><span> </span><span>=</span><span> </span><span>&quot;roadmap&quot;</span><span>,</span><span> </span><span>zoom</span><span> </span><span>=</span><span> </span><span>11</span><span>)</span><span>
</span><span>ggmap</span><span>(</span><span>wtn</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>longitude</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>latitude</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>crash</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>my_map_theme</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Crashes in Wellington, 2000 to 2017&quot;</span><span>,</span><span>
</span><span>&quot;(Including non-fatal)&quot;</span><span>)</span></code></pre>
</figure>
<p>But clearly there’s a limit to how many small, static maps I’ll want to make. So I made an interactive version covering the whole country:</p>
<p><iframe width="450" src="http://ellisp.github.io/img/0113-crashes.html"></iframe></p>
<p>You can zoom in and out, move the map around, and hover or click on points to get more information. <a rel="nofollow" href="http://ellisp.github.io/img/0113-crashes.html" target="_blank">Full screen version also available.</a></p>
<h2>Is there a trend over time?</h2>
<p>Much of the public discussion is about whether there is significant evidence of the recent uptick in deaths being more than the usual year-on-year variation. The most thorough discussion was in Thomas Lumley’s <a rel="nofollow" href="https://www.statschat.org.nz/2017/10/13/road-deaths-up/" target="_blank">StatsChat</a>. He fit a state space model to the fatality counts, allowing latent tendency for fatalities per kilometres travelled to change at random over time as a Markov chain, with extra randomness each year on top of the randomness coming from a Poisson distribution of the actual count of deaths. He concluded that there is strong evidence of a minimum some time around 2013 or 2014, and growth since then.</p>
<p>To keep the Bayesian modelling part of my brain in practice I set out to repeat and expand this, using six monthly data rather than annual. The disaggregated crash data published by NZTA has both calendar year and financial year for each crash, which we can use to deduce the six month period it was in. The vehicle kilometres travelled in Sam Warburton’s data is at a quarterly grain, although it looks like the quarters are rolling annual averages.</p>
<p>First, to get that data into shape and check out the vehicle kilometres travelled data. I decided to divide the original numbers by four on the assumption they are rolling annual averages to convert them to a quarterly grain. I might be missing something, but it looked to me that the kilometres travelled data stop in first quarter of 2016, so I forecast forward another couple of years using the <code>forecastHybrid</code> R package as a simple way to get an average of ARIMA and exponential state space smoothing models. The forecast looks like this:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-vkt.svg"></p>
<figure>
<pre><code data-lang="r"><span>#------------------vehicle kilometres travelled--------------------------
</span><span>vkt</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>&quot;../data/vkt2.1.xlsx&quot;</span><span>,</span><span> </span><span>sheet</span><span> </span><span>=</span><span> </span><span>&quot;VKT forecasts (2)&quot;</span><span>,</span><span>
</span><span>cols</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>rows</span><span> </span><span>=</span><span> </span><span>6</span><span>:</span><span>62</span><span>,</span><span> </span><span>colNames</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>/</span><span> </span><span>4</span><span>
</span><span>vkt_ts</span><span> </span><span>&lt;-</span><span> </span><span>ts</span><span>(</span><span>vkt</span><span>$</span><span>X</span><span>1</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2001</span><span>,</span><span> </span><span>4</span><span>),</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span>
</span><span>vkt_mod</span><span> </span><span>&lt;-</span><span> </span><span>hybridModel</span><span>(</span><span>vkt_ts</span><span>,</span><span> </span><span>models</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;ae&quot;</span><span>))</span><span>
</span><span>vkt_fc</span><span> </span><span>&lt;-</span><span> </span><span>forecast</span><span>(</span><span>vkt_mod</span><span>,</span><span> </span><span>h</span><span> </span><span>=</span><span> </span><span>8</span><span>)</span><span>
</span><span>autoplot</span><span>(</span><span>vkt_fc</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Vehicle kilometres travelled per quarter in New Zealand, 2001 to 2017&quot;</span><span>,</span><span>
</span><span>&quot;Forecasts from 2016 second quarter are an average of auto.arima and ets&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Billions of vehicle kilometres travelled&quot;</span><span>,</span><span>
</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;Prediction\nintervals&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: NZTA data compiled by Sam Warburton;\nForecasts by Peter's Stats Stuff&quot;</span><span>)</span></code></pre>
</figure>
<p>Then there’s a bit of mucking about to get the data in a six monthly state and ready for modelling but it wasn’t too fiddly. I adapted <a rel="nofollow" href="https://gist.github.com/tslumley/b73d54d1505a3d4478998c79ef271d39" target="_blank">Professor Lumley’s model</a> to Stan (which I’m more familiar with than JAGS which he used) to a six monthly observations. Here’s the end result</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-model-results.svg"></p>
<p>Consistent with Professor Lumley, I find that 88% of simulations show the first half of 2017 (where my data stops) having a higher latent crash rate per km travelled than the second half of 2013.</p>
<p>Fitting a model like this takes three steps:</p>
<ul>
<li>data management in R</li>
<li>model fitting in Stan</li>
<li>presentation of results in R</li>
</ul>
<p>First, here’s the code for the two R stages. The trick here is that I make one column with <code>year</code> (eg 2005) and one called <code>six_month_period</code> which is <code>a</code> for the first six months of the year, and <code>b</code> for the rest. I do this separately for the vehicle kilometres travelled (which needs to be aggregated up from quarterly) and the deaths (which needs to be aggregated up from individual, taking advantage of the fact that we know both calendar and financial years in which it occurred).</p>
<figure>
<pre><code data-lang="r"><span>#==================combining vkt and crashes============
</span><span>vkt_df</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>vkt</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>vkt</span><span>[</span><span> </span><span>,</span><span>1</span><span>,</span><span> </span><span>drop</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>],</span><span> </span><span>vkt_fc</span><span>$</span><span>mean</span><span>),</span><span>
</span><span>t</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>time</span><span>(</span><span>vkt_ts</span><span>),</span><span> </span><span>time</span><span>(</span><span>vkt_fc</span><span>$</span><span>mean</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>six_month_period</span><span> </span><span>=</span><span> </span><span>as.character</span><span>(</span><span>ifelse</span><span>(</span><span>t</span><span> </span><span>%%</span><span> </span><span>1</span><span> </span><span></span><span> </span><span>0.4</span><span>,</span><span> </span><span>&quot;a&quot;</span><span>,</span><span> </span><span>&quot;b&quot;</span><span>)),</span><span>
</span><span>year</span><span> </span><span>=</span><span> </span><span>as.integer</span><span>(</span><span>t</span><span> </span><span>%/%</span><span> </span><span>1</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>year</span><span>,</span><span> </span><span>six_month_period</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>vkt</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>vkt</span><span>))</span><span>
</span><span>crash_six_month</span><span> </span><span>&lt;-</span><span> </span><span>crash</span><span> </span><span>%>%</span><span>
</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>fatal_count</span><span> </span><span>></span><span> </span><span>0</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>fin_year_end</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>str_sub</span><span>(</span><span>crash_fin_year</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>-4</span><span>)),</span><span>
</span><span>six_month_period</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>fin_year_end</span><span> </span><span>==</span><span> </span><span>crash_year</span><span>,</span><span> </span><span>&quot;a&quot;</span><span>,</span><span> </span><span>&quot;b&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>year</span><span> </span><span>=</span><span> </span><span>crash_year</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>year</span><span>,</span><span> </span><span>six_month_period</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>fatal_count</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>fatal_count</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>left_join</span><span>(</span><span>vkt_df</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;year&quot;</span><span>,</span><span> </span><span>&quot;six_month_period&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>deaths_per_billion_vkt</span><span> </span><span>=</span><span> </span><span>fatal_count</span><span> </span><span>/</span><span> </span><span>vkt</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>year</span><span> </span><span>></span><span> </span><span>2001</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>year</span><span>,</span><span> </span><span>six_month_period</span><span>)</span><span>
</span><span>dpbv_ts</span><span> </span><span>&lt;-</span><span> </span><span>ts</span><span>(</span><span>crash_six_month</span><span>$</span><span>deaths_per_billion_vkt</span><span>,</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2002</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span># high autocorrelation, as we'd expect:
</span><span>ggtsdisplay</span><span>(</span><span>dpbv_ts</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Crash deaths per billion vehicle kilometres travelled, six monthly aggregation&quot;</span><span>)</span><span> </span><span># fit an ARIMA model just to see what we get:
</span><span>auto.arima</span><span>(</span><span>dpbv_ts</span><span>)</span><span> </span><span># ARIMA(1,1,0)(0,0,1)[2] with drift
</span><span>
</span><span>#===============bayesian model=====================
</span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>
</span><span>n</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>crash_six_month</span><span>),</span><span>
</span><span>deaths</span><span> </span><span>=</span><span> </span><span>crash_six_month</span><span>$</span><span>fatal_count</span><span>,</span><span>
</span><span>vkt</span><span> </span><span>=</span><span> </span><span>crash_six_month</span><span>$</span><span>vkt</span><span>
</span><span>)</span><span>
</span><span># fit model
</span><span>stan_mod</span><span> </span><span>&lt;-</span><span> </span><span>stan</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;0113-crashes.stan&quot;</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>,</span><span> </span><span>control</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>adapt_delta</span><span> </span><span>=</span><span> </span><span>0.99</span><span>,</span><span> </span><span>max_treedepth</span><span> </span><span>=</span><span> </span><span>15</span><span>))</span><span>
</span><span># extract and reshape results:
</span><span>mu</span><span> </span><span>&lt;-</span><span> </span><span>t</span><span>(</span><span>as.data.frame</span><span>(</span><span>extract</span><span>(</span><span>stan_mod</span><span>,</span><span> </span><span>&quot;mu&quot;</span><span>)))</span><span>
</span><span>mu_gathered</span><span> </span><span>&lt;-</span><span> </span><span>mu</span><span> </span><span>%>%</span><span>
</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>period</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>time</span><span>(</span><span>dpbv_ts</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>run</span><span>,</span><span> </span><span>mu</span><span>,</span><span> </span><span>-</span><span>period</span><span>)</span><span>
</span><span>mu_summarised</span><span> </span><span>&lt;-</span><span> </span><span>mu_gathered</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>period</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>low80</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>mu</span><span>,</span><span> </span><span>0.1</span><span>),</span><span>
</span><span>high80</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>mu</span><span>,</span><span> </span><span>0.9</span><span>),</span><span>
</span><span>low95</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>mu</span><span>,</span><span> </span><span>0.025</span><span>),</span><span>
</span><span>high95</span><span> </span><span>=</span><span> </span><span>quantile</span><span>(</span><span>mu</span><span>,</span><span> </span><span>0.975</span><span>),</span><span>
</span><span>middle</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>mu</span><span>))</span><span>
</span><span># chart results:
</span><span>mu_gathered</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>period</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.02</span><span>,</span><span> </span><span>aes</span><span>(</span><span>group</span><span> </span><span>=</span><span> </span><span>run</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>mu</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_ribbon</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>mu_summarised</span><span>,</span><span> </span><span>aes</span><span>(</span><span>ymin</span><span> </span><span>=</span><span> </span><span>low80</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>high80</span><span>),</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>,</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>mu_summarised</span><span>,</span><span> </span><span>aes</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>middle</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>crash_six_month</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>year</span><span> </span><span>+</span><span> </span><span>(</span><span>six_month_period</span><span> </span><span>==</span><span> </span><span>&quot;b&quot;</span><span>)</span><span> </span><span>*</span><span> </span><span>0.5</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>deaths_per_billion_vkt</span><span>),</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>21</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>4</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Latent rate of deaths per billion km&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: data from NZTA and Sam Warburton;
Analysis by Peter's Stats Stuff&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;State space model of crash deaths in New Zealand&quot;</span><span>,</span><span>
</span><span>&quot;Six monthly observations&quot;</span><span>)</span><span>
</span><span>mu_gathered</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>period</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>2017.0</span><span>,</span><span> </span><span>2013.5</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>mean</span><span>(</span><span>mu</span><span>[</span><span>period</span><span> </span><span>==</span><span> </span><span>2017.0</span><span>]</span><span> </span><span>></span><span> </span><span>mu</span><span>[</span><span>period</span><span> </span><span>==</span><span> </span><span>2013.5</span><span>]))</span><span> </span><span>#</span><span> </span><span>88</span><span>%</span></code></pre>
</figure>
<p>…and here’s the model specification in Stan:</p>
<figure>
<pre><code data-lang="stan">// save as 0113-crashes.stan

// Simple Bayesian model for traffic deaths in New Zealand at six monthly intervals
// Peter Ellis, 15 October 2017

// State space model with random walk in the latent variable and actual measurements
// are log normal - Poisson combination.
// Adapted from Thomas Lumley's model for the same data (but annual) at 
// https://gist.github.com/tslumley/b73d54d1505a3d4478998c79ef271d39


data {
  int n;          // number of six-monthly observations
  int deaths[n];  // deaths (actual count), six month period
  real vkt[n];    // vehicle kilometres travelled in billions
}

parameters {
  real mu1;            // value of mu in the first period
  real delta[n - 1];  // innovation in mu from period to period
  vector[n] epsilon;         // the 'bonus' deviation in each period
  real &lt;lower=0> tau;   // variance of delta
  real &lt;lower=0> sigma; // variance of epsilon
  
}

transformed parameters {
  real mu[n];         // the amount to multiply km travelled by to get lambda.  In other words, death per billion vkt
  real lambda[n];    // lambda is the expected mean death count in any year
  
  mu[1] = mu1;
  for(i in 2:n) mu[i] = mu[i - 1] * exp(delta[ i - 1] * tau);
  
  for(i in 1:n)  lambda[i] = mu[i] * vkt[i] * exp(epsilon[i] * sigma);
  
}

model {
  // priors
  mu1 ~ gamma(2, 1.0 / 10);
  tau ~ gamma(1, 1);
  sigma ~ gamma(1, 1);
  
  // innovation each period:
  delta ~ normal(0, 1);
  
  // bonus deviation each period:
  epsilon ~ normal(0, 1);
  
  //measurement
  deaths ~ poisson(lambda);
}</code></pre>
</figure>
<h2>Objects hit during crashes</h2>
<p>Finally, there were a few things I spotted along the way I wanted to explore.</p>
<p>Here is a graphic of the objects (excluding other vehicles and pedestrians) hit during crashes.</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-factors.svg"></p>
<figure>
<pre><code data-lang="r"><span>crash</span><span> </span><span>%>%</span><span>
</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>fatal_count</span><span> </span><span>></span><span> </span><span>0</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>fin_year_end</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>str_sub</span><span>(</span><span>crash_fin_year</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>-4</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>fin_year_end</span><span>,</span><span> </span><span>post_or_pole</span><span>:</span><span>other</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>id</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>n</span><span>())</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>cause</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>fin_year_end</span><span>,</span><span> </span><span>-</span><span>id</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>fin_year_end</span><span>,</span><span> </span><span>cause</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>prop_accidents</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>value</span><span> </span><span>></span><span> </span><span>0</span><span>)</span><span> </span><span>/</span><span> </span><span>length</span><span>(</span><span>unique</span><span>(</span><span>id</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>cause</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;_&quot;</span><span>,</span><span> </span><span>&quot; &quot;</span><span>,</span><span> </span><span>cause</span><span>),</span><span>
</span><span>cause</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>cause</span><span>,</span><span> </span><span>-</span><span>prop_accidents</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>fin_year_end</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>prop_accidents</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>se</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>cause</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Proportion of accidnets with this as a factor\n&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Factors relating to fatal crashes 2000 - 2017&quot;</span><span>,</span><span>
</span><span>&quot;Ordered from most commonly recorded to least&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: NZTA disaggregated crash data&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Year ending June&quot;</span><span>)</span></code></pre>
</figure>
<h2>Regional relationship with holidays</h2>
<p>I expected there to be a relationship between region and holidays – some regions would see a higher proportion of their accidents in particular periods than others. I was only able to scratch the surface of this, as everything else, of course.</p>
<p>I started by creating a three-way cross tab of year, region and holiday (four holidays plus “none”). I used the frequency count in each cell of this cross tab as the response in a generalized linear model and tested for an interaction effect between region and holiday. It’s there but it’s marginal; a Chi square test shows up as definitely significant (p value &lt; 0.01), but on other criteria the simpler model could easily be the better one.</p>
<p>I extracted the interaction effects and presented them in this chart:<br>
<img width="100%" src="http://ellisp.github.io/img/0113-glm-holidays.svg"></p>
<p>So we can see, for example, that the West Coast (on the South Island) has a particularly strong Christmas time effect (expected); Gisborne gets disproportionate rate of its fatal accidents on Labour Day Weekend (not expected, by me anyway); and so on. The horizontal lines indicate 95% confidence intervals.</p>
<p>To check this wasn’t out to lunch, I also did the simpler calculation of just looking at what percentage of each region’s fatal accidents were on each of the four holidays:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0113-props-holidays.svg"></p>
<p>This is much easier to interpret and explain!</p>
<p>Here’s the code for that region-holiday analysis:</p>
<figure>
<pre><code data-lang="r"><span>#=========================changing relationship between holidays and numbers==============
</span><span>
</span><span>crash_counts</span><span> </span><span>&lt;-</span><span> </span><span>crash</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>crash_fin_year</span><span>,</span><span> </span><span>lg_region_desc</span><span>,</span><span> </span><span>holiday</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>fatal_count</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>fatal_count</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>lg_region_desc</span><span> </span><span>!=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>holiday</span><span> </span><span>=</span><span> </span><span>relevel</span><span>(</span><span>holiday</span><span>,</span><span> </span><span>&quot;None&quot;</span><span>))</span><span>
</span><span>mod1</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>fatal_count</span><span> </span><span>~</span><span> </span><span>crash_fin_year</span><span> </span><span>+</span><span> </span><span>lg_region_desc</span><span> </span><span>+</span><span> </span><span>holiday</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;poisson&quot;</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>crash_counts</span><span>)</span><span>
</span><span>mod2</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>fatal_count</span><span> </span><span>~</span><span> </span><span>crash_fin_year</span><span> </span><span>+</span><span> </span><span>lg_region_desc</span><span> </span><span>*</span><span> </span><span>holiday</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;poisson&quot;</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>crash_counts</span><span>)</span><span>
</span><span># marginal choice between these two models - there's a lot of extra degrees of freedom
# used up in mod2 for marginal improvement in deviance. AIC suggests use the simpler # model; Chi square test says the interaction effect is &quot;significant&quot; and use the complex one.
</span><span>anova</span><span>(</span><span>mod1</span><span>,</span><span> </span><span>mod2</span><span>,</span><span> </span><span>test</span><span> </span><span>=</span><span> </span><span>&quot;Chi&quot;</span><span>)</span><span>
</span><span>AIC</span><span>(</span><span>mod1</span><span>,</span><span> </span><span>mod2</span><span>)</span><span>
</span><span># tidy up coefficients:
</span><span>cfs</span><span> </span><span>&lt;-</span><span> </span><span>tidy</span><span>(</span><span>mod2</span><span>)</span><span>
</span><span>cfs</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>grepl</span><span>(</span><span>&quot;:&quot;</span><span>,</span><span> </span><span>term</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>extract</span><span>(</span><span>term</span><span>,</span><span> </span><span>into</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;region&quot;</span><span>,</span><span> </span><span>&quot;holiday&quot;</span><span>),</span><span> </span><span>regex</span><span> </span><span>=</span><span> </span><span>&quot;(.+):(.+)&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>region</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;lg_region_desc&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>region</span><span>),</span><span>
</span><span>holiday</span><span> </span><span>=</span><span> </span><span>gsub</span><span>(</span><span>&quot;holiday&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>holiday</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>estimate</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>region</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>p.value</span><span> </span><span></span><span> </span><span>0.05</span><span>,</span><span> </span><span>as.character</span><span>(</span><span>region</span><span>),</span><span> </span><span>&quot;&quot;</span><span>)))</span><span> </span><span>+</span><span>
</span><span>geom_vline</span><span>(</span><span>xintercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_segment</span><span>(</span><span>aes</span><span>(</span><span>yend</span><span> </span><span>=</span><span> </span><span>region</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>estimate</span><span> </span><span>-</span><span> </span><span>2</span><span> </span><span>*</span><span> </span><span>std.error</span><span>,</span><span> </span><span>xend</span><span> </span><span>=</span><span> </span><span>estimate</span><span> </span><span>+</span><span> </span><span>2</span><span> </span><span>*</span><span> </span><span>std.error</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey20&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>3.5</span><span>,</span><span> </span><span>nudge_y</span><span> </span><span>=</span><span> </span><span>0.34</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Distinctive region - holiday interactions from a statistical model&quot;</span><span>,</span><span>
</span><span>&quot;Higher values indicate the holiday has a higher relative accident rate in that particular region than in Auckland&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: NZTA Disaggregated Crash Data&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Estimated interaction effect in a generalized linear model with Poisson response:
frequency ~ year + holiday * region&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>holiday</span><span>)</span><span>
</span><span>crash</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>lg_region_desc</span><span> </span><span>!=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>lg_region_desc</span><span>,</span><span> </span><span>holiday</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>fatal_count</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>fatal_count</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>lg_region_desc</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>share</span><span> </span><span>=</span><span> </span><span>fatal_count</span><span> </span><span>/</span><span> </span><span>sum</span><span>(</span><span>fatal_count</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>holiday</span><span> </span><span>!=</span><span> </span><span>&quot;None&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>share</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lg_region_desc</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>holiday</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>&quot;\nProportion of all fatalities in region that occur in this holiday&quot;</span><span>,</span><span>
</span><span>label</span><span> </span><span>=</span><span> </span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: NZTA Disaggregated Crash Data&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Proportion of each regions fatalities on various holidays&quot;</span><span>,</span><span>
</span><span>&quot;This simpler calculation is designed as a check on the modelled results in the previous chart&quot;</span><span>)</span><span>
</span><span>#==========clean up===========
</span><span>unlink</span><span>(</span><span>&quot;disaggregated-crash-data.zip&quot;</span><span>)</span><span>
</span><span>unlink</span><span>(</span><span>&quot;disaggregated-crash-data.csv&quot;</span><span>)</span></code></pre>
</figure>
<p>That will do for now. Interesting data, very unsatisfactory state of the world.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ellisp.github.io/blog/2017/10/15/traffic-crashes"> Peter's stats stuff - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/T3x6qcjaqZY" height="1"></a></div>