<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/tinkering-with-competitive-supertimes/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.ouseful.info/2018/04/06/tinkering-with-competitive-supertimes/"> Rstats – OUseful.Info, the blog…</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’m back on the R thang with F1 data from ergast.com, and started having a look at how drivers and teams compare at a circuit.</p>
<p>One metric I came across for comparing teams over a season is the <em>supertime</em>, typically calculated for each manufacturer as the average of their fastest single lap recorded by the team at each race weekend expressed as a percentage of the fastest single lap overall.</p>
<p>It struck me that we can also derive a reduced <em>competitive supertime</em> by basing the calculation on best laptime recorded across the qualifying and race sessions, omitting laptimes recorded in the practice sessions.</p>
<p>We can draw on the notion of supertimes to derive two simple measures for comparing team performances based on laptime:</p>
<ul>
<li>evolution of manufacturer competitive supertime for a circuit over the years;</li>
<li>evolution of manufacturer competitive supertime for each circuit over the course of a season.</li>
</ul>
<p>We can also produce driver performance metrics based on the competitive supertime of each driver.</p>
<p>So here are some notes on doing just that… I’m pulling the data from a MySQL database built from a datadump published via ergast.com.</p>
<pre>
q=paste0('SELECT circuitRef, c.name AS circuit, r.name as race, location, country FROM races r JOIN circuits c WHERE r.circuitId=c.circuitId AND year=',year,' AND round=',round)
cct = as.list(dbGetQuery(ergastdb, q))

cct
$circuitRef
[1] &quot;bahrain&quot;

$circuit
[1] &quot;Bahrain International Circuit&quot;

$race
[1] &quot;Bahrain Grand Prix&quot;

$location
[1] &quot;Sakhir&quot;

$country
[1] &quot;Bahrain&quot;
</pre>
<p>Calculate some competitive supertimes for manufacturers:</p>
<pre>library(reshape2)
library(plyr)

q=paste0('SELECT q1, q2, q3, fastestLapTime, cn.name, d.code, year FROM races r JOIN circuits c JOIN results rs JOIN constructors cn JOIN qualifying q JOIN drivers d WHERE r.circuitId=c.circuitId AND d.driverId=rs.driverId AND r.raceId=rs.raceId AND cn.constructorId=rs.constructorId AND q.raceId=r.raceId AND q.driverId=rs.driverId AND cn.name IN (',teamsThisYear_str,') AND circuitref=&quot;',cct$circuitRef,'&quot; ORDER BY year')
st = dbGetQuery(ergastdb, q)


st = melt(st,
          id.vars = c(&quot;name&quot;,&quot;code&quot;, &quot;year&quot;),
          measure.vars = c(&quot;q1&quot;, &quot;q2&quot;, &quot;q3&quot;, &quot;fastestLapTime&quot;))

st['time'] = as.numeric(apply(st['value'], 1, timeInS))

# Normalise the time
st = ddply(st, .(year), transform, ntime = time/min(time, na.rm=TRUE))

#Find the best time for each manufacturer per race weekend (quali+race)
stt = ddply(st,
            .(name,year),
            summarise, 
            stime = min(ntime, na.rm=TRUE))

stt$label = as.factor(stt$name)
stt
</pre>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2018/04/dropbox_wranglingf1datawithr_f1djr_-_master_-_rstudio.png?w=370&amp;h=232&amp;fit=370%2C232&amp;resize=370%2C232" data-orig-size="788,494" data-medium-file="https://ouseful.files.wordpress.com/2018/04/dropbox_wranglingf1datawithr_f1djr_-_master_-_rstudio.png?w=370&amp;h=232&amp;fit=370%2C232&amp;resize=370%2C232?w=300" data-attachment-id="17694" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2018/04/dropbox_wranglingf1datawithr_f1djr_-_master_-_rstudio.png?w=370&h=232&fit=370%2C232&resize=370%2C232" data-permalink="https://blog.ouseful.info/2018/04/06/tinkering-with-competitive-supertimes/__dropbox_wranglingf1datawithr_f1djr_-_master_-_rstudio/" data-large-file="https://ouseful.files.wordpress.com/2018/04/dropbox_wranglingf1datawithr_f1djr_-_master_-_rstudio.png?w=370&amp;h=232&amp;fit=370%2C232&amp;resize=370%2C232?w=788" alt="" data-image-title="__Dropbox_wranglingf1datawithr_f1djR_-_master_-_RStudio" sizes="(max-width: 370px) 100vw, 370px" width="370" data-comments-opened="1" class="wp-image-17694" data-recalc-dims="1" height="232"></p>
<p>Here’s a chart theme I’ve used elsewhere:</p>
<pre>
library(ggplot2)
library(directlabels)
library(ggrepel)
library(ggthemes)

champChartTheme=function(g){
  g = g + theme_minimal(base_family=&quot;Arial Narrow&quot;)
  #g = g + theme(panel.grid.major.y=element_blank())
  g = g + theme(panel.grid.minor=element_blank())
  g = g + theme(axis.line.y=element_line(color=&quot;#2b2b2b&quot;, size=0.15))
  g = g + theme(axis.text.y=element_text(margin=margin(r=0, l=0)))
  g = g + theme(plot.margin=unit(rep(30, 4), &quot;pt&quot;))
  g = g + theme(plot.title=element_text(face=&quot;bold&quot;))
  g = g + theme(plot.subtitle=element_text(margin=margin(b=10)))
  g = g + theme(plot.caption=element_text(size=8, margin=margin(t=10)))
  g
}
</pre>
<p>Create a supertime chart plotting function:</p>
<pre>compSupertime = function(df, cct, labelSize=0.7, smooth=FALSE){
  g=ggplot(data=df,aes(x=year,y=stime))
  if (smooth){g=g+stat_smooth(aes(colour=label),method = &quot;lm&quot;, formula= y ~ x + I(x^2), se=FALSE, size=0.7)}
  else { g=g+geom_line(aes(colour=label))}
  g=g+ guides(colour=FALSE)+xlim(min(df$year),max(df$year)+1)
  #cex is label size
  g=g+geom_dl(aes(label = label, colour=label),method = list(cex = labelSize, dl.trans(x = x + .3), &quot;last.bumpup&quot;))
  g=g+labs(x=NULL,y='Competitive Supertime (% of best)',
           title=paste0('F1 ', cct$race, ' - ','Competitive Supertimes',', ', min(stt$year), ' to ', max(df$year)),
           subtitle = paste0(cct$circuit,', ',cct$location,', ',cct$country),
           caption=&quot;Data from Ergast database, ergast.com/mrd&quot;)
  champChartTheme(g)
}
</pre>
<p>Let’s have a look…</p>
<pre>compSupertime(stt,cct,smooth=F)
</pre>
<p> </p>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2018/04/supertimecircuit-1.png?w=450&amp;h=369&amp;fit=517%2C369" data-orig-size="672,480" data-medium-file="https://ouseful.files.wordpress.com/2018/04/supertimecircuit-1.png?w=450&amp;h=369&amp;fit=517%2C369?w=300" data-attachment-id="17696" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2018/04/supertimecircuit-1.png?w=450&h=369&fit=517%2C369" data-permalink="https://blog.ouseful.info/2018/04/06/tinkering-with-competitive-supertimes/supertimecircuit-1/" data-large-file="https://ouseful.files.wordpress.com/2018/04/supertimecircuit-1.png?w=450&amp;h=369&amp;fit=517%2C369?w=672" alt="" data-image-title="supertimeCircuit-1" sizes="(max-width: 517px) 100vw, 517px" data-comments-opened="1" class="wp-image-17696" data-recalc-dims="1"><br>
Alternatively:</p>
<pre>compSupertime(stt,cct,smooth=T)
</pre>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2018/04/supertimecircuitsmooth-1.png?w=450&amp;h=374&amp;fit=524%2C374" data-orig-size="672,480" data-medium-file="https://ouseful.files.wordpress.com/2018/04/supertimecircuitsmooth-1.png?w=450&amp;h=374&amp;fit=524%2C374?w=300" data-attachment-id="17695" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2018/04/supertimecircuitsmooth-1.png?w=450&h=374&fit=524%2C374" data-permalink="https://blog.ouseful.info/2018/04/06/tinkering-with-competitive-supertimes/supertimecircuitsmooth-1/" data-large-file="https://ouseful.files.wordpress.com/2018/04/supertimecircuitsmooth-1.png?w=450&amp;h=374&amp;fit=524%2C374?w=672" alt="" data-image-title="supertimeCircuitSmooth-1" sizes="(max-width: 524px) 100vw, 524px" data-comments-opened="1" class="wp-image-17695" data-recalc-dims="1"></p>
<p>The best fit / model line obviously leaves something to be desired, eg in the case of Renault. But it’s a start.</p>
<p>I’ve also started working on a workflow to autopublish stuff to Gitbooks via github. For example, here’s the site in progress around the <a rel="nofollow" href="https://bah2018gp.f1datajunkie.com/the-story-so-far/drivers-and-constructors-circuit-competitive-supertimes" target="_blank">Bahrain F1 2018 Grand Prix</a>. Here’s an earlier example (subject to updates/reflowing!) around the <a rel="nofollow" href="https://aus2018gp.f1datajunkie.com/" target="_blank">Australia 2018 F1 Grand Prix</a>.</p>
<p>PS As I get back in to this, I’ll probably start updating the <a rel="nofollow" href="https://leanpub.com/wranglingf1datawithr" target="_blank">Wrangling F1 Data With R</a> with recipes again… Also on the to do list is a second edition based on the <em>tidyverse</em> way of doing things…</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.ouseful.info/2018/04/06/tinkering-with-competitive-supertimes/"> Rstats – OUseful.Info, the blog…</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/AE3HcldSmA4" height="1"></a></div>