*Yet all experience is an arch wherethro’
Gleams that untravell’d world whose margin fades
For ever and forever when I move.
How dull it is to pause, to make an end,
To rust unburnish’d, not to shine in use!*

```
Ulysses by Alfred Tennyson
```

This is an introductory post in which I introduce a cricketing package **‘cricketr’** whicj I have created. This package was a natural culmination to many earlier posts on cricketers and my completing 10 modules of an absorbing topics in Data Science Specialization, from John Hopkins University at Coursera. The thought of creating this package struck me some time back, and I have finally been able to bring this to fruition.

So here it is. My R package **‘cricketr!!!’**

This package uses the statistics info available in ESPN Cricinfo Statsguru. The current version of this package supports all formats of the game including Test, ODI and Twenty20 versions.

You should be able to install the package from GitHub and use the many functions available in the package. Please mindful of the ESPN Cricinfo Terms of Use

Take a look at my short video tutorial on my R package cricketr on Youtube - R package cricketr - A short tutorial

Do check out my interactive Shiny app implementation using the cricketr package - Sixer - R package cricketr’s new Shiny avatar

The cricketr package has several functions that perform several different analyses on both batsman and bowlers. The package has function that plot percentage frequency runs or wickets, runs likelihood for a batsman, relative run/strike rates of batsman and relative performance/economy rate for bowlers are available.

Other interesting functions include batting performance moving average, forecast and a function to check whether the batsmans in in-form or out-of-form.

The data for a particular player can be obtained with the getPlayerData() function. To do you will need to go to ESPN CricInfo Player and type in the name of the player for e.g Ricky Ponting, Sachin Tendulkar etc. This will bring up a page which have the profile number for the player e.g. for Sachin Tendulkar this would be http://www.espncricinfo.com/india/content/player/35320.html. Hence, Sachin’s profile is 35320. This can be used to get the data for Tendulkar as shown below

The cricketr package is now available from **CRAN!!!** You should be able to install directly with

```
if (!require("cricketr")){
install.packages("cricketr",lib = "c:/test")
}
library(cricketr)
```

The cricketr package includes some pre-packaged sample (.csv) files. You can use these sample to test functions as shown below

```
# Retrieve the file path of a data file installed with cricketr
pathToFile <- system.file("data", "tendulkar.csv", package = "cricketr")
batsman4s(pathToFile, "Sachin Tendulkar")
```

```
# The general format is pkg-function(pathToFile,par1,...)
#batsman4s(<path-To-File>,"Sachin Tendulkar")
```

Alternatively, the cricketr package can be installed from GitHub with

```
if (!require("cricketr")){
library(devtools)
install_github("tvganesh/cricketr")
}
library(cricketr)
```

The pre-packaged files can be accessed as shown above. To get the data of any player use the function getPlayerData()

```
tendulkar <- getPlayerData(35320,dir="..",file="tendulkar.csv",type="batting",homeOrAway=c(1,2),
result=c(1,2,4))
```

**Important Note** This needs to be done only once for a player. This function stores the player’s data in a CSV file (for e.g. tendulkar.csv as above) which can then be reused for all other functions. Once we have the data for the players many analyses can be done. This post will use the stored CSV file obtained with a prior getPlayerData for all subsequent analyses

The 3 plots below provide the following for Tendulkar

- Frequency percentage of runs in each run range over the whole career
- Mean Strike Rate for runs scored in the given range
- A histogram of runs frequency percentages in runs ranges

```
par(mfrow=c(1,3))
par(mar=c(4,4,2,2))
batsmanRunsFreqPerf("./tendulkar.csv","Tendulkar")
batsmanMeanStrikeRate("./tendulkar.csv","Tendulkar")
batsmanRunsRanges("./tendulkar.csv","Tendulkar")
```

`dev.off()`

```
## null device
## 1
```

```
par(mfrow=c(1,3))
par(mar=c(4,4,2,2))
batsman4s("./tendulkar.csv","Tendulkar")
batsman6s("./tendulkar.csv","Tendulkar")
batsmanDismissals("./tendulkar.csv","Tendulkar")
```

`dev.off()`

```
## null device
## 1
```

The plots below show the 3D scatter plot of Sachin Runs versus Balls Faced and Minutes at crease. A linear regression model is then fitted between Runs and Balls Faced + Minutes at crease

`battingPerf3d("./tendulkar.csv","Sachin Tendulkar")`

The plot below gives the average runs scored by Tendulkar at different grounds. The plot also the number of innings at each ground as a label at x-axis. It can be seen Tendulkar did great in Colombo (SSC), Melbourne overseas and Mumbai, Mohali and Bangalore at home

`batsmanAvgRunsGround("./tendulkar.csv","Tendulkar")`

This plot computes the average runs scored by Tendulkar against different countries. The x-axis also gives the number of innings against each team

`batsmanAvgRunsOpposition("./tendulkar.csv","Tendulkar")`

The plot below shows the Runs Likelihood for a batsman. For this the performance of Sachin is plotted as a 3D scatter plot with Runs versus Balls Faced + Minutes at crease. K-Means. The centroids of 3 clusters are conputed and plotted. In this plot Sachin Tendulkar’s highest tendencies are computed and plotted using K-Means

`batsmanRunsLikelihood("./tendulkar.csv","Tendulkar")`

```
## Summary of Tendulkar 's runs scoring likelihood
## **************************************************
##
## There is a 16.51 % likelihood that Tendulkar will make 139 Runs in 251 balls over 353 Minutes
## There is a 58.41 % likelihood that Tendulkar will make 16 Runs in 31 balls over 44 Minutes
## There is a 25.08 % likelihood that Tendulkar will make 66 Runs in 122 balls over 167 Minutes
```

The batsmen with the most hundreds in test cricket are

- Sachin Tendulkar :
**Average:53.78,100’s - 51, 50’s - 68** - Jacques Kallis :
**Average: 55.47, 100’s - 45, 50’s - 58** - Ricky Ponting :
**Average: 51.85, 100’s - 41 , 50’s - 62** - Kumara Sangakarra:
**Average: 58.04 ,100’s - 38 , 50’s - 52**

in that order.

The following plots take a closer at their performances. The box plots show the mean (red line) and median (blue line). The two ends of the boxplot display the 25th and 75th percentile.

This plot shows a combined boxplot of the Runs ranges and a histogram of the Runs Frequency

`batsmanPerfBoxHist("./tendulkar.csv","Sachin Tendulkar")`

`batsmanPerfBoxHist("./kallis.csv","Jacques Kallis")`

`batsmanPerfBoxHist("./ponting.csv","Ricky Ponting")`

`batsmanPerfBoxHist("./sangakkara.csv","K Sangakkara")`

The plot below shows the contribution of Tendulkar, Kallis, Ponting and Sangakarra in matches won and lost. The plots show the range of runs scored as a boxplot (25th & 75th percentile) and the mean scored. The total matches won and lost are also printed in the plot.

All the players have scored more in the matches they won than the matches they lost. Ricky Ponting is the only batsman who seems to have more matches won to his credit than others. This could also be because he was a member of strong Australian team

For the 2 functions below you will have to use the getPlayerDataSp() function. I have commented this as I already have these files

```
#tendulkarsp <- getPlayerDataSp(35320,tdir=".",tfile="tendulkarsp.csv",ttype="batting")
#kallissp <- getPlayerDataSp(45789,tdir=".",tfile="kallissp.csv",ttype="batting")
#pontingsp <- getPlayerDataSp(7133,tdir=".",tfile="pontingsp.csv",ttype="batting")
#sangakkarasp <- getPlayerDataSp(50710,tdir=".",tfile="sangakkarasp.csv",ttype="batting")
```

```
par(mfrow=c(2,2))
par(mar=c(4,4,2,2))
batsmanContributionWonLost("tendulkarsp.csv","Tendulkar")
batsmanContributionWonLost("kallissp.csv","Kallis")
batsmanContributionWonLost("pontingsp.csv","Ponting")
batsmanContributionWonLost("sangakkarasp.csv","Sangakarra")
```

`dev.off()`

```
## null device
## 1
```

From the plot below it can be seen

Tendulkar has more matches overseas than at home and his performace is consistent in all venues at home or abroad. Ponting has lesser innings than Tendulkar and has an equally good performance at home and overseas.Kallis and Sangakkara’s performance abroad is lower than the performance at home.

This function also requires the use of getPlayerDataSp() as shown above

```
par(mfrow=c(2,2))
par(mar=c(4,4,2,2))
batsmanPerfHomeAway("tendulkarsp.csv","Tendulkar")
batsmanPerfHomeAway("kallissp.csv","Kallis")
batsmanPerfHomeAway("pontingsp.csv","Ponting")
batsmanPerfHomeAway("sangakkarasp.csv","Sangakarra")
```