By Tony Hirst
With the new F1 season upon us, I’ve started tinkering with bits of code from the Wrangling F1 Data With R book and looking at the data in some new ways.
For example, I started wondering whether we might be able to learn something interesting about the race strategies by looking at laptimes on a stint by stint basis.
To begin with, we need some data – I’m going to grab it directly from the ergast API using some functions that are bundled in with the Leanpub book…
#ergast functions described in: https://leanpub.com/wranglingf1datawithr/ #Get laptime data from the ergast API l2=lapsData.df(2016,2) #Get pits data from the ergast API p2=pitsData.df(2016,2) #merge pit data into the laptime data l3=merge(l2,p2[,c('driverId','lap','rawduration')],by=c('driverId','lap'),all=T) #generate an inlap flag (inlap is the lap assigned the pit time) l3['inlap']=!is.na(l3['rawduration']) #generate an outlap flag (outlap is the first lap of the race lap's starting from the pits l3=ddply(l3,.(driverId),transform,outlap=c(T,!is.na(head(rawduration,-1)))) #use the pitstop flag to number stints; note: a drive through penalty increments the stint count l3=arrange(l3,driverId, -lap) l3=ddply(l3,.(driverId),transform,stint=1+sum(inlap)-cumsum(inlap)) #number the laps in each stint l3=arrange(l3,driverId, lap) l3=ddply(l3,.(driverId,stint),transform,lapInStint=1:length(stint)) l3=arrange(l3,driverId, lap)
The laptimes associated with the in- and out- lap associated with a pit stop add noise to the full lap times completed within each stint, so lets flag those laps so we can then filter them out:
#Discount the inlap and outlap l4=l3[!l3['outlap'] & !l3['inlap'],]
We can now look at the data… I’m going to facet by driver, and also group the laptimes associated with each stint. Then we can plot just the raw laptimes, and also a simple linear model based on the full lap times within each stint:
#Generate a base plot g=ggplot(l4,aes(x=lapInStint, y=rawtime, col=factor(stint)))+facet_wrap(~driverId) #Chart the raw laptimes within each stint g+geom_line() #Plot a simple linear model for each stint g+ geom_smooth(method = "lm", formula = y ~ x)
So for example, here are the raw laptimes, excluding inlap and outlap, by stint for each driver in the recent 2016 Bahrain Formual One Grand Prix:
And here’s the simple linear model:
<img …read more
Source:: r-bloggers.com