Wednesday, June 10, 2015

How the F1 Canadian Grand Prix Race Evolved on Track

In responses to an email query about battlemaps, I had a quick look at ways of viewing the on track evolution of the race (and in doing so noticed I'd fallen short in the laptime data I was grabbing from the ergastAPI - it has a cutoff of 1000 records in a response and with 70 laps in the race, this was capping out the data I got back at just over 50 laps - so the previous battlemaps ran short of the whole race distance. No-one pointed it out though, so I guess I am just talking to myself here...!)

Anyway, data grabber hack patched for now (I really should tidy up the fetcher to go a generic paged request),  here's a quick chart to show how the race evolved on track overall. The vertical y-axis is the leadlap, the horizontal x-axis is gap to leader. Cars off the lead lap are highlighted.

#Grab some data
lapTimes =lapsData.df(2015,7)

#Process the laptimes
lapTimes=battlemap_encoder(laptimes)

#Find the accumulated race time at the start of each leader's lap
lapTimes=ddply(lapTimes,.(leadlap),transform,lstart=min(acctime))

#Find the on-track gap to leader
lapTimes['trackdiff']=lapTimes['acctime']-lapTimes['lstart']

#Plot the on-track gap to leader versus leader lap
g=ggplot(lapTimes)
g=g+geom_point(aes(x=trackdiff,y=leadlap,col=(lap==leadlap)))

#Highlight a particular driver
g=g+geom_point(data=lapTimes[lapTimes['driverId']=='vettel',],
               aes(x=trackdiff,y=leadlap),pch=1)
#Overplot with laps behind for lapped drivers
g=g+geom_text(data=lapTimes[lapTimes['lapsbehind']>0,],
              aes(x=trackdiff,y=leadlap,label=lapsbehind),size=3)

g



The chart helps us identify areas of the field where battles appearing to be taking place, and by highlighting particular drivers we can see how they fared compared to the leader (or we might highlight two drivers who were close at the end to see how their various race strategies played out.

We also notice something of the speed of the lead car - around about laps 38 to 40 we see cars in 2nd, 3rd and 4th gaining time back from the leader, for example. We can also see that cars in 5th (Vettel) and 6th (Massa) were keeping pace with the leader from about lap 38.

To find battles automatically, we could use the stint detection code explored previously. It also struck me that we could generate a graph for each lap going trackpos1-trackpos2-trackpos3 etc with a weight equal to the gap time between cars, and then simply prune the graph with gaps above a certain size to identify battlegroupings. (See also Identifying Position Change Groupings in Rank Ordered Lists for another handy way of using graph based approaches.)

No comments:

Post a Comment

There seem to be a few issues with posting comments. I think you need to preview your comment before you can submit it... Any problems, send me a message on twitter: @psychemedia