<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Make money from data - CYBAEA.NET</title>
<link>https://www.cybaea.net/Journal/#category=R</link>
<atom:link href="https://www.cybaea.net/Journal/index-r.xml" rel="self" type="application/rss+xml"/>
<description>Make money from data - CYBAEA.NET</description>
<language>en-GB</language>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Mon, 10 Jul 2023 23:00:00 GMT</lastBuildDate>
<item>
  <title>Launching Data Navigator: the essential benchmarking tool for independent schools</title>
  <dc:creator>Allan Engelhardt</dc:creator>
  <link>https://www.cybaea.net/Journal/2023/07/11/Launching-Data-Navigator-for-Schools.html</link>
  <description><![CDATA[ 






<p>Most of our <a href="https://www.barnett-waddingham.co.uk/MDA/">team at Barnett Waddingham</a> spent the first seven months of 2023 building and launching <a href="https://www.barnett-waddingham.co.uk/data-navigator/">Data Navigator for Schools</a> (<abbr title="Data Navigator for Schools">DN4S</abbr>). This is a large application built using R and <a href="https://shiny.posit.co/">Shiny</a> and deployed on an in-house docker infrastructure.</p>
<p>Developed in partnership with the Independent Schools’ Bursars Association (<a href="https://www.theisba.org.uk/"><abbr title="Independent Schools' Bursars Association">ISBA</abbr></a>) the tool provides <strong>financial and operational benchmarking with scenario planning</strong> for the sector across the UK and its <span title="The Crown Dependencies are the Bailiwick of Jersey, the Bailiwick of Guernsey and the Isle of Man">Crown Dependencies</span>.</p>
<p>Watch the video introduction by John Murphie of ISBA and <a href="../../../../About/Allan-Engelhardt.html">Allan Engelhardt</a> below:</p>
<video controls="" class="w-100 d-block mb-3" poster="/images/DN4S/data-navigator-intro-image.png" preload="auto">
<source src="https://mediadatanavigator.blob.core.windows.net/media/data-navigator-intro.webm" type="video/webm">
<source src="https://mediadatanavigator.blob.core.windows.net/media/data-navigator-intro.mp4" type="video/mp4">
<p>Download <a href="https://mediadatanavigator.blob.core.windows.net/media/data-navigator-intro.webm">WEBM</a> or <a href="https://mediadatanavigator.blob.core.windows.net/media/data-navigator-intro.mp4">MP4</a> videos. </p>
<p>ISBA says:</p>
<blockquote class="blockquote">
<p>ISBA is delighted to launch the Data Navigator project with Barnett Waddingham for schools. This piece of work is intended to give schools a subscription service on which they can compare their financial performance to other schools of their type both locally and nationally. The information will build up over years and trends will be able to be identified, with policy set and decisions made to accommodate or accentuate the trends. John Murphie from ISBA and Allan Engelhardt from Barnett Waddingham explain exactly how it works.</p>
</blockquote>
<p>There is much more information about the tool on the dedicated website:</p>
<p><a href="https://datanavigator.barnett-waddingham.co.uk/" class="btn btn-primary rounded">Data Navigator for Schools</a></p>



</video> ]]></description>
  <category>Sector:Education</category>
  <category>R</category>
  <category>R-Shiny</category>
  <category>BW</category>
  <guid>https://www.cybaea.net/Journal/2023/07/11/Launching-Data-Navigator-for-Schools.html</guid>
  <pubDate>Mon, 10 Jul 2023 23:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/DN4S/data-navigator-intro-image.png" medium="image" type="image/png" height="82" width="144"/>
</item>
<item>
  <title>Training dates for Marketing Analytics course announced</title>
  <link>https://www.cybaea.net/Journal/2017/05/31/Training-Dates-for-Marketing-Analytics-announced.html</link>
  <description><![CDATA[ 






<p><strong>Classes 3-7 July 2017</strong>: We have updated our very popular training course to take advantage of the latest Microsoft Advanced Analytics technology and re-launched it as Marketing Analytics using Microsoft R and Azure. We have just announced public training dates and are looking forward to giving the course in the week 3-7 July 2017. This is a live, instructor-led course in an online classroom environment. There are class times to suit everyone; participants in <a href="https://www.eventbrite.co.uk/e/marketing-analytics-using-microsoft-r-and-azure-europe-middle-east-and-asia-tickets-34705103900">Middle East and Asia register here</a> for live classes 08:00-12:00 London time while participants in <a href="https://www.eventbrite.co.uk/e/marketing-analytics-using-microsoft-r-and-azure-europe-and-americas-tickets-34705341611">North and South America register here</a> for classes 10:00-14:00 New York time. Please see the listings for additional time zones. <em>This course has consistently achieved 100% positive promoter scores both on recommending the course and recommending the instructor.</em></p>
<p>This intensive course gives you hands-on experience using Microsoft R and Azure covering the key techniques for analysing customer data for Sales and Marketing. The focus is on getting to the business results and you will return to your organization with the skills you need to deliver and demonstrate commercial impact from your work.</p>
<p>For more information and detailed syllabus, please see the course page: <a href="../../../../Training/Marketing-Analytics-using-Microsoft-Advanced-Analytics/">Marketing Analytics using Microsoft R and Azure</a></p>
<p>We are offering the course in partnership with Microsoft (following their acquisition of Revolutions); you can see <a href="http://learnanalytics.microsoft.com/home/training/?filter=Partner-89">our listing on Microsoft’s Learn Analytics portal</a>.</p>
<p>Hope to see you there! If you have any questions, just <a href="../../../../Contact/">contact us</a>.</p>



 ]]></description>
  <category>business</category>
  <category>data science</category>
  <category>R</category>
  <category>training</category>
  <guid>https://www.cybaea.net/Journal/2017/05/31/Training-Dates-for-Marketing-Analytics-announced.html</guid>
  <pubDate>Tue, 30 May 2017 23:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/wide/MAUR2-2017-1100w.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>What is wrong with lift curves</title>
  <link>https://www.cybaea.net/Journal/2016/10/31/What-is-wrong-with-lift-curves.html</link>
  <description><![CDATA[ 






<p>The first part of our <a href="../../../../go/MAUR/">Marketing Analytics Using R</a> course covers campaign analysis with test- and control groups and campaign optimisation using lift curves and predicted responses. Among the many topics covered, we discuss what is wrong with lift curves. They are a standard tool in marketing to select a target group for a campaign based on predicted response propensity, but they way they are used is wrong, or at least sub-optimal.</p>
<h2 id="lift-curves-and-the-wrong-way-to-use-them" class="anchored">Lift curves and the wrong way to use them</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/CYBAEA-Marketing-Analytics-Halloween-1088w.jpg" class="img-fluid figure-img"></p>
<figcaption>Marketing Analytics Using R training on Halloween</figcaption>
</figure>
</div>
<p>This time, the first day of the course was Halloween. But we didn’t have any special spooky data sets or exercises, just the usual programme of an introduction to marketing and analytics in marketing followed by an introduction to set up and evaluate direct marketing campaigns with practical exercises using the R platform for statistical computing and data visualisation.</p>
<p>Lift curves are often used in marketing. You first create a model that predict the probability that a given customer will respond positively to your campaign. A positive response usually means they buy something, but you might have other objectives such as getting more visits to your web site, have your customers refer friends to become your customers, or other outcomes. The model is created by considering the responses from previous campaigns or by comparing test and control groups from a trial. In <a href="../../../../go/MAUR/">the course</a> we show how to create the model using generalised linear models (glm), decision trees (<a href="https://cran.r-project.org/package=rpart">rpart</a>, <a href="https://cran.r-project.org/package=party">party</a>/<a href="https://cran.r-project.org/package=partykit">partykit</a>), and ensembles of trees, primarily generalised boosted regression models (<a href="https://cran.r-project.org/web/packages/gbm/">gbm</a>) but also forests of trees (<a href="https://cran.r-project.org/package=randomForest">randomForest</a>) (which we are somehow discouraged from calling <a href="https://www.stat.berkeley.edu/~breiman/RandomForests/">Random Forests</a> due to trademark problems). These are of course not all the model types you could consider, but we are limited by the duration of the course and in practise they work well for a wide class of problems. By consistently using the <a href="https://cran.r-project.org/package=caret">caret</a> package we give the students a scalable toolkit for exploring other model approaches.</p>
<p>Once you have a model that predicts the probability of a positive response, you score your customer base (or the subset eligible for the campaign) and sort the list by the probability from high to low. The cumulative sum of probabilities to <var>n</var> gives you the expected sales from contacting <var>n</var> customers, and the line of this is the <dfn>lift curve<dfn>.</dfn></dfn></p>
<p>The way marketing people tend to use it is that they have a budget for <var>N</var> contacts, for example direct mailings, and they read off the curve how many responses they are going to get. Or if they need <var>M</var> responses (typically sales), they can find the number of contacts needed by starting on <var>M</var> on the y-axis and finding the corresponding <var>N</var> on the x-axis.</p>
<p>Simple.</p>
<p>And <strong><span class="text-danger">wrong</span></strong>.</p>
<h2 id="the-right-way-to-do-it" class="anchored">The right way to do it</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/01-01-Lost-net-sales-small.png" class="img-fluid figure-img"></p>
<figcaption>The lost sales from using the traditional lift curve approach; in this example it amounts to nearly 20% less sales in the worst case.</figcaption>
</figure>
</div>
<p>Or at least the approach is not optimal in the setting where the product you are selling (or the behaviour you are encouraging) is readily available to all. The problem is that some of the people who buy are people who would buy anyhow, without your marketing effort, so you are wasting some of your budget. You want to target the customers whose behaviour you are most likely to influence. It should not be a surprise that some of the people who purchased during your test campaign, the test campaign you are using to create the model, would have bought from you anyhow.</p>
<p>You have both a treatment and a control group and it seems a real shame not to use the data from both. The treatment group obviously gives you the model for the propensity to buy.</p>
<p>We teach our students to make a baseline model from the control group to predict who would buy without any stimuli. The difference in the probability from the two models is the <dfn>net response probability</dfn> and this is the number you want to sort from high to low to decide on your campaign list.</p>
<p>The two lessons are</p>
<ol type="1">
<li><p>Never throw away good data. In this case, do not ignore the response from the control group.</p></li>
<li><p>The outcome of marketing activities is to <em>change</em> human behaviour, so make sure you model the change from your efforts and realise that this is not the same as the overall behaviour. You are not the centre of the universe.</p></li>
</ol>
<h2 id="learn-more" class="anchored">Learn more</h2>
<p>If you want to learn more, consider signing up for our training course <a href="../../../../go/MAUR/">Marketing Analytics Using R</a> or feel free to <a href="../../../../Contact/">contact us</a> for an informal conversation.</p>



 ]]></description>
  <category>marketing</category>
  <category>business</category>
  <category>R</category>
  <category>training</category>
  <guid>https://www.cybaea.net/Journal/2016/10/31/What-is-wrong-with-lift-curves.html</guid>
  <pubDate>Mon, 31 Oct 2016 00:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/wide/CYBAEA-Marketing-Analytics-Halloween-1100w.png" medium="image" type="image/png" height="52" width="144"/>
</item>
<item>
  <title>How do we know that marketing works?</title>
  <link>https://www.cybaea.net/Journal/2016/10/17/How-do-we-know-that-marketing-works.html</link>
  <description><![CDATA[ 






<p>How do we know that marketing works? It was <a href="https://nationalpoetryday.co.uk/">National Poetry Day</a> in Britain recently, and I do believe that poetry and imagination, and the wisdom and insights it can bring, has a place in business and corporate world. But <a href="../../../../Journal/2011/01/03/Why.html">wisdom isn’t knowledge</a>; it does not immediately convince in the way that facts presented as a coherent story does. So when I am asked “how do we know marketing works?” the question usually means “how do we collect facts and present them in a persuasive narrative that will convince the board?”</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/wide/experiment-knowledge-2200w.jpg" class="img-fluid figure-img"></p>
<figcaption>Experiment is the only means of knowledge at our disposal. Everything else is poetry, imagination. (Max Planck)</figcaption>
</figure>
</div>
<p>My background is the hard science of experimental particle physics at <a href="https://www.cern.ch/">CERN</a> where nothing is discovered unless you have replicated the experiment with at least <a href="https://blogs.scientificamerican.com/observations/five-sigmawhats-that/">five sigma</a> significance. Max Planck is one of the founders of this science, so when he says that <q>experiment is the only means of knowledge</q> I am with him. It is in my DNA.</p>
<p>The beauty of Marketing is that in many, many cases we can indeed experiment and gain knowledge that way. We go through all the basics both concepts and practical with hands-on exercises on real data in our <a href="../../../../go/MAUR/">Marketing Analytics Using R</a> training course.</p>
<p>Consider a <dfn>direct marketing</dfn> campaign, that is, a message or a series of messages directed to identified individuals with the objective of changing their behaviour. It could be delivered classically as addressed postal letters, emails, telephone calls, and so on, or as targeted web site promotions or social media campaigns but to identified users. The key is that you know who gets to see the message (at least initially; we’ll leave the effect of sharing and viral campaigns to the <a href="../../../../go/MAUR/">course</a>). This is sometimes known as ‘below the line’ (BTL) advertising.</p>
<p>This is different from what we might call <dfn>advertising</dfn>, that is broad messages to groups of individuals. You might think classical TV, radio, or newspaper ads, or broad banner ads, google ads, paid-for blog posts, and so on. While the target group may be identified, e.g.&nbsp;by the TV they watch, magazines they read, or web sites they visit, the <em>individual</em> is not known, only the aggregate group characteristics. Old school marketing managers may call this ‘above the line’ (ATL) advertising.</p>
<p>For direct marketing the key concept is that of <dfn>test and control groups</dfn>. If you come from a digital marketing background then you may know this as ‘A/B testing’. The basic idea is that you generate your target list of individuals to whom you want to send your message and then take a <em>ramdom</em> sample of that list away as your control group to which you do not communicate. If the two groups are really comparable in terms of the types of individuals in them, and they should be if you selected a true random, unbiased sample, and the only difference in your treatment of the two groups is sending or not sending the marketing message, then any difference in future behaviour, such as different spending patters, <em>must</em> be down to your campaign. <strong>This is science</strong>, this is experiment, and Planck would be proud. There are some subtleties to make sure you select the groups right (such as <a href="http://www.evanmiller.org/how-not-to-run-an-ab-test.html">no peeking</a> of the results during the experiment) but not much. It is simple, it is effective, and it works.</p>
<p>If you are not using robust test and control groups for your direct marketing campaigns, then start now. Today. Get measuring. Then stop all the campaigns that produce a negative result.</p>
<p>Yes, <em>negative result</em>; campaigns that make customers <em>less</em> likely to buy, <em>more</em> likely to defect to the competition, or <em>more</em> needing of (expensive and unproductive) support from you. We call it Stop Doing Stupid Stuff. At <a href="../../../../Case-Studies/">client after client</a> the quick win has more often than not been to Stop Doing Stupid Stuff. Don’t beat yourself up if you have some campaigns with negative returns; I can assure you that you are not alone. It is very easy to become so hung up on your great ideas about what <em>should</em> work that you forget to question if it really <em>does</em> work. Imagination can be useful, but to enhance our understanding of what is real, not instead of it. Just figure out today what real and what is imagines, and then stop the campaigns with negative outcomes. Get the facts.</p>
<p>In our <a href="../../../../Training/Marketing-Analytics-using-R/">course</a> syllabus we first establish the principles of test and control groups, then we introduce lift curves and consider the difference between gross and net responses before moving on to modelling campaign profitability and optimising your marketing spend: when you have multiple campaigns to run, which one(s) do you execute on which targeted individuals to maximise your profits (or other business objectives)?</p>
<blockquote class="blockquote">
<p>
“Understanding the <b>reasons</b> for churn, devising the right (effective) churn prevention <b>offers</b>, and <b>measuring</b> (and continue to measure) their effectiveness is usually by far the hardest part of churn management.”
</p>
</blockquote>
<p>By comparison, the actual churn model is usually quite easy to develop.</p>
<p>Using the same principles we cover <dfn>churn models</dfn> to address the business problem of customers leaving you. Some people get confused: what does test and control groups have to do with churn? After all, you don’t choose who will leave you. But then <a href="../../../../Journal/2011/01/04/Commercial-churn-modelling.html">you are doing churn modelling wrong</a>. You typically do not want to predict churn, as in the probability that an individual will leave you. Say you did do this model, and say it was a great model. Now what are you going to do? The model does not directly address the possible business actions or the decisions that the business need to make. Typically, what you want to do is to offer some sort of incentive to say to some of the customers who are likely to leave. So the decision is: for each individual customer, given the range of possible retention incentives that I have at my disposal, would offering one of these incentives at this time to this customer increase my overall profitability? You might frame this question as ‘what is the expected change in customer lifetime value from presenting each of these offers at this time?’ which is something that you can usefully model. You need the cost of the offers, the propensity to accept it, the baseline customer lifetime value, and the change in spend and tenure or loyalty given the offer.</p>
<p>Sure, it is more complicated, but it is <em>useful</em> in a way that the simple ‘Will this customer churn?’ model is not. We wrote more on <a href="../../../../Journal/2011/01/04/Commercial-churn-modelling.html">commercial churn modelling</a> and we cover it in practical detail in the <a href="../../../../go/MAUR/">Marketing Analytics Using R</a> training course.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/What-Is-Marketing-Analytics-2200w.png" class="img-fluid figure-img"></p>
<figcaption>What is Marketing Analytics slide</figcaption>
</figure>
</div>
<p>Our philosophy is simple. The focus is always, or should be, on the business actions. <em>Models do not make money</em>, changing the way you do business may. Start with the end in mind, measure the right outcomes, and directly model the required business decisions, and you should be fine. But it is easy to get absorbed in the data and modelling and we have plenty of examples of that. Business understanding is key to successful Marketing Analytics.</p>
<p>We do not usually go into details on the statistics. Typically in the problems with which we deal in Marketing Analytics, we have thousands if not millions of records. Thousands of customers, tens or hundreds of thousands of purchases or transactions, or millions of web site visits. A signal from thousands of observations will usually be statistically significant, at least in the naive sense, and with an emphasis on experimentation that is normally good enough. So we spend more time on feature selection and model optimisation.</p>
<p>Feature selection is important on the wide data sets that are typical of our problems, and creating the right (derived) features is very often the key to developing a successful model. The small response rates and wide data sets means that we often use modern modelling techniques that are extremely powerful but may make interpretation more difficult. We compare simple and complex models in the course and show how to optimise feature selection and model performance using the <a href="https://cran.r-project.org/web/packages/caret/"><code>caret</code></a> package and framework. (The course instructor is an acknowledged contributor to the <a href="https://cran.r-project.org/web/packages/caret/"><code>caret</code></a> package.)</p>
<h2 id="marketing-analytics-using-r-training-course" class="anchored">Marketing Analytics Using R training course</h2>
<p>Learn more about our <a href="../../../../go/MAUR/">Marketing Analytics Using R</a> training course. You can also <a href="../../../../Contact/">Contact us</a> to register your interest and receive more information on this course, and we will let you know the next time we run a public class. If you have several colleagues who are interested then we can also run a course just for you.</p>



 ]]></description>
  <category>marketing</category>
  <category>business</category>
  <category>R</category>
  <guid>https://www.cybaea.net/Journal/2016/10/17/How-do-we-know-that-marketing-works.html</guid>
  <pubDate>Sun, 16 Oct 2016 23:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/wide/projects-1100w.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Marketing Analytics Using R Training Course 31 October - 04 November 2016</title>
  <link>https://www.cybaea.net/Journal/2016/10/03/Marketing-Analytics-Using-R-Training.html</link>
  <description><![CDATA[ 






<p><strong>Back by popular demand!</strong> We have been asked many times when we would run again the Marketing Analytics Using R training course. We used to do it with <a href="http://blog.revolutionanalytics.com/">Revolution Analytics</a> which is now part of Microsoft but due to <a href="http://www.cybaea.net/go/BG/">client-side commitments</a> we have not had a public course for 2 years.</p>
<p>That has now changed. We are pleased to announce new dates for this popular course with the next training class scheduled for 31 October - 04 November 2016 at a time that could suit participants from Dubai to San Fransisco.</p>
<p>The format of the course is 5 half-day live tutorials delivered using an online classroom platform. This format is chosen so students still have time to do their day-jobs. This means 20 hours of face-time with the teacher plus the opportunity to dive deeper into the technical aspects through homework exercises for which the instructor is available by email or chat.</p>
<p><strong>Register here</strong>: <a href="https://www.eventbrite.co.uk/e/marketing-analytics-using-r-training-course-tickets-28327153278">Event Registration</a>. Note that there is a <strong>generous early bird discount</strong> available.</p>
<p><strong>Read more about the course</strong> including the outline syllabus here: <a href="../../../../Training/Marketing-Analytics-using-R/">Marketing Analytics Using R training course</a></p>
<p>Announcement with some background: <a href="https://www.linkedin.com/pulse/marketing-analytics-using-r-allan-engelhardt">LinkedIn announcement</a></p>
<p>And if you are reading this after this course has finished, <a href="../../../../Contact/">contact us</a> to register your interest and be on the mailing list when a new schedule is announced.</p>



 ]]></description>
  <category>R</category>
  <category>training</category>
  <category>analytics</category>
  <guid>https://www.cybaea.net/Journal/2016/10/03/Marketing-Analytics-Using-R-Training.html</guid>
  <pubDate>Sun, 02 Oct 2016 23:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/wide/MAUR-Course-1-1100w.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Microsoft Analytics in 2016</title>
  <link>https://www.cybaea.net/Journal/2016/06/24/Microsoft-Analytics-in-2016.html</link>
  <description><![CDATA[ 






<p>If you had asked me two years ago if Microsoft was a serious vendor for data science and analytics infrastructure and tools, I would have laughed. At the time their offering seemed to me to consist of Excel against SQL Server. There is nothing really wrong (or exciting) about SQL Server, but friends don’t let friends use Excel for data analysis or indeed for anything that matters at all, so that whole proposition was a non-starter. But things have moved on, so how does Microsoft stack up now (mid-2016)? (You can skip right to our conclusions if you are impatient.)</p>
<p>In 2015 Microsoft acquired Revolution Analytics (<a href="https://blogs.technet.microsoft.com/machinelearning/2015/04/06/microsoft-closes-acquisition-of-revolution-analytics/">MSFT</a>, <a href="http://blog.revolutionanalytics.com/2015/04/revolution-analytics-microsoft.html">Revo</a>) which gave them a serious and scalable tool for data science, visualization, and statistical analysis. This is based on the <a href="http://www.r-project.org/">open source R</a> implementation which has massive mind-share making it easily the most popular serious data science tool, but adds easy scalability and enterprise features.</p>
<p>And they announced integration with Apache Spark (Microsoft Azure HDInsight), Azure Machine Learning, and SQL Server, which suddenly looks like a serious platform. Add a <a href="https://blogs.microsoft.com/blog/2015/01/27/microsoft-brings-business-intelligence-mainstream-enhancements-power-bi/">75% reduction in the price of Power BI</a> (and a reasonable free version) and various other enhancements, and it is looking really interesting.</p>
<p>So is it ready for prime-time? I had the opportunity this week to spend four days with the Microsoft teams to find out. We focused on the R environments and integrations and received plenty of hands-on experience.</p>
<h2 id="solutions-components" class="anchored">Solutions components</h2>
<p>We covered the following solutions components:</p>
<ol type="1">
<li>The scalable <code>rx*</code> model and data processing functions using the XDF file format.</li>
<li>The integration with the <code>dplyr</code> package through <a href="https://github.com/RevolutionAnalytics/dplyrXdf"><code>dplyrXdf</code></a> for streaming data manipulation.</li>
<li>The Apache Spark / HDInsight integration: using essentially the same syntax, but automatically distributed over a Spark cluster.</li>
<li>The Microsoft SQL Server integration: Using the same <code>rx*</code> functions against database tables either from the client or running on the SQL Server.</li>
<li>Running R inside the SQL Server using stored procedures (and <code>sp_execute_external_script</code>).</li>
</ol>
<h3 id="scalable-model-and-data-processing-functions" class="anchored">1. Scalable model and data processing functions</h3>
<p>This is the venerable technology from Revolutions. It provides functions that work transparently on chunks of data which means you have no issues of memory limitations. For data processing the key function is <code>rxDataStep</code> which does all the <a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-data-transform">transformations</a> you would expect and is supported by <code>rxMerge</code> for merging data and <code>rxImport</code> for importing data files. This all works well and was the core offering from Revolutions, along with the XDF file storage format which allows efficient processing of large data.</p>
<p>For data exploration you have comprehensive summaries (including approximate median), contingency tables, and cross tabulations, and there is a small but useful selection of data visualization functions. Models are currently limited to</p>
<ul>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-linear-model">Linear Regression Models</a> (<code>rxLinMod</code>)</li>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-logistic-regression">Logistic Regression</a> (<code>rxLogit</code>)</li>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-generalized-linear-model">Generalized Linear Models</a> (<code>rxGlm</code>)</li>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-decision-tree">Classification and Regression Trees</a> (<code>rxDTree</code>)</li>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-boosting">Stochastic Gradient Boosted Decision Trees</a> (<code>rxBTrees</code>)</li>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-decision-forest">Classification and Regression Decision Forests</a> (<code>rxDForest</code>)</li>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-cluster">K-Means Clustering</a> (<code>rxKmeans</code>)</li>
<li><a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler-user-guide-naive-bayes">Naive Bayes Classifier</a> (<code>rxNaiveBayes</code>)</li>
</ul>
<p>More model types are apparently in the works. I hope to see association rules (the <a href="https://en.wikipedia.org/wiki/Apriori_algorithm">Apriori</a> and Eclat algorithms at least) and anomaly detection (eg one-class support vector machines) at least. I hear rumours that support vector machines (SVM) are coming soon, and of course they provide the infrastructure for you to implement your own models.</p>
<p>Nothing new here compared to the Revolutions offering when you are working on XDF files. What is new is the seamless integration with Spark clusters meaning that you can easily run any of these models on an efficient distributed environment.</p>
<p>The code we went through is here: <a href="https://github.com/Azure/Cortana-Intelligence-Gallery-Content/blob/master/Tutorials/MRS-for-SAS-Users/MRS%20for%20SAS%20Users.md">MRS for SAS Users</a></p>
<h3 id="streaming-data-manipulation-the-dplyr-way" class="anchored">2. Streaming data manipulation the <code>dplyr</code> way</h3>
<p>This is the <code>dplyrXdf</code> package which was <a href="https://blogs.technet.microsoft.com/machinelearning/2016/03/16/the-dplyrxdf-package-bringing-data-munging-with-pipes-to-xdf-files-2/">announced</a> about three months ago. At the moment you have to install it from the <a href="https://github.com/RevolutionAnalytics/dplyrXdf"><code>dplyrXdf</code> GitHub</a> page.</p>
<p>I was skeptical at the start but quickly convinced. If you want to quickly compare the syntax you can see the course material <a href="https://github.com/akzaidi/adv-r-tutorials/blob/master/dplyrXdf.ipynb">here</a>. So you get the <code>dplyr</code> syntax, that’s nice. But you get it distributed on a Spark cluster with (almost) no change of code: that’s awesome! (And you don’t have to keep track of your temporary files: that is sometimes useful.)</p>
<h3 id="microsoft-r-with-spark-on-hdinsight" class="anchored">3. Microsoft R with Spark on HDInsight</h3>
<p>It is easy to set up (though not <em>quite</em> as easy as Amazon EMR yet); the instructions are <a href="https://github.com/akzaidi/adv-r-tutorials/tree/master/Spark">here</a> but note that this is rapidly changing. The quick summary is that it works really well. The key function here, and for the SQL Server integration, is <code>rxSetComputeContext</code> which is a general function that defines where your computation is taking place. On Spark you have <a href="https://azure.microsoft.com/en-gb/documentation/articles/hdinsight-hadoop-r-server-compute-contexts/">many options</a> but you end up with code along the lines of:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">computeContext <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">RxSpark</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">consoleOutput =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb1-2">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nameNode =</span> myNameNode,</span>
<span id="cb1-3">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">port =</span> myPort,</span>
<span id="cb1-4">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">executorCores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, </span>
<span id="cb1-5">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">executorMem =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3g"</span>, </span>
<span id="cb1-6">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">executorOverheadMem =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3g"</span>, </span>
<span id="cb1-7">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">persistentRun =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb1-8">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extraSparkConfig =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--conf spark.speculation=true"</span>)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rxSetComputeContext</span>(computeContext)</span>
<span id="cb1-11"></span>
<span id="cb1-12">linmod <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rxLinMod</span>(tip_pct <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> trip_distance, </span>
<span id="cb1-13">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> taxi_xdf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">blocksPerRead =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<p>There is some good documentation here: here: <a href="https://azure.microsoft.com/en-gb/documentation/articles/hdinsight-hadoop-r-server-overview/">Overview of R Server on HDInsight</a></p>
<p>Bottom line: it is easy to use, it works, it scales. But you still have to write good R code, of course.</p>
<h3 id="microsoft-r-and-sql-server" class="anchored">4. Microsoft R and SQL Server</h3>
<p>For an overview and to really understand the workflow for this and the following component, this document is the best: <a href="https://msdn.microsoft.com/en-gb/library/mt612857.aspx">Data Science End-to-End Walkthrough</a>.</p>
<p>But briefly you can work in two modes. The first is a transparent use of SQL tables as yet another XDF compatible data source, all processed in chunks and on your local machine. Works well, with the obvious limitations of <a href="https://msdn.microsoft.com/en-us/library/mt590948.aspx">data type compatibility</a> etc. The downside is that all the data has to move from the server to the R instance. But the second mode will fix that: with a suitable invocation of our old friend <code>rxSetComputeContext</code> you can move the computation to the database server.</p>
<p>At the moment, at least, the compute context interface is rather clunky and you end up with lots of local files that created as part of the communications with the server. (See the <code>shareDir=</code>{:.language-r} argument of the <a href="https://msdn.microsoft.com/en-us/microsoft-r/scaler/rxinsqlserver"><code>RxInSqlServer</code></a> function.) This is ugly and error-prone.</p>
<p>Currently this only really works with the (Enterprise) SQL Server and not, for example, with the Azure SQL service. Very much work in progress, but give it time.</p>
<h3 id="running-r-as-stored-procedure-in-sql-server" class="anchored">5. Running R as stored procedure in SQL Server</h3>
<p>For its final trick, SQL Server is able to run R directly in the server as a stored procedure or similar. The key function is <a href="https://msdn.microsoft.com/en-GB/library/mt604368.aspx"><code>sp_execute_external_script</code></a> which you can include in stored procedures and similar.</p>
<p>In a typical use case, the analyst builds the model using the <code>rx*</code> model functions described above using one of the standard user interfaces and appropriate compute context. She then stores the (serialized) model in the database and the DBA sets up a stored procedure to calculate the scores for new (eg in a trigger) or changed (eg scheduling a stored procedure) data, or when the model changes. In other words, this is mainly a deployment option. It works well.</p>
<h2 id="conclusions" class="anchored">Conclusions</h2>
<p>There is much to like here. Microsoft is building an infrastructure and services platform that is robust and scalable, and with a good or great partner ecosystem. They are one of the most interesting technology companies in this space, and I am not the only one who is excited for their vision; <a href="https://info.microsoft.com/CO-AAIoT-CNTNT-FY16-07Dec15-GartnerMQ-AdvancedAnalytics-Register.html">Gartner</a> says something similar.</p>
<p><strong>Who is it for?</strong> This is a technical platform for technically savvy data scientists. If you want something easy to use, go to <a href="http://www.oracle.com/technetwork/database/options/advanced-analytics/odm/overview/index.html">Oracle Data Miner</a> where everything is simple and works almost as if by magic, but you sacrifice some control for this convenience. The <a href="http://www.oracle.com/technetwork/database/enterprise-edition/odm-techniques-algorithms-097163.html">Oracle set of parallel algorithms</a> is more comprehensive than the <code>rx*</code> models described above which in the current implementation in particular lack association rules and integration into text mining, and the integration to R in the Oracle database appears more mature and complete.</p>
<p>But Oracle does not have a simple and consistent interface that works across files, databases, and Spark clusters. And in particular they do not have anything really comparable to Azure and the ecosystem of integrated services you find there to help you build your solution fast and cost-effective. In that sense the nearest competitor is Amazon Web Services which has even more integrated services, many of which are more mature. (For example, it is easy to have R Studio Server pre-configured on the EMR Spark service at AWS but a bit of a hack is needed on Azure.) But it does not have the same integration with Microsoft R Server for the large data set models so you will have some more work to use that.</p>
<p>Microsoft is by no means the only play in town, but they are certainly credible right now for your data and data science infrastructure and they are an interesting company to watch.</p>



 ]]></description>
  <category>R</category>
  <category>business</category>
  <category>technology</category>
  <guid>https://www.cybaea.net/Journal/2016/06/24/Microsoft-Analytics-in-2016.html</guid>
  <pubDate>Thu, 23 Jun 2016 23:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/wide/data-mining-01-1100w.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Net Promoter Score confidence intervals 2: maths and stats</title>
  <link>https://www.cybaea.net/Journal/2016/01/15/NPS-confidence-intervals-stats.html</link>
  <description><![CDATA[ 






<p>We looked at a rule of thumb for confidence intervals and what that means for a business manager in our <a href="../../../../Journal/2016/01/04/NPS-confidence-intervals.html">previous post</a>. Now we do the maths, stats, and R code for the practitioner.</p>
<p>The Net Promoter Score (<dfn>NPS</dfn>) is a metric for measuring the loyalty and engagement of a firm’s customers that is extremely popular in across many industries. Originally introduced by Fred Reichheld in a 2003 article in the <cite>Harvard Business Review</cite> (<a href="https://hbr.org/2003/12/the-one-number-you-need-to-grow/">link</a>); the current reference is the book <cite>The Ultimate Question 2.0</cite> by Reichheld and Markey (Amazon paid link <a rel="nofollow" class="affiliate-marked" href="https://amzn.to/3QXA3lZ">UK</a> | <a rel="nofollow" class="affiliate-marked" href="https://amzn.to/3ssyUsq">US</a>).</p>
<p>The score is calculated by asking the firm’s customers a single question, <q>How likely is it that you would recommend our company/product/service to a friend or colleague?</q>, on a scale from 0 (very unlikely) to 10 (very likely). Those customers who respond 9 or 10 are considered <dfn>Promoters</dfn>, those who score 7 or 8 are <dfn>Passives</dfn>, and the rest scoring 0-6 are <dfn>Detractors</dfn>. The Net Promoter Score (<dfn>NPS</dfn>) is then simply the percentage Promoters minus the percentage Detractors, which gives a number between -100 and +100.</p>
<p>Our concern here is estimating the measurement error of the score given the responses to the survey.</p>
<h2 id="maths-and-stats-closed-form-for-the-variance" class="anchored">Maths and Stats: Closed form for the variance</h2>
<div class="d-none">
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CDeclareMathOperator%7B%5CVar%7D%7BVar%7D%0A%5CDeclareMathOperator%7B%5CCov%7D%7BCov%7D%0A%5CDeclareMathOperator%7B%5Csd%7D%7Bsd%7D%0A%5CDeclareMathOperator%7B%5CCI%7D%7BCI%7D%0A"></p>
</div>
<p>Let’s start by deriving the closed form for the variance of the Net Promoter Score. Skip this one if you only want the code.</p>
<p>Let \( n \) be the number of respondents to our survey and let \( p \) and \( d \) be the proportion of respondents who are Promoters and Detractors, respectively. Then</p>
<p><img src="https://latex.codecogs.com/png.latex?%0ANPS%20=%20100%20%5Ctimes%20(%20p%20-%20d%20)%0A"></p>
<p>and by the usual formula for variance we have</p>
<p>\[ \Var(NPS) = 100^2 \times ( \Var(p) + \Var(d) - 2 \times \Cov(p, d) ) \]</p>
<p>where \( \Cov \) denotes the covariance.</p>
<p>We can find the variance and covariance from the simple sample and the <a href="https://en.wikipedia.org/wiki/Categorical_distribution">Categorical Distribution</a> so \( \Var(p) = p \times (1-p) / n \) and \( \Cov(p, d) = {-p} d / n \) where we assume the total population \( N \) of possible respondents is much greater than \( n \). We then get</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%5CVar(NPS)%20&amp;%20=%20100%5E2%20%5Ctimes%20%5Cfrac%7Bp(1-p)%20+%20d(1-d)%20+%202pd%7D%7Bn%7D%20&amp;&amp;%20%5Ctext%7Bor,%7D%5C%5C%5C%5C%5C%5C%5C%5C%0A%5Csd(NPS)%20%20&amp;%20=%20100%20%5Ctimes%20%5Csqrt%7B%5Cfrac%7Bp(1-p)%20+%20d(1-d)%20+%202pd%7D%7Bn%7D%7D%0A%5Cend%7Balign%7D%0A"></p>
<p>where \( \sd \) denotes the standard deviation.</p>
<p>For the special case \( p = d = ⅓ \) (which is one case of a zero NPS) we considered in <a href="../../../../Journal/2016/01/04/NPS-confidence-intervals.html">the previous post</a> and using the normal approximation for the 95% confidence interval \( \CI \approx \pm 1.96\sd \), we get for the full width of the interval</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A%5CCI%20&amp;%20%5Capprox%20%5Cfrac%7B320%7D%7B%5Csqrt%7Bn%7D%7D%20&amp;&amp;%20%5Ctext%7Bso,%7D%20%5C%5C%5C%5C%5C%5C%5C%5C%0An%20%20%20&amp;%20%5Capprox%20%5Cleft(%20320%20/%20%5CCI%20%5Cright)%5E2%0A%5Cend%7Balign%7D%0A"></p>
<p>For a full width confidence interval of 10 or 1, you need 1024 or 102,400 responses, respectively, which gives us the rule of thumb we mentioned in the previous post:</p>
<blockquote class="blockquote">
<p>For the very impatient, a good rule of thumb is that with 1000 responses you (only) know your Net Promoter Score to within 10 points (±5) and it takes fully 100,000 responses to know the score to one point (±0.5).</p>
</blockquote>
<h2 id="practical-implementation-using-r" class="anchored">Practical implementation using R</h2>
<p>Knowing the formula for the variance is all well and good but we are data science people so let’s work with the data to create the confidence intervals from the data in a way that works even when the score is near 100 and which extends to other values you may want to derive from the data. In the code examples below we use the <a href="https://www.r-project.org/">R software environment for statistical computing and graphics</a> but it should be easy to translate to other languages.</p>
<p>To create our confidence intervals we use resampling techniques, specifically the <a href="https://en.wikipedia.org/wiki/Bootstrapping_%28statistics%29">bootstrap</a> method as implemented in the <code>boot</code> library. The code below implements a simple function for bootstrapping the errors.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"boot"</span>)</span>
<span id="cb1-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Function for bootstrapping the sample error estimates</span></span>
<span id="cb1-3">NPS.boot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"basic"</span>) {</span>
<span id="cb1-4">  NPS <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)</span>
<span id="cb1-5">  b   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boot</span>(x, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, i) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NPS</span>(data[i]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R =</span> R)</span>
<span id="cb1-6">  ci  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boot.ci</span>(b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf =</span> conf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> type)</span>
<span id="cb1-7">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NPS</span>(x), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x), ci[[type]][<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]))</span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NPS"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"conf"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>)</span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(res)</span>
<span id="cb1-10">}</span></code></pre></div></div>
<p>In the code, <code>x</code> is a vector of responses to the key NPS question (Likelihood to recommend), <code>R</code> is the number of resamples you want to do, and <code>conf</code> is the confidence interval. We take the confidence interval from the observed quantiles of the distribution as indicated by <code>type = "basic"</code>; see the <code>boot.ci</code> help for other options but this choice is our recommendation.</p>
<p>Then we can use the function simply as <code>NPS.boot(nps$Likelihood.to.Recommend)</code> if <code>nps</code> is our data of survey responses and <code>Likelihood.to.Recommend</code> is the integer 0-10 value from the response to the NPS question. Or, more likely, we can use it to generate confidence intervals for a range of periods, eg months, quarters, or years, as outlined below.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Note that we use the data.table syntax below.</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb2-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Assume nps holds your survey data, one row per response where</span></span>
<span id="cb2-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## - Likelihood.to.Recommend is the 0-10 score which we assume is not NA here</span></span>
<span id="cb2-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## - quarter is the time period over which you want to aggregate, here quarterly</span></span>
<span id="cb2-6">nps[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NPS.boot</span>(Likelihood.to.Recommend), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> quarter]</span></code></pre></div></div>
<p>To try out the code above, we can generate some data</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Note that we use the data.table syntax and its IDate class below.</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20160115</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12e3</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate this many responses</span></span>
<span id="cb3-6">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Response probabilities; true NPS = 0</span></span>
<span id="cb3-7"></span>
<span id="cb3-8">nps <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Likelihood.to.Recommend =</span></span>
<span id="cb3-10">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>L<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>L, n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> p),</span>
<span id="cb3-11">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Response.Date =</span></span>
<span id="cb3-12">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.IDate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2013-01-01"</span>),</span>
<span id="cb3-13">                          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.IDate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2015-12-31"</span>),</span>
<span id="cb3-14">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day"</span>),</span>
<span id="cb3-15">                      n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb3-16"></span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add column `quarter`</span></span>
<span id="cb3-18">nps[, quarter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(Response.Date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quarters"</span>)]</span>
<span id="cb3-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setkey</span>(nps, quarter)</span>
<span id="cb3-20"></span>
<span id="cb3-21">qs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nps[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NPS.boot</span>(Likelihood.to.Recommend), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> quarter]</span>
<span id="cb3-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(qs)</span></code></pre></div></div>
<pre><code>       quarter        NPS    n conf         Low      High
 1: 2013-01-01  0.3211991  934 0.95 -4.81798715  5.674518
 2: 2013-04-01 -0.3036437  988 0.95 -5.36437247  4.655870
 3: 2013-07-01  2.1632252 1017 0.95 -2.85152409  7.079646
 4: 2013-10-01 -0.4935834 1013 0.95 -5.42941757  4.343534
 5: 2014-01-01 -2.9927761  969 0.95 -8.25593395  2.270382
 6: 2014-04-01  0.3992016 1002 0.95 -4.79041916  5.489022
 7: 2014-07-01  1.7329256  981 0.95 -3.46585117  6.931702
 8: 2014-10-01  5.1536174 1009 0.95  0.09910803 10.208127
 9: 2015-01-01 -2.6679842 1012 0.95 -7.80632411  2.371542
10: 2015-04-01 -3.4644195 1068 0.95 -8.33333333  1.404494
11: 2015-07-01  0.1021450  979 0.95 -5.20939734  5.209397
12: 2015-10-01  1.0700389 1028 0.95 -3.98832685  6.031128</code></pre>
<p>Note that, even with 1000 responses per quarter, one of the 95% confidence intervals (number 8) exclude the true value of NPS=0; with 12 periods that is quite reasonable (probability 46%). Note also that we have an average of 1000 responses per quarter and the full width of our confidence intervals are indeed around 10, as per our rule of thumb.</p>
<p>For a simple plot of the data we could use</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb5-2"></span>
<span id="cb5-3">qs.plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(qs, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> quarter, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> NPS, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> Low, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> High)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">atop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quarterly NPS"</span>,</span>
<span id="cb5-9">                          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">atop</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">italic</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"with 95% confidence intervals"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>))))</span>
<span id="cb5-10"></span>
<span id="cb5-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(qs.plot)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/nps-quarter.svg" class="img-fluid figure-img"></p>
<figcaption>Simulated quarterly NPS scores with bootstrapped 95% confidence intervals; red line indicates true population NPS of 0.</figcaption>
</figure>
</div>
<p>Note that there is no true trend in this data. If you think you see a pattern where the first two quarters of the year are low and the second two are high then you are just imagining it. If you think your score fell in Q1 2015 but is now on the mends since Q3, you are just imagining it.</p>
<p>The approach extends trivially to other metrics you want to calculate from the survey data. One of our automotive clients tracked the proportion of ‘negative’ responses 0-4 to the Likelihood to recommend question; negative in the sense that the customer had selected below the middle (5) on the 0-10 scale.</p>
<p>We can do that easily</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"boot"</span>)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb6-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Function for bootstrapping the sample error estimates</span></span>
<span id="cb6-5">negative.boot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"basic"</span>) {</span>
<span id="cb6-6">  neg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)</span>
<span id="cb6-7">  b   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boot</span>(x, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, i) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">neg</span>(data[i]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R =</span> R)</span>
<span id="cb6-8">  ci  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boot.ci</span>(b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf =</span> conf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> type)</span>
<span id="cb6-9">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">neg</span>(x), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x), ci[[type]][<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]))</span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Negative"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"conf"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>)</span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(res)</span>
<span id="cb6-12">}</span>
<span id="cb6-13"></span>
<span id="cb6-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reproducible bootstrap values</span></span>
<span id="cb6-15"></span>
<span id="cb6-16">nqs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nps[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">negative.boot</span>(Likelihood.to.Recommend), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> quarter]</span>
<span id="cb6-17"></span>
<span id="cb6-18">nqs.plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(nqs, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> quarter, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Negative, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> Low, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> High)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_date</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">atop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Proportion of Negative responses"</span>,</span>
<span id="cb6-25">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">atop</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">italic</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"with 95% confidence intervals"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>))))</span>
<span id="cb6-26"></span>
<span id="cb6-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(nqs.plot)</span></code></pre></div></div>
<p>This gives us:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/negative-quarter.svg" class="img-fluid figure-img"></p>
<figcaption>Simulated quarterly ‘negative’ (0-4) scores with bootstrapped 95% confidence intervals; red line indicates true population value of 500/21.</figcaption>
</figure>
</div>
<p>If you have, say, a threshold of 25% for this metric and you only use the mean from the data, you would have had 3 false positives in this three months period. Or if your threshold was 22.5% you would have had 1 false negative.</p>
<h2 id="conclusion" class="anchored">Conclusion</h2>
<p>I hope to see many more plots of NPS with error bars. As we mentioned <a href="../../../../Journal/2016/01/04/NPS-confidence-intervals.html">previously</a>, the score is not really the core of the system; closing the loop with the customers, understanding the root causes, and taking appropriate action <em>is</em> the core. Nevertheless, it is easy for an organization to become somewhat fixated on the score which leads to demands to ‘explain’ every change in the measured value.</p>
<p>Hopefully this analysis will allow the practitioner to have an informed conversation about the magnitude of the change and whether it may just be down to sampling. But note that we do not advocate ignoring the ‘why’ question even when sample sizes are small and the change is not statistically significant: it is extremely valuable to be collecting hypotheses for what may affect the score and try to test these. That too is part of closing the loop.</p>
<p>Feel free to contact us with any questions or comments.</p>



 ]]></description>
  <category>NPS</category>
  <category>statistics</category>
  <category>R</category>
  <guid>https://www.cybaea.net/Journal/2016/01/15/NPS-confidence-intervals-stats.html</guid>
  <pubDate>Fri, 15 Jan 2016 00:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/wide/nps-quarter-wide.svg" medium="image" type="image/svg+xml"/>
</item>
<item>
  <title>R in Insurance 2016</title>
  <link>https://www.cybaea.net/Journal/2016/01/06/R-in-Insurance-2016.html</link>
  <description><![CDATA[ 






<p>The 2016 conference on <a href="https://web.archive.org/web/20160112120545/http://www.cass.city.ac.uk/news-and-events/conferences/r-in-insurance2016">R in Insurance</a> will be held on Monday 11 July 2016. This year we are back at Cass Business School in London, UK. This is the fourth time the conference is held and CYBAEA is proud to have sponsored all four conferences.</p>
<p>The intended audience of the conference includes both academics and practitioners who are active or interested in the applications of R in Insurance.</p>
<p>Attendance of the whole conference is the equivalent of 6.5 hours of CPD for members of the Actuarial Profession.</p>
<p>Sign up here: <a href="https://web.archive.org/web/20160109182724/http://www.cass.city.ac.uk/news-and-events/conferences/r-in-insurance2016/registration-and-fees">R in Insurance 2016: Registration and Fees</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/R-in-Insurance-2016.jpg" class="img-fluid figure-img"></p>
<figcaption>CYBAEA is proud to sponsor R in Insurance 2016</figcaption>
</figure>
</div>
<div class="fw-lighter text-info" style="font-size: smaller;">
<p>
<a href="https://www.flickr.com/photos/98063470@N00/325380948/" rel="nofollow">Image</a> credit: <a href="https://www.flickr.com/photos/98063470@N00/" rel="nofollow">TheFriendlyFiend</a> on <a href="https://www.flickr.com/" rel="nofollow">Flickr</a> under a <a href="https://creativecommons.org/licenses/by-sa/2.0/" rel="nofollow">CC BY-SA 2.0</a> license.
</p>
</div>



 ]]></description>
  <category>R</category>
  <category>insurance</category>
  <category>event</category>
  <guid>https://www.cybaea.net/Journal/2016/01/06/R-in-Insurance-2016.html</guid>
  <pubDate>Wed, 06 Jan 2016 00:00:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/R-in-Insurance-2016.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>R code for Chapter 2 of Non-Life Insurance Pricing with GLM</title>
  <link>https://www.cybaea.net/Journal/2012/03/13/R-code-for-Chapter-2-of-Non_Life-Insurance-Pricing-with-GLM.html</link>
  <description><![CDATA[ 






<p>We continue working our way through the examples, case studies, and exercises of what is affectionately known here as “the two bears book” (Swedish björn = bear) and more formally as <cite>Non-Life Insurance Pricing with Generalized Linear Models</cite> by Esbjörn Ohlsson and Börn Johansson (Amazon paid links <a href="https://amzn.to/3OVJShq" class="affiliate-marked">UK</a> | <a href="https://amzn.to/3ORxiQf" class="affiliate-marked">US</a>).</p>

<div class="no-row-height column-margin column-container"><div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/books/two-bears.jpg" class="img-fluid figure-img"></p>
<figcaption>Amazon paid links <a href="https://amzn.to/3OVJShq" class="affiliate-marked">UK</a> | <a href="https://amzn.to/3ORxiQf" class="affiliate-marked">US</a></figcaption>
</figure>
</div></div><p>At this stage, our purpose is to reproduce the analysis from the book using the <a href="https://www.r-project.org/">R statistical computing and analysis platform</a>, and to answer the data analysis elements of the exercises and case studies. Any critique of the approach and of pricing and modeling in the Insurance industry in general will wait for a later article.</p>
<p>In the following, we will assume that the reader has a copy of the book and a working installation of R. We will be using the <a href="https://cran.r-project.org/package=data.table"><code>data.table</code></a>, <a href="https://cran.r-project.org/package=foreach"><code>foreach</code></a>, and <a href="https://cran.r-project.org/package=ggplot2"><code>ggplot2</code></a> packages which are not part of the standard distribution, so the reader should install them first (e.g.&nbsp;by executing the following line from within an R session:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"foreach"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dependencies =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Code is for an older version of packages
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Note that Hadley broke the API to package <code>ggplot2</code> so the code as it stands here will not work with the latest versions.</strong> This is one reason we normally use the <code>lattice</code> package instead and will take a little while to fix.</p>
</div>
</div>
<p>The beginning of the code is not so important, but here goes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb2-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## PricingGLM-2.r - Code for Chapter 2 of "Non-Life Insurance Pricing with GLM"</span></span>
<span id="cb2-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2012 CYBAEA Limited (http://www.cybaea.net)</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## @book{ohlsson2010non,</span></span>
<span id="cb2-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   title={Non-Life Insurance Pricing with Generalized Linear Models},</span></span>
<span id="cb2-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   author={Ohlsson, E. and Johansson, B.},</span></span>
<span id="cb2-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   isbn={9783642107900},</span></span>
<span id="cb2-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   series={Eaa Series: Textbook},</span></span>
<span id="cb2-10"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   url={http://books.google.com/books?id=l4rjeflJ\_bIC},</span></span>
<span id="cb2-11"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   year={2010},</span></span>
<span id="cb2-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   publisher={Springer Verlag}</span></span>
<span id="cb2-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## }</span></span></code></pre></div></div>
<h2 id="example-2.5-moped-insurance-continued" class="anchored">Example 2.5: Moped insurance continued</h2>
<p>We continue the moped insurance example, and we use the data that we saved in our <a href="../01/R-code-for-Chapter-1-of-Non_Life-Insurance-Pricing-with-GLM.html">Chapter 1 session</a>. The goal is to reproduce Table 2.7 so we start building that as a data frame after loading the data.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Load the data from last.</span></span>
<span id="cb3-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table.1.2"</span>))</span>
<span id="cb3-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table.1.2.RData"</span>)</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"foreach"</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## We are looking to reproduce table 2.7 which we start building here,</span></span>
<span id="cb3-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## adding columns as we go.</span></span>
<span id="cb3-9">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rating.factor =</span></span>
<span id="cb3-11">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vehicle class"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>premiekl)),</span>
<span id="cb3-12">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vehicle age"</span>,   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>moptva)),</span>
<span id="cb3-13">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zone"</span>,          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>zon))),</span>
<span id="cb3-14">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span></span>
<span id="cb3-15">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>premiekl),</span>
<span id="cb3-16">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>moptva),</span>
<span id="cb3-17">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>zon)),</span>
<span id="cb3-18">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<p>We next calculate the duration and number of claims for each level of each rating factor. We also set the contrasts for the levels, using the same idiom as in our <a href="../01-R-code-for-Chapter-1-of-Non_Life-Insurance-Pricing-with-GLM.html">Chapter 1 session</a>. The {foreach} package is convenient to use here, but you can of course do it with a normal loop and a couple of variables.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Calculate duration per rating factor level and also set the</span></span>
<span id="cb4-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## contrasts (using the same idiom as in the code for the previous</span></span>
<span id="cb4-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## chapter). We use foreach here to execute the loop both for its</span></span>
<span id="cb4-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## side-effect (setting the contrasts) and to accumulate the sums.</span></span>
<span id="cb4-5">new.cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb4-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foreach</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rating.factor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"premiekl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"moptva"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zon"</span>),</span>
<span id="cb4-7">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combine =</span> rbind) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%do%</span></span>
<span id="cb4-8">{</span>
<span id="cb4-9">    nclaims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>antskad, table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>[[rating.factor]], sum)</span>
<span id="cb4-10">    sums <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dur, table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>[[rating.factor]], sum)</span>
<span id="cb4-11">    n.levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>[[rating.factor]])</span>
<span id="cb4-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>[[rating.factor]]) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb4-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.treatment</span>(n.levels)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sums, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), ]</span>
<span id="cb4-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">duration =</span> sums, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.claims =</span> nclaims)</span>
<span id="cb4-15">}</span>
<span id="cb4-16">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span>, new.cols)</span>
<span id="cb4-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(new.cols)</span></code></pre></div></div>
<p>Next, we need to build the frequency and severity models separately, as in the discussion at the beginning of section 2.3.4 on page 34.</p>
<h3 id="frequency-model" class="anchored">Frequency model</h3>
<p>Note here the use of the <code>offset()</code> term as opposed to the <code>weights=</code> argument we have used before. The offset is <code>log(dur)</code> because our link function is <code>log</code>.</p>
<p>See <code>help("Insurance", package = "MASS")</code> for a similar example and sections 7.1 (p.&nbsp;189–190) and 7.3 of <cite>Modern Applied Statistics with S</cite> (Amazon paid links <a href="https://amzn.to/3OLrfNe" class="affiliate-marked">UK</a> | <a href="https://amzn.to/3YUobmp" class="affiliate-marked">US</a> for a (very brief) discussion.</p>

<div class="no-row-height column-margin column-container"><div class="d-none d-lg-block">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://m.media-amazon.com/images/I/61jKaGQJytL._SL1360_.jpg" class="img-fluid figure-img"></p>
<figcaption>Amazon paid link <a href="https://amzn.to/46FzphU" class="affiliate-marked">UK</a> | <a href="https://amzn.to/46FxGJg" class="affiliate-marked">US</a></figcaption>
</figure>
</div>
</div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">model.frequency <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> premiekl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> moptva <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> zon <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(dur)),</span>
<span id="cb5-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> poisson)</span>
<span id="cb5-4"></span>
<span id="cb5-5">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>( model.frequency )</span>
<span id="cb5-6">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rels[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] )</span>
<span id="cb5-7">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rels.frequency <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb5-9">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb5-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)])</span></code></pre></div></div>
<h3 id="severity-model" class="anchored">Severity model</h3>
<p>There are a couple of points to note here:</p>
<ol type="1">
<li>We will model using a Gamma distribution for the errors, following the discussion on page 20 and also pages 33-34. Note the point that this is only one of several plausible candidate distributions.</li>
<li>Because we are using the Gamma distribution we need to remove the zero values from the data; we do this using the <code>table.1.2[table.1.2$medskad &gt; 0, ]</code> construct.</li>
<li>To reproduce the values from the book, we use the non-canonical “<code>log</code>” link function even though the canonical function (“<code>inverse</code>”) gives a slightly better fit (residual deviance 5.9 versus 8.0 on 16 degrees of freedom). This follows the approach discussed in Example 2.3 on page 30.</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">model.severity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(medskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> premiekl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> moptva <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> zon,</span>
<span id="cb6-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>[table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>medskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, ],</span>
<span id="cb6-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Gamma</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> antskad)</span>
<span id="cb6-5"></span>
<span id="cb6-6">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>( model.severity )</span>
<span id="cb6-7">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rels[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] )</span>
<span id="cb6-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Aside: For the canonical link function use</span></span>
<span id="cb6-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## rels &lt;- rels[1] / (rels[1] + rels[-1])</span></span>
<span id="cb6-10"></span>
<span id="cb6-11">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rels.severity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb6-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb6-14">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)])</span></code></pre></div></div>
<h3 id="combining-the-models" class="anchored">Combining the models</h3>
<p>Now it is trivial to combine and display the results.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rels.pure.premium <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span>, rels.frequency <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rels.severity)</span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<table class="table">
<thead>
<tr>
<th>
</th>
<th>
rating.factor
</th>
<th>
class
</th>
<th>
duration
</th>
<th>
n.claims
</th>
<th>
rels.frequency
</th>
<th>
rels.severity
</th>
<th>
rels.pure.premium
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
1
</td>
<td>
Vehicle class
</td>
<td>
1
</td>
<td align="right">
9833.20
</td>
<td align="right">
391
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
</tr>
<tr>
<td align="right">
2
</td>
<td>
Vehicle class
</td>
<td>
2
</td>
<td align="right">
8825.10
</td>
<td align="right">
395
</td>
<td align="right">
0.78
</td>
<td align="right">
0.55
</td>
<td align="right">
0.42
</td>
</tr>
<tr>
<td align="right">
11
</td>
<td>
Vehicle age
</td>
<td>
1
</td>
<td align="right">
1918.40
</td>
<td align="right">
141
</td>
<td align="right">
1.55
</td>
<td align="right">
1.79
</td>
<td align="right">
2.78
</td>
</tr>
<tr>
<td align="right">
21
</td>
<td>
Vehicle age
</td>
<td>
2
</td>
<td align="right">
16739.90
</td>
<td align="right">
645
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
</tr>
<tr>
<td align="right">
12
</td>
<td>
Zone
</td>
<td>
1
</td>
<td align="right">
1451.40
</td>
<td align="right">
206
</td>
<td align="right">
7.10
</td>
<td align="right">
1.21
</td>
<td align="right">
8.62
</td>
</tr>
<tr>
<td align="right">
22
</td>
<td>
Zone
</td>
<td>
2
</td>
<td align="right">
2486.30
</td>
<td align="right">
209
</td>
<td align="right">
4.17
</td>
<td align="right">
1.07
</td>
<td align="right">
4.48
</td>
</tr>
<tr>
<td align="right">
3
</td>
<td>
Zone
</td>
<td>
3
</td>
<td align="right">
2888.70
</td>
<td align="right">
132
</td>
<td align="right">
2.23
</td>
<td align="right">
1.07
</td>
<td align="right">
2.38
</td>
</tr>
<tr>
<td align="right">
4
</td>
<td>
Zone
</td>
<td>
4
</td>
<td align="right">
10069.10
</td>
<td align="right">
207
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
</tr>
<tr>
<td align="right">
5
</td>
<td>
Zone
</td>
<td>
5
</td>
<td align="right">
246.10
</td>
<td align="right">
6
</td>
<td align="right">
1.20
</td>
<td align="right">
1.21
</td>
<td align="right">
1.46
</td>
</tr>
<tr>
<td align="right">
6
</td>
<td>
Zone
</td>
<td>
6
</td>
<td align="right">
1369.20
</td>
<td align="right">
23
</td>
<td align="right">
0.79
</td>
<td align="right">
0.98
</td>
<td align="right">
0.78
</td>
</tr>
<tr>
<td align="right">
7
</td>
<td>
Zone
</td>
<td>
7
</td>
<td align="right">
147.50
</td>
<td align="right">
3
</td>
<td align="right">
1.00
</td>
<td align="right">
1.20
</td>
<td align="right">
1.20
</td>
</tr>
</tbody>
</table>
<h2 id="case-study-motorcycle-insurance" class="anchored">Case Study: Motorcycle Insurance</h2>
<p>The authors use the term Case Study for larger exercises. There is quite a lot to cover here, and some of the questions are more about discussing the results. We will focus on getting the results.</p>
<h3 id="the-data" class="anchored">The data</h3>
<h4 id="obtaining-the-data" class="anchored">Obtaining the data</h4>
<p>First we read the (fixed width) data. The column names are the same (based on Swedish language) as in the book.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">columns <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">agarald =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">kon =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zon =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mcklass =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fordald =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>L,</span>
<span id="cb8-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bonuskl =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">duration =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">antskad =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skadkost =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>L)</span>
<span id="cb8-3">column.classes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>,</span>
<span id="cb8-4">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(columns) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(column.classes))</span>
<span id="cb8-6">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">url</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://www2.math.su.se/~esbj/GLMbook/mccase.txt"</span>)</span>
<span id="cb8-7">mccase <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.fwf</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">widths =</span> columns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb8-8">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(columns),</span>
<span id="cb8-9">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> column.classes,</span>
<span id="cb8-10">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.strings =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment.char =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb8-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">close</span>(con), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb8-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(columns, column.classes, con)</span>
<span id="cb8-13">mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mcklass <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordered</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mcklass)</span>
<span id="cb8-14">mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bonuskl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordered</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bonuskl)</span></code></pre></div></div>
<h4 id="adding-meta-data-information" class="anchored">Adding meta-data information</h4>
<p>We are more than a little obsessed with documenting our data, so here goes. See the book for the details.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Title: Partial casco insurance for motorcycles from Wasa, 1994--1998"</span>,</span>
<span id="cb9-3">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: http://www2.math.su.se/~esbj/GLMbook/mccase.txt"</span>,</span>
<span id="cb9-4">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Copyright: http://www2.math.su.se/~esbj/GLMbook/"</span>)</span>
<span id="cb9-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>agarald) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The owner's age, between 0 and 99"</span>,</span>
<span id="cb9-7">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Age of Owner"</span>)</span>
<span id="cb9-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>kon) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Gender of Owner"</span>,</span>
<span id="cb9-10">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: M=Male"</span>,</span>
<span id="cb9-11">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: K=Female"</span>)</span>
<span id="cb9-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>zon) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Geographic Zone"</span>,</span>
<span id="cb9-14">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 1=Central and semi-central parts of Sweden's three largest cities"</span>,</span>
<span id="cb9-15">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 2=suburbs and middle-sized towns"</span>,</span>
<span id="cb9-16">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 3=Lesser towns, except those in 5 or 7"</span>,</span>
<span id="cb9-17">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 4=Small towns and countryside, except 5--7"</span>,</span>
<span id="cb9-18">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 5=Northern towns"</span>,</span>
<span id="cb9-19">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 6=Northern countryside"</span>,</span>
<span id="cb9-20">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 7=Gotland (Sweden's largest island)"</span>)</span>
<span id="cb9-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mcklass) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: MC Class"</span>,</span>
<span id="cb9-23">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Description: A classification by the EV ratio, defined as (engine power in kW × 100) / (vehicle weight in kg + 75), rounded to the nearest lower integer."</span>,</span>
<span id="cb9-24">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 1=EV ratio &lt;= 5"</span>,</span>
<span id="cb9-25">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 2=EV ratio 6--8"</span>,</span>
<span id="cb9-26">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 3=EV ratio 9--12"</span>,</span>
<span id="cb9-27">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 4=EV ratio 13--15"</span>,</span>
<span id="cb9-28">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 5=EV ratio 16--19"</span>,</span>
<span id="cb9-29">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 6=EV ratio 20--24"</span>,</span>
<span id="cb9-30">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 7=EV ratio &gt;= 25"</span>)</span>
<span id="cb9-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fordald) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vehicle age, between 0 and 99"</span>,</span>
<span id="cb9-33">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Vehicle Age"</span>)</span>
<span id="cb9-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bonuskl) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-35">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Bonus Class"</span>,</span>
<span id="cb9-36">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Description: A driver starts with bonus class 1; for each claim-free year the bonus class is increased by 1. After the first claim the bonus is decreased by 2; the driver cannot return to class 7 with less than 6 consecutive claim free years."</span>)</span>
<span id="cb9-37"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Duration"</span>,</span>
<span id="cb9-39">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Comment: The number of policy years"</span>,</span>
<span id="cb9-40">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unit: year"</span>)</span>
<span id="cb9-41"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>antskad) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-42">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Number of Claims"</span>)</span>
<span id="cb9-43"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>skadkost) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb9-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Cost of Claims"</span>,</span>
<span id="cb9-45">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unit: SEK"</span>)</span></code></pre></div></div>
<h4 id="rating-factors" class="anchored">Rating factors</h4>
<p>We finally add the rating factors from the book.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>zon</span>
<span id="cb10-2">mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mcklass</span>
<span id="cb10-3">mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb10-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cut</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fordald, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>),</span>
<span id="cb10-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include.lowest =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb10-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ordered_result =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb10-7">mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ordered</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bonuskl) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Drop comments</span></span>
<span id="cb10-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(mccase<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine levels</span></span>
<span id="cb10-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span></code></pre></div></div>
<h4 id="save-the-data" class="anchored">Save the data</h4>
<p>Never forget to save.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(mccase, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.RData"</span>)</span></code></pre></div></div>
<p>Now we can start tackling the exercises.</p>
<h3 id="problem-1-aggregate-to-cells-of-current-tariff" class="anchored">Problem 1: Aggregate to cells of current tariff</h3>
<blockquote class="blockquote">
<p>Aggregate the data to the cells of the current tariff. Compute the empirical claim frequency and severity at this level.</p>
</blockquote>
<p>We really like the <code>data.table</code> package. The syntax is much easier on the eye (and the hand). As a bonus, it scales well to much larger data sets than <code>data.frame</code>. If you are not already using it, now is the time to start.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase"</span>))</span>
<span id="cb12-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.RData"</span>)</span>
<span id="cb12-3"></span>
<span id="cb12-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Conver to data.table</span></span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb12-6">mccase <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(mccase, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>))</span>
<span id="cb12-7"></span>
<span id="cb12-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Aggregate to levels of current rating factors</span></span>
<span id="cb12-9">mccase.current <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb12-10">    mccase[,</span>
<span id="cb12-11">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">duration =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(duration),</span>
<span id="cb12-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">antskad =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(antskad),</span>
<span id="cb12-13">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skadkost =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(skadkost),</span>
<span id="cb12-14">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">num.policies =</span> .N),</span>
<span id="cb12-15">           by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">key</span>(mccase)]             </span>
<span id="cb12-16"></span>
<span id="cb12-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Claim frequency and severity. Change NaN to NA.</span></span>
<span id="cb12-18">mccase.current<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>claim.freq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb12-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(mccase.current, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(duration <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> duration, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>))</span>
<span id="cb12-20">mccase.current<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>severity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb12-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(mccase.current, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, skadkost <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> antskad, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>))</span>
<span id="cb12-22"></span>
<span id="cb12-23"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Save</span></span>
<span id="cb12-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(mccase.current, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.current.RData"</span>)</span></code></pre></div></div>
<h3 id="problem-2-determine-how-the-duration-and-number-of-claims-is-distributed" class="anchored">Problem 2: Determine how the duration and number of claims is distributed</h3>
<blockquote class="blockquote">
<p>Determine how the duration and number of claims is distributed for each of the rating factor classes, as an indication of the accuracy of the statistical analysis.</p>
</blockquote>
<p>This is one of those ‘there is no right answer’ questions, but let us have a look at the data. First load it if needed and also load the libraries we will be using.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Load data if needed</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb13-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase"</span>))</span>
<span id="cb13-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.RData"</span>)</span>
<span id="cb13-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.current"</span>))</span>
<span id="cb13-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.current.RData"</span>)</span>
<span id="cb13-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is</span>(mccase, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>))</span>
<span id="cb13-8">    mccase <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(mccase, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>))</span>
<span id="cb13-9"></span>
<span id="cb13-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid"</span>)</span>
<span id="cb13-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span></code></pre></div></div>
<h4 id="number-of-claims-antskad" class="anchored">1. Number of claims (antskad)</h4>
<p>Let us start with the number of claims for the undelying data. First we plot the number of policies for each number of claims (range of number of claims is {0, 1, 2}); this is one way to do it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">plot.titles <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geo. zone"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MC class"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vehicle age"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bonus class"</span>)</span>
<span id="cb14-2">plots <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb14-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb14-4">           <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i)</span>
<span id="cb14-5">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mccase, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(antskad))</span>
<span id="cb14-6">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> duration))</span>
<span id="cb14-7">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb14-8">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>()</span>
<span id="cb14-9">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating."</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ~ ."</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>),</span>
<span id="cb14-10">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span>)</span>
<span id="cb14-11">           <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## We drop the axis titles to make more room for the data</span></span>
<span id="cb14-12">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb14-13">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb14-14">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb14-15">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> plot.titles[i])</span>
<span id="cb14-16">           )</span>
<span id="cb14-17"></span>
<span id="cb14-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.newpage</span>()</span>
<span id="cb14-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pushViewport</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)))</span>
<span id="cb14-20"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## We can ignore the warnings from displaying the plots for now</span></span>
<span id="cb14-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb14-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(plots[[i]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout.pos.col =</span> i))</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/files/PricingGLM/PricingGLM-2-1.png" class="img-fluid figure-img"></p>
<figcaption>Exploring the claims distribution across rating factors</figcaption>
</figure>
</div>
<p>There is certainly some variation there, especially on the first rating factor.</p>
<p>Recall from the discussion on page 18 the assumption that the number of claims for a single policy is a Poisson process and the distribution of the number of claims in a tariff cell is Poisson distributed. Let us first look at the whole data set (and note that the <code>data.table</code> notation is used; if using <code>data.frame</code> be sure to have <code>drop=FALSE</code>):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mccase[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(antskad), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> .N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(duration)), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> antskad]</span>
<span id="cb15-2">M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> antskad, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">poisson</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> w, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb15-3">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>predicted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(M, data[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(antskad)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>))</span>
<span id="cb15-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(antskad, N, predicted)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<table class="table">
<thead>
<tr>
<th>
</th>
<th>
antskad
</th>
<th>
n
</th>
<th>
predicted
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
1
</td>
<td align="right">
0
</td>
<td align="right">
63878
</td>
<td align="right">
63878.0
</td>
</tr>
<tr>
<td align="right">
2
</td>
<td align="right">
1
</td>
<td align="right">
643
</td>
<td align="right">
646.0
</td>
</tr>
<tr>
<td align="right">
3
</td>
<td align="right">
2
</td>
<td align="right">
27
</td>
<td align="right">
7.0
</td>
</tr>
</tbody>
</table>
<p>Not too bad, but then it can’t really be with that heavy weighting to the first value of antskad. We can show the same split by the levels of the first rating factor as an example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb16-2">    mccase[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>, antskad),</span>
<span id="cb16-3">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> .N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(duration)),</span>
<span id="cb16-4">           by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>, antskad)]</span>
<span id="cb16-5">modelPoisson <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data) {</span>
<span id="cb16-6">    M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> antskad, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">poisson</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> w, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb16-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(M)</span>
<span id="cb16-8">}</span>
<span id="cb16-9">predictionPoisson <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> (data) {</span>
<span id="cb16-10">    M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">modelPoisson</span>(data)</span>
<span id="cb16-11">    p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(M, data[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(antskad)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)</span>
<span id="cb16-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(p)</span>
<span id="cb16-13">}</span>
<span id="cb16-14">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>predicted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb16-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>),</span>
<span id="cb16-16">                  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> (l) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictionPoisson</span>(data[rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> l]))))</span>
<span id="cb16-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>, antskad, N, predicted)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<table class="table">
<thead>
<tr>
<th>
</th>
<th>
rating.1
</th>
<th>
antskad
</th>
<th>
n
</th>
<th>
predicted
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
1
</td>
<td>
1
</td>
<td align="right">
0
</td>
<td align="right">
8409
</td>
<td align="right">
8409.0
</td>
</tr>
<tr>
<td align="right">
2
</td>
<td>
1
</td>
<td align="right">
1
</td>
<td align="right">
163
</td>
<td align="right">
164.0
</td>
</tr>
<tr>
<td align="right">
3
</td>
<td>
1
</td>
<td align="right">
2
</td>
<td align="right">
10
</td>
<td align="right">
3.0
</td>
</tr>
<tr>
<td align="right">
4
</td>
<td>
2
</td>
<td align="right">
0
</td>
<td align="right">
11632
</td>
<td align="right">
11632.0
</td>
</tr>
<tr>
<td align="right">
5
</td>
<td>
2
</td>
<td align="right">
1
</td>
<td align="right">
157
</td>
<td align="right">
157.0
</td>
</tr>
<tr>
<td align="right">
6
</td>
<td>
2
</td>
<td align="right">
2
</td>
<td align="right">
5
</td>
<td align="right">
2.0
</td>
</tr>
<tr>
<td align="right">
7
</td>
<td>
3
</td>
<td align="right">
0
</td>
<td align="right">
12604
</td>
<td align="right">
12604.0
</td>
</tr>
<tr>
<td align="right">
8
</td>
<td>
3
</td>
<td align="right">
1
</td>
<td align="right">
113
</td>
<td align="right">
113.0
</td>
</tr>
<tr>
<td align="right">
9
</td>
<td>
3
</td>
<td align="right">
2
</td>
<td align="right">
5
</td>
<td align="right">
1.0
</td>
</tr>
<tr>
<td align="right">
10
</td>
<td>
4
</td>
<td align="right">
0
</td>
<td align="right">
24626
</td>
<td align="right">
24626.0
</td>
</tr>
<tr>
<td align="right">
11
</td>
<td>
4
</td>
<td align="right">
1
</td>
<td align="right">
184
</td>
<td align="right">
185.0
</td>
</tr>
<tr>
<td align="right">
12
</td>
<td>
4
</td>
<td align="right">
2
</td>
<td align="right">
6
</td>
<td align="right">
1.0
</td>
</tr>
<tr>
<td align="right">
13
</td>
<td>
5
</td>
<td align="right">
0
</td>
<td align="right">
2368
</td>
<td align="right">
2368.0
</td>
</tr>
<tr>
<td align="right">
14
</td>
<td>
5
</td>
<td align="right">
1
</td>
<td align="right">
9
</td>
<td align="right">
9.0
</td>
</tr>
<tr>
<td align="right">
15
</td>
<td>
6
</td>
<td align="right">
0
</td>
<td align="right">
3867
</td>
<td align="right">
3867.0
</td>
</tr>
<tr>
<td align="right">
16
</td>
<td>
6
</td>
<td align="right">
1
</td>
<td align="right">
16
</td>
<td align="right">
16.0
</td>
</tr>
<tr>
<td align="right">
17
</td>
<td>
6
</td>
<td align="right">
2
</td>
<td align="right">
1
</td>
<td align="right">
0.0
</td>
</tr>
<tr>
<td align="right">
18
</td>
<td>
7
</td>
<td align="right">
0
</td>
<td align="right">
372
</td>
<td align="right">
372.0
</td>
</tr>
<tr>
<td align="right">
19
</td>
<td>
7
</td>
<td align="right">
1
</td>
<td align="right">
1
</td>
<td align="right">
1.0
</td>
</tr>
</tbody>
</table>
<p>You get the idea….</p>
<p>If we can’t really see very much at the individual claims level, how does it appear when we look at the levels aggregated to the current tariff cells? Here, of course, we have much more data with up to 33 claims in one cell, but we will limit the display to the first few number of claims.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">high.limit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>L                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only display up to this many claims</span></span>
<span id="cb17-2">plots <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb17-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb17-4">           <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i)</span>
<span id="cb17-5">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mccase.current, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(antskad))</span>
<span id="cb17-6">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>high.limit, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> duration))</span>
<span id="cb17-7">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, high.limit), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>high.limit)</span>
<span id="cb17-8">           <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Following xlim() needed for scale_x_discrete only; see</span></span>
<span id="cb17-9">           <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## https://groups.google.com/d/msg/ggplot2/wLWGCUz8K6k/DeVudyfXyKgJ</span></span>
<span id="cb17-10">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, high.limit)</span>
<span id="cb17-11">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>()</span>
<span id="cb17-12">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating."</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ~ ."</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>),</span>
<span id="cb17-13">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span>)</span>
<span id="cb17-14">           <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## We drop the axis titles to make more room for the data</span></span>
<span id="cb17-15">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb17-16">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb17-17">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb17-18">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> plot.titles[i])</span>
<span id="cb17-19">           )</span>
<span id="cb17-20"></span>
<span id="cb17-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.newpage</span>()</span>
<span id="cb17-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pushViewport</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)))</span>
<span id="cb17-23"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb17-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(plots[[i]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout.pos.col =</span> i))</span></code></pre></div></div>
<p><img src="https://www.cybaea.net/files/PricingGLM/PricingGLM-2-2.png" class="img-fluid"></p>
<p>There is certainly something going on for low bonus classes and high vehicle ages that is not straightforward Poisson distribution. Looking at the two in combination we see the old motorcycle - low bonus class (rating.3 = 3 and rating.4 = 1) is the main culpit, but also note that there is relatively little data in these cells.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mccase.current, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(antskad)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>high.limit, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> duration)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_discrete</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, high.limit), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>high.limit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, high.limit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label_both"</span>)</span></code></pre></div></div>
<p><img src="https://www.cybaea.net/files/PricingGLM/PricingGLM-2-3.png" class="img-fluid"></p>
<p>We can fit the poisson distribution using the same <code>predictionPoisson()</code> function as before. We also look at the fit in a simple way (the - much! - better approach is to use the residuals from the fitted model)</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## rating.3 is the vehicle age.</span></span>
<span id="cb19-2">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb19-3">    mccase.current[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, antskad),</span>
<span id="cb19-4">                   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> .N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(duration)),</span>
<span id="cb19-5">                   by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, antskad)]</span>
<span id="cb19-6">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>predicted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb19-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>),</span>
<span id="cb19-8">                  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> (l) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictionPoisson</span>(data[rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> l]))))</span>
<span id="cb19-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Show the fit</span></span>
<span id="cb19-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data[antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> high.limit,</span>
<span id="cb19-11">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, antskad, N, predicted)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb19-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Show simplistic residuals per level of the rating factor</span></span>
<span id="cb19-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data[antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> high.limit,</span>
<span id="cb19-14">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">res =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(predicted <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> N))),</span>
<span id="cb19-15">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>])</span>
<span id="cb19-16"></span>
<span id="cb19-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## rating.4 is the bonus class</span></span>
<span id="cb19-18">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb19-19">    mccase.current[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>, antskad),</span>
<span id="cb19-20">                   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> .N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(duration)),</span>
<span id="cb19-21">                   by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>, antskad)]</span>
<span id="cb19-22">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>predicted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb19-23">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>),</span>
<span id="cb19-24">                  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> (l) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictionPoisson</span>(data[rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> l]))))</span>
<span id="cb19-25"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Show the fit</span></span>
<span id="cb19-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data[antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> high.limit,</span>
<span id="cb19-27">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>, antskad, N, predicted)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb19-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Show simplistic residuals per rating factor</span></span>
<span id="cb19-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data[antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> high.limit,</span>
<span id="cb19-30">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">res =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(predicted <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> N))),</span>
<span id="cb19-31">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>])</span></code></pre></div></div>
<p>We will not show the tables here (as we said: there is a better way). Instead, we can show the fitted Poisson distribution:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> antskad, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> N)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>L<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>high.limit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>( <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> predicted), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, high.limit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div></div>
<p><img src="https://www.cybaea.net/files/PricingGLM/PricingGLM-2-4.png" class="img-fluid"></p>
<h4 id="duration" class="anchored">2. Duration</h4>
<p>We can plot the distribution of duration as before. Here 440 is the 90% quantile for the duration (412) rounded up to the next multiplier of binwidth.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">plots <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb21-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb21-3">           <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i)</span>
<span id="cb21-4">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mccase.current, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(duration))</span>
<span id="cb21-5">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)</span>
<span id="cb21-6">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">440</span>)</span>
<span id="cb21-7">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>()</span>
<span id="cb21-8">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating."</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ~ ."</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>),</span>
<span id="cb21-9">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixed"</span>)</span>
<span id="cb21-10">           <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## We drop the axis titles to make more room for the data</span></span>
<span id="cb21-11">           <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb21-12">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb21-13">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blank</span>(),</span>
<span id="cb21-14">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> plot.titles[i])</span>
<span id="cb21-15">           )</span>
<span id="cb21-16"></span>
<span id="cb21-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.newpage</span>()</span>
<span id="cb21-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pushViewport</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)))</span>
<span id="cb21-19"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## We can ignore the warnings from displaying the plots for now</span></span>
<span id="cb21-20"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb21-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(plots[[i]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">viewport</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout.pos.col =</span> i))</span></code></pre></div></div>
<p><img src="https://www.cybaea.net/files/PricingGLM/PricingGLM-2-5.png" class="img-fluid"></p>
<p>However, in this case it is much more illuminating to look at the overall distribution:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mccase, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(duration)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">binwidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opts</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Duration of motorcycle policies"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram bin width = 0.05"</span>,</span>
<span id="cb22-6">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<p>![Duration of motorcycle policies](/files/PricingGLM/PricingGLM-2-6.png</p>
<p>Big spikes near 1/2 and 1 year (and in general spikes around 1/2 year multiples) suggests that either (1) this is not a random sample of policies or (2) there is a strong seasonal effect in the time of year people initially sign up for the policy.</p>
<h3 id="problem-3-determine-the-relativities-for-claim-frequency-and-severity" class="anchored">Problem 3: Determine the relativities for claim frequency and severity</h3>
<blockquote class="blockquote">
<p>Determine the relativities for claim frequency and severity separately, by using GLMs; use the results to get relativities for the pure premium.</p>
</blockquote>
<p>We first load the data (if needed) and create a data frame to hold the output, following the structure of Table 2.8 in the book.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb23-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.current"</span>))</span>
<span id="cb23-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mccase.current.RData"</span>)</span>
<span id="cb23-4"></span>
<span id="cb23-5">case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb23-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rating.factor =</span></span>
<span id="cb23-7">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zone"</span>,        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(mccase.current<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>)),</span>
<span id="cb23-8">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MC class"</span>,    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(mccase.current<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>)),</span>
<span id="cb23-9">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vehicle age"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(mccase.current<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>)),</span>
<span id="cb23-10">                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bonus class"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(mccase.current<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>))),</span>
<span id="cb23-11">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span></span>
<span id="cb23-12">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(mccase.current,</span>
<span id="cb23-13">                    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>),</span>
<span id="cb23-14">                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>))),</span>
<span id="cb23-15">               <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## These are the values from Table 2.8 in the book:</span></span>
<span id="cb23-16">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">relativity =</span></span>
<span id="cb23-17">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.678</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.227</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.336</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.734</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.402</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.402</span>,</span>
<span id="cb23-18">                 <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.625</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.769</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.406</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.875</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.062</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.873</span>,</span>
<span id="cb23-19">                 <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.000</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.200</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.000</span>,</span>
<span id="cb23-20">                 <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.250</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.125</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.000</span>),</span>
<span id="cb23-21">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb23-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<table class="table">
<thead>
<tr>
<th>
</th>
<th>
rating.factor
</th>
<th>
class
</th>
<th>
relativity
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
1
</td>
<td>
zone
</td>
<td>
1
</td>
<td align="right">
7.678
</td>
</tr>
<tr>
<td align="right">
2
</td>
<td>
zone
</td>
<td>
2
</td>
<td align="right">
4.227
</td>
</tr>
<tr>
<td align="right">
3
</td>
<td>
zone
</td>
<td>
3
</td>
<td align="right">
1.336
</td>
</tr>
<tr>
<td align="right">
4
</td>
<td>
zone
</td>
<td>
4
</td>
<td align="right">
1.000
</td>
</tr>
<tr>
<td align="right">
5
</td>
<td>
zone
</td>
<td>
5
</td>
<td align="right">
1.734
</td>
</tr>
<tr>
<td align="right">
6
</td>
<td>
zone
</td>
<td>
6
</td>
<td align="right">
1.402
</td>
</tr>
<tr>
<td align="right">
7
</td>
<td>
zone
</td>
<td>
7
</td>
<td align="right">
1.402
</td>
</tr>
<tr>
<td align="right">
8
</td>
<td>
mc class
</td>
<td>
1
</td>
<td align="right">
0.625
</td>
</tr>
<tr>
<td align="right">
9
</td>
<td>
mc class
</td>
<td>
2
</td>
<td align="right">
0.769
</td>
</tr>
<tr>
<td align="right">
10
</td>
<td>
mc class
</td>
<td>
3
</td>
<td align="right">
1.000
</td>
</tr>
<tr>
<td align="right">
11
</td>
<td>
mc class
</td>
<td>
4
</td>
<td align="right">
1.406
</td>
</tr>
<tr>
<td align="right">
12
</td>
<td>
mc class
</td>
<td>
5
</td>
<td align="right">
1.875
</td>
</tr>
<tr>
<td align="right">
13
</td>
<td>
mc class
</td>
<td>
6
</td>
<td align="right">
4.062
</td>
</tr>
<tr>
<td align="right">
14
</td>
<td>
mc class
</td>
<td>
7
</td>
<td align="right">
6.873
</td>
</tr>
<tr>
<td align="right">
15
</td>
<td>
vehicle age
</td>
<td>
1
</td>
<td align="right">
2.000
</td>
</tr>
<tr>
<td align="right">
16
</td>
<td>
vehicle age
</td>
<td>
2
</td>
<td align="right">
1.200
</td>
</tr>
<tr>
<td align="right">
17
</td>
<td>
vehicle age
</td>
<td>
3
</td>
<td align="right">
1.000
</td>
</tr>
<tr>
<td align="right">
18
</td>
<td>
bonus class
</td>
<td>
1
</td>
<td align="right">
1.250
</td>
</tr>
<tr>
<td align="right">
19
</td>
<td>
bonus class
</td>
<td>
2
</td>
<td align="right">
1.125
</td>
</tr>
<tr>
<td align="right">
20
</td>
<td>
bonus class
</td>
<td>
3
</td>
<td align="right">
1.000
</td>
</tr>
</tbody>
</table>
<p>Close enough to the book.</p>
<p>First we set the contrasts so the baseline for the models is the level with the highest duration. This is the same approach we used before.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"foreach"</span>)</span>
<span id="cb24-2">new.cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb24-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foreach</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rating.factor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rating"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>),</span>
<span id="cb24-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combine =</span> rbind) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%do%</span></span>
<span id="cb24-5">{</span>
<span id="cb24-6">    totals <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mccase.current[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">D =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(duration),</span>
<span id="cb24-7">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(antskad),</span>
<span id="cb24-8">                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">C =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(skadkost)),</span>
<span id="cb24-9">                             by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> rating.factor]</span>
<span id="cb24-10">    n.levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(mccase.current[[rating.factor]])</span>
<span id="cb24-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(mccase.current[[rating.factor]]) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb24-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.treatment</span>(n.levels)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>totals[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), ]</span>
<span id="cb24-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">duration =</span> totals[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>]],</span>
<span id="cb24-14">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.claims =</span> totals[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>]],</span>
<span id="cb24-15">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skadkost =</span> totals[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>]])</span>
<span id="cb24-16">}</span>
<span id="cb24-17">case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>, new.cols)</span>
<span id="cb24-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(new.cols)</span></code></pre></div></div>
<p>We next fit the models and combine the results as in Example 2.5 above. Here we also make a note of a simple measure of the goodness of fit: the residual deviance and the degrees of freedom. The frequency model is a good fit, the severity model is not.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Model the frequency</span></span>
<span id="cb25-2"></span>
<span id="cb25-3">model.frequency <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb25-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span></span>
<span id="cb25-5">        rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">offset</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(duration)),</span>
<span id="cb25-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mccase.current[duration <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> poisson)</span>
<span id="cb25-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Res. dev. 360 on 389 dof</span></span>
<span id="cb25-8"></span>
<span id="cb25-9">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>( model.frequency )</span>
<span id="cb25-10">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rels[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] )</span>
<span id="cb25-11">case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rels.frequency <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb25-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb25-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb25-14">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb25-15">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)])</span>
<span id="cb25-16"></span>
<span id="cb25-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Model the severity. We stick with the non-canonical link function</span></span>
<span id="cb25-18"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## for the time being.</span></span>
<span id="cb25-19"></span>
<span id="cb25-20">model.severity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb25-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(skadkost <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rating<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>,</span>
<span id="cb25-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> mccase.current[skadkost <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,],</span>
<span id="cb25-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Gamma</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> antskad)</span>
<span id="cb25-24"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Res.dev. 516 on 164 dof</span></span>
<span id="cb25-25"></span>
<span id="cb25-26">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>( model.severity )</span>
<span id="cb25-27">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rels[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] )</span>
<span id="cb25-28">case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rels.severity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb25-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb25-30">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb25-31">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)],</span>
<span id="cb25-32">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)])</span>
<span id="cb25-33"></span>
<span id="cb25-34"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Combine the frequency and severity</span></span>
<span id="cb25-35">case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>rels.pure.prem <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>, rels.frequency <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rels.severity)</span></code></pre></div></div>
<p>Finally we convert, save, and compare with the current values.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Convert to data.table</span></span>
<span id="cb26-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table"</span>)</span>
<span id="cb26-3">case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>)</span>
<span id="cb26-4"></span>
<span id="cb26-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Save</span></span>
<span id="cb26-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"case.2.4.RData"</span>)</span>
<span id="cb26-7"></span>
<span id="cb26-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Compare with current values</span></span>
<span id="cb26-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>[,</span>
<span id="cb26-10">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rating.factor, class, duration, n.claims,</span>
<span id="cb26-11">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skadkostK =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(skadkost <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e3</span>),</span>
<span id="cb26-12">                    relativity, rels.pure.prem)],</span>
<span id="cb26-13">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<table class="table">
<thead>
<tr>
<th>
</th>
<th>
rating.factor
</th>
<th>
class
</th>
<th>
duration
</th>
<th>
n.claims
</th>
<th>
skadkostK
</th>
<th>
relativity
</th>
<th>
rels.pure.prem
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
1
</td>
<td>
Zone
</td>
<td>
1
</td>
<td align="right">
6205.310
</td>
<td align="right">
183
</td>
<td align="right">
5540.000
</td>
<td align="right">
7.678
</td>
<td align="right">
6.239
</td>
</tr>
<tr>
<td align="right">
2
</td>
<td>
Zone
</td>
<td>
2
</td>
<td align="right">
10103.090
</td>
<td align="right">
167
</td>
<td align="right">
4811.000
</td>
<td align="right">
4.227
</td>
<td align="right">
3.179
</td>
</tr>
<tr>
<td align="right">
3
</td>
<td>
Zone
</td>
<td>
3
</td>
<td align="right">
11676.573
</td>
<td align="right">
123
</td>
<td align="right">
2523.000
</td>
<td align="right">
1.336
</td>
<td align="right">
0.993
</td>
</tr>
<tr>
<td align="right">
4
</td>
<td>
Zone
</td>
<td>
4
</td>
<td align="right">
32628.493
</td>
<td align="right">
196
</td>
<td align="right">
3775.000
</td>
<td align="right">
1.000
</td>
<td align="right">
1.000
</td>
</tr>
<tr>
<td align="right">
5
</td>
<td>
Zone
</td>
<td>
5
</td>
<td align="right">
1582.112
</td>
<td align="right">
9
</td>
<td align="right">
105.000
</td>
<td align="right">
1.734
</td>
<td align="right">
0.155
</td>
</tr>
<tr>
<td align="right">
6
</td>
<td>
Zone
</td>
<td>
6
</td>
<td align="right">
2799.945
</td>
<td align="right">
18
</td>
<td align="right">
288.000
</td>
<td align="right">
1.402
</td>
<td align="right">
0.223
</td>
</tr>
<tr>
<td align="right">
7
</td>
<td>
Zone
</td>
<td>
7
</td>
<td align="right">
241.288
</td>
<td align="right">
1
</td>
<td align="right">
1.000
</td>
<td align="right">
1.402
</td>
<td align="right">
0.002
</td>
</tr>
<tr>
<td align="right">
8
</td>
<td>
MC class
</td>
<td>
1
</td>
<td align="right">
5190.351
</td>
<td align="right">
46
</td>
<td align="right">
993.000
</td>
<td align="right">
0.625
</td>
<td align="right">
0.395
</td>
</tr>
<tr>
<td align="right">
9
</td>
<td>
MC class
</td>
<td>
2
</td>
<td align="right">
3990.115
</td>
<td align="right">
57
</td>
<td align="right">
883.000
</td>
<td align="right">
0.769
</td>
<td align="right">
0.536
</td>
</tr>
<tr>
<td align="right">
10
</td>
<td>
MC class
</td>
<td>
3
</td>
<td align="right">
21665.679
</td>
<td align="right">
166
</td>
<td align="right">
5372.000
</td>
<td align="right">
1.000
</td>
<td align="right">
1.000
</td>
</tr>
<tr>
<td align="right">
11
</td>
<td>
MC class
</td>
<td>
4
</td>
<td align="right">
11739.882
</td>
<td align="right">
98
</td>
<td align="right">
2192.000
</td>
<td align="right">
1.406
</td>
<td align="right">
0.574
</td>
</tr>
<tr>
<td align="right">
12
</td>
<td>
MC class
</td>
<td>
5
</td>
<td align="right">
13439.926
</td>
<td align="right">
149
</td>
<td align="right">
3297.000
</td>
<td align="right">
1.875
</td>
<td align="right">
1.413
</td>
</tr>
<tr>
<td align="right">
13
</td>
<td>
MC class
</td>
<td>
6
</td>
<td align="right">
8880.134
</td>
<td align="right">
175
</td>
<td align="right">
4161.000
</td>
<td align="right">
4.062
</td>
<td align="right">
4.876
</td>
</tr>
<tr>
<td align="right">
14
</td>
<td>
MC class
</td>
<td>
7
</td>
<td align="right">
330.723
</td>
<td align="right">
6
</td>
<td align="right">
145.000
</td>
<td align="right">
6.873
</td>
<td align="right">
0.602
</td>
</tr>
<tr>
<td align="right">
15
</td>
<td>
Vehicle age
</td>
<td>
1
</td>
<td align="right">
4955.403
</td>
<td align="right">
126
</td>
<td align="right">
4964.000
</td>
<td align="right">
2.000
</td>
<td align="right">
4.715
</td>
</tr>
<tr>
<td align="right">
16
</td>
<td>
Vehicle age
</td>
<td>
2
</td>
<td align="right">
9753.811
</td>
<td align="right">
145
</td>
<td align="right">
5507.000
</td>
<td align="right">
1.200
</td>
<td align="right">
2.021
</td>
</tr>
<tr>
<td align="right">
17
</td>
<td>
Vehicle age
</td>
<td>
3
</td>
<td align="right">
50527.597
</td>
<td align="right">
426
</td>
<td align="right">
6570.000
</td>
<td align="right">
1.000
</td>
<td align="right">
1.000
</td>
</tr>
<tr>
<td align="right">
18
</td>
<td>
Bonus class
</td>
<td>
1
</td>
<td align="right">
19893.370
</td>
<td align="right">
207
</td>
<td align="right">
4558.000
</td>
<td align="right">
1.250
</td>
<td align="right">
0.797
</td>
</tr>
<tr>
<td align="right">
19
</td>
<td>
Bonus class
</td>
<td>
2
</td>
<td align="right">
9615.764
</td>
<td align="right">
121
</td>
<td align="right">
3627.000
</td>
<td align="right">
1.125
</td>
<td align="right">
0.604
</td>
</tr>
<tr>
<td align="right">
20
</td>
<td>
Bonus class
</td>
<td>
3
</td>
<td align="right">
35727.677
</td>
<td align="right">
369
</td>
<td align="right">
8857.000
</td>
<td align="right">
1.000
</td>
<td align="right">
1.000
</td>
</tr>
</tbody>
</table>
<p>It is really rather different from the current relativity, as we will also see below. But remember that the severity model was a poor fit: that would be my first place to start looking if staying within the insurance pricing framework outlined in this book.</p>
<h3 id="problem-4-discussions" class="anchored">Problem 4: Discussions</h3>
<p>Just note here that the ratio for the relativity we calculated and for the existing one can be found as</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(rels.pure.prem) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(rels.pure.prem)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3552</span></span>
<span id="cb27-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(case.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(relativity) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(relativity))         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   12</span></span></code></pre></div></div>
<p>Note the huge range of relativities in our new model (3552 versus 12).</p>



 ]]></description>
  <category>R</category>
  <category>Insurance</category>
  <category>data</category>
  <guid>https://www.cybaea.net/Journal/2012/03/13/R-code-for-Chapter-2-of-Non_Life-Insurance-Pricing-with-GLM.html</guid>
  <pubDate>Tue, 13 Mar 2012 16:57:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/books/two-bears.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>R code for Chapter 1 of Non-Life Insurance Pricing with GLM</title>
  <link>https://www.cybaea.net/Journal/2012/03/01/R-code-for-Chapter-1-of-Non_Life-Insurance-Pricing-with-GLM.html</link>
  <description><![CDATA[ 






<div class="float-end w-25 d-none d-lg-block">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/books/two-bears.jpg" class="img-fluid figure-img"></p>
<figcaption>Amazon paid links <a href="https://amzn.to/3OVJShq" class="affiliate-marked">UK</a> | <a href="https://amzn.to/3ORxiQf" class="affiliate-marked">US</a></figcaption>
</figure>
</div>
</div>
<p>Insurance pricing is backwards and primitive, harking back to an era before (powerful) computers. One standard (and good) textbook on the topic is <cite>Non-Life Insurance Pricing with Generalized Linear Models</cite> by Esbjorn Ohlsson and Born Johansson (Amazon paid links <a href="https://amzn.to/3OVJShq" class="affiliate-marked">UK</a> | <a href="https://amzn.to/3ORxiQf" class="affiliate-marked">US</a>). We have been doing some work in this area recently. Needing a robust internal training course and documented methodology, we have been working our way through the book again and converting the examples and exercises to <a href="https://www.r-project.org/">R, the statistical computing and analysis platform</a>. This is part of a series of posts containing elements of the R code.</p>
<p class="clearfix">
Let’s get the preliminaries out of the way:
</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb1-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## PricingGLM-1.r - Code for Chapter 1 of "Non-Life Insurance Pricing with GLM"</span></span>
<span id="cb1-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2012 CYBAEA Limited (http://www.cybaea.net/)</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## @book{ohlsson2010non,</span></span>
<span id="cb1-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   title={Non-Life Insurance Pricing with Generalized Linear Models},</span></span>
<span id="cb1-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   author={Ohlsson, E. and Johansson, B.},</span></span>
<span id="cb1-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   isbn={9783642107900},</span></span>
<span id="cb1-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   series={Eaa Series: Textbook},</span></span>
<span id="cb1-10"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   url={http://books.google.com/books?id=l4rjeflJ\_bIC},</span></span>
<span id="cb1-11"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   year={2010},</span></span>
<span id="cb1-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   publisher={Springer Verlag}</span></span>
<span id="cb1-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## }</span></span></code></pre></div></div>
<p>Now we can get started.</p>
<h2 id="example-1.2" class="anchored">Example 1.2</h2>
<p>We grab the data for Table 1.2 from the book’s web site and store it as an R object with lots of good meta information.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################</span></span>
<span id="cb2-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### Example 1.2</span></span>
<span id="cb2-3">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">url</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://www2.math.su.se/~esbj/GLMbook/moppe.sas"</span>)</span>
<span id="cb2-4">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">encoding =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unknown"</span>)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">close</span>(con)</span>
<span id="cb2-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Find the data range</span></span>
<span id="cb2-7">data.start <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^cards;"</span>, data) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L</span>
<span id="cb2-8">data.end   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^;"</span>, data[data.start<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">999</span>L]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> data.start <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>L</span>
<span id="cb2-9">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> data[data.start<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>data.end],</span>
<span id="cb2-10">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quote =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb2-11">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"premiekl"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"moptva"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zon"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dur"</span>,</span>
<span id="cb2-12">                             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"medskad"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"antskad"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"riskpre"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"helpre"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cell"</span>),</span>
<span id="cb2-13">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.strings =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb2-14">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</span>,</span>
<span id="cb2-15">                             <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NULL"</span>),</span>
<span id="cb2-16">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment.char =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb2-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(con, data, data.start, data.end)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cleanup</span></span>
<span id="cb2-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Title: Partial casco moped insurance from Wasa insurance, 1994--1999"</span>,</span>
<span id="cb2-20">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: http://www2.math.su.se/~esbj/GLMbook/moppe.sas"</span>,</span>
<span id="cb2-21">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Copyright: http://www2.math.su.se/~esbj/GLMbook/"</span>)</span>
<span id="cb2-22"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## See the SAS code for this derived field</span></span>
<span id="cb2-23">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>skadfre <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, antskad <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> dur)</span>
<span id="cb2-24"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## English language column names as comments:</span></span>
<span id="cb2-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>premiekl) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Class"</span>,</span>
<span id="cb2-27">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 1=Weight over 60kg and more than 2 gears"</span>,</span>
<span id="cb2-28">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 2=Other"</span>)</span>
<span id="cb2-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>moptva)   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Age"</span>,</span>
<span id="cb2-31">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 1=At most 1 year"</span>,</span>
<span id="cb2-32">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 2=2 years or more"</span>)</span>
<span id="cb2-33"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>zon)      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-34">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Zone"</span>,</span>
<span id="cb2-35">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 1=Central and semi-central parts of Sweden's three largest cities"</span>,</span>
<span id="cb2-36">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 2=suburbs and middle-sized towns"</span>,</span>
<span id="cb2-37">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 3=Lesser towns, except those in 5 or 7"</span>,</span>
<span id="cb2-38">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 4=Small towns and countryside, except 5--7"</span>,</span>
<span id="cb2-39">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 5=Northern towns"</span>,</span>
<span id="cb2-40">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 6=Northern countryside"</span>,</span>
<span id="cb2-41">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code: 7=Gotland (Sweden's largest island)"</span>)</span>
<span id="cb2-42"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dur)      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-43">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Duration"</span>,</span>
<span id="cb2-44">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unit: year"</span>)</span>
<span id="cb2-45"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>medskad)  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-46">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Claim severity"</span>,</span>
<span id="cb2-47">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unit: SEK"</span>)</span>
<span id="cb2-48"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>antskad)  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: No. claims"</span></span>
<span id="cb2-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>riskpre)  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-50">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Pure premium"</span>,</span>
<span id="cb2-51">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unit: SEK"</span>)</span>
<span id="cb2-52"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>helpre)   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-53">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Actual premium"</span>,</span>
<span id="cb2-54">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Note: The premium for one year according to the tariff in force 1999"</span>,</span>
<span id="cb2-55">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unit: SEK"</span>)</span>
<span id="cb2-56"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comment</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>skadfre)  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-57">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Name: Claim frequency"</span>,</span>
<span id="cb2-58">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Unit: /year"</span>)</span>
<span id="cb2-59"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Save results for later</span></span>
<span id="cb2-60"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table.1.2.RData"</span>)</span>
<span id="cb2-61"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Print the table (not as pretty as the book)</span></span>
<span id="cb2-62"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>)</span>
<span id="cb2-63"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################</span></span></code></pre></div></div>
<table class="table">
<thead class="text-end">
<tr>
<th>
</th>
<th class="text-start">
premiekl
</th>
<th class="text-start">
moptva
</th>
<th class="text-start">
zon
</th>
<th>
dur
</th>
<th>
medskad
</th>
<th>
antskad
</th>
<th>
riskpre
</th>
<th>
helpre
</th>
<th>
skadfre
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
1
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
<td align="right">
62.9000
</td>
<td align="right">
18256
</td>
<td align="right">
17
</td>
<td align="right">
4936
</td>
<td align="right">
2049
</td>
<td align="right">
0.2703
</td>
</tr>
<tr>
<td align="right">
2
</td>
<td>
1
</td>
<td>
1
</td>
<td>
2
</td>
<td align="right">
112.9000
</td>
<td align="right">
13632
</td>
<td align="right">
7
</td>
<td align="right">
845
</td>
<td align="right">
1230
</td>
<td align="right">
0.0620
</td>
</tr>
<tr>
<td align="right">
3
</td>
<td>
1
</td>
<td>
1
</td>
<td>
3
</td>
<td align="right">
133.1000
</td>
<td align="right">
20877
</td>
<td align="right">
9
</td>
<td align="right">
1411
</td>
<td align="right">
762
</td>
<td align="right">
0.0676
</td>
</tr>
<tr>
<td align="right">
4
</td>
<td>
1
</td>
<td>
1
</td>
<td>
4
</td>
<td align="right">
376.6000
</td>
<td align="right">
13045
</td>
<td align="right">
7
</td>
<td align="right">
242
</td>
<td align="right">
396
</td>
<td align="right">
0.0186
</td>
</tr>
<tr>
<td align="right">
5
</td>
<td>
1
</td>
<td>
1
</td>
<td>
5
</td>
<td align="right">
9.4000
</td>
<td align="right">
0
</td>
<td align="right">
0
</td>
<td align="right">
0
</td>
<td align="right">
990
</td>
<td align="right">
0.0000
</td>
</tr>
<tr>
<td align="right">
6
</td>
<td>
1
</td>
<td>
1
</td>
<td>
6
</td>
<td align="right">
70.8000
</td>
<td align="right">
15000
</td>
<td align="right">
1
</td>
<td align="right">
212
</td>
<td align="right">
594
</td>
<td align="right">
0.0141
</td>
</tr>
<tr>
<td align="right">
7
</td>
<td>
1
</td>
<td>
1
</td>
<td>
7
</td>
<td align="right">
4.4000
</td>
<td align="right">
8018
</td>
<td align="right">
1
</td>
<td align="right">
1829
</td>
<td align="right">
396
</td>
<td align="right">
0.2273
</td>
</tr>
<tr>
<td align="right">
8
</td>
<td>
1
</td>
<td>
2
</td>
<td>
1
</td>
<td align="right">
352.1000
</td>
<td align="right">
8232
</td>
<td align="right">
52
</td>
<td align="right">
1216
</td>
<td align="right">
1229
</td>
<td align="right">
0.1477
</td>
</tr>
<tr>
<td align="right">
9
</td>
<td>
1
</td>
<td>
2
</td>
<td>
2
</td>
<td align="right">
840.1000
</td>
<td align="right">
7418
</td>
<td align="right">
69
</td>
<td align="right">
609
</td>
<td align="right">
738
</td>
<td align="right">
0.0821
</td>
</tr>
<tr>
<td align="right">
10
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td align="right">
1378.3000
</td>
<td align="right">
7318
</td>
<td align="right">
75
</td>
<td align="right">
398
</td>
<td align="right">
457
</td>
<td align="right">
0.0544
</td>
</tr>
<tr>
<td align="right">
11
</td>
<td>
1
</td>
<td>
2
</td>
<td>
4
</td>
<td align="right">
5505.3000
</td>
<td align="right">
6922
</td>
<td align="right">
136
</td>
<td align="right">
171
</td>
<td align="right">
238
</td>
<td align="right">
0.0247
</td>
</tr>
<tr>
<td align="right">
12
</td>
<td>
1
</td>
<td>
2
</td>
<td>
5
</td>
<td align="right">
114.1000
</td>
<td align="right">
11131
</td>
<td align="right">
2
</td>
<td align="right">
195
</td>
<td align="right">
594
</td>
<td align="right">
0.0175
</td>
</tr>
<tr>
<td align="right">
13
</td>
<td>
1
</td>
<td>
2
</td>
<td>
6
</td>
<td align="right">
810.9000
</td>
<td align="right">
5970
</td>
<td align="right">
14
</td>
<td align="right">
103
</td>
<td align="right">
356
</td>
<td align="right">
0.0173
</td>
</tr>
<tr>
<td align="right">
14
</td>
<td>
1
</td>
<td>
2
</td>
<td>
7
</td>
<td align="right">
62.3000
</td>
<td align="right">
6500
</td>
<td align="right">
1
</td>
<td align="right">
104
</td>
<td align="right">
238
</td>
<td align="right">
0.0161
</td>
</tr>
<tr>
<td align="right">
15
</td>
<td>
2
</td>
<td>
1
</td>
<td>
1
</td>
<td align="right">
191.6000
</td>
<td align="right">
7754
</td>
<td align="right">
43
</td>
<td align="right">
1740
</td>
<td align="right">
1024
</td>
<td align="right">
0.2244
</td>
</tr>
<tr>
<td align="right">
16
</td>
<td>
2
</td>
<td>
1
</td>
<td>
2
</td>
<td align="right">
237.3000
</td>
<td align="right">
6933
</td>
<td align="right">
34
</td>
<td align="right">
993
</td>
<td align="right">
615
</td>
<td align="right">
0.1433
</td>
</tr>
<tr>
<td align="right">
17
</td>
<td>
2
</td>
<td>
1
</td>
<td>
3
</td>
<td align="right">
162.4000
</td>
<td align="right">
4402
</td>
<td align="right">
11
</td>
<td align="right">
298
</td>
<td align="right">
381
</td>
<td align="right">
0.0677
</td>
</tr>
<tr>
<td align="right">
18
</td>
<td>
2
</td>
<td>
1
</td>
<td>
4
</td>
<td align="right">
446.5000
</td>
<td align="right">
8214
</td>
<td align="right">
8
</td>
<td align="right">
147
</td>
<td align="right">
198
</td>
<td align="right">
0.0179
</td>
</tr>
<tr>
<td align="right">
19
</td>
<td>
2
</td>
<td>
1
</td>
<td>
5
</td>
<td align="right">
13.2000
</td>
<td align="right">
0
</td>
<td align="right">
0
</td>
<td align="right">
0
</td>
<td align="right">
495
</td>
<td align="right">
0.0000
</td>
</tr>
<tr>
<td align="right">
20
</td>
<td>
2
</td>
<td>
1
</td>
<td>
6
</td>
<td align="right">
82.8000
</td>
<td align="right">
5830
</td>
<td align="right">
3
</td>
<td align="right">
211
</td>
<td align="right">
297
</td>
<td align="right">
0.0362
</td>
</tr>
<tr>
<td align="right">
21
</td>
<td>
2
</td>
<td>
1
</td>
<td>
7
</td>
<td align="right">
14.5000
</td>
<td align="right">
0
</td>
<td align="right">
0
</td>
<td align="right">
0
</td>
<td align="right">
198
</td>
<td align="right">
0.0000
</td>
</tr>
<tr>
<td align="right">
22
</td>
<td>
2
</td>
<td>
2
</td>
<td>
1
</td>
<td align="right">
844.8000
</td>
<td align="right">
4728
</td>
<td align="right">
94
</td>
<td align="right">
526
</td>
<td align="right">
614
</td>
<td align="right">
0.1113
</td>
</tr>
<tr>
<td align="right">
23
</td>
<td>
2
</td>
<td>
2
</td>
<td>
2
</td>
<td align="right">
1296.0000
</td>
<td align="right">
4252
</td>
<td align="right">
99
</td>
<td align="right">
325
</td>
<td align="right">
369
</td>
<td align="right">
0.0764
</td>
</tr>
<tr>
<td align="right">
24
</td>
<td>
2
</td>
<td>
2
</td>
<td>
3
</td>
<td align="right">
1214.9000
</td>
<td align="right">
4212
</td>
<td align="right">
37
</td>
<td align="right">
128
</td>
<td align="right">
229
</td>
<td align="right">
0.0305
</td>
</tr>
<tr>
<td align="right">
25
</td>
<td>
2
</td>
<td>
2
</td>
<td>
4
</td>
<td align="right">
3740.7000
</td>
<td align="right">
3846
</td>
<td align="right">
56
</td>
<td align="right">
58
</td>
<td align="right">
119
</td>
<td align="right">
0.0150
</td>
</tr>
<tr>
<td align="right">
26
</td>
<td>
2
</td>
<td>
2
</td>
<td>
5
</td>
<td align="right">
109.4000
</td>
<td align="right">
3925
</td>
<td align="right">
4
</td>
<td align="right">
144
</td>
<td align="right">
297
</td>
<td align="right">
0.0366
</td>
</tr>
<tr>
<td align="right">
27
</td>
<td>
2
</td>
<td>
2
</td>
<td>
6
</td>
<td align="right">
404.7000
</td>
<td align="right">
5280
</td>
<td align="right">
5
</td>
<td align="right">
65
</td>
<td align="right">
178
</td>
<td align="right">
0.0124
</td>
</tr>
<tr>
<td align="right">
28
</td>
<td>
2
</td>
<td>
2
</td>
<td>
7
</td>
<td align="right">
66.3000
</td>
<td align="right">
7795
</td>
<td align="right">
1
</td>
<td align="right">
118
</td>
<td align="right">
119
</td>
<td align="right">
0.0151
</td>
</tr>
</tbody>
</table>
<p>That was easy. Now for something a little harder.</p>
<h2 id="example-1.3" class="anchored">Example 1.3</h2>
<p>Here we are concerned with replicating Table 1.4. We do it slowly, step-by-step, for pedagogical reasons.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################</span></span>
<span id="cb3-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### Example 1.3</span></span>
<span id="cb3-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table.1.2"</span>))</span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table.1.2.RData"</span>)</span>
<span id="cb3-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## We calculate each of the columns individually and slowly here</span></span>
<span id="cb3-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## to show each step</span></span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## First we have simply the labels of the table</span></span>
<span id="cb3-9">rating.factor <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>,</span>
<span id="cb3-11">         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vehicle class"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(premiekl)),</span>
<span id="cb3-12">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vehicle age"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(moptva)),</span>
<span id="cb3-13">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zone"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(zon))))</span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The Class column</span></span>
<span id="cb3-16">class.num <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(premiekl), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(moptva), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(zon)))</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The Duration is the sum of durations within each class</span></span>
<span id="cb3-19">duration.total <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(dur, premiekl, sum)),</span>
<span id="cb3-21">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(dur, moptva, sum)),</span>
<span id="cb3-22">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(dur, zon, sum)))</span>
<span id="cb3-23"></span>
<span id="cb3-24"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Calculate relativities in the tariff</span></span>
<span id="cb3-25"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The denominator of the fraction is the class with the highest exposure</span></span>
<span id="cb3-26"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## (i.e. the maximum total duration): we make that explicit with the</span></span>
<span id="cb3-27"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## which.max() construct.  We also set the contrasts to use this as the base,</span></span>
<span id="cb3-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## which will be useful for the glm() model later.</span></span>
<span id="cb3-29">class.base <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(duration.total[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb3-30">age.base   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(duration.total[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb3-31">zone.base  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(duration.total[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>])</span>
<span id="cb3-32"></span>
<span id="cb3-33">rt.class <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(helpre, premiekl, sum))</span>
<span id="cb3-34">rt.class <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rt.class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> rt.class[class.base]</span>
<span id="cb3-35">rt.age   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(helpre, moptva, sum))</span>
<span id="cb3-36">rt.age   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rt.age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> rt.age[age.base]</span>
<span id="cb3-37">rt.zone  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tapply</span>(helpre, zon, sum))</span>
<span id="cb3-38">rt.zone  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rt.zone <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> rt.zone[zone.base]</span>
<span id="cb3-39"></span>
<span id="cb3-40"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>premiekl) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-41">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.treatment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>premiekl))[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>duration.total[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb3-42">                                                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), ]</span>
<span id="cb3-43"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>moptva) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.treatment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>moptva))[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>duration.total[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>],</span>
<span id="cb3-45">                                                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), ]</span>
<span id="cb3-46"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrasts</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>zon) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-47">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contr.treatment</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>zon))[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>duration.total[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>],</span>
<span id="cb3-48">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), ]</span></code></pre></div></div>
<p>The contrasts could also have been set with the <code>base=</code> argument, e.g.&nbsp;<code>contrasts(table.1.2$zon) &lt;- contr.treatment(nlevels(table.1.2$zon), base = zone.base)</code>, which would be closer in spirit to the SAS code. But I like the idiom presented here where we follow the duration order; it also extends well to other (i.e.&nbsp;not treatment) contrasts. I just wish <code>rank()</code> had a <code>decreasing=</code> argument like <code>order()</code> which I think would be clearer than using <code>rank(-x)</code> to get a decreasing sort order.</p>
<p>That was the easy part. At this stage in the book you are not really expected to understand the next step so do not despair! We just show how easy it is to replicate the SAS code in R. An alternative approach using direct optimization is outlined in Exercise 1.3 below.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Relativities of MMT; we use the glm approach here as per the book’s</span></span>
<span id="cb4-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## SAS code at http://www2.math.su.se/~esbj/GLMbook/moppe.sas</span></span>
<span id="cb4-3">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(riskpre <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> premiekl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> moptva <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> zon, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>,</span>
<span id="cb4-4">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">poisson</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> dur)</span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## If the next line is a mystery then you need to</span></span>
<span id="cb4-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## (1) read up on contrasts or</span></span>
<span id="cb4-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## (2) remember that the link function is log() which is why we use exp here</span></span>
<span id="cb4-9">rels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>( <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m)[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(m)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb4-10"></span>
<span id="cb4-11">rm.class <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># See rm.zone below for the</span></span>
<span id="cb4-12">rm.age   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># general approach</span></span>
<span id="cb4-13">rm.zone  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, rels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>])[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rank</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>duration.total[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ties.method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)]</span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Create and save the data frame</span></span>
<span id="cb4-16">table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb4-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rating.factor =</span> rating.factor, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Class =</span> class.num,</span>
<span id="cb4-18">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Duration =</span> duration.total,</span>
<span id="cb4-19">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rel.tariff =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rt.class, rt.age, rt.zone),</span>
<span id="cb4-20">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Rel.MMT    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rm.class, rm.age, rm.zone))</span>
<span id="cb4-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table.1.4.RData"</span>)</span>
<span id="cb4-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(table.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb4-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(rating.factor, class.num, duration.total, class.base, age.base, zone.base,</span>
<span id="cb4-24">   rt.class, rt.age, rt.zone, rm.class, rm.age, rm.zone, m, rels)</span>
<span id="cb4-25"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################</span></span></code></pre></div></div>
<p>The result is something like this:</p>
<table class="table">
<thead class="text-end">
<tr>
<th>
</th>
<th class="text-start">
Rating.factor
</th>
<th class="text-start">
Class
</th>
<th>
Duration
</th>
<th>
Rel.tariff
</th>
<th>
Rel.MMT
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
1
</td>
<td>
Vehicle class
</td>
<td>
1
</td>
<td align="right">
9833.20
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
</tr>
<tr>
<td align="right">
2
</td>
<td>
Vehicle class
</td>
<td>
2
</td>
<td align="right">
8825.10
</td>
<td align="right">
0.50
</td>
<td align="right">
0.43
</td>
</tr>
<tr>
<td align="right">
3
</td>
<td>
Vehicle age
</td>
<td>
1
</td>
<td align="right">
1918.40
</td>
<td align="right">
1.67
</td>
<td align="right">
2.73
</td>
</tr>
<tr>
<td align="right">
4
</td>
<td>
Vehicle age
</td>
<td>
2
</td>
<td align="right">
16739.90
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
</tr>
<tr>
<td align="right">
5
</td>
<td>
Zone
</td>
<td>
1
</td>
<td align="right">
1451.40
</td>
<td align="right">
5.17
</td>
<td align="right">
8.97
</td>
</tr>
<tr>
<td align="right">
6
</td>
<td>
Zone
</td>
<td>
2
</td>
<td align="right">
2486.30
</td>
<td align="right">
3.10
</td>
<td align="right">
4.19
</td>
</tr>
<tr>
<td align="right">
7
</td>
<td>
Zone
</td>
<td>
3
</td>
<td align="right">
2888.70
</td>
<td align="right">
1.92
</td>
<td align="right">
2.52
</td>
</tr>
<tr>
<td align="right">
8
</td>
<td>
Zone
</td>
<td>
4
</td>
<td align="right">
10069.10
</td>
<td align="right">
1.00
</td>
<td align="right">
1.00
</td>
</tr>
<tr>
<td align="right">
9
</td>
<td>
Zone
</td>
<td>
5
</td>
<td align="right">
246.10
</td>
<td align="right">
2.50
</td>
<td align="right">
1.24
</td>
</tr>
<tr>
<td align="right">
10
</td>
<td>
Zone
</td>
<td>
6
</td>
<td align="right">
1369.20
</td>
<td align="right">
1.50
</td>
<td align="right">
0.74
</td>
</tr>
<tr>
<td align="right">
11
</td>
<td>
Zone
</td>
<td>
7
</td>
<td align="right">
147.50
</td>
<td align="right">
1.00
</td>
<td align="right">
1.23
</td>
</tr>
</tbody>
</table>
<p>Note the rather unusual and apparently inconsistent rounding in the book: 147, 1.66, and 5.16 would be better as 148 (the value is 147.5), 1.67, and 5.17.</p>
<h2 id="exercise-1.3" class="anchored">Exercise 1.3</h2>
<p>Here it gets interesting as we get a different value from the authors. Possibly a small bug on our part but at least we provide the code for you to check. So if you spot a problem let us know in the comments.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################</span></span>
<span id="cb5-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Exercise 1.3</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The values from the book</span></span>
<span id="cb5-5">g0  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03305</span></span>
<span id="cb5-6">g12 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.01231</span></span>
<span id="cb5-7">g22 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.74288</span></span>
<span id="cb5-8">dim.names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Milage =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>),</span>
<span id="cb5-9">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old"</span>))</span>
<span id="cb5-10">pyears <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">47039</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">56455</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">190513</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28612</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb5-11">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> dim.names)</span>
<span id="cb5-12">claims <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.033</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.067</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.025</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.049</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb5-13">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> dim.names)</span>
<span id="cb5-14"></span>
<span id="cb5-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Function to calculate the error of the estimate</span></span>
<span id="cb5-16">GvalsError <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> (gvals) {</span>
<span id="cb5-17">    <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The current estimates</span></span>
<span id="cb5-18">    g0  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gvals[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb5-19">    g12 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gvals[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb5-20">    g22 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gvals[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb5-21">    <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The current estimates in convenient matrix form</span></span>
<span id="cb5-22">    G  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, g12, g22), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb5-23">    G1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, g12), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb5-24">    G2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, g22), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb5-25">    <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The calculated values</span></span>
<span id="cb5-26">    G0  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addmargins</span>(claims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pyears)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sum"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ( <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(pyears <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> G1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> G2) )</span>
<span id="cb5-27">    G12 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addmargins</span>(claims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pyears)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sum"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span></span>
<span id="cb5-28">        ( g0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addmargins</span>(pyears <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> G2)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sum"</span>] )</span>
<span id="cb5-29">    G22 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addmargins</span>(claims <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pyears)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span></span>
<span id="cb5-30">        ( g0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addmargins</span>(pyears <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> G1)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Old"</span>] )</span>
<span id="cb5-31">    <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## The sum of squared errors</span></span>
<span id="cb5-32">    error <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (g0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> G0)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (g12 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> G12)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (g22 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> G22)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb5-33">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(error)</span>
<span id="cb5-34">}</span>
<span id="cb5-35"></span>
<span id="cb5-36"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Minimize the error function to obtain our estimate</span></span>
<span id="cb5-37">gamma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">optim</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(g0, g12, g22), GvalsError)</span>
<span id="cb5-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(gamma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>convergence <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb5-39">gamma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gamma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>par</span>
<span id="cb5-40"></span>
<span id="cb5-41">values <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Our calculation"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Book value"</span>),</span>
<span id="cb5-42">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g0  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(gamma[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], g0),</span>
<span id="cb5-43">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g12 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(gamma[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], g12),</span>
<span id="cb5-44">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">g22 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(gamma[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], g22),</span>
<span id="cb5-45">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"legend"</span>)</span>
<span id="cb5-46"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(values, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb5-47"></span>
<span id="cb5-48"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Close, but not the same.</span></span>
<span id="cb5-49"></span>
<span id="cb5-50"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(g0, g12, g22, dim.names, pyears, claims, gamma, values)</span>
<span id="cb5-51"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################</span></span></code></pre></div></div>
<p>The resulting table is something like:</p>
<table class="table">
<thead class="text-end">
<tr>
<th>
</th>
<th>
g0
</th>
<th>
g12
</th>
<th>
g22
</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">
Our calculation
</td>
<td align="right">
0.0334
</td>
<td align="right">
1.9951
</td>
<td align="right">
0.7452
</td>
</tr>
<tr>
<td align="right">
Book value
</td>
<td align="right">
0.0331
</td>
<td align="right">
2.0123
</td>
<td align="right">
0.7429
</td>
</tr>
</tbody>
</table>
<p>Close, but not the same. Perhaps they used a different error function.</p>



 ]]></description>
  <category>R</category>
  <category>Insurance</category>
  <category>data</category>
  <guid>https://www.cybaea.net/Journal/2012/03/01/R-code-for-Chapter-1-of-Non_Life-Insurance-Pricing-with-GLM.html</guid>
  <pubDate>Thu, 01 Mar 2012 18:11:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/books/two-bears.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>R versus SAS/SPSS in corporations</title>
  <link>https://www.cybaea.net/Journal/2011/10/28/R-versus-SAS-SPSS-in-corporations.html</link>
  <description><![CDATA[ 






<p>We have created and managed analytics teams in commercial organizations (mainly telecommunications) across Europe. The teams were using SAS or SPSS. Our company now has a commercial analytics as a service offering and we mainly use R.</p>
<p>The benefits of R is productivity. We want to spend time on the actions from the analytical insights, not the coding, and we choose our tool accordingly. Being a consulting type organization it is easier for us to attract and retain talent.</p>
<h2 id="the-advantages-of-sasspss-in-a-commercial-environment" class="anchored">The advantages of SAS/SPSS in a commercial environment</h2>
<h3 id="you-can-buy-the-tool-for-money" class="anchored">1. You can buy the tool for money</h3>
<p>Big corporations have procurement departments who do not have a process for free software. Also software spend goes on the balance sheet in a way that the CFO prefers to people but something like R will take a little talent to set up initally. (And yes, we know the Revolutions guys well, but they are not really credible in Europe yet.)</p>
<p>This will change as (a) companies become more mature in their procurement and as (b) commercial support for R improves. (On the latter point, <a href="https://docs.oracle.com/cd/E11882_01/doc.112/e36761/intro.htm#OREUG109">Oracle’s R integration</a> to the database is great news.)</p>
<h3 id="you-can-recruit-for-the-commercial-tool" class="anchored">2. You can recruit for the commercial tool</h3>
<ol type="1">
<li><p>Recruiters are familiar with SAS and SPSS but not with R so it is easier to brief them and to get good quality CVs. This will change and R becomes ever more popular and prevalent. [And yes, we could in theory change recruiters to someone clued in, but again in large corporations there are procurement processes to be followed and existing agreements to be honoured so it will all take months or years.]</p></li>
<li><p>There are recognised training programmes for SPSS and (especially) SAS which makes it easier to recruit the technical skills. How do you know what somebody knows when they say they “know R”? How do you even begin to quantify it from a CV? How do you separate the guy who downloaded the tool and just read <a href="https://cran.r-project.org/doc/manuals/R-intro.html">An Introduction to R</a> from the Frank Harrells of this world?</p>
<p>Yes, I would argue (and in fact have argued in <a href="../../../2011/10/05/Commercial-Analytics-The-Capabilities.html">Commercial Analytics: The Capabilities</a>) that technical skill is not the most important in an analyst (and can be learned anyhow) but it does help filter the CVs and, you guessed it, fits well with the corporation’s processes.</p>
<p>(One reason we use R internally is that we find that it is, on average, a more interesting type of analyst who is proficient in that tool. It seems to encourage curiosity and love or learning in a way that menu-based tools do not.)</p>
<p>I think the commercial R companies are really missing a trick here to provide recognised certification.</p></li>
<li><p>You can’t search for R. Seriously: <a href="https://www.linkedin.com/search/results/all/?keywords=R">try searching for R on LinkedIn</a>. Much easier to find SAS / SPSS skills in a large CV database (like LinkedIn where this discussion started).</p></li>
</ol>
<h3 id="you-can-recruit-for-the-commercial-tool." class="anchored">3. You can recruit for the commercial tool.</h3>
<p>Yes I know I already said that but there is another reason why this is critical.</p>
<p>R takes talent to use. (That is kind of why we like it.) It takes talent to maintain.</p>
<p>My problem as the manager of a commercial analytical insights team is that it is very hard for me to retain that talent. Think about it: what can I offer in terms of career progression? If you are an analyst you might become a senior analyst but you will always be an analyst. There are no examples of a way up the organization (except perhaps out through IT and then up to CIO). [This too will change with time.] And new challenges: yes, some, but we are not a research university and it tends to be the same few problem types that we are always working on. So if you are an analyst looking for new challenges and more pay, the best thing—the logical and rational thing to do—is to get a new job. And your time with Big Corporation will look good on your CV and you will probably land the job easily.</p>
<h2 id="we-can-help" class="anchored">We can help</h2>
<p>If you want to set up a commercial analytical group we can help you get it right first time. The right people, the right processes, the right infrastructure and most importantly the right results. We have done it before and are not tied to any specific tool or vendor.</p>
<p>If you want to improve or enhance your existing analytical teams, then we can Reboot your Analytics to deliver both rapid and sustained commercial results.</p>
<p>And if you just want the results we can provide commercial analytics as a service where we provide the insights and then work with you to turn those insights into commercial actions and better understanding of your business, markets, and customers, leaving you to focus on what you do best.</p>
<p><a href="../../../../Contact/">Contact us</a> now and get results from your analytics.</p>



 ]]></description>
  <category>R</category>
  <category>data</category>
  <category>tools</category>
  <guid>https://www.cybaea.net/Journal/2011/10/28/R-versus-SAS-SPSS-in-corporations.html</guid>
  <pubDate>Fri, 28 Oct 2011 10:10:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/CC0/adam-nowakowski-D4LDw5eXhgg-unsplash.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>A warning on the R save format</title>
  <link>https://www.cybaea.net/Journal/2011/08/23/A-warning-on-the-R-save-format.html</link>
  <description><![CDATA[ 






<p>The <code>save()</code> function in the R platform for statistical computing is very convenient and I suspect many of us use it a lot. But I was recently bitten by a “feature” of the format which meant I could not recover my data.</p>
<h2 id="how-to-lose-your-data-with-save" class="anchored">How to lose your data with save()</h2>
<p>I am using Windows on my travel laptop and Linux on my workstation. To speed things up on the latter and make use of my many (well, four) cores, I use the ‘multicore’ package, which I do not have available on the Windows machine.</p>
<p>To illustrate the problem with the save file format, I created a file on the Linux machine simply as:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multicore"</span>)</span>
<span id="cb1-2">a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> mclapply)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(a, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a.RData"</span>)</span></code></pre></div></div>
<p>What could be simpler? The <code>mclapply</code> is a function from the ‘{multicore}’ package but it clearly has no impact on the stored data. (We will show a more realistic example below – work with me here.)</p>
<p>But try to open the save file on a machine without the package installed, like my Windows laptop, and you get:</p>
<pre><code>Error in loadNamespace(name) : there is no package called 'multicore'</code></pre>
<p>There is no way of getting to your precious data without installing the missing package.</p>
<p>If the package has been withdrawn or is no longer available then your data is basically lost.</p>
<h2 id="what-can-you-do" class="anchored">What can you do?</h2>
<p>Some suggestions from the helpful people on R-help:</p>
<ol type="1">
<li><p>(Uwe Ligges): You could try to rewrite <code>./src/main/saveload.R</code> and <code>serialize.R</code> to extract only the parts you need. “This is probably not worth the effort.”</p></li>
<li><p>(Prof.&nbsp;Brian Ripley): You could try installing the missing package; <code>R CMD INSTALL --fake</code> should be sufficient to let you load the data. Also suggests that the proposal above would be very hard indeed.</p></li>
<li><p>(Martin Morgan): Don’t store package functions with your code.</p></li>
</ol>
<p>That is three good answers from three of the heavy-weights in the R community. Thank you all!</p>
<p>Martin’s comment is worth expanding. We can change the above example to:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multicore"</span>)</span>
<span id="cb3-2">computeFunction <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(...) {</span>
<span id="cb3-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">require</span>(multicore)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mclapply</span>(...)</span>
<span id="cb3-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(...) </span>
<span id="cb3-5">}</span>
<span id="cb3-6">a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun =</span> computeFunction)</span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(a, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a.RData"</span>)</span></code></pre></div></div>
<p>Now everything works fine! No data is horribly lost: the file loads fine on the {multicore}-less machine.</p>
<p>And for the more realistic example, I had been using <code>caret::rfe</code> as Martin knew in the example he provided:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caret"</span>)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(BloodBrain)</span>
<span id="cb4-3"></span>
<span id="cb4-4">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(bbbDescr[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nearZeroVar</span>(bbbDescr)])</span>
<span id="cb4-5">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findCorrelation</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(x), .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)]</span>
<span id="cb4-6">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(x)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-9">lmProfile <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, logBBB,</span>
<span id="cb4-10">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>),</span>
<span id="cb4-11">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">functions =</span> lmFuncs,</span>
<span id="cb4-12">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">number =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb4-13">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">computeFunction =</span> mclapply))</span>
<span id="cb4-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(lmProfile, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lmProfile.RData"</span>)</span></code></pre></div></div>
<p>Slightly less obvious that there is a reference to the external namespace in this code, but easy enough to see if you know what to look for.</p>
<p>For old files I will use the <code>R CMD INSTALL --fake</code> suggestion, but for new data I am going with the last approach and using a <code>computeFunction</code> like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### MCCompute: A computeFunction for caret::rfeControl and caret::trainControl </span></span>
<span id="cb5-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### that does not leave a reference to the multicore package in the save file</span></span>
<span id="cb5-3">MCCompute <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(X, FUN, ...) {</span>
<span id="cb5-4">    FUN <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match.fun</span>(FUN)</span>
<span id="cb5-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.vector</span>(X) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.object</span>(X)) </span>
<span id="cb5-6">        X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(X)</span>
<span id="cb5-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multicore"</span>)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mclapply</span>(X, FUN, ...)</span>
<span id="cb5-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(X, FUN, ...)</span>
<span id="cb5-9">}</span></code></pre></div></div>
<p>I know that Max Kuhn is rewriting the caret package which should make this a moot point in the near future for that specific case. But the indirection approach is generally useful and will also be relevant in other situations.</p>
<h2 id="recommendations" class="anchored">Recommendations</h2>
<p>My recommendations:</p>
<ol type="1">
<li><p>Save data in a data format, not using the <code>save()</code> function which is really for objects (data and code). Suitable formats include CSV and variants, <a href="https://cran.r-project.org/web/packages/h5/index.html">HDF5</a>, and <a href="http://cran.r-project.org/web/packages/ncdf4/index.html">CDF</a>, as well as others.</p></li>
<li><p>Avoid references to packages in your objects by using the one level indirection trick exemplified by the <code>MCCompute</code> function shown.</p></li>
</ol>
<p>What is your approach? Suggestions in the comments below, please.</p>



 ]]></description>
  <category>R</category>
  <guid>https://www.cybaea.net/Journal/2011/08/23/A-warning-on-the-R-save-format.html</guid>
  <pubDate>Tue, 23 Aug 2011 06:20:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/wide/Rlogo.svg" medium="image" type="image/svg+xml"/>
</item>
<item>
  <title>Spreadsheet errors</title>
  <link>https://www.cybaea.net/Journal/2011/04/20/Spreadsheet-errors.html</link>
  <description><![CDATA[ 






<p>For my sins, I have done more than my fair share of analysis in Excel. I am quite capable of building and maintaining 130Mb spreadsheets (I had a dozen of them for one client). Excel is pretty much installed everywhere, so it is sometimes the only way to get started getting commercial value of the data in the organisation. But I don’t like it and let’s have a look at one reason why. In order not to always pick on Microsoft, we use another application, but you get the same results with Excel.</p>
<div class="float-lg-end ms-3 p-1" style="overflow-y: scroll; height: 20em">
<table class="excel">
<thead>
<tr>
<th>
Y
</th>
<th>
X1
</th>
<th>
X2
</th>
<th>
X3
</th>
<th>
X4
</th>
</tr>
</thead>
<tbody style="text-align: right;">
<tr>
<td>
5.88
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
2.56
</td>
<td>
6
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
11.11
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
0.79
</td>
<td>
6
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
6
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
0
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
15.6
</td>
<td>
8
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
3.7
</td>
<td>
4
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
8.49
</td>
<td>
3
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
51.2
</td>
<td>
6
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
14.2
</td>
<td>
7
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
7.14
</td>
<td>
5
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
4.2
</td>
<td>
7
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
6.15
</td>
<td>
4
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
10.46
</td>
<td>
6
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
8
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
10.42
</td>
<td>
2
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
17.36
</td>
<td>
5
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
13.41
</td>
<td>
8
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
41.67
</td>
<td>
0
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
2.78
</td>
<td>
0
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
2.98
</td>
<td>
8
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
9.62
</td>
<td>
7
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
0
</td>
<td>
1
</td>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
4.65
</td>
<td>
5
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
3.13
</td>
<td>
3
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
24.58
</td>
<td>
6
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
1
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
5.56
</td>
<td>
4
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
9.26
</td>
<td>
3
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
0
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
0
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
3.13
</td>
<td>
1
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
0
</td>
<td>
1
</td>
<td>
0
</td>
<td>
2
</td>
</tr>
<tr>
<td>
7.56
</td>
<td>
5
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
9.93
</td>
<td>
6
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
8
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
16.67
</td>
<td>
6
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
16.89
</td>
<td>
7
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
13.71
</td>
<td>
6
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
6.35
</td>
<td>
5
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
2.5
</td>
<td>
3
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
2.47
</td>
<td>
7
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
21.74
</td>
<td>
3
</td>
<td>
0
</td>
<td>
1
</td>
<td>
3
</td>
</tr>
<tr>
<td>
23.6
</td>
<td>
8
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
11.11
</td>
<td>
8
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
7
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
3.57
</td>
<td>
8
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
2.9
</td>
<td>
5
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
2.94
</td>
<td>
3
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
2.42
</td>
<td>
8
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
18.75
</td>
<td>
4
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
0.00
</td>
<td>
5
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
<tr>
<td>
2.27
</td>
<td>
3
</td>
<td>
0
</td>
<td>
0
</td>
<td>
4
</td>
</tr>
</tbody>
</table>
</div>
<p>Spreadsheets are good for some things, but analysing data is not one of them. The example data in the table on the right is from Jeffrey S. Simonoff, “<a href="https://web.archive.org/web/20220610034848/http://pages.stern.nyu.edu/~jsimonof/classes/1305/pdf/excelreg.pdf">Statistical analysis using Microsoft Excel</a>” (2008), and looks at first (and maybe even second) glance like a reasonable set of observations.</p>
<p>However, the predictors are (accidentally) collinear so no meaningful fit is possible, unless one of them are dropped. We see that very easily if we try to do the analysis using the <a href="http://www.r-project.org/">R statistical computing and analysis platform</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.delim</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"clipboard"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read DATA range from clipboard</span></span>
<span id="cb1-2"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d))</span>
<span id="cb1-3"></span>
<span id="cb1-4">Call<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d)</span>
<span id="cb1-6"></span>
<span id="cb1-7">Residuals<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb1-8">    Min      <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>Q  Median      <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>Q     Max </span>
<span id="cb1-9"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.222</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.821</span>  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.546</span>   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.171</span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">40.750</span> </span>
<span id="cb1-10"></span>
<span id="cb1-11">Coefficients<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> not defined because of singularities)</span>
<span id="cb1-12">            Estimate Std. Error t value <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Pr</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>)</span>
<span id="cb1-13">(Intercept)   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.1945</span>     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.9749</span>   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.055</span>    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.296</span></span>
<span id="cb1-14">X1            <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3862</span>     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5652</span>   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.683</span>    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.497</span></span>
<span id="cb1-15">X2            <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2308</span>     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.1590</span>   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.073</span>    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.942</span></span>
<span id="cb1-16">X3            <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.7072</span>     <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.9922</span>   <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.239</span>    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.221</span></span>
<span id="cb1-17">X4                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>         <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>       <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb1-18"></span>
<span id="cb1-19">Residual standard error<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.14</span> on <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> degrees of freedom</span>
<span id="cb1-20">Multiple R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>squared<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.04767</span>,    Adjusted R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>squared<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.009466</span> </span>
<span id="cb1-21">F<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>statistic<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8343</span> on <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> and <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> DF,  p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4814</span></span></code></pre></div></div>
<p>Everybody likes to pick on Excel, so let us load the data into version 3.3.2 of <a href="http://www.libreoffice.org/">LibreOffice</a>, the free Open Source personal productivity suite, instead. It faithfully implements many of the worst features of Excel. You can grab a copy of the spreadsheet <a href="../../../../files/GS-spreadsheet-error.ods">GS-spreadsheet-error.ods</a> yourself and see the results. The relevant function in both Excel and LibreOffice for linear regression is <code>LINEST</code> and applying it to the data set give us:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/files/GS-spreadsheet-error-1.png" class="img-fluid figure-img"></p>
<figcaption>Using LINEST function on X1, X2, X3, and X4</figcaption>
</figure>
</div>
<p>Of the 16 values returned by the function, fully 12 of them are incorrect (highlighted in red), and the ‘#VALUE!’ entries are the only thing that suggests we may have a problem. (The ‘#N/A’ values are a feature of the function and not a problem.) Excluding the X4 values from the function call gives meaningful (and correct) results:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/files/GS-spreadsheet-error-2.png" class="img-fluid figure-img"></p>
<figcaption>Using LINEST function on X1, X2, and X3</figcaption>
</figure>
</div>
<p>There is so much wrong with doing even this trivial analysis in a spreadsheet that it is hard to know where to start. Some of the problems:</p>
<dl>
<dt>
Garbage results instead of errors
</dt>
<dd>
Instead of giving meaningful errors or warnings, the spreadsheets simply produce garbage results. This is nearly impossible to debug.
</dd>
<dt>
No help on how to correct the problem
</dt>
<dd>
In the erroneous results of the first figure, there is no clue, no hint, no help to figure out how to correct the problem. You could argue about R correcting the issue ”automagically”, but at least it finds a solution to the problem and tells you about it.
</dd>
<dt>
Error prone output formats
</dt>
<dd>
I put in the row and column headings because otherwise it is just too hard to read the data. Where does the function stuff the F statistics again?
</dd>
</dl>
<p>And don’t get me started on version control and documentation. Don’t even mention that the maths in Excel are wrong. Don’t bring up the list of academically researched <a href="http://www.eusprig.org/horror-stories.htm">spreadsheet horror stories</a>. Just remember: Friends do not let friends do data analysis in spreadsheets.</p>



 ]]></description>
  <category>data</category>
  <category>R</category>
  <guid>https://www.cybaea.net/Journal/2011/04/20/Spreadsheet-errors.html</guid>
  <pubDate>Wed, 20 Apr 2011 10:19:00 GMT</pubDate>
</item>
<item>
  <title>Getting started with the Heritage Health Price competition</title>
  <link>https://www.cybaea.net/Journal/2011/04/08/Getting-started-with-the-Heritage-Health-Price-competition.html</link>
  <description><![CDATA[ 






<p>The US$ 3 million <a href="https://web.archive.org/web/20110420181947/http://www.heritagehealthprize.com/c/hhp">Heritage Health Price</a> <span class="fw-light">[link to archive.org]</span> competition is on, so we take a look at how to get started using the R statistical computing and analysis platform.</p>
<p>We do not have the full set of data yet, so this is a simple warm-up session to predict the days in hospital in year 2 based on the year 1 data.</p>
<h2 id="prerequisites" class="anchored">Prerequisites</h2>
<p>Obviously you need to have <a href="http://www.r-project.org/">R</a> installed, and you should also have signed up for the competition (be sure to read the terms carefully) and downloaded and extracted the release 1 data file.</p>
<h2 id="data-preparation" class="anchored">Data preparation</h2>
<p>Let’s load the data into R and do some basic housekeeping:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb1-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## example001.R - simple benchmarks for the HHP</span></span>
<span id="cb1-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2011 CYBAEA Limited - http://www.cybaea.net/</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##############################</span></span>
<span id="cb1-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">#### DATA PREPARATION</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##++++</span></span>
<span id="cb1-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Members</span></span>
<span id="cb1-10">members   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HHP_release1/Members_Y1.csv"</span>,</span>
<span id="cb1-11">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb1-12">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment.char =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##----</span></span>
<span id="cb1-14"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##++++</span></span>
<span id="cb1-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Claims</span></span>
<span id="cb1-16">claims.Y1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HHP_release1/Claims_Y1.csv"</span>,</span>
<span id="cb1-17">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-18">                          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>),</span>
<span id="cb1-19">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># paydelay</span></span>
<span id="cb1-20">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"character"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LengthOfStay</span></span>
<span id="cb1-21">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"character"</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dsfs</span></span>
<span id="cb1-22">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>,     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PrimaryConditionGroup</span></span>
<span id="cb1-23">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"character"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CharlsonIndex</span></span>
<span id="cb1-24">                          ),</span>
<span id="cb1-25">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment.char =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-26"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Utility function</span></span>
<span id="cb1-27">make.numeric <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(cv, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean) {</span>
<span id="cb1-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### make a character vector numeric by splitting on '-'</span></span>
<span id="cb1-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">strsplit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[^[:digit:]]+"</span>,</span>
<span id="cb1-30">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>,</span>
<span id="cb1-31">                         cv,</span>
<span id="cb1-32">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perl =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-33">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>,</span>
<span id="cb1-34">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-35">           <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">FUN</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(x)))</span>
<span id="cb1-36">}</span>
<span id="cb1-37"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Length of stay as days</span></span>
<span id="cb1-38">{</span>
<span id="cb1-39">    z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.numeric</span>(claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>LengthOfStay)</span>
<span id="cb1-40">    z.week <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"week"</span>, claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>LengthOfStay, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-41">    z[z.week] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> z[z.week] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Weeks are 7 days</span></span>
<span id="cb1-42">    z[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.nan</span>(z)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb1-43">    claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>LengthOfStay.days <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> z</span>
<span id="cb1-44">}</span>
<span id="cb1-45">los.levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1 day"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%d days"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>),</span>
<span id="cb1-46">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1- 2 weeks"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2- 4 weeks"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4- 8 weeks"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"8-12 weeks"</span>,</span>
<span id="cb1-47">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12-26 weeks"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"26+ weeks"</span>)</span>
<span id="cb1-48"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>LengthOfStay <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> los.levels))</span>
<span id="cb1-49">claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>LengthOfStay <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>LengthOfStay,</span>
<span id="cb1-50">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> los.levels,</span>
<span id="cb1-51">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0 days"</span>, los.levels[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]),</span>
<span id="cb1-52">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ordered =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-53"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Months since first claim</span></span>
<span id="cb1-54">claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dsfs.months <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.numeric</span>(claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dsfs)</span>
<span id="cb1-55"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## dsfs is an ordered factor and gives the ordering of the claims</span></span>
<span id="cb1-56">dsfs.levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0- 1 month"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%d-%2d months"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb1-57">claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dsfs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>dsfs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> dsfs.levels, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ordered =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-58"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Index as numeric</span></span>
<span id="cb1-59">claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CharlsonIndex.numeric <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.numeric</span>(claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CharlsonIndex)</span>
<span id="cb1-60">claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CharlsonIndex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(claims.Y1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CharlsonIndex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ordered =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-61"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##----</span></span>
<span id="cb1-62"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##++++</span></span>
<span id="cb1-63"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Days in hospital</span></span>
<span id="cb1-64">dih.Y2    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HHP_release1/DayInHospital_Y2.csv"</span>,</span>
<span id="cb1-65">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factor"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>),</span>
<span id="cb1-66">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comment.char =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-67"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(dih.Y2)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MemberID"</span>          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fix broken file</span></span>
<span id="cb1-68"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##----</span></span>
<span id="cb1-69"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(members, claims.Y1, dih.Y2,</span>
<span id="cb1-70">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HHPR1.RData"</span>)</span>
<span id="cb1-71"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##############################</span></span></code></pre></div></div>
<h2 id="scoring" class="anchored">Scoring</h2>
<p>We will need a function to score our predictions p against the actual values a. The formula is on the evaluation page and we implement it as:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb2-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## example001.R - simple benchmarks for the HHP</span></span>
<span id="cb2-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2011 CYBAEA Limited - http://www.cybaea.net/</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##############################</span></span>
<span id="cb2-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">#### FUNCTION TO CALCULATE SCORE</span></span>
<span id="cb2-7">HPPScore <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p, a) {</span>
<span id="cb2-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### Scorng function after</span></span>
<span id="cb2-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### http://www.heritagehealthprize.com/c/hhp/Details/Evaluation</span></span>
<span id="cb2-10"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### Base 10 log from http://www.heritagehealthprize.com/forums/default.aspx?g=posts&amp;m=2226#post2226</span></span>
<span id="cb2-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> a, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb2-12">}</span>
<span id="cb2-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##############################</span></span></code></pre></div></div>
<h2 id="the-simplest-benchmarks" class="anchored">The simplest benchmarks</h2>
<p>The simplest models don’t really model at all: they just use the average and are simple benchmarks.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb3-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## example001.R - simple benchmarks for the HHP</span></span>
<span id="cb3-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2011 CYBAEA Limited - http://www.cybaea.net/</span></span>
<span id="cb3-4"></span>
<span id="cb3-5">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dih.Y2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>DaysInHospital_Y2           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Actual</span></span>
<span id="cb3-6">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(y), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NROW</span>(dih.Y2))</span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Score using mean  : %8.6f</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HPPScore</span>(p, y)))</span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Score using mean  : 0.278725</span></span>
<span id="cb3-9"></span>
<span id="cb3-10">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(y), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NROW</span>(dih.Y2))</span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Score using median: %8.6f</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HPPScore</span>(p, y)))</span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Score using median: 0.267969</span></span></code></pre></div></div>
<h2 id="simple-single-variable-linear-models" class="anchored">Simple single-variable linear models</h2>
<p>OK, a model that doesn’t use past data isn’t much of a model, so let’s improve on that:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb4-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## example001.R - simple benchmarks for the HHP</span></span>
<span id="cb4-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2011 CYBAEA Limited - http://www.cybaea.net/</span></span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reshape2"</span>)</span>
<span id="cb4-5"></span>
<span id="cb4-6">vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dcast</span>(claims.Y1, MemberID <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., sum, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value_var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LengthOfStay.days"</span>)</span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(vars)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LengthOfStay"</span></span>
<span id="cb4-8">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(vars, dih.Y2)</span>
<span id="cb4-9"></span>
<span id="cb4-10">model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(DaysInHospital_Y2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> LengthOfStay, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb4-11">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model)</span>
<span id="cb4-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Score using lm(LengthOfStay): %8.6f</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HPPScore</span>(p, y)))</span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Score using lm(LengthOfStay): 0.279062</span></span>
<span id="cb4-14"></span>
<span id="cb4-15">model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(DaysInHospital_Y2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> LengthOfStay,</span>
<span id="cb4-16">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quasipoisson</span>(),</span>
<span id="cb4-17">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb4-18">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>)</span>
<span id="cb4-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Score using glm(LengthOfStay): %8.6f</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HPPScore</span>(p, y)))</span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Score using glm(LengthOfStay): 0.278914</span></span></code></pre></div></div>
<p>Let the competition begin.</p>



 ]]></description>
  <category>R</category>
  <category>data</category>
  <guid>https://www.cybaea.net/Journal/2011/04/08/Getting-started-with-the-Heritage-Health-Price-competition.html</guid>
  <pubDate>Fri, 08 Apr 2011 07:39:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/CC0/hush-naidoo-jade-photography-yo01Z-9HQAw-unsplash.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Benchmarking feature selection with Boruta and caret</title>
  <link>https://www.cybaea.net/Journal/2010/11/25/Benchmarking-feature-selection-with-Boruta-and-caret.html</link>
  <description><![CDATA[ 






<p>Feature selection is the data mining process of selecting the variables from our data set that may have an impact on the outcome we are considering. For commercial data mining, which is often characterised by having too many variables for model building, this is an important step in the analysis process. And since we often work on very large data sets the performance of our process is very important to us.</p>
<p>Having looked at <a href="../15/Feature-selection-All-relevant-selection-with-the-Boruta-package.html">feature selection using the Boruta package</a> and <a href="../16/Feature-selection-Using-the-caret-package.html">feature selection using the caret package</a> separately, we now consider the performance of the two approaches.</p>
<p>For our tests we will use an artificially constructed trivial data sets that the automated process should have no problems with (but we will be disappointed later on this expectation, as we will see). The data set has an equal number of normal and uniform random variables with mean 0 and variance 1 of which 20% are used for the target variable. There are 10 time as many observations as variables. We create a function to set this up:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: "fig-data"</span></span>
<span id="cb1-2">make.data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n.var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m.rand =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m.obs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) {</span>
<span id="cb1-3">  n.col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> m.rand</span>
<span id="cb1-4">  n.obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> m.obs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-5">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n.col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n.obs),</span>
<span id="cb1-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n.obs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> n.col</span>
<span id="cb1-8">    ),</span>
<span id="cb1-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">U =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(</span>
<span id="cb1-10">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n.col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n.obs,</span>
<span id="cb1-11">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-12">    ), n.obs, n.col)</span>
<span id="cb1-13">  )</span>
<span id="cb1-14">  deps.n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n.var</span>
<span id="cb1-15">  deps.u <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n.col)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(n.var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n.col)</span>
<span id="cb1-16">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(x[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(deps.n, deps.u)]))</span>
<span id="cb1-17">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P"</span>)))</span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vars"</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(x)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(deps.n, deps.u)]</span>
<span id="cb1-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(x)</span>
<span id="cb1-20">}</span></code></pre></div></div>
<h2 id="the-boruta-package" class="anchored">The Boruta package</h2>
<p>Then we run a test using the Boruta package for different sizes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb2-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## bench.R - benchmark Boruta package</span></span>
<span id="cb2-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2010 Allan Engelhardt (https://www.cybaea.net/)</span></span>
<span id="cb2-4">run.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bench-1"</span></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Boruta"</span>)</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-8"></span>
<span id="cb2-9">sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb2-10">n.sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(sizes)</span>
<span id="cb2-11">bench <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.vars =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">elapsed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wrong =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb2-12">file.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(run.name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RData"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(sizes)) {</span>
<span id="cb2-15">  size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sizes[n]</span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[%s] Size = %3d: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.time</span>()), size))</span>
<span id="cb2-17">  tries <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb2-18">  n.right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-19">  n.wrong <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-20">  elapsed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-21">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (try <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>tries) {</span>
<span id="cb2-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(tries <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> try, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb2-23">    x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.data</span>(size)</span>
<span id="cb2-24">    x.vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vars"</span>)</span>
<span id="cb2-25">    elapsed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> elapsed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-26">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb2-27">        b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Boruta</span>(x[, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NCOL</span>(x)], x[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NCOL</span>(x)])</span>
<span id="cb2-28">      })[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsed"</span>]</span>
<span id="cb2-29">    b.vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>finalDecision)[b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>finalDecision <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rejected"</span>]</span>
<span id="cb2-30">    n.right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.right <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(b.vars, x.vars))</span>
<span id="cb2-31">    n.wrong <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.wrong <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(b.vars, x.vars))</span>
<span id="cb2-32">  }</span>
<span id="cb2-33">  elapsed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> elapsed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tries</span>
<span id="cb2-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" Elapsed = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(elapsed, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" seconds</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb2-35">  n.right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.right <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tries</span>
<span id="cb2-36">  n.wrong <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.wrong <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tries</span>
<span id="cb2-37">  bench[n, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(size, elapsed, n.right, n.wrong)</span>
<span id="cb2-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(bench, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> file.name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ascii =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compress =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-39">}</span>
<span id="cb2-40"></span>
<span id="cb2-41"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(bench)</span></code></pre></div></div>
<p>As it turned out, our expectations for the size of data set we could handle were wildly optimistic and we killed the process at size 30. We add to the data set a field with the total number of variables in the x data set and plot the results.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">load</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bench-1.RData"</span>)</span>
<span id="cb3-2">bench <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">na.omit</span>(bench)</span>
<span id="cb3-3">bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n.elem <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n.var<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e3</span></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(elapsed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> n.elem,</span>
<span id="cb3-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bench, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b"</span>,</span>
<span id="cb3-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Feature selections with Boruta"</span>,</span>
<span id="cb3-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sub =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elapsed time versus number of data elements"</span>,</span>
<span id="cb3-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xy"</span>,</span>
<span id="cb3-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elements in data set"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elapsed time (seconds)"</span></span>
<span id="cb3-10">)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/Boruta-feature-benchmark.png" class="img-fluid figure-img"></p>
<figcaption>Benchmarking results for feature selection with Boruta package shows linear scaling (slope is 1.01 with standard error 0.025 and adjusted R² 0.993)</figcaption>
</figure>
</div>
<p>A quick check using <code>summary(lm(log(elapsed) ~ log(n.elem), data = bench))</code> shows us a linear scaling with the number of elements (slope is 1.01 with standard error 0.025 and adjusted R² 0.993). The algorithm selects all the right features up to <code>n.vars = 10</code> when it starts to miss some of them:</p>
<table class="table">
<caption class="caption">
Benchmark results for Boruta package
</caption>
<thead>
<tr>
<th>
n.vars
</th>
<th>
right
</th>
<th>
wrong
</th>
</tr>
</thead>
<tbody style="text-align: right">
<tr>
<td>
1
</td>
<td>
2.00000
</td>
<td>
1.1000000
</td>
</tr>
<tr>
<td>
2
</td>
<td>
4.00000
</td>
<td>
1.2000000
</td>
</tr>
<tr>
<td>
3
</td>
<td>
6.00000
</td>
<td>
1.6666667
</td>
</tr>
<tr>
<td>
4
</td>
<td>
8.00000
</td>
<td>
1.3333333
</td>
</tr>
<tr>
<td>
5
</td>
<td>
10.00000
</td>
<td>
1.6666667
</td>
</tr>
<tr>
<td>
6
</td>
<td>
12.00000
</td>
<td>
1.3333333
</td>
</tr>
<tr>
<td>
7
</td>
<td>
14.00000
</td>
<td>
1.0000000
</td>
</tr>
<tr>
<td>
8
</td>
<td>
16.00000
</td>
<td>
1.3333333
</td>
</tr>
<tr>
<td>
9
</td>
<td>
18.00000
</td>
<td>
0.6666667
</td>
</tr>
<tr>
<td>
10
</td>
<td>
20.00000
</td>
<td>
0.3333333
</td>
</tr>
<tr>
<td>
20
</td>
<td>
39.33333
</td>
<td>
0.0000000
</td>
</tr>
<tr>
<td>
30
</td>
<td>
56.33333
</td>
<td>
0.0000000
</td>
</tr>
</tbody>
</table>
<p>A higher accuracy in the feature selection for the larger problems could presumably be achieved by adjusting the <code>maxRuns</code> and perhaps confidence parameters on the <code>Boruta</code> call.</p>
<p>In summary, the Boruta package performs well up to about 20 features out of 100 (<code>n.vars = 10</code>) which runs in about 11 minutes on my machine. If we changed the technical implementation to support multicore, MPI, and other parallel frameworks, then the out of the box settings would be useful up to <code>n.vars</code> of 20 or 30 (40-60 features out of 200-300) which an 8-core machine should be able to complete in 20 minutes or so.</p>
<p>This is still a lot less than the size of data sets we normally work with. (Our usual benchmark is 15,000 variables and 50,000 observations.)</p>
<h2 id="the-caret-package" class="anchored">The caret package</h2>
<p>One of the nice features of the caret package is that is supports most parallel processing frameworks out of the box, but for comparison with the previous analysis we will (somewhat unfairly) not use that here. The setup is then quite simple, using the same make.data function as before.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/Rscript</span></span>
<span id="cb4-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## bench.R - benchmark caret package</span></span>
<span id="cb4-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2010 Allan Engelhardt (http://www.cybaea.net/)</span></span>
<span id="cb4-4">run.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bench-2"</span></span>
<span id="cb4-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caret"</span>)</span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"randomForest"</span>)</span>
<span id="cb4-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-8"></span>
<span id="cb4-9">control <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfeControl</span>(</span>
<span id="cb4-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">functions =</span> rfFuncs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnResamp =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"final"</span></span>
<span id="cb4-12">)</span>
<span id="cb4-13"></span>
<span id="cb4-14"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## if ( require("multicore", quietly = TRUE, warn.conflicts = FALSE) ) {</span></span>
<span id="cb4-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##     control$workers &lt;- multicore:::detectCores()</span></span>
<span id="cb4-16"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##     control$computeFunction &lt;- mclapply</span></span>
<span id="cb4-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##     control$computeArgs &lt;- list(mc.preschedule = FALSE, mc.set.seed = FALSE)</span></span>
<span id="cb4-18"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## }</span></span>
<span id="cb4-19"></span>
<span id="cb4-20">our.sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span>
<span id="cb4-21">n.sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(our.sizes)</span>
<span id="cb4-22">bench <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.vars =</span> our.sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">elapsed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wrong =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb4-23">file.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(run.name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RData"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span>
<span id="cb4-24"></span>
<span id="cb4-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(our.sizes)) {</span>
<span id="cb4-26">  size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> our.sizes[n]</span>
<span id="cb4-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[%s] Size = %3d: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.time</span>()), size))</span>
<span id="cb4-28">  tries <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> size, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb4-29">  n.right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb4-30">  n.wrong <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb4-31">  elapsed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb4-32">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (try <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>tries) {</span>
<span id="cb4-33">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(tries <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> try, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb4-34">    x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.data</span>(size)</span>
<span id="cb4-35">    x.vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vars"</span>)</span>
<span id="cb4-36">    elapsed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> elapsed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-37">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb4-38">        p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x[, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NCOL</span>(x)], x[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NCOL</span>(x)],</span>
<span id="cb4-39">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> size), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control</span>
<span id="cb4-40">        )</span>
<span id="cb4-41">      })[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsed"</span>]</span>
<span id="cb4-42">    p.vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(p)</span>
<span id="cb4-43">    n.right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.right <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(p.vars, x.vars))</span>
<span id="cb4-44">    n.wrong <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.wrong <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(p.vars, x.vars))</span>
<span id="cb4-45">  }</span>
<span id="cb4-46">  elapsed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> elapsed <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tries</span>
<span id="cb4-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" Elapsed = "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(elapsed, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" seconds</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb4-48">  n.right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.right <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tries</span>
<span id="cb4-49">  n.wrong <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.wrong <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tries</span>
<span id="cb4-50">  bench[n, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(size, elapsed, n.right, n.wrong)</span>
<span id="cb4-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(bench, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> file.name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ascii =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compress =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb4-52">}</span>
<span id="cb4-53"></span>
<span id="cb4-54"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(bench)</span></code></pre></div></div>
<p>This uses the <code>randomForest</code> classifier from the package of the same name. To use the ipredbagg bagging classifier from Andrea Peters and Torsten Hothorn’s <a href="https://cran.r-project.org/package=ipred">ipred: Improved Predictors</a> package we simply change the control object to:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">control <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfeControl</span>(</span>
<span id="cb5-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">functions =</span> treebagFuncs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnResamp =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"final"</span></span>
<span id="cb5-4">)</span></code></pre></div></div>
<p>As usual, we were widely optimistic in our guesses for the size of problems we could handle, and had to abort the run.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/caret-rf-feature-benchmark.png" class="img-fluid figure-img"></p>
<figcaption>Benchmarking results for feature selection with caret package using randomForest classifier (slope is 1.17 with standard error 0.024 and adjusted R² 0.996)</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/caret-treebag-feature-benchmark.png" class="img-fluid figure-img"></p>
<figcaption>Benchmarking results for feature selection with caret package using treebag classifier shows non-power behaviour (nevertheless, a linear log-log fit gives a slope of 1.12 with standard error 0.067 and adjusted R² 0.96)</figcaption>
</figure>
</div>
<table class="table">
<caption class="caption">
Benchmark results for caret package using randomForest classifier
</caption>
<thead>
<tr>
<th>
n.vars
</th>
<th>
right
</th>
<th>
wrong
</th>
</tr>
</thead>
<tbody style="text-align: right">
<tr>
<td>
2
</td>
<td>
3.20000
</td>
<td>
3.200000
</td>
</tr>
<tr>
<td>
3
</td>
<td>
5.00000
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
4
</td>
<td>
7.00000
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
5
</td>
<td>
9.00000
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
6
</td>
<td>
11.00000
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
7
</td>
<td>
13.00000
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
8
</td>
<td>
14.66667
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
9
</td>
<td>
16.66667
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
10
</td>
<td>
19.00000
</td>
<td>
0.000000
</td>
</tr>
<tr>
<td>
20
</td>
<td>
38.66667
</td>
<td>
1.333333
</td>
</tr>
<tr>
<td>
30
</td>
<td>
54.00000
</td>
<td>
86.000000
</td>
</tr>
</tbody>
</table>
<table class="table">
<caption class="caption">
Benchmark results for caret package using ipredbagg classifier
</caption>
<thead>
<tr>
<th>
n.vars
</th>
<th>
right
</th>
<th>
wrong
</th>
</tr>
</thead>
<tbody style="text-align: right">
<tr>
<td>
2
</td>
<td>
3.00000
</td>
<td>
0
</td>
</tr>
<tr>
<td>
3
</td>
<td>
5.00000
</td>
<td>
0
</td>
</tr>
<tr>
<td>
4
</td>
<td>
7.00000
</td>
<td>
0
</td>
</tr>
<tr>
<td>
5
</td>
<td>
9.00000
</td>
<td>
0
</td>
</tr>
<tr>
<td>
6
</td>
<td>
10.33333
</td>
<td>
0
</td>
</tr>
<tr>
<td>
7
</td>
<td>
13.00000
</td>
<td>
0
</td>
</tr>
<tr>
<td>
8
</td>
<td>
14.33333
</td>
<td>
0
</td>
</tr>
<tr>
<td>
9
</td>
<td>
16.00000
</td>
<td>
0
</td>
</tr>
<tr>
<td>
10
</td>
<td>
18.66667
</td>
<td>
0
</td>
</tr>
<tr>
<td>
20
</td>
<td>
35.33333
</td>
<td>
0
</td>
</tr>
<tr>
<td>
30
</td>
<td>
54.33333
</td>
<td>
0
</td>
</tr>
<tr>
<td>
40
</td>
<td>
69.66667
</td>
<td>
0
</td>
</tr>
</tbody>
</table>
<p>Remember that the right number of significant features are <code>2 * n.vars</code> and we see that the caret package apparently always miss one feature in its selection, which is very odd and possibly a bug. It is less likely to select the wrong features than Boruta, but that could be partially due to “Tentative” data in Boruta. Timing-wise, performance is a little worse in the non-parallel situation but realistically of course a lot better than Boruta depending on the number of cores on your processor or nodes in your cluster.</p>
<p>Neither approach is suitable out of the box for the sizes of data sets that we normally work with.</p>



 ]]></description>
  <category>R</category>
  <category>data</category>
  <guid>https://www.cybaea.net/Journal/2010/11/25/Benchmarking-feature-selection-with-Boruta-and-caret.html</guid>
  <pubDate>Thu, 25 Nov 2010 13:43:00 GMT</pubDate>
</item>
<item>
  <title>Feature selection: Using the caret package</title>
  <link>https://www.cybaea.net/Journal/2010/11/16/Feature-selection-Using-the-caret-package.html</link>
  <description><![CDATA[ 






<p>Feature selection is an important step for practical commercial data mining which is often characterised by data sets with far too many variables for model building. In a previous post we looked at <a href="../15-Feature-selection-All-relevant-selection-with-the-Boruta-package.html">all-relevant feature selection using the Boruta package</a> while in this post we consider the same (artificial, toy) examples using the <a href="https://cran.r-project.org/package=caret">caret</a> package. Max Kuhn kindly listed me as a contributor for some performance enhancements I submitted, but the genius behind the package is all his.</p>
<p>The caret package provides a very flexible framework for the analysis as we shall see, but first we set up the artificial test data set as in the previous article.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Feature-bc.R - Compare Boruta and caret feature selection</span></span>
<span id="cb1-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2010 Allan Engelhardt (http://www.cybaea.net/)</span></span>
<span id="cb1-3">run.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"feature-bc"</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"caret"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Load early to get the warnings out of the way:</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"randomForest"</span>)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ipred"</span>)</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gbm"</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Set up artificial test data for our analysis</span></span>
<span id="cb1-14">n.var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb1-15">n.obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span>
<span id="cb1-16">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n.var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> n.obs), n.obs, n.var))</span>
<span id="cb1-17">n.dep <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(n.var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of dependent variables is"</span>, n.dep, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-19">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(n.dep<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## These are our four test targets</span></span>
<span id="cb1-22">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>))</span>
<span id="cb1-23">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(x[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n.dep]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> m) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>)</span>
<span id="cb1-24">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>)</span>
<span id="cb1-25">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(x[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n.dep] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-26">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(x[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n.dep] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<p>The flexibility of the caret package is to a large extent implemented by using control objects. Here we specify to use the <code>randomForest</code> classification algorithm (which is also what Boruta uses) and if the multicore package is available then we use that for extra perfomance (you can also use MPI etc – see the documentation):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">control <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfeControl</span>(</span>
<span id="cb2-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">functions =</span> rfFuncs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"boot"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb2-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnResamp =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"final"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">number =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb2-4">)</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multicore"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quietly =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)) {</span>
<span id="cb2-7">  control<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>workers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> multicore<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">detectCores</span>()</span>
<span id="cb2-8">  control<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>computeFunction <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mclapply</span>
<span id="cb2-9">  control<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>computeArgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.preschedule =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.set.seed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-10">}</span></code></pre></div></div>
<p>We will consider from one to six features (using the <code>sizes</code> variable) and then we simply let it lose:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">sizes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Use randomForest for prediction</span></span>
<span id="cb3-4">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rf     : Profile 1 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-6">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rf     : Profile 2 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-8">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb3-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rf     : Profile 3 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-10">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rf     : Profile 4 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<p>The results are:</p>
<pre><code>rf     : Profile 1 predictors: V.1 V.16 V.6
rf     : Profile 2 predictors: V.1 V.2
rf     : Profile 3 predictors: V.4 V.1 V.2
rf     : Profile 4 predictors: V.10 V.11 V.7</code></pre>
<p>If you recall the <a href="../15/Feature-selection-All-relevant-selection-with-the-Boruta-package.html">feature selection with Boruta article</a>, then the results there were</p>
<pre><code>Profile 1: V.1, (V.16, V.17)
Profile 2: V.1, V.2, V,3, (V.8, V.9, V.4)
Profile 3: V.1, V.4, V.3, V.2, (V.7, V.6)
Profile 4: V.10, (V.11, V.13)</code></pre>
<p>To show the flexibility of caret, we can run the analysis with another of the built-in classifiers:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Use ipred::ipredbag for prediction</span></span>
<span id="cb6-2">control<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>functions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> treebagFuncs</span>
<span id="cb6-3">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treebag: Profile 1 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-5">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treebag: Profile 2 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-7">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb6-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treebag: Profile 3 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-9">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control)</span>
<span id="cb6-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treebag: Profile 4 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<p>This gives:</p>
<pre><code>treebag: Profile 1 predictors: V.1 V.16
treebag: Profile 2 predictors: V.2 V.1
treebag: Profile 3 predictors: V.1 V.3 V.2
treebag: Profile 4 predictors: V.10 V.11 V.1 V.7 V.13</code></pre>
<p>And of course, if you have your own favourite model class that is not already implemented, then you can easily do that yourself. We like gbm from the package of the same name, which is kind of silly to use here because it provides variable importance automatically as part of the fitting process, but may still be useful. It needs numeric predictors so we do:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Use gbm for prediction</span></span>
<span id="cb8-2">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-3">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-4">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-5">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-6"></span>
<span id="cb8-7">gbmFuncs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> treebagFuncs</span>
<span id="cb8-8">gbmFuncs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, y, first, last, ...) {</span>
<span id="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gbm"</span>)</span>
<span id="cb8-10">  n.levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(y))</span>
<span id="cb8-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n.levels <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) {</span>
<span id="cb8-12">    distribution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bernoulli"</span></span>
<span id="cb8-13">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb8-14">    distribution <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gaussian"</span></span>
<span id="cb8-15">  }</span>
<span id="cb8-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gbm.fit</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">distribution =</span> distribution, ...)</span>
<span id="cb8-17">}</span>
<span id="cb8-18">gbmFuncs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pred <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(object, x) {</span>
<span id="cb8-19">  n.trees <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gbm.perf</span>(object,</span>
<span id="cb8-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.it =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb8-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OOB"</span></span>
<span id="cb8-22">  ))</span>
<span id="cb8-23">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n.trees <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) n.trees <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> object<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n.trees</span>
<span id="cb8-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(object, x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.trees =</span> n.trees, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"link"</span>)</span>
<span id="cb8-25">}</span>
<span id="cb8-26">control<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>functions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gbmFuncs</span>
<span id="cb8-27"></span>
<span id="cb8-28">n.trees <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e2</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Default value for gbm is 100</span></span>
<span id="cb8-29"></span>
<span id="cb8-30">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>,</span>
<span id="cb8-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb8-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.trees =</span> n.trees</span>
<span id="cb8-33">)</span>
<span id="cb8-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gbm    : Profile 1 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb8-35">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>,</span>
<span id="cb8-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb8-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.trees =</span> n.trees</span>
<span id="cb8-38">)</span>
<span id="cb8-39"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gbm    : Profile 2 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb8-40">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>,</span>
<span id="cb8-41">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb8-42">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.trees =</span> n.trees</span>
<span id="cb8-43">)</span>
<span id="cb8-44"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gbm    : Profile 3 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb8-45">profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rfe</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>,</span>
<span id="cb8-46">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sizes =</span> sizes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rfeControl =</span> control, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb8-47">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.trees =</span> n.trees</span>
<span id="cb8-48">)</span>
<span id="cb8-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gbm    : Profile 4 predictors:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predictors</span>(profile<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<p>And we get the results below:</p>
<pre><code>gbm    : Profile 1 predictors: V.1 V.10 V.11 V.12 V.13
gbm    : Profile 2 predictors: V.1 V.2
gbm    : Profile 3 predictors: V.4 V.1 V.2 V.3 V.7
gbm    : Profile 4 predictors: V.11 V.10 V.1 V.6 V.7 V.18</code></pre>
<p>It is all good and very flexible, for sure, but I can’t really say it is better than the Boruta approach for these simple examples.</p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{2010,
  author = {},
  title = {Feature Selection: {Using} the Caret Package},
  date = {2010-11-16},
  url = {https://www.cybaea.net/Journal/2010/11/16/Feature-selection-Using-the-caret-package.html},
  langid = {en-GB-oxendict}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-2010" class="csl-entry quarto-appendix-citeas">
<span>“Feature Selection: Using the Caret Package.”</span> 2010.
November 16. <a href="https://www.cybaea.net/Journal/2010/11/16/Feature-selection-Using-the-caret-package.html">https://www.cybaea.net/Journal/2010/11/16/Feature-selection-Using-the-caret-package.html</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>data</category>
  <guid>https://www.cybaea.net/Journal/2010/11/16/Feature-selection-Using-the-caret-package.html</guid>
  <pubDate>Tue, 16 Nov 2010 19:35:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/caret-rf-feature-benchmark.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Feature selection: All-relevant selection with the Boruta package</title>
  <link>https://www.cybaea.net/Journal/2010/11/15/Feature-selection-All-relevant-selection-with-the-Boruta-package.html</link>
  <description><![CDATA[ 






<p>Feature selection is an important step for practical commercial data mining which is often characterised by data sets with far too many variables for model building. There are two main approaches to selecting the features (variables) we will use for the analysis: the minimal-optimal feature selection which identifies a small (ideally minimal) set of variables that gives the best possible classification result (for a class of classification models) and the all-relevant feature selection which identifies all variables that are in some circumstances relevant for the classification.</p>
<p>In this article we take a first look at the problem of all-relevant feature selection using the <a href="https://www.jstatsoft.org/article/view/v036i11">Boruta package</a> by Miron B. Kursa and Witold R. Rudnicki. This package is developed for the <a href="https://www.r-project.org/">R statistical computing and analysis platform</a>.</p>
<h2 id="background" class="anchored">Background</h2>
<p>All-relevant feature selection is extremely useful for commercial data miners. We deploy it when we want to understand the mechanisms behind the behaviour or subject of interest, rather than just building a black-box predictive model. This understanding leads us to a better appreciation of our customers (or other subject under investigation) and not just how, but why they behave as they do, which is useful for all areas of the business, including strategy and product development. More narrowly, it also help us define the variables that we want to observe which is what will really make a difference in our ability to predict behaviour (as opposed to, say, run the data mining application a little longer).</p>
<p>I really like the theoretical approach that the Boruta package tries to implement. It is based on the more general idea that by adding randomness to a system and then collecting results from random samples of the bigger system, one can actually reduce the misleading impact of randomness in the original sample.</p>
<p>For the implementation, the Boruta package relies on a random forest classification algorithm. This provides an intrinsic measure of the importance of each feature, known as the Z score. While this score is not directly a statistical measure of the significance of the feature, we can compare it to random permutations of (a selection of) the variables to test if it is higher than the scores from random variables. This is the essence of the implementation in Boruta.</p>
<h2 id="the-tests" class="anchored">The tests</h2>
<p>This article is a first investigation into the performance of the Boruta package. For this initial examination we will use a test data sample that we can control so we know what is important and what is not. We will consider 200 observations of 20 normally distributed random variables:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">run.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"feature-1"</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Boruta"</span>)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Set up artificial test data for our analysis</span></span>
<span id="cb1-5">n.var <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span></span>
<span id="cb1-6">n.obs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span>
<span id="cb1-7">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">V=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n.var<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>n.obs), n.obs, n.var))</span></code></pre></div></div>
<p>Normal distribution has the advantage of simplicity, but for commercial application where highly non-normally distributed features like money spent are important may not be the best test. Nevertheless, we will use it for now and define a simple utility function before we get on to the tests:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Utility function to make plots of Boruta test results</span></span>
<span id="cb2-2">make.plots <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(b, num,</span>
<span id="cb2-3">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">true.var =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span id="cb2-4">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Boruta feature selection for test"</span>, num)) {</span>
<span id="cb2-5">    write.text <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(b, true.var) {</span>
<span id="cb2-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(true.var) ) {</span>
<span id="cb2-7">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attStats</span>(b)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>meanZ), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-8">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True vars are V.1-V."</span>,</span>
<span id="cb2-9">                     true.var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>))        </span>
<span id="cb2-10">        }</span>
<span id="cb2-11">    }</span>
<span id="cb2-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> main, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">las =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb2-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.text</span>(b, true.var)</span>
<span id="cb2-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">png</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(run.name, num, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>,</span>
<span id="cb2-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">res =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pointsize =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb2-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(b, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> main, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">las =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb2-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.text</span>(b, true.var)</span>
<span id="cb2-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dev.off</span>()</span>
<span id="cb2-19">}</span></code></pre></div></div>
<h2 id="test-1-simple-test-of-single-significant-variable" class="anchored">Test 1: Simple test of single significant variable</h2>
<p>For a simple classification based on a single variable, Boruta performs well: while it identifies three variables as being potentially important, this does include the true variable (V.1) and the plot clearly shows it as being by far the most significant.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 1. Simple test of single variable</span></span>
<span id="cb3-2">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>( <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>( x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>V<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B'</span> ) )</span>
<span id="cb3-3"></span>
<span id="cb3-4">b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Boruta</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doTrace =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.plots</span>(b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<div id="fig-1" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://www.cybaea.net/images/feature-1.1.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Simple test of Boruta feature selection with single variable
</figcaption>
</figure>
</div>
<h2 id="test-2-simple-test-of-linear-combination-of-variables" class="anchored">Test 2: Simple test of linear combination of variables</h2>
<p>With a test of a linear combination of the first four variables where the weights are decreasing from 4 to 1, we begin to get closer to the limitations of the approach.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 2. Simple test of linear combination</span></span>
<span id="cb4-2">n.dep <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(n.var<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(n.dep)</span>
<span id="cb4-4"></span>
<span id="cb4-5">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(n.dep<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-6"></span>
<span id="cb4-7">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>( <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(x[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n.dep]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> m) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span> )</span>
<span id="cb4-8">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>)</span>
<span id="cb4-9"></span>
<span id="cb4-10">b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Boruta</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doTrace =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.plots</span>(b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, n.dep)</span></code></pre></div></div>
<div id="fig-2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://www.cybaea.net/images/feature-1.2.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Simple test of Boruta feature selection with linear combination of four variables
</figcaption>
</figure>
</div>
<p>The implementation correctly identified the first three variables (with weights 4, 3, and 2, respectively) as being important, but it had the fourth variable as possible along with the two random variables V.8 and V.9. Still, six variables are more approachable than twenty.</p>
<h2 id="test-3-simple-test-of-less-linear-combination-of-four-variables" class="anchored">Test 3: Simple test of less-linear combination of four variables</h2>
<p>For this text and the following we consider less obvious combinations of the first four variables. If we just count how many of them are positive, then we get to a situation where Boruta excels (because random forests excel at this type of problem).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 3. Simple test of less-linear combination</span></span>
<span id="cb5-2">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(x[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n.dep] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>))</span>
<span id="cb5-4">b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Boruta</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doTrace =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>)</span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.plots</span>(b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, n.dep)</span></code></pre></div></div>
<div id="fig-3" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://www.cybaea.net/images/feature-1.3.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Simple test of Boruta feature selection counting the positives of four variables
</figcaption>
</figure>
</div>
<h2 id="test-4-simple-test-of-non-linear-combination" class="anchored">Test 4: Simple test of non-linear combination</h2>
<p>For a spectacular fail of the Boruta approach we will have to consider a classification in the hyperplane of the four variables. For this simple example, we simply count if there are an even or odd number of positive values among the first four variables:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 4. Simple test of non-linear combination</span></span>
<span id="cb6-2">y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(x[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n.dep] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-3">b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Boruta</span>(x, y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">doTrace =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>)</span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make.plots</span>(b<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, n.dep)</span></code></pre></div></div>
<div id="fig-4" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://www.cybaea.net/images/feature-1.4.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Simple test of Boruta feature selection with non-linear combination of four variables
</figcaption>
</figure>
</div>
<p>Ouch. The package rejects the four known significant variables. It is too hard for the random forest approach. Increasing the number of observations to 1,000 does not help though at 5,000 observations Boruta identifies the four variables right.</p>
<h2 id="limitations" class="anchored">Limitations</h2>
<p>Some limitations of the Boruta package are worth highlighting:</p>
<ol type="1">
<li><p>It only works with classification (factor) target variables. I am not sure why: as far as I remember, the random forest algorithm also provides a variable significance score when it is used as a predictor, not just when it is run as a classifier.</p></li>
<li><p>It does not handle missing (<code>NA</code>) values at all. This is quite a problem when working with real data sets, and a shame as random forests are in principle very good at handling missing values. A simple re-write of the package using the party package instead of randomForest should be able to fix this issue.</p></li>
<li><p>It does not seem to be completely stable. I have crashed it on several real-world data sets and am working on a minimal set to send to the authors.</p></li>
</ol>
<p>But this is a really promising approach, if somewhat slow on large sets. I will have a look at some real-world data in a future post.</p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{2010,
  author = {},
  title = {Feature Selection: {All-relevant} Selection with the {Boruta}
    Package},
  date = {2010-11-15},
  url = {https://www.cybaea.net/Journal/2010/11/15/Feature-selection-All-relevant-selection-with-the-Boruta-package.html},
  langid = {en-GB-oxendict}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-2010" class="csl-entry quarto-appendix-citeas">
<span>“Feature Selection: All-Relevant Selection with the Boruta
Package.”</span> 2010. November 15. <a href="https://www.cybaea.net/Journal/2010/11/15/Feature-selection-All-relevant-selection-with-the-Boruta-package.html">https://www.cybaea.net/Journal/2010/11/15/Feature-selection-All-relevant-selection-with-the-Boruta-package.html</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>data</category>
  <guid>https://www.cybaea.net/Journal/2010/11/15/Feature-selection-All-relevant-selection-with-the-Boruta-package.html</guid>
  <pubDate>Mon, 15 Nov 2010 10:04:00 GMT</pubDate>
</item>
<item>
  <title>Big data for R</title>
  <link>https://www.cybaea.net/Journal/2010/08/05/Big-data-for-R.html</link>
  <description><![CDATA[ 






<p>Revolutions Analytics recently <a href="https://blog.revolutionanalytics.com/2010/08/announcing-big-data-for-revolution-r.html">announced their “big data” solution</a> for R. This is great news and a lovely piece of work by the team at Revolutions.</p>
<p>However, if you want to replicate their analysis in standard R, then you can absolutely do so, and we show you how.</p>
<h2 id="data-preparation" class="anchored">Data preparation</h2>
<p>First you need to prepare the rather large data set that they use in the Revolutions white paper. The preparation script shown below does two passes over all the files which is not needed: changing it to a single pass is left as an exercise for the reader…. Note that the following script will take a while to run and will need some 30-odd gig of free disk space (another exercise: get rid of the airlines.csv file), but once it is done the analysis is fast.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## big.R - Preprocess the airline data</span></span>
<span id="cb1-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2010 Allan Engelhardt (http://www.cybaea.net/)</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Install the packages we will use</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bigmemory"</span>,</span>
<span id="cb1-6">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dependencies =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Depends"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Suggests"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enhances"</span>))</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Data sets are downloaded from the Data Expo '09 web site at</span></span>
<span id="cb1-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## http://stat-computing.org/dataexpo/2009/the-data.html</span></span>
<span id="cb1-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (year <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1987</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2008</span>) {</span>
<span id="cb1-11">    file.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(year, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csv.bz2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(file.name) ) {</span>
<span id="cb1-13">        url.text <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://stat-computing.org/dataexpo/2009/"</span>,</span>
<span id="cb1-14">                          year, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".csv.bz2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-15">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Downloading missing data file "</span>, file.name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-16">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(url.text, file.name)</span>
<span id="cb1-17">    }</span>
<span id="cb1-18">}</span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Read sample file to get column names and types</span></span>
<span id="cb1-21">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2008.csv.bz2"</span>)</span>
<span id="cb1-22">integer.columns <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(d, is.integer)</span>
<span id="cb1-23">factor.columns  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(d, is.factor)</span>
<span id="cb1-24">factor.levels   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(d[, factor.columns], levels)</span>
<span id="cb1-25">n.rows <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>L</span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Process each file determining the factor levels</span></span>
<span id="cb1-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">TODO</span><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">: Combine with next loop</span></span>
<span id="cb1-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (year <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1987</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2008</span>) {</span>
<span id="cb1-30">    file.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(year, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csv.bz2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span>
<span id="cb1-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Processing "</span>, file.name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-32">    d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(file.name)</span>
<span id="cb1-33">    n.rows <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n.rows <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NROW</span>(d)</span>
<span id="cb1-34">    new.levels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(d[, factor.columns], levels)</span>
<span id="cb1-35">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ( i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(factor.levels)) ) {</span>
<span id="cb1-36">        factor.levels[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(factor.levels[[i]], new.levels[[i]])</span>
<span id="cb1-37">    }</span>
<span id="cb1-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(d)</span>
<span id="cb1-39">}</span>
<span id="cb1-40"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(integer.columns, factor.columns, factor.levels, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factors.RData"</span>)</span>
<span id="cb1-41"></span>
<span id="cb1-42"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Now convert all factors to integers so we can create a bigmatrix of the data</span></span>
<span id="cb1-43">col.classes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(integer.columns))</span>
<span id="cb1-44">col.classes[factor.columns] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"character"</span></span>
<span id="cb1-45">cols  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(factor.columns)</span>
<span id="cb1-46">first <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb1-47">csv.file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"airlines.csv"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Write combined integer-only data to this file</span></span>
<span id="cb1-48">csv.con  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file</span>(csv.file, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">open =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"w"</span>)</span>
<span id="cb1-49"></span>
<span id="cb1-50"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (year <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1987</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2008</span>) {</span>
<span id="cb1-51">    file.name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(year, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csv.bz2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span>
<span id="cb1-52">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Processing "</span>, file.name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-53">    d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(file.name, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> col.classes)</span>
<span id="cb1-54">    <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Convert the strings to integers</span></span>
<span id="cb1-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ( i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(factor.levels)) ) {</span>
<span id="cb1-56">        col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cols[i]</span>
<span id="cb1-57">        d[, col] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(d[, col], factor.levels[[i]])</span>
<span id="cb1-58">    }</span>
<span id="cb1-59">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.table</span>(d, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> csv.con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, </span>
<span id="cb1-60">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> first)</span>
<span id="cb1-61">    first <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb1-62">}</span>
<span id="cb1-63"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">close</span>(csv.con)</span>
<span id="cb1-64"></span>
<span id="cb1-65"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Now convert to a big.matrix</span></span>
<span id="cb1-66"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bigmemory"</span>)</span>
<span id="cb1-67">backing.file    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"airlines.bin"</span></span>
<span id="cb1-68">descriptor.file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"airlines.des"</span></span>
<span id="cb1-69">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.big.matrix</span>(csv.file, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb1-70">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer"</span>,</span>
<span id="cb1-71">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">backingfile =</span> backing.file,</span>
<span id="cb1-72">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">descriptorfile =</span> descriptor.file,</span>
<span id="cb1-73">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">extraCols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>))</span></code></pre></div></div>
<h2 id="sample-analysis" class="anchored">Sample analysis</h2>
<p>All done now. Sample analysis:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## bigScale.R - Replicate the analysis from http://bit.ly/aTFXeN with normal R</span></span>
<span id="cb2-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   http://info.revolutionanalytics.com/bigdata.html</span></span>
<span id="cb2-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## See big.R for the preprocessing of the data</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Load required libraries</span></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"biglm"</span>)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bigmemory"</span>)</span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"biganalytics"</span>)</span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bigtabulate"</span>)</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Use parallel processing if available</span></span>
<span id="cb2-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## (Multicore is for "anything-but-Windows" platforms)</span></span>
<span id="cb2-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multicore"</span>) ) {</span>
<span id="cb2-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"doMC"</span>)</span>
<span id="cb2-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoMC</span>()</span>
<span id="cb2-16">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb2-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Consider registering a multi-core 'foreach' processor."</span>)</span>
<span id="cb2-18">}</span>
<span id="cb2-19"></span>
<span id="cb2-20">day.names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monday"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tuesday"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wednesday"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thursday"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Friday"</span>,</span>
<span id="cb2-21">               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Saturday"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sunday"</span>)</span>
<span id="cb2-22"></span>
<span id="cb2-23"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Attach to the data</span></span>
<span id="cb2-24">descriptor.file <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"airlines.des"</span></span>
<span id="cb2-25">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attach.big.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dget</span>(descriptor.file))</span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Replicate Table 5 in the Revolutions document:</span></span>
<span id="cb2-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Table 5</span></span>
<span id="cb2-29">t<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bigtabulate</span>(data,</span>
<span id="cb2-30">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ccols =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DayOfWeek"</span>,</span>
<span id="cb2-31">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary.cols =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ArrDelay"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">summary.na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-32"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Pretty-fy the outout</span></span>
<span id="cb2-33">stat.names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dimnames</span>(t.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb2-34">t.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.</span>p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(t<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb2-35">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(t<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary),</span>
<span id="cb2-36">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(stat.names),</span>
<span id="cb2-37">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(day.names, stat.names)),</span>
<span id="cb2-38">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ValidObs =</span> t<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table)</span>
<span id="cb2-39"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(t.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.</span>p)</span>
<span id="cb2-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#             min  max     mean       sd    NAs ValidObs</span></span>
<span id="cb2-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Monday    -1410 1879 6.669515 30.17812 385262 18136111</span></span>
<span id="cb2-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tuesday   -1426 2137 5.960421 29.06076 417965 18061938</span></span>
<span id="cb2-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Wednesday -1405 2598 7.091502 30.37856 405286 18103222</span></span>
<span id="cb2-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Thursday  -1395 2453 8.945047 32.30101 400077 18083800</span></span>
<span id="cb2-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Friday    -1437 1808 9.606953 33.07271 384009 18091338</span></span>
<span id="cb2-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saturday  -1280 1942 4.187419 28.29972 298328 15915382</span></span>
<span id="cb2-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sunday    -1295 2461 6.525040 31.11353 296602 17143178</span></span>
<span id="cb2-48"></span>
<span id="cb2-49"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Figure 1</span></span>
<span id="cb2-50"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(t.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.</span>p[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average arrival delay"</span>)</span></code></pre></div></div>
<p>Just like the Revolutions paper. You can now use <code>biglm.big.matrix</code> and <code>bigglm.big.matrix</code> for basic regression and there are also k-means clustering and other functions.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>2023 update: use {speedglm}
</div>
</div>
<div class="callout-body-container callout-body">
<p>Last year we migrated a UK insurer away from {RevoScaleR}. This is the product referenced here. Revolutions got acquired by Microsoft who eventually <a href="https://cloudblogs.microsoft.com/sqlserver/2021/06/30/looking-to-the-future-for-r-in-azure-sql-and-sql-server/">abandoned</a> the whole project.</p>
<p>We moved them to <a href="https://cran.r-project.org/package=speedglm">{speedglm}</a> for their main models. Works well and was relatively painless. (It should be: {speedglm} is just another implementation of the same algorithm.)</p>
<p>So if it is still around 2023 when you read this and your are stuck with the abandoned Microsoft version of R and {RevoScaleR} then feel free to <a href="https://www.linkedin.com/in/cybaea/">ping me</a> so we can get you sorted.</p>
</div>
</div>
<p>I must admit here that I do not understand the Revolutions regression example, so I have not attempted to replicate it here. It seems kind of sad if they change the syntax to be incompatible with standard R formulas, which is what appears to be happening.</p>
<p>Credit to Michael Kane and Jay Emerson of Yale who showed much of this in their poster <a href="http://stat-computing.org/dataexpo/2009/posters/kane-emerson.pdf">The Airline Data Set… What’s the big deal?</a>.</p>


<!-- -->


 ]]></description>
  <category>R</category>
  <category>data</category>
  <guid>https://www.cybaea.net/Journal/2010/08/05/Big-data-for-R.html</guid>
  <pubDate>Thu, 05 Aug 2010 07:22:00 GMT</pubDate>
  <media:content url="https://www.cybaea.net/images/Big-data-for-R.png" medium="image" type="image/png" height="45" width="144"/>
</item>
<item>
  <title>Area Plots with Intensity Coloring</title>
  <link>https://www.cybaea.net/Journal/2010/07/13/Area-Plots-with-Intensity-Coloring.html</link>
  <description><![CDATA[ 






<p>I am not sure apeescape’s <em>ggplot2 area plot with intensity colouring</em> (dead link) is really the best way of presenting the information, but it had me intrigued enough to replicate it using base R graphics.</p>
<p>The key technique is to draw a gradient line which R does not support natively so we have to roll our own code for that. Unfortunately, <code>lines(..., type="l")</code> does not recycle the colour <code>col=</code> argument, so we end up with rather more loops than I thought would be necessary.</p>
<p>(The answer is not to use <code>lines(..., type="h")</code> which, confusingly, does recycle the colour <code>col=</code> argument. This one had me for a while, but the type=h lines always start from zero so you do not get the gradient feature.)</p>
<p>We also get a nice opportunity to use the under-appreciated <code>read.fwf</code> function.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## nino.R - another version of http://bit.ly/9P9Gh1</span></span>
<span id="cb1-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2010 Allan Engelhardt (http://www.cybaea.net/)</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Get the data from the NOAA server</span></span>
<span id="cb1-5">nino <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.fwf</span>(</span>
<span id="cb1-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.cpc.ncep.noaa.gov/data/indices/wksst.for"</span>,</span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">widths =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)),</span>
<span id="cb1-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">skip =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb1-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Week"</span>,</span>
<span id="cb1-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb1-12">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(</span>
<span id="cb1-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nino12"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nino3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nino34"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nino4"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-14">      ),</span>
<span id="cb1-15">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SST"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SSTA"</span>),</span>
<span id="cb1-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span></span>
<span id="cb1-17">    )</span>
<span id="cb1-18">  )</span>
<span id="cb1-19">)</span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Make the date column something useful</span></span>
<span id="cb1-22">nino<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Week <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(nino<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Week, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%d%b%Y"</span>)</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Make colour gradients</span></span>
<span id="cb1-25">ncol <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span></span>
<span id="cb1-26">grad.neg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hsv</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> ncol), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Blue gradient</span></span>
<span id="cb1-27">grad.pos <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hsv</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> ncol), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Red gradient</span></span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Make plot</span></span>
<span id="cb1-30">{ <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## &lt;- This block to work in R Markdown / Quarto</span></span>
<span id="cb1-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(</span>
<span id="cb1-32">    Nino34.SSTA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Week,</span>
<span id="cb1-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nino,</span>
<span id="cb1-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>,</span>
<span id="cb1-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nino34"</span>,</span>
<span id="cb1-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>,</span>
<span id="cb1-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SSTA"</span>,</span>
<span id="cb1-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb1-39">  )</span>
<span id="cb1-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(</span>
<span id="cb1-41">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(...) {</span>
<span id="cb1-42">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rect</span>(..., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gray85"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb1-43">    },</span>
<span id="cb1-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"usr"</span>)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)])</span>
<span id="cb1-45">  )</span>
<span id="cb1-46"></span>
<span id="cb1-47">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nino<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Nino34.SSTA <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The values we will plot</span></span>
<span id="cb1-48">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nino<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Week</span>
<span id="cb1-49"></span>
<span id="cb1-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axis.Date</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-51">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x,</span>
<span id="cb1-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tck =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span></span>
<span id="cb1-54">  )</span>
<span id="cb1-55">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axis</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tck =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)</span>
<span id="cb1-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>()</span>
<span id="cb1-57"></span>
<span id="cb1-58">  idx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">NROW</span>(nino))</span>
<span id="cb1-59">  idx[y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-60">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(y[y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ncol <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(y[y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb1-61">  idx[y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-62">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>y[y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (ncol <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>y[y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb1-63"></span>
<span id="cb1-64">  draw.gradient <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, ys, cols) {</span>
<span id="cb1-65">    xs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-66">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(ys) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) {</span>
<span id="cb1-67">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot.xy</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> xs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(ys[i], ys[i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])),</span>
<span id="cb1-68">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span></span>
<span id="cb1-69">          cols[i]</span>
<span id="cb1-70">      )</span>
<span id="cb1-71">    }</span>
<span id="cb1-72">  }</span>
<span id="cb1-73"></span>
<span id="cb1-74">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)) {</span>
<span id="cb1-75">    ys <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, y[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> idx[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-76">    cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (y[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb1-77">      grad.pos</span>
<span id="cb1-78">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-79">      grad.neg</span>
<span id="cb1-80">    })</span>
<span id="cb1-81">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw.gradient</span>(x[i], ys, cols)</span>
<span id="cb1-82">  }</span>
<span id="cb1-83">}</span></code></pre></div></div>
<p>The result is a decent gradient:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/nino-800.png" class="img-fluid figure-img"></p>
<figcaption>Example gradient plot</figcaption>
</figure>
</div>
<p>I deliberately omitted the scale legend on the right hand side following Allan’s First Law of Happy Graphics: Thou shall not present the same information twice.</p>
<p>For less dense information, you should increase the line width. That is left to the reader. (Hint: it is hard to get just right in base graphics, but <code>lwd &lt;- ceiling(par("pin")[1] / dev.size("in")[1] * dev.size("px")[1] / length(x))</code> could be a starting point for an approximation. We really need gradient-filled polygons in base R.)</p>



 ]]></description>
  <category>R</category>
  <guid>https://www.cybaea.net/Journal/2010/07/13/Area-Plots-with-Intensity-Coloring.html</guid>
  <pubDate>Tue, 13 Jul 2010 06:47:00 GMT</pubDate>
</item>
<item>
  <title>Employee productivity as function of number of workers revisited</title>
  <link>https://www.cybaea.net/Journal/2010/06/22/Employee-productivity-as-function-of-number-of-workers-revisited.html</link>
  <description><![CDATA[ 






<p>We have a mild obsession with employee productivity and how that declines as companies get bigger. We have previously found that <a href="../../../2006/10/19/The-3-2-rule-revisited.html">when you treble the number of workers, you halve their individual productivity</a> which is mildly scary.</p>
<p>Let’s try the FTSE-100 index of leading UK companies to see if they are significantly different from the S&amp;P 500 leading American companies that <a href="../../../2006/10/16/The-3-2-rule-of-employee-productivity.html">we analyzed four years ago</a>.</p>
<p>We will of course use the <a href="https://www.r-project.org/">R statistical computing and analysis platform</a> for our analysis, and once again we are grateful to <a href="https://uk.finance.yahoo.com/">Yahoo Finance</a> for providing the data.</p>
<p>The analysis script is available as <a href="../../../../files/ftse100.R">ftse100.R</a> and is really simple:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## ftse100.R - Display employee productivity for FTSE-100 consitituents</span></span>
<span id="cb1-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Copyright © 2010 Allan Engelhardt &lt;http://www.cybaea.net/&gt;</span></span>
<span id="cb1-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## All Rights Reserved.</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Get the index constituents.</span></span>
<span id="cb1-6">ftse<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.100</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://uk.old.finance.yahoo.com/d/quotes.csv?s=@%5EFTSE&amp;f=s&amp;e=.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">header =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(ftse<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.100</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"symbol"</span>)</span>
<span id="cb1-9">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbol =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb1-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">employees =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb1-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">profit =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb1-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sector =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb1-15">  )</span>
<span id="cb1-16"></span>
<span id="cb1-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## For each stock symbol, get employees, profit, and sector</span></span>
<span id="cb1-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (symbol <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ftse<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.100</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>symbol) {</span>
<span id="cb1-19">  profile.url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://uk.finance.yahoo.com/q/pr?s="</span>, symbol, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb1-21">  con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">url</span>(profile.url, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">open =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>)</span>
<span id="cb1-22">  text <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readChar</span>(con, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># enough bytes</span></span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">close</span>(con)</span>
<span id="cb1-24">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(</span>
<span id="cb1-26">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".*Number of employees:&lt;/td&gt;&lt;td.*?&gt;[[:space:]]*([[:digit:],]+).*"</span>,</span>
<span id="cb1-27">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>,</span>
<span id="cb1-28">      text,</span>
<span id="cb1-29">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ignore.case =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb1-30">    )</span>
<span id="cb1-31">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, x)</span>
<span id="cb1-32">  empl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(</span>
<span id="cb1-33">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(x),</span>
<span id="cb1-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warning =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-35">      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb1-36">    }</span>
<span id="cb1-37">  )</span>
<span id="cb1-38">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-39">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(</span>
<span id="cb1-40">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".*Net Profit.*?&lt;/td&gt;&lt;td.*?&gt;[[:space:]]*([+-]?[[:digit:],]+).*"</span>,</span>
<span id="cb1-41">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>,</span>
<span id="cb1-42">      text</span>
<span id="cb1-43">    )</span>
<span id="cb1-44">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, x)</span>
<span id="cb1-45">  profit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(</span>
<span id="cb1-46">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>,</span>
<span id="cb1-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warning =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-48">      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb1-49">    }</span>
<span id="cb1-50">  )</span>
<span id="cb1-51">  sector <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".*Sector:&lt;/td&gt;&lt;td.*?&gt;(.*?)&lt;/td&gt;.*"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>, text)</span>
<span id="cb1-52">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">any</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(empl, profit) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(empl, profit)))) {</span>
<span id="cb1-53">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Error parsing symbol"</span>, symbol, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"see"</span>, profile.url, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-54">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-55">    data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-56">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(</span>
<span id="cb1-57">        data,</span>
<span id="cb1-58">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-59">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symbol =</span> symbol,</span>
<span id="cb1-60">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">employees =</span> empl,</span>
<span id="cb1-61">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">profit =</span> profit,</span>
<span id="cb1-62">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sector =</span> sector</span>
<span id="cb1-63">        )</span>
<span id="cb1-64">      )</span>
<span id="cb1-65">  }</span>
<span id="cb1-66">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-67">}</span>
<span id="cb1-68"></span>
<span id="cb1-69"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Save the data so we don't have to hit Yahoo all the time.</span></span>
<span id="cb1-70"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">save</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.RData"</span>)</span>
<span id="cb1-71"></span>
<span id="cb1-72"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Save plot to file:</span></span>
<span id="cb1-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># png(filename="ftse100.png", width=800, height=800, pointsize=14, bg="white", res=100)</span></span>
<span id="cb1-74"></span>
<span id="cb1-75">opar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(</span>
<span id="cb1-76">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex.sub =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)),</span>
<span id="cb1-77">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">font.sub =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-78">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">font.lab =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-79">)</span>
<span id="cb1-80"></span>
<span id="cb1-81"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## x and y coordinates of plot and plot limits</span></span>
<span id="cb1-82">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(data, employees)</span>
<span id="cb1-83">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(data, profit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> employees)</span>
<span id="cb1-84">xlim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(x))), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceiling</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(x))))</span>
<span id="cb1-85">ylim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(y))), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceiling</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(y))))</span>
<span id="cb1-86"></span>
<span id="cb1-87"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Set up to display different color and symbols</span></span>
<span id="cb1-88">plot_col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-89">plot_pch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-90">markers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span></span>
<span id="cb1-91">pchs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb1-92">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(markers, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceiling</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sector)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(markers)))</span>
<span id="cb1-93"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rainbow</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sector)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb1-94"></span>
<span id="cb1-95"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make empty plot:</span></span>
<span id="cb1-96"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot.new</span>()</span>
<span id="cb1-97"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(</span>
<span id="cb1-98">  profit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> employees <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> employees,</span>
<span id="cb1-99">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data[<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, ],</span>
<span id="cb1-100">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>,</span>
<span id="cb1-101">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> pchs[plot_pch],</span>
<span id="cb1-102">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> plot_col,</span>
<span id="cb1-103">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xy"</span>,</span>
<span id="cb1-104">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xaxp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(xlim, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb1-105">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yaxp =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(ylim, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb1-106">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> xlim,</span>
<span id="cb1-107">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> ylim,</span>
<span id="cb1-108">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profit per employee (FTSE 100)"</span>,</span>
<span id="cb1-109">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Employees"</span>,</span>
<span id="cb1-110">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profit per employees (GBP)"</span></span>
<span id="cb1-111">)</span>
<span id="cb1-112"></span>
<span id="cb1-113"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Plot each sector</span></span>
<span id="cb1-114"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (sector <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sector)) {</span>
<span id="cb1-115">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot.xy</span>(</span>
<span id="cb1-116">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xy.coords</span>(</span>
<span id="cb1-117">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sector <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> sector, ], employees),</span>
<span id="cb1-118">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sector <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> sector, ], profit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> employees),</span>
<span id="cb1-119">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xy"</span>,</span>
<span id="cb1-120">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb1-121">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb1-122">    ),</span>
<span id="cb1-123">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>,</span>
<span id="cb1-124">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> pchs[plot_pch],</span>
<span id="cb1-125">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> plot_col,</span>
<span id="cb1-126">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg =</span> plot_col</span>
<span id="cb1-127">  )</span>
<span id="cb1-128">  plot_pch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plot_pch <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-129">  plot_col <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> plot_col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-130">}</span>
<span id="cb1-131"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(</span>
<span id="cb1-132">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottomleft"</span>,</span>
<span id="cb1-133">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sector),</span>
<span id="cb1-134">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Industry Sectors"</span>,</span>
<span id="cb1-135">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette</span>(),</span>
<span id="cb1-136">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pt.bg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">palette</span>(),</span>
<span id="cb1-137">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> pchs,</span>
<span id="cb1-138">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cex =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-139">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pt.cex =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-140">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-141">)</span>
<span id="cb1-142"></span>
<span id="cb1-143"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Fit a linear model to the log-log data:</span></span>
<span id="cb1-144">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(x))</span>
<span id="cb1-145">xl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(xlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, xlim[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb1-146">yl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(m, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> xl))</span>
<span id="cb1-147"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lines</span>(xl,</span>
<span id="cb1-148">  yl,</span>
<span id="cb1-149">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>,</span>
<span id="cb1-150">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>,</span>
<span id="cb1-151">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-152">)</span>
<span id="cb1-153">t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power = %0.3g"</span>, m<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coefficients[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb1-154"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">text</span>(</span>
<span id="cb1-155">  xl[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-156">  yl[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-157">  t,</span>
<span id="cb1-158">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adj =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>),</span>
<span id="cb1-159">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkred"</span>,</span>
<span id="cb1-160">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">font =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-161">)</span>
<span id="cb1-162"></span>
<span id="cb1-163"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## All done.</span></span>
<span id="cb1-164"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(opar)</span>
<span id="cb1-165"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dev.off</span>()</span></code></pre></div></div>
<p>Leave it to run and this is what you get:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/ftse100.png" class="img-fluid figure-img"></p>
<figcaption>Profit per employee for the FTSE 100 constituent companies.</figcaption>
</figure>
</div>
<p>The power law still broadly holds. In a large company, the productivity of the individual employee is only ¼ of the productivity in a company with one-tenth of the number of workers.</p>
<p>The analysis for the FTSE All-Share index is easy (<a href="../../../../files/ftse-all.R">ftse-all.R</a>) and gives a slope of -0.7605541 for the 301 companies with the required information, which is much worse. More convincingly, fitting the companies with more than 1,000 employees (to avoid some bias of smaller companies needing to have large profits per employee in order to be big enough to afford a stock market listing) gives a slope of -0.2838.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/ftse-all.png" class="img-fluid figure-img"></p>
<figcaption>Profit per employee for the FTSE All-Share companies</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.cybaea.net/images/ftse-all-big.png" class="img-fluid figure-img"></p>
<figcaption>Profit per employee for the FTSE All-Share companies with more than 1,000 employees.</figcaption>
</figure>
</div>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{2010,
  author = {},
  title = {Employee Productivity as Function of Number of Workers
    Revisited},
  date = {2010-06-22},
  url = {https://www.cybaea.net/Journal/2010/06/22/Employee-productivity-as-function-of-number-of-workers-revisited.html},
  langid = {en-GB-oxendict}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-2010" class="csl-entry quarto-appendix-citeas">
<span>“Employee Productivity as Function of Number of Workers
Revisited.”</span> 2010. June 22. <a href="https://www.cybaea.net/Journal/2010/06/22/Employee-productivity-as-function-of-number-of-workers-revisited.html">https://www.cybaea.net/Journal/2010/06/22/Employee-productivity-as-function-of-number-of-workers-revisited.html</a>.
</div></div></section></div> ]]></description>
  <category>business</category>
  <category>R</category>
  <guid>https://www.cybaea.net/Journal/2010/06/22/Employee-productivity-as-function-of-number-of-workers-revisited.html</guid>
  <pubDate>Tue, 22 Jun 2010 10:20:00 GMT</pubDate>
</item>
</channel>
</rss>
