Simulate a service game given a number of parameters for the server, that include probability of the server winning a point on their first serve, on their second serve, and the probability that their first serve will go in.
simGame(p, p2 = NULL, firstServe = NULL, detail = FALSE, player = "A")
param | details | default |
---|---|---|
p | probability of server winning point on their first serve | |
p2 | probability of server winning point on their second serve | NULL |
firstServe | probability of first serve being in | NULL |
detail | return a detailed list about the game, including service probabilities, points won, and result | FALSE |
player | player name | “A” |
The default function returns a 1, if the server wins the game, or 0, if the returner wins the game. If detail
argument is changed to TRUE
, then the function will return a list with details about the simulated game, including service probabilities, points won by the server and returner, and the result (1 or 0)
There are a number of generic methods available for an object returned by simGame
, these are print
and summary
, example use is show below.
simGame(p = .78, p2 = .56, firstServe = .67)
## [1] 1
# a look at a detailed return
game <- simGame(p = .78, p2 = .56, firstServe = .67, detail = TRUE)
The object game
has a class of svR_game which comes with a few methods
game
## Service Game Result
##
## Returner won the game.
## player points
## server 1
## returner 4
summary(game)
## Service Game Result:
## Returner won the game.
## player points
## server 1
## returner 4
##
## Probability of Server (A) winning point:
## p p2 firstServe
## 0.78 0.56 0.67
To simulate many games, you can either use replicate
, as shown below which is used with detail = FALSE
, or simGames which will return a detailed list with data about the simulated games, rather than just the result.
game <- replicate(1e3, simGame(p = .78, p2 = .56, firstServe = .67))
head(game)
## [1] 1 1 1 1 1 1
# summarise results
table(game) / sum(table(game))
## game
## 0 1
## 0.1 0.9
simDf
If the function is called with detail = TRUE
, then the list returned can be converted into a dataset using the simDf function, producing a dataset containing the following variables:
variables | about |
---|---|
player | player name |
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 |
result | 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 |