xg_sim_season_pts: Uses xG data to simulate a season of football matches and...

View source: R/xg_sim_season_pts.R

xg_sim_season_ptsR Documentation

Uses xG data to simulate a season of football matches and returns the points total for each team

Description

Uses xG data to simulate a season of football matches and returns the points total for each team

Usage

xg_sim_season_pts(
  df,
  team = team,
  opponent = opponent,
  team_xg_list = team_xg_list,
  opp_xg_list = opp_xg_list,
  n_sim = 10000,
  points_method = "share"
)

Arguments

df

A data frame with four columns: team, opponent, team_xg_list, opponent_xg_list. Each row is a match.

n_sim

The number of simulations to run (default = 10000).

points_method

The method used to calculate points. Options are "share" (default) or "full".

team_a

A column containing the home team or a team name.

team_b

A column containing the away team or opponent name.

team_a_xg_list

A column containing a list of the home team's xG eg. c(0.1, 0.1, 0.3).

team_b_xg_list

A column containing a list of the away team's xG eg. c(0.1, 0.2, 0.1).

Examples

# Create data simulating a season of 4 teams playing each team once
set.seed(123)

teams <- tibble(team = c("Team A", "Team B", "Team C", "Team D"), opponent = c("Team A", "Team B", "Team C", "Team D"))

season_data <- teams |>
  expand(team, opponent) |>
  filter(team != opponent) |>
  mutate(match_id = row_number()) |>
  rowwise() |>
  mutate(team_shots_xg = list(round(rnorm(sample(6:14, 1), 0.1, 0.05),2)),
         opp_shots_xg = list(round(rnorm(sample(6:14, 1), 0.1, 0.05),2))) |>
  ungroup()

xg_full_season_sim(season_data)
##  A tibble: 4 × 4
##  team_name points n_games  rank
##  <chr>      <dbl>   <int> <int>
##1 Team D        13       6     1
##2 Team A         8       6     2
##3 Team B         7       6     3
##4 Team C         6       6     4

stxlen/soccr documentation built on Feb. 17, 2025, 1:23 a.m.