btd_foot | R Documentation |
Fits a Bayesian Bradley-Terry-Davidson model using Stan. Supports both static and dynamic ranking models, allowing for the estimation of team strengths over time.
btd_foot(
data,
dynamic_rank = FALSE,
home_effect = FALSE,
prior_par = list(logStrength = normal(0, 3), logTie = normal(0, 0.3), home = normal(0,
5)),
rank_measure = "median",
method = "MCMC",
...
)
data |
A data frame containing the observations with columns:
The data frame must not contain missing values. |
dynamic_rank |
A logical value indicating whether a dynamic ranking model is used (default is |
home_effect |
A logical value indicating the inclusion of a home effect in the model. (default is |
prior_par |
A list specifying the prior distributions for the parameters of interest, using the
Only normal priors are allowed for this model. |
rank_measure |
A character string specifying the method used to summarize the posterior distributions of the team strengths. Options are:
|
method |
A character string specifying the method used to obtain the Bayesian estimates. Options are:
|
... |
Additional arguments passed to |
An object of class "btdFoot"
, which is a list containing:
fit
: The fitted CmdStanFit
object returned by cmdstanr
.
rank
: A data frame with the rankings, including columns:
periods
: The time period.
team
: The team name.
rank_points
: The estimated strength of the team based on the chosen rank_measure
.
data
: The input data.
stan_data
: The data list passed to Stan.
stan_code
: The Stan code of the underline model.
stan_args
: The optional cmdstanr
parameters passed to (...
).
rank_measure
: The summary statistic used to compute the rankings.
alg_method
: The inference algorithm used to obtain the Bayesian estimates.
Roberto Macrì Demartino roberto.macridemartino@deams.units.it.
## Not run:
if (instantiate::stan_cmdstan_exists()) {
library(dplyr)
data("italy")
italy_2020_2021 <- italy %>%
dplyr::select(Season, home, visitor, hgoal, vgoal) %>%
dplyr::filter(Season == "2020" | Season == "2021") %>%
dplyr::mutate(match_outcome = dplyr::case_when(
hgoal > vgoal ~ 1, # Home team wins
hgoal == vgoal ~ 2, # Draw
hgoal < vgoal ~ 3 # Away team wins
)) %>%
dplyr::mutate(periods = dplyr::case_when(
dplyr::row_number() <= 190 ~ 1,
dplyr::row_number() <= 380 ~ 2,
dplyr::row_number() <= 570 ~ 3,
TRUE ~ 4
)) %>% # Assign periods based on match number
dplyr::select(periods,
home_team = home,
away_team = visitor, match_outcome
)
# Dynamic Ranking Example with Median Rank Measure
fit_result_dyn <- btd_foot(
data = italy_2020_2021,
dynamic_rank = TRUE,
home_effect = TRUE,
prior_par = list(
logStrength = normal(0, 10),
logTie = normal(0, 5),
home = normal(0, 5)
),
rank_measure = "median",
iter_sampling = 1000,
parallel_chains = 2,
chains = 2
)
print(fit_result_dyn)
print(fit_result_dyn, pars = c("logStrength", "home"), teams = c("AC Milan", "AS Roma"))
# Static Ranking Example with MAP Rank Measure
fit_result_stat <- btd_foot(
data = italy_2020_2021,
dynamic_rank = FALSE,
prior_par = list(
logStrength = normal(0, 10),
logTie = normal(0, 5),
home = normal(0, 5)
),
rank_measure = "map",
iter_sampling = 1000,
parallel_chains = 2,
chains = 2
)
print(fit_result_stat)
}
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.