# Simulate a set of volleyball

`vs_simulate_set.Rd`

`vs_simulate_set_theor`

and `vs_simulate_set_mc`

are convenience functions for `vs_simulate_set(..., method = "theoretical")`

and `vs_simulate_set(..., method = "monte carlo")`

respectively.

## Usage

```
vs_simulate_set(
rates,
process_model = "phase",
serving = NA,
go_to = 25,
simple = FALSE,
id = NULL,
method = "theoretical"
)
vs_simulate_set_mc(...)
vs_simulate_set_theor(...)
```

## Arguments

- rates
list: A two-element list, each element of which is a set of rates as returned by

`vs_estimate_rates`

. Experimental: for`process_model`

"sideout", the`sideout`

rate component can be a function. This function will be called at each step of the simulation with the parameters:`team_1_score`

- the score of team 1 at each point in the set so far`team_2_score`

- the score of team 2 at each point in the set so far`team_1_rotation`

- the rotation of team 1 at each point in the set so far (rotations are counted relative to the team's starting rotation, which is 1)`team_2_rotation`

- the rotation of team 2 at each point in the set so far (rotations are counted relative to the team's starting rotation, which is 1)`serving`

- the serving team 1 or 2 at each point in the set so far`point_won_by`

- which team won each point in the set so far (this will be NA for the last entry, because that's the current point that hasn't been simulated yet)`outcome`

- the outcome of each point in the set so far, either "Sideout" or "Breakpoint" if`process_model`

is "sideout", or details TBD if`process_model`

is "phase" The function should return the sideout rate of the receiving team.

- process_model
string: either "sideout" or "phase". The "sideout" model uses the estimated sideout rates (in the

`rates`

object) directly. The "phase" model breaks play down into different phases (serve, serve receive, etc) and uses the rates associated with those separate phases- serving
logical: if

`TRUE`

, team 1 will serve first. If`NA`

, the team serving first will be chosen at random- go_to
integer: the minimum score that must be reached to end the set (typically 25 for indoor volleyball in sets 1 to 4, 15 in set 5, or 21 in beach volleyball)

- simple
logical: if

`TRUE`

, return simplified output. Only applicable to`method`

"monte carlo". If`simple = TRUE`

, return the team (1 or 2) that won the set. If`simple = FALSE`

, return extra details in a data.frame- id
: an optional value that (if non-

`NULL`

) will be returned in the`id`

column of the returned data frame, if`simple`

is`FALSE`

- method
string: the simulation method to use. Either "monte carlo" or "theoretical". Details TBD

- ...
: parameters as for

`vs_simulate_set`

.`vs_simulate_set_theor`

and`vs_simulate_set_mc`

are convenience functions for`vs_simulate_set(..., method = "theoretical")`

and`vs_simulate_set(..., method = "monte carlo")`

respectively

## Examples

```
if (FALSE) {
library(datavolley)
x <- dv_read(dv_example_file())
rates <- vs_estimate_rates(x, target_team = "each")
vs_simulate_set(rates) ## simulate a single set
vs_simulate_match(rates) ## simulate a match
## so given the performances of the two teams during that match, we expect
## that the home team should have won, with 3-0 being the most likely scoreline
## compare to the actual match result
summary(x)
}
## sideout rates as a function for team 2
sofun2 <- function(serving, point_won_by, ...) {
## if team 2 won their previous sideout opportunity, their sideout rate is 0.6
## otherwise it's 0.5
prevso <- tail(na.omit(point_won_by[serving == 1]), 1)
if (length(prevso) < 1 || prevso == 1) {
## first sideout opportunity or lost the last one
0.5
} else {
0.6
}
}
rates <- list(list(sideout = 0.55), ## first team has constant 55% sideout rate
list(sideout = sofun2)) ## function for team 2's sideout rate
## need to use method = "monte carlo" for this
vs_simulate_set(rates = rates, process_model = "sideout", method = "monte carlo")
#> team_1_score team_2_score team_1_rotation team_2_rotation serving
#> 1 0 0 1 1 1
#> 2 1 0 1 1 1
#> 3 2 0 1 1 1
#> 4 2 1 1 2 2
#> 5 2 2 1 2 2
#> 6 3 2 2 2 1
#> 7 4 2 2 2 1
#> 8 4 3 2 3 2
#> 9 4 4 2 3 2
#> 10 5 4 3 3 1
#> 11 5 5 3 4 2
#> 12 6 5 4 4 1
#> 13 7 5 4 4 1
#> 14 8 5 4 4 1
#> 15 9 5 4 4 1
#> 16 9 6 4 5 2
#> 17 10 6 5 5 1
#> 18 11 6 5 5 1
#> 19 11 7 5 6 2
#> 20 12 7 6 6 1
#> 21 13 7 6 6 1
#> 22 13 8 6 1 2
#> 23 13 9 6 1 2
#> 24 14 9 1 1 1
#> 25 14 10 1 2 2
#> 26 15 10 2 2 1
#> 27 16 10 2 2 1
#> 28 17 10 2 2 1
#> 29 17 11 2 3 2
#> 30 17 12 2 3 2
#> 31 18 12 3 3 1
#> 32 19 12 3 3 1
#> 33 20 12 3 3 1
#> 34 21 12 3 3 1
#> 35 21 13 3 4 2
#> 36 22 13 4 4 1
#> 37 23 13 4 4 1
#> 38 23 14 4 5 2
#> 39 24 14 5 5 1
#> point_won_by set_won_by outcome
#> 1 1 1 Breakpoint
#> 2 1 1 Breakpoint
#> 3 2 1 Sideout
#> 4 2 1 Breakpoint
#> 5 1 1 Sideout
#> 6 1 1 Breakpoint
#> 7 2 1 Sideout
#> 8 2 1 Breakpoint
#> 9 1 1 Sideout
#> 10 2 1 Sideout
#> 11 1 1 Sideout
#> 12 1 1 Breakpoint
#> 13 1 1 Breakpoint
#> 14 1 1 Breakpoint
#> 15 2 1 Sideout
#> 16 1 1 Sideout
#> 17 1 1 Breakpoint
#> 18 2 1 Sideout
#> 19 1 1 Sideout
#> 20 1 1 Breakpoint
#> 21 2 1 Sideout
#> 22 2 1 Breakpoint
#> 23 1 1 Sideout
#> 24 2 1 Sideout
#> 25 1 1 Sideout
#> 26 1 1 Breakpoint
#> 27 1 1 Breakpoint
#> 28 2 1 Sideout
#> 29 2 1 Breakpoint
#> 30 1 1 Sideout
#> 31 1 1 Breakpoint
#> 32 1 1 Breakpoint
#> 33 1 1 Breakpoint
#> 34 2 1 Sideout
#> 35 1 1 Sideout
#> 36 1 1 Breakpoint
#> 37 2 1 Sideout
#> 38 1 1 Sideout
#> 39 1 1 Breakpoint
```