<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/starry-night-plots/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.maxhumber.com/2017/02/05/starry-night.html"> max humber</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I think good data science reads like a good story. In that it flows. Has an arc. And is compelling.</p>
<p>But data science has a dirty secret. For every piece that works, there are about nine others that didn’t. Nine other stories that look like they were typed up by a monkey with a typewriter (and in a <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Infinite_monkey_theorem">finite amount of time!</a>)</p>
<p>The problem with this problem is that unless you did the work you never get to see the monkey scripts. The stuff that never worked. The stuff that got thrown out.</p>
<p>This creates, I think, unrealistic expectations about how data science gets done. Because every tutorial, textbook, lesson, workshop, talk, and blog post about data science just works. And we forget that this stuff has been designed and vetted to work. Most real world stuff doesn’t just work…</p>
<p>I initially wanted this post to be about the Super Bowl. To build some simple toy model on “Points For/Against” and push it through a new plot that I’ve been working on. But nothing was working. The data and my toy model told a story that literally made zero sense.</p>
<p>But then it struck me. This is perfect. I was able to catch and diagnose a garbage model exactly because of the plot I wanted to talk about!</p>
<p>So. Here’s my monkey script and the “Starry Night Plot” that helped me throw it out:</p>
<h2>SETUP</h2>
<div>
<pre><code><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>viridis</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<h2>DATA</h2>
<div>
<pre><code><span>df</span><span> </span><span>&lt;-</span><span> </span><span>read_csv</span><span>(</span><span>paste0</span><span>(</span><span>
</span><span>&quot;https://raw.githubusercontent.com/maxhumber/&quot;</span><span>,</span><span>
</span><span>&quot;maxhumber.com/master/assets/data/superbowl_data.csv&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p></p>
<p>These data are scraped from <a target="_blank" rel="nofollow" href="http://www.pro-football-reference.com/">pro-football-reference.com</a> with <code>purrr</code> and <code>rvest</code>. If you want to learn more about webscraping within the <em>tidverse</em> I have a quick post on the process <a target="_blank" rel="nofollow" href="http://maxhumber.com/2017/01/08/vorp-hockey.html">here</a>.</p>
<h2>MODEL</h2>
<p>I was trying to build a toy model to predict who might win Super Bowl LI. I don’t know much about the NFL, but I thought that I might be able to do something with “Season Average Points For” and “Season Average Points Against”. Basically, I wanted to tease out the relative importance of Defense and Offense for past Super Bowl contenders:</p>
<div>
<pre><code><span>mod1</span><span> </span><span>&lt;-</span><span> </span><span>glm</span><span>(</span><span>
</span><span>win</span><span> </span><span>~</span><span> </span><span>pf</span><span> </span><span>+</span><span> </span><span>pa</span><span>,</span><span>
</span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;binomial&quot;</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>df</span><span>)</span><span>
</span><span># summary(mod1)
</span></code></pre>
</div>
<p></p>
<p>If you run <code>summary(mod1)</code> you can see that the p-values aren’t traditionally “significant”, however, <code>pf</code> hits 0.1 which gets you a little <code>.</code> if you’re “star-gazing” the printout.</p>
<p>Pumping the model through a prediction grid of all likely/possible points combinations:</p>
<div>
<pre><code><span>pred_grid</span><span> </span><span>&lt;-</span><span> </span><span>expand.grid</span><span>(</span><span>
</span><span>pf</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>40</span><span>,</span><span> </span><span>pa</span><span> </span><span>=</span><span> </span><span>0</span><span>:</span><span>40</span><span>)</span><span>
</span><span>pred_grid</span><span>$</span><span>prob</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>
</span><span>mod1</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>pred_grid</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;response&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<p>And the putting that grid through a Tile Plot gets you:</p>
<div>
<pre><code><span>pred_grid</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>pf</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>pa</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_tile</span><span>(</span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>prob</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="center" src="https://i1.wp.com/www.maxhumber.com/assets/img/starry_1.png?w=456" data-recalc-dims="1"></p>
<p>Sweet. Looks alright. But what does it mean? It’s kind of hard to tell…</p>
<p>To improve the interpretability of <code>geom_tile</code> I added a contour layer and made the breaks hit the same sequence.</p>
<div>
<pre><code><span>br</span><span> </span><span>&lt;-</span><span> </span><span>0.2</span><span>
</span><span>pred_grid</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>pf</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>pa</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_tile</span><span>(</span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>prob</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_contour</span><span>(</span><span>aes</span><span>(</span><span>z</span><span> </span><span>=</span><span> </span><span>prob</span><span>),</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>lty</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>binwidth</span><span> </span><span>=</span><span> </span><span>br</span><span>)</span><span> </span><span>+</span><span> </span><span>scale_fill_continuous</span><span>(</span><span>
</span><span>breaks</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>0</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>br</span><span>),</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>scales</span><span>::</span><span>percent_format</span><span>())</span><span>
</span></code></pre>
</div>
<p><img alt="center" src="https://i1.wp.com/www.maxhumber.com/assets/img/starry_2.png?w=456" data-recalc-dims="1"></p>
<p>Now I can see that my dumb toy model thinks that “Points For” is more predictive than “Points Against”. And that “Season Average Points For” is a bad thing when it comes to winning the Super Bowl.</p>
<p>Basically, if I put Atlanta and New England through this model the better team on paper would be expected to lose the Super Bowl (lol).</p>
<p>I wanted to figure out my toy model was behaving so strangely, so I added in the underlying data through some <code>geom_point</code> layers (Os for actual wins, Xs for actual losses) and a bit of polish:</p>
<div>
<pre><code><span>br</span><span> </span><span>&lt;-</span><span> </span><span>0.2</span><span>
</span><span>pred_grid</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>pf</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>pa</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_tile</span><span>(</span><span>aes</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>prob</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_contour</span><span>(</span><span>aes</span><span>(</span><span>z</span><span> </span><span>=</span><span> </span><span>prob</span><span>),</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>lty</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>binwidth</span><span> </span><span>=</span><span> </span><span>br</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>(</span><span>df</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>win</span><span> </span><span>==</span><span> </span><span>1</span><span>)),</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>16</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>(</span><span>df</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>win</span><span> </span><span>==</span><span> </span><span>0</span><span>)),</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_fill_viridis</span><span>(</span><span>
</span><span>direction</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>0.85</span><span>,</span><span> </span><span>option</span><span> </span><span>=</span><span> </span><span>&quot;D&quot;</span><span>,</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>0</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>br</span><span>),</span><span>
</span><span>labels</span><span> </span><span>=</span><span> </span><span>scales</span><span>::</span><span>percent_format</span><span>())</span><span> </span><span>+</span><span> </span><span>coord_cartesian</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>10</span><span>,</span><span> </span><span>40</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>10</span><span>,</span><span> </span><span>40</span><span>))</span><span> </span><span>+</span><span> </span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Super Dumb Super Bowl Model&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Points For&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Points Against&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;Prob&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_rect</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<p><img alt="center" src="https://i0.wp.com/www.maxhumber.com/assets/img/starry_3.png?w=456" data-recalc-dims="1"></p>
<p>Now I can see that the behaviour of this model makes some sense (small data, and big upsets).</p>
<p>There’s nothing really new about this type of plot. It’s just a bunch of <code>tile</code>, <code>contour</code> and <code>point</code> layers and the viridis colourmap but I’ve been calling it the Starry Night Plot because it kind of looks like the painting. And I wanted to write about it because it has been super helpful in diagnosing and explaining model behaviour to other people.</p>
<p>I guess I hope you might be able to use it.</p>
<p>~</p>
<p>P.S. I also tried building models on point differntials for the exact matchups and conditioning the points for/against on Strength of Schedule… but then I just realized that I was p-hacking…</p>
<p>P.S.S. Go Falcons.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.maxhumber.com/2017/02/05/starry-night.html"> max humber</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/xSc52dB5exA"></a></div>