Simulate a match between two players, given a number of parameters, that include probability of each player winning a point on their first serve, on their second serve, and the probability that their first serve will go in.
simMatch(pA, pB, sets = c(3, 5), tiebreaks = TRUE,
         finalSetTiebreak = FALSE, players = c("A", "B"), detail = FALSE,
         p2A = NULL, firstServeA = NULL,
         p2B = NULL, firstServeB = NULL)
| param | details | default | 
|---|---|---|
| pA | probability of player A winning point on their first serve | |
| pB | probability of player B winning point on their first serve | |
| sets | number of sets (3 or 5) | |
| tiebreaks | play tiebreaks in the event the set reaches 6 games all | TRUE | 
| finalSetTiebreak | play a final set tiebreak if sets and games are tied, otherwise keep playing | FALSE | 
| players | player names (vector of length 2) | c(“A”, “B”) | 
| detail | return a detailed list about the match, including service probabilities, sets won, set data, games won, game data and result | FALSE | 
| p2A | probability of player A winning point on their second serve | NULL | 
| firstServeA | probability of player A’s first serve being in | NULL | 
| p2A | probability of player B winning point on their second serve | NULL | 
| firstServeB | probability of player B’s first serve being in | NULL | 
The default function returns a 1, if player A wins the match, or 0, if player B wins the match. If detail argument is changed to TRUE, then the function will return a list with details about the simulated match, including service probabilities, sets and games won by each player, and the result (1 or 0)
There are a number of generic methods available for an object returned by simMatch, these are print and summary, example use is show below.
simMatch(sets = 3, finalSetTiebreak = TRUE,
         pA = .78, pB = .70,
         p2A = .56, firstServeA = .67,
         p2B = .6, firstServeB = .7)
## [1] 1
Adding detail = TRUE will return a detailed list with data about the simulated match.
egMatch <- simMatch(sets = 3, finalSetTiebreak = TRUE, detail = TRUE,
                    pA = .78, pB = .70,
                    p2A = .56, firstServeA = .67,
                    p2B = .6, firstServeB = .7)
The object egMatch has a class of svR_match which comes with a print method
egMatch
## Match Result:
##  B won the match.
## 
##  player sets set1 set2
##       A    0    4    4
##       B    2    6    6
summary(egMatch)
## Match Result:
##  B won the match.
## 
##  player sets set1 set2
##       A    0    4    4
##       B    2    6    6
## 
## Server probabilities:
## 
##  player    p   p2 firstServe
##       A 0.78 0.56       0.67
##       B 0.70 0.60       0.70
simDfIf the function is called with detail = TRUE, then the list returned can be converted into a dataset using the simDf function:
df <- simDf(egMatch)
head(df)
##   playerA playerB mA mB result setNo pA pB setA setB set_res gameNo
## 1       A       B  0  2      0     1  A  B    4    6       0      1
## 2       A       B  0  2      0     1  A  B    4    6       0      2
## 3       A       B  0  2      0     1  A  B    4    6       0      3
## 4       A       B  0  2      0     1  A  B    4    6       0      4
## 5       A       B  0  2      0     1  A  B    4    6       0      5
## 6       A       B  0  2      0     1  A  B    4    6       0      6
##   serving    p   p2 firstServe game_res server returner
## 1       A 0.78 0.56       0.67        1      4        1
## 2       B 0.70 0.60       0.70        1      4        2
## 3       A 0.78 0.56       0.67        1      4        1
## 4       B 0.70 0.60       0.70        1      4        1
## 5       A 0.78 0.56       0.67        1      4        2
## 6       B 0.70 0.60       0.70        1      4        2
The dataset contains the following variables:
| variables | about | 
|---|---|
| playerA | player A (who started the match serving) | 
| playerB | player B (who started the match returning) | 
| mA | sets won by player A | 
| mB | sets won by player B | 
| result | match result, (1 if player A wins, 0 otherwise) | 
| setNo | set number | 
| pA | player A (who started the set serving) | 
| pB | player B (who started the set returning) | 
| setA | games won by player A | 
| setB | games won by player B | 
| set_res | set result (1 if player A wins, 0 if player B wins) | 
| gameNo | game number in set | 
| serving | player serving | 
| p | probability of server winning a point on their first serve | 
| p2 | probability of server winning a point on their second serve | 
| firstServe | probability of first serve being in | 
| game_res | game result (1 if server wins, 0 if returner wins) | 
| server | number of points won by server in service game | 
| returner | number of points won by returner in service game |