yorkr pads up for Twenty20s:Part 4- Individual batting and bowling performances!
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Introduction
In theory, theory and practice are the same. In practice, they’re not.
Yogi Berra
There are two ways to write error-free programs; only the third one works.
Alan Perlis
Simplicity does not precede complexity, but follows it.
Alan Perlis
Talk is cheap. Show me the code.
Linux Torvalds
This post is the 4th and the last part of yorkr padding for the Twenty20s. In this post I look at the top individual batting and bowling performances in the Twenty20s. Also please take a look at my 3 earlier post on yorkr’s handling of Twenty20 matches
- yorkr pads up for the Twenty20s: Part 1- Analyzing team“s match performance.
- yorkr pads up for the Twenty20s: Part 2-Head to head confrontation between teams
- yorkr pads up for the Twenty20s:Part 3:Overall team performance against all oppositions!
The 1st part included functions dealing with a specific T20 match, the 2nd part dealt with functions between 2 opposing teams in T20 confrontations. The 3rd part dealt with functions between a team and all T20 matches with all oppositions. This 4th part includes individual batting and bowling performances in T20 matches and deals with Class 4 functions.
This post has also been published at RPubs yorkrT20-Part4 and can also be downloaded as a PDF document from yorkrT20-Part4.pdf.
You can clone/fork the code for the package yorkr from Github at yorkr-package
The list of Class 4 functions are shown below.The Twenty20 features will be available from yorkr_0.0.4
Batsman functions
- batsmanRunsVsDeliveries
- batsmanFoursSixes
- batsmanDismissals
- batsmanRunsVsStrikeRate
- batsmanMovingAverage
- batsmanCumulativeAverageRuns (yorkr_0.0.3)
- batsmanCumulativeStrikeRate (yorkr_0.0.3)
- batsmanRunsAgainstOpposition
- batsmanRunsVenue
- batsmanRunsPredict
Bowler functions
- bowlerMeanEconomyRate
- bowlerMeanRunsConceded
- bowlerMovingAverage
- bowlerCumulativeAvgWickets (yorkr_0.0.3)
- bowlerCumulativeAvgEconRate (yorkr_0.0.3)
- bowlerWicketPlot
- bowlerWicketsAgainstOpposition
- bowlerWicketsVenue
- bowlerWktsPredict
Note: The yorkr package in its current avatar only supports ODI & Twenty20 matches. I will be upgrading the package to handle IPL in the months to come.
library(yorkr) library(gridExtra) library(rpart.plot) library(dplyr) library(ggplot2) rm(list=ls())
A. Batsman functions
1. Get Team Batting details
The function below gets the overall team batting details based on the RData file available in T20 matches. This is currently also available in Github at [yorkrData] (https://github.com/tvganesh/yorkrData/tree/master/Twenty20/T20-matches). The batting details of the team in each match is created and a huge data frame is created by rbinding the individual dataframes. This can be saved as a RData file
setwd("C:/software/cricket-package/york-test/yorkrData/Twenty20/T20-matches") india_details <- getTeamBattingDetails("India",dir=".", save=TRUE) sa_details <- getTeamBattingDetails("South Africa",dir=".",save=TRUE) nz_details <- getTeamBattingDetails("New Zealand",dir=".",save=TRUE) eng_details <- getTeamBattingDetails("England",dir=".",save=TRUE) pak_details <- getTeamBattingDetails("Pakistan",dir=".",save=TRUE) aus_details <- getTeamBattingDetails("Australia",dir=".",save=TRUE) wi_details <- getTeamBattingDetails("West Indies",dir=".",save=TRUE)
2. Get batsman details
This function is used to get the individual T20 batting record for a the specified batsman of the country as in the functions below. For analyzing the batting performances the top T20 batsmen from different countries have been chosen. The batting scorecard functions from yorkr pads up for the Twenty20s:Part 3:Overall team performance against all oppositions! was used for selecting these batsmen
- Virat Kohli (Ind)
- DA Warner (Aus)
- Umar Akmal (Pak)
- BB McCullum (NZ)
- EJG Morgan (Eng)
- CH Gayle (WI)
setwd("C:/software/cricket-package/cricsheet/cleanup/T20/rmd/part4") kohli <- getBatsmanDetails(team="India",name="Kohli",dir=".") ## [1] "./India-BattingDetails.RData" warner <- getBatsmanDetails(team="Australia",name="DA Warner") ## [1] "./Australia-BattingDetails.RData" akmal <- getBatsmanDetails(team="Pakistan",name="Umar Akmal",dir=".") ## [1] "./Pakistan-BattingDetails.RData" mccullum <- getBatsmanDetails(team="New Zealand",name="BB McCullum",dir=".") ## [1] "./New Zealand-BattingDetails.RData" emorgan <- getBatsmanDetails(team="England",name="EJG Morgan",dir=".") ## [1] "./England-BattingDetails.RData" gayle <- getBatsmanDetails(team="West Indies",name="CH Gayle",dir=".") ## [1] "./West Indies-BattingDetails.RData"
3. Runs versus deliveries
Chris Gayle and B McCullum have an astounding strike rate and touch close to 120 runs in 60 balls. David Warner also has a great strike rate
p1 <-batsmanRunsVsDeliveries(kohli,"Kohli") p2 <-batsmanRunsVsDeliveries(warner, "DA Warner") p3 <-batsmanRunsVsDeliveries(akmal,"U Akmal") p4 <-batsmanRunsVsDeliveries(mccullum,"BB McCullum") p5 <-batsmanRunsVsDeliveries(emorgan,"EJG Morgan") p6 <-batsmanRunsVsDeliveries(gayle,"CH Gayle") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
4. Batsman Total runs, Fours and Sixes
The plots below show the total runs, fours and sixes by the batsmen. Gayle tops in the runs from sixes
kohli46 <- select(kohli,batsman,ballsPlayed,fours,sixes,runs) p1 <- batsmanFoursSixes(kohli46,"Kohli") warner46 <- select(warner,batsman,ballsPlayed,fours,sixes,runs) p2 <- batsmanFoursSixes(warner46,"DA Warner") akmal46 <- select(akmal,batsman,ballsPlayed,fours,sixes,runs) p3 <- batsmanFoursSixes(akmal46, "U Akmal") mccullum46 <- select(mccullum,batsman,ballsPlayed,fours,sixes,runs) p4 <- batsmanFoursSixes(mccullum46,"BB McCullum") emorgan46 <- select(emorgan,batsman,ballsPlayed,fours,sixes,runs) p5 <- batsmanFoursSixes(emorgan46,"EJG Morgan") gayle46 <- select(gayle,batsman,ballsPlayed,fours,sixes,runs) p6 <- batsmanFoursSixes(gayle46,"CH Gayle") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
5. Batsman dismissals
The type of dismissal for each batsman is shown below
p1 <-batsmanDismissals(kohli,"Kohli") p2 <-batsmanDismissals(warner, "DA Warner") p3 <-batsmanDismissals(akmal,"U Akmal") p4 <-batsmanDismissals(mccullum,"BB McCullum") p5 <-batsmanDismissals(emorgan,"EJG Morgan") p6 <-batsmanDismissals(gayle,"CH Gayle") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
6. Runs versus Strike Rate
Gayle’s and McCullum’s strike rate touch 120% for runs in the range of 130-150
p1 <-batsmanRunsVsStrikeRate(kohli,"Kohli") p2 <-batsmanRunsVsStrikeRate(warner, "DA Warner") p3 <-batsmanRunsVsStrikeRate(akmal,"U Akmal") p4 <-batsmanRunsVsStrikeRate(mccullum,"BB McCullum") p5 <-batsmanRunsVsStrikeRate(emorgan,"EJG Morgan") p6 <-batsmanRunsVsStrikeRate(gayle,"CH Gayle") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
7. Batsman moving average
Kohli and Gayle T20 average is on the increase touching 50. Eoin Morgan and BB McCullum average around 40.
p1 <-batsmanMovingAverage(kohli,"Kohli") p2 <-batsmanMovingAverage(warner, "DA Warner") p3 <-batsmanMovingAverage(akmal,"U Akmal") p4 <-batsmanMovingAverage(mccullum,"BB McCullum") p5 <-batsmanMovingAverage(emorgan,"EJG Morgan") p6 <-batsmanMovingAverage(gayle,"CH Gayle") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
8. Batsman cumulative average
Kohli’s cumulative average steadies around 40, McCullum shows a gentle decline from 40+ to 35+. Gayle oscillates between 30+ to 40-.
p1 <-batsmanCumulativeAverageRuns(kohli,"Kohli") p2 <-batsmanCumulativeAverageRuns(warner, "DA Warner") p3 <-batsmanCumulativeAverageRuns(akmal,"U Akmal") p4 <-batsmanCumulativeAverageRuns(mccullum,"BB McCullum") p5 <-batsmanCumulativeAverageRuns(emorgan,"EJG Morgan") p6 <-batsmanCumulativeAverageRuns(gayle,"CH Gayle") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
9. Cumulative Average Strike Rate
BB McCullum has the best overall cumulative strike rate which hovered around the 150 and steadies around 130. Gayle has a rocky cumulative strike between 150 -130s. Warner is steady around 120.
p1 <-batsmanCumulativeStrikeRate(kohli,"Kohli") p2 <-batsmanCumulativeStrikeRate(warner, "DA Warner") p3 <-batsmanCumulativeStrikeRate(akmal,"U Akmal") p4 <-batsmanCumulativeStrikeRate(mccullum,"BB McCullum") p5 <-batsmanCumulativeStrikeRate(emorgan,"EJG Morgan") p6 <-batsmanCumulativeStrikeRate(gayle,"CH Gayle") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
10. Batsman runs against opposition
#Kohli's best performances are against New Zealand and Sri Lanka batsmanRunsAgainstOpposition(kohli,"Kohli")
batsmanRunsAgainstOpposition(warner, "DA Warner")
batsmanRunsAgainstOpposition(akmal,"U Akmal")
batsmanRunsAgainstOpposition(mccullum,"BB McCullum")
batsmanRunsAgainstOpposition(emorgan,"EJG Morgan")
# Gayle's best performance is against India and South Africa batsmanRunsAgainstOpposition(gayle,"CH Gayle")
11. Runs at different venues
The plots below give the performances of the batsmen at different grounds.
batsmanRunsVenue(kohli,"Kohli")
batsmanRunsVenue(warner, "DA Warner")
batsmanRunsVenue(akmal,"U Akmal")
batsmanRunsVenue(mccullum,"BB McCullum")
batsmanRunsVenue(emorgan,"EJG Morgan")
batsmanRunsVenue(gayle,"CH Gayle")
12. Predict number of runs to deliveries
The plots below use rpart classification tree to predict the number of deliveries required to score the runs in the leaf node. For e.g. Kohli takes <32 deliveries to score 22 runs and for higher number of deliveries scores around 66 runs. Devilliers needs <94 deliveries to score 84 runs and for greater deliveries scores around 109runs
par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) batsmanRunsPredict(kohli,"Kohli") batsmanRunsPredict(warner, "DA Warner") batsmanRunsPredict(akmal,"U Akmal")
# BB McCullum needs >32 deliveries to score 69+ runs while Gayle needs >28 deliveries to score 67 runs par(mfrow=c(1,3)) par(mar=c(4,4,2,2)) batsmanRunsPredict(mccullum,"BB McCullum") batsmanRunsPredict(emorgan,"EJG Morgan") batsmanRunsPredict(gayle,"CH Gayle")
B. Bowler functions
13. Get bowling details
The function below gets the overall team T20 bowling details based on the RData file available in T20 matches. This is currently also available in Github at [yorkrData] (https://github.com/tvganesh/yorkrData/tree/master/Twenty20/T20-matches). The T20 bowling details of the team in each match is created and a huge data frame is created by rbinding the individual dataframes. This can be saved as a RData file
setwd("C:/software/cricket-package/york-test/yorkrData/Twenty20/T20-matches") ind_bowling <- getTeamBowlingDetails("India",dir=".",save=TRUE) dim(ind_bowling) ## [1] 872 12 aus_bowling <- getTeamBowlingDetails("Australia",dir=".",save=TRUE) dim(aus_bowling) ## [1] 1364 12 eng_bowling <- getTeamBowlingDetails("England",dir=".",save=TRUE) dim(eng_bowling) ## [1] 1183 12 sa_bowling <- getTeamBowlingDetails("South Africa",dir=".",save=TRUE) dim(sa_bowling) ## [1] 995 12 pak_bowling <- getTeamBowlingDetails("Pakistan",dir=".",save=TRUE) dim(pak_bowling) ## [1] 1186 12 nz_bowling <- getTeamBowlingDetails("New Zealand",dir=".",save=TRUE) dim(nz_bowling) ## [1] 1295 12
14. Get bowling details of the individual bowlers
This function is used to get the individual bowling record for a specified bowler of the country as in the functions below. For analyzing the bowling performances the following cricketers have been chosen
- Ravichander Ashwin (Ind)
- SR Watson (Aus)
- SCJ Broad (Eng)
- Saeed Ajmal (Pak)
- Dale Steyn (SA)
- NL McCullum (NZ)
ashwin <- getBowlerWicketDetails(team="India",name="Ashwin",dir=".") watson <- getBowlerWicketDetails(team="Australia",name="SR Watson",dir=".") broad <- getBowlerWicketDetails(team="England",name="SCJ Broad",dir=".") ajmal <- getBowlerWicketDetails(team="Pakistan",name="Saeed Ajmal",dir=".") steyn <- getBowlerWicketDetails(team="South Africa",name="Steyn",dir=".") nmccullum <- getBowlerWicketDetails(team="New Zealand",name="NL McCullum",dir=".")
15. Bowler Mean Economy Rate
Ashwin has a mean economy rate of 5.0 for 3 & 4 overs. Saeed Ajmal is more expensive
p1<-bowlerMeanEconomyRate(ashwin,"R Ashwin") p2<-bowlerMeanEconomyRate(watson, "SR Watson") p3<-bowlerMeanEconomyRate(broad, "SCJ Broad") p4<-bowlerMeanEconomyRate(ajmal, "Saeed Ajmal") p5<-bowlerMeanEconomyRate(steyn, "D Steyn") p6<-bowlerMeanEconomyRate(nmccullum, "NL Mccullum") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
16. Bowler Mean Runs conceded
p1<-bowlerMeanRunsConceded(ashwin,"R Ashwin") p2<-bowlerMeanRunsConceded(watson, "SR Watson") p3<-bowlerMeanRunsConceded(broad, "SCJ Broad") p4<-bowlerMeanRunsConceded(ajmal, "Saeed Ajmal") p5<-bowlerMeanRunsConceded(steyn, "D Steyn") p6<-bowlerMeanRunsConceded(nmccullum, "NL Mccullum") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
17. Bowler Moving average
Aswin, SCJ Broad and Steyn have an improving performance in T20s. NL McCullum has a drop and Ajmal’s performance is on the decline
p1<-bowlerMovingAverage(ashwin,"R Ashwin") p2<-bowlerMovingAverage(watson, "SR Watson") p3<-bowlerMovingAverage(broad, "SCJ Broad") p4<-bowlerMovingAverage(ajmal, "Saeed Ajmal") p5<-bowlerMovingAverage(steyn, "D Steyn") p6<-bowlerMovingAverage(nmccullum, "NL Mccullum") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
17. Bowler cumulative average wickets (yorkr_0.0.3)
Interestingly Ajmal and NL McCullum have a cumulative average wickets of around 2.0. Steyn also has a cumulative average of 2.0+
p1<-bowlerCumulativeAvgWickets(ashwin,"R Ashwin") p2<-bowlerCumulativeAvgWickets(watson, "SR Watson") p3<-bowlerCumulativeAvgWickets(broad, "SCJ Broad") p4<-bowlerCumulativeAvgWickets(ajmal, "Saeed Ajmal") p5<-bowlerCumulativeAvgWickets(steyn, "D Steyn") p6<-bowlerCumulativeAvgWickets(nmccullum, "NL Mccullum") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
18. Bowler cumulative Economy Rate (ER) (yorkr_0.0.3)
Ajmal’s economy rate deteriorates from a excellent rate of 5.5, while Ashwin’s economy rate improves from a terrible rate of 9.0+.
p1<-bowlerCumulativeAvgEconRate(ashwin,"R Ashwin") p2<-bowlerCumulativeAvgEconRate(watson, "SR Watson") p3<-bowlerCumulativeAvgEconRate(broad, "SCJ Broad") p4<-bowlerCumulativeAvgEconRate(ajmal, "Saeed Ajmal") p5<-bowlerCumulativeAvgEconRate(steyn, "D Steyn") p6<-bowlerCumulativeAvgEconRate(nmccullum, "NL Mccullum") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
19. Bowler wicket plot
The plot below gives the average wickets versus number of overs
p1<-bowlerWicketPlot(ashwin,"R Ashwin") p2<-bowlerWicketPlot(watson, "SR Watson") p3<-bowlerWicketPlot(broad, "SCJ Broad") p4<-bowlerWicketPlot(ajmal, "Saeed Ajmal") p5<-bowlerWicketPlot(steyn, "D Steyn") p6<-bowlerWicketPlot(nmccullum, "NL Mccullum") grid.arrange(p1,p2,p3,p4,p5,p6, ncol=3)
20. Bowler wicket against opposition
#Ashwin's best pertformance are against South Africa,Sri Lanka, Bangaldesh and Afghanistan bowlerWicketsAgainstOpposition(ashwin,"R Ashwin")
#Watson's bets pertformance are against England, Ireland and New Zealand bowlerWicketsAgainstOpposition(watson, "SR Watson")
bowlerWicketsAgainstOpposition(broad, "SCJ Broad")
#Ajmal's best performances are against Sri Lanka, New Zealand and South Africa bowlerWicketsAgainstOpposition(ajmal, "Saeed Ajmal")
#Steyn has good performances against New Zealand, Sri Lanka, Pakistan, West Indies bowlerWicketsAgainstOpposition(steyn, "D Steyn")
bowlerWicketsAgainstOpposition(nmccullum, "NL Mccullum")
21. Bowler wicket at cricket grounds
bowlerWicketsVenue(ashwin,"R Ashwin")
bowlerWicketsVenue(watson, "SR Watson")
bowlerWicketsVenue(broad, "SCJ Broad")
bowlerWicketsVenue(ajmal, "Saeed Ajmal")
bowlerWicketsVenue(steyn, "D Steyn")
bowlerWicketsVenue(nmccullum, "NL Mccullum")
22. Get Delivery wickets for bowlers
This function creates a dataframe of deliveries and the wickets taken
setwd("C:/software/cricket-package/york-test/yorkrData/Twenty20/T20-matches") ashwin1 <- getDeliveryWickets(team="India",dir=".",name="Ashwin",save=FALSE) watson1 <- getDeliveryWickets(team="Australia",dir=".",name="SR Watson",save=FALSE) broad1 <- getDeliveryWickets(team="England",dir=".",name="SCJ Broad",save=FALSE) ajmal1 <- getDeliveryWickets(team="Pakistan",dir=".",name="Saeed Ajmal",save=FALSE) steyn1 <- getDeliveryWickets(team="South Africa",dir=".",name="Steyn",save=FALSE) nmccullum1 <- getDeliveryWickets(team="New Zealand",dir=".",name="NL McCullum",save=FALSE)
23. Predict number of deliveries to wickets
#Ashwin takes <12 deliveries for a wicket while Watson takes around 9 deliveries par(mfrow=c(1,2)) par(mar=c(4,4,2,2)) bowlerWktsPredict(ashwin1,"R Ashwin") bowlerWktsPredict(watson1,"SR Watson")
#Broad and Ajmal need around 8 deliveries for a wicket par(mfrow=c(1,2)) par(mar=c(4,4,2,2)) bowlerWktsPredict(broad1,"SCJ Broad") bowlerWktsPredict(ajmal1,"Saeed Ajmal")
par(mfrow=c(1,2)) par(mar=c(4,4,2,2)) bowlerWktsPredict(steyn1,"D Steyn") bowlerWktsPredict(nmccullum1,"NL Mccullum")
Conclusion
This concludes the 4 part writeup of yorkr’s handling of Twenty20’s. I will be addding functionsto the ckage to handle IPL matches soon. You can fork/clone the code from Github at yorkr.
Hope you have a great time with my yorkr package!
Also see
- Introducing cricket package yorkr: Part 2-Trapped leg before wicket!
- Introducing cricket package yorkr: Part 3-Foxed by flight!
- Introducing cricketr! : An R package to analyze performances of cricketers
- Cricket analytics with cricketr in paperback and Kindle versions
- Bend it like Bluemix, MongoDB with auto-scaling – Part 3
- The dark side of the Internet
- Modeling a Car in Android
- Hand detection through haar-training: A hands-on approach
- Cricket analytics with cricketr
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.