<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/criminal-goings-on-in-a-random-forest/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://thinkr.biz/2018/03/01/crime-random-forest/"> R – thinkr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Supervised machine learning</h2>
<p>In <a rel="nofollow" href="https://thinkr.biz/2018/01/29/cluster-of-six/" target="_blank">the “cluster of six”</a>, we used <em>unsupervised</em> machine learning, to reveal hidden structure in unlabelled data, and analyse the voting patterns of Labour Members of Parliament. In this blog post, we’ll use <em>supervised</em> machine learning to see how well we can predict crime in London. Perhaps not specific crimes. But we can use <a rel="nofollow" href="https://data.gov.uk/dataset/recorded-crime-summary-data-for-london-borough-level" target="_blank">recorded crime summary data at London borough-level</a>, with some degree of accuracy, to predict crime counts by type and location.</p>
<p>Along the way, we’ll see the pay-off from an exploration of multiple models.</p>
<p><span></span></p>
<p>Why might one want to predict crime counts? Perhaps we have the responsibility to deploy, or plan a budget for, police resources. Maybe we are considering where we might invest in additional CCTV infrastructure. Robust models can support decision-making by providing predictions grounded in facts, and are especially useful where complexity in the data is otherwise harder to unpick.</p>
<h2>No “one size fits all”</h2>
<p>There are a range of modelling techniques available. At one end of the spectrum, we have the more intuitive and interpretable models: Given conditions A and B, then we can anticipate outcome X. At the other end, we have more powerful and complex models where one accepts the hidden nature of their inner machinery in exchange for potentially greater predictive power.</p>
<p>There is no “one size fits all”, and the predictive power of each model will vary depending on the data being modelled.  That alone is a good reason to consider multiple models. And I don’t mind admitting that I encountered another very good reason whilst preparing my five-model analysis for this post.</p>
<p>I almost abandoned the model that ultimately delivered the lowest prediction error. It was because the other models were generating stronger predictions that I questioned my execution of the fifth. The<span> fuller, but by no means exhaustive methodology, including code, is <a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/02/Univariate_Regression.html" target="_blank">available here</a>. </span></p>
<h2>Preliminary exploration</h2>
<p>Building familiarity with the data is an important first step. We’ll begin with 32 mini-plots; one for each London borough. Within each are the crime trends across nine major crime categories. <span>What does this tell us?</span></p>
<p><em>Borough</em> is likely to be a key predictor given the considerable variation in crime counts associated with this categorical variable. Contrast, for example, the vertical scaling for “Westminster” with “Sutton”, in the bottom-right corner.</p>
<p><em>Major crime category</em> will also likely be a key predictor, with “theft &amp; handling”, and “violence against the person”, associated with significantly more crime across all London boroughs.</p>
<p>There is also a possible interplay between borough and crime category which we may need to account for in models sensitive to interaction. This is evident where more affluent boroughs, or those attracting more visitors, such as “Kensington &amp; Chelsea”, and “Westminster”, have significantly higher counts for “theft &amp; handling”. Contrast these boroughs with, for example, “Lewisham”, where “violence against the person” plays a more significant role.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2018/02/Explore-detail.png?w=450&ssl=1" alt="London crime by borough" class="wp-image-709" data-recalc-dims="1"></p>
<p>A summary of each potential predictor also exposes their possible influence, for example, the growth in crime count over time. (I may dedicate a future post to time-series forecasting.)</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2018/02/Explore-summary.png?w=450&ssl=1" alt="Exploratory data analysis" class="wp-image-710" data-recalc-dims="1"></p>
<h2>Do all four potential predictors matter?</h2>
<p>One way to address this question is to use <a rel="nofollow" href="https://en.wikipedia.org/wiki/Recursive_partitioning" target="_blank">recursive partitioning</a> to create a tree diagram. At the top of the tree we have 100% of the over-eleven-thousand observations. The first, and most important split, is based on the major crime category: 23% of the observations are partitioned off to the right (to node 3), for “theft and handling” (abbreviated as T&amp;H) and “violence against the person” (VATP), with the balance branching left.</p>
<p>Similarly, borough appears early in the recursive partitioning where node 3 splits based on this variable.</p>
<p>We could go to lower levels of granularity, but our purpose here is a preliminary assessment of the most important variables. This shows that month is of lesser significance. However, we’ll keep it in our initial modelling to see if it’s significant enough to influence our models’ predictive power.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2018/02/Which-matter.png?w=450&ssl=1" alt="" class="wp-image-712" data-recalc-dims="1"></p>
<h2>Training and testing our models</h2>
<p>Cross validation is a comparatively simple and popular method for estimating errors in predictions.  We’ll use repeated cross-validation to train the models on randomly-selected cuts of the data, and validate them on the remaining cut. This approach is designed to strengthen the models’ ability to perform well on as-yet-unseen observations.</p>
<p>There are many modelling choices we can make to enhance their performance, for example: The initial selection of models; how we pre-process the data; and how we utilise tuning parameters to optimise their performance. The choices made for this post, and I by no means explored every possibility, are discussed in the <a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/02/Univariate_Regression.html" target="_blank">supporting documentation</a>.</p>
<p>For the purposes of this article, we’ll jump to assessing the models’ predictions versus the known actuals to see how they performed.</p>
<h2>Comparing predictive power</h2>
<p>Optimal predictions sit close to, or on, the dashed line in the graphic below, i.e. where the prediction for each observation equals the actual. The <a rel="nofollow" href="https://en.wikipedia.org/wiki/Root-mean-square_deviation" target="_blank">Root Mean Squared Error</a> (RMSE) measures the average differences, so should be as small as possible. And R-squared measures the correlation between prediction and actual, where 0 reflects no correlation, and 1 perfect positive correlation.</p>
<p>Our supervised machine learning outcomes from the <a rel="nofollow" href="https://en.wikipedia.org/w/index.php?title=Classification_and_regression_tree&redirect=no" target="_blank">CART</a> and <a rel="nofollow" href="https://en.wikipedia.org/wiki/Generalized_linear_model" target="_blank">GLM</a> models have weaker RMSEs, and visually exhibit some dispersion in the predictions at higher counts. <a rel="nofollow" href="https://en.wikipedia.org/wiki/Gradient_boosting#Stochastic_gradient_boosting" target="_blank">Stochastic Gradient Boosting</a>, Cubist and <a rel="nofollow" href="https://en.wikipedia.org/wiki/Random_forest" target="_blank">Random Forest</a> have handled the higher counts better as we see from the visually tighter clustering.</p>
<p>It was Random Forest that produced marginally the smallest prediction error. And it was a parameter unique to the Random Forest model which almost tripped me up as discussed in the <a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/02/Univariate_Regression.html" target="_blank">supporting documentation</a>.</p>
<p>The moral of the story reinforces the value of exploring multiple models. One can’t be certain which is best adapted to the data in hand. And model comparison also provides a very helpful check and balance from which the ultimate outcome may be all the stronger.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i0.wp.com/thinkr.biz/wp-content/uploads/2018/02/Plot-performance.png?w=450&ssl=1" alt="Comparison of five regression models" class="wp-image-711" data-recalc-dims="1"></p>
<h2>R toolkit</h2>
<div>
<table>
<thead>
<tr>
<th> </th>
<th><strong>Packages</strong></th>
<th><strong>Functions</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2017/10/Program-e1509478672361.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-135" data-recalc-dims="1" height="100"></td>
<td>purrr</td>
<td>map_df</td>
</tr>
<tr>
<td><img src="https://i0.wp.com/thinkr.biz/wp-content/uploads/2017/10/Import-e1509478685299.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-134" data-recalc-dims="1" height="100"></td>
<td>readr</td>
<td>read_csv</td>
</tr>
<tr>
<td rowspan="13"><img src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2017/10/Transform-e1509478657749.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-136" data-recalc-dims="1" height="100"></td>
<td>dplyr</td>
<td>mutate; select; group_by; summarise; filter; rename</td>
</tr>
<tr>
<td>tidyr</td>
<td>separate; gather</td>
</tr>
<tr>
<td>stringr</td>
<td>str_c; str_pad; str_replace_all; str_wrap; str_detect</td>
</tr>
<tr>
<td>lubridate</td>
<td>ymd</td>
</tr>
<tr>
<td>tibble</td>
<td>data_frame</td>
</tr>
<tr>
<td>rpart</td>
<td>rpart</td>
</tr>
<tr>
<td>rpart.plot</td>
<td>prp</td>
</tr>
<tr>
<td>gbm</td>
<td>gbm</td>
</tr>
<tr>
<td>ranger</td>
<td>ranger</td>
</tr>
<tr>
<td>cubist</td>
<td>cubist</td>
</tr>
<tr>
<td>caret</td>
<td>trainControl; train; varImp</td>
</tr>
<tr>
<td>broom</td>
<td>tidy</td>
</tr>
<tr>
<td>modelr</td>
<td>gather_residuals; spread_predictions; rmse; rsquare</td>
</tr>
<tr>
<td rowspan="3"><img src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2017/10/Visualize-e1509478640644.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-137" data-recalc-dims="1" height="100"></td>
<td>ggplot2</td>
<td>geom_line; geom_smooth; facet_wrap; geom_point; geom_abline; geom_hline; geom_text; geom_col</td>
</tr>
<tr>
<td>ggthemes</td>
<td>theme_economist; economist_pal</td>
</tr>
<tr>
<td>gridExtra</td>
<td>grid.arrange</td>
</tr>
</tbody>
</table>
</div>
<p></p>
<p><a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/02/Univariate_Regression.html" target="_blank">View the code here</a>.</p>
<h3>Citations / Attributions</h3>
<p>Contains public sector information licensed under the <a rel="nofollow" href="http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/" target="_blank">Open Government Licence v3.0</a>.</p>
<p>R Development Core Team (2008). R: A language and environment for<br>
statistical computing. R Foundation for Statistical Computing,<br>
Vienna, Austria. ISBN 3-900051-07-0, URL <a rel="nofollow" href="http://www.r-project.org/" target="_blank">http://www.R-project.org</a>.</p>
<p>Max Kuhn and Ross Quinlan (2017). Cubist: Rule- And Instance-Based Regression Modeling. R package version 0.2.1. <a rel="nofollow" href="https://cran.r-project.org/package=Cubist" target="_blank">https://CRAN.R-project.org/package=Cubist</a></p>
<p>Terry Therneau, Beth Atkinson and Brian Ripley (2017). rpart: Recursive Partitioning and Regression Trees. R package version 4.1-11.<br>
<a rel="nofollow" href="https://cran.r-project.org/package=rpart" target="_blank">https://CRAN.R-project.org/package=rpart</a></p>
<p>Marvin N. Wright, Andreas Ziegler (2017). ranger: A Fast Implementation of Random Forests for High Dimensional Data in C++ and R. Journal of Statistical Software, 77(1), 1-17. doi:10.18637/jss.v077.i01</p>
<p>Greg Ridgeway with contributions from others (2017). gbm: Generalized Boosted Regression Models. R package version 2.1.3. <a rel="nofollow" href="https://cran.r-project.org/package=gbm" target="_blank">https://CRAN.R-project.org/package=gbm</a></p>
<p>Max Kuhn. Contributions from Jed Wing, Steve Weston, Andre Williams, Chris Keefer, Allan Engelhardt, Tony Cooper, Zachary Mayer, Brenton Kenkel, the R Core Team, Michael Benesty, Reynald Lescarbeau, Andrew Ziem, Luca Scrucca, Yuan Tang, Can Candan and Tyler Hunt. (2017). caret: Classification and Regression Training. R package version 6.0-78. <a rel="nofollow" href="https://cran.r-project.org/package=caret" target="_blank">https://CRAN.R-project.org/package=caret</a></p>
<p>The post <a rel="nofollow" href="https://thinkr.biz/2018/03/01/crime-random-forest/" target="_blank">Criminal goings-on in a random forest</a> appeared first on <a rel="nofollow" href="https://thinkr.biz/" target="_blank">thinkr</a>.</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="https://thinkr.biz/2018/03/01/crime-random-forest/"> R – thinkr</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/6j9Grv5olGA" height="1"></a></div>