View source: R/plot_btdPosterior.R
plot_btdPosterior | R Documentation |
btdFoot
ObjectsPlots for the posterior distributions of team log-strengths and other parameters with customizable plot types and facets.
plot_btdPosterior(
x,
pars = "logStrength",
plot_type = "boxplot",
teams = NULL,
ncol = NULL,
scales = NULL
)
x |
An object of class |
pars |
A character string specifying the parameter to plot.
Choices are |
plot_type |
A character string specifying the type of plot.
Choices are |
teams |
An optional character vector specifying team names to include in the posterior
boxplots or density plots. If |
ncol |
An optional integer specifying the number of columns in the facet wrap
when using a dynamic Bayesian Bradley-Terry-Davidson model.
Default is |
scales |
An optional character string specifying the scales for the facets
when using a dynamic Bayesian Bradley-Terry-Davidson model.
Options include |
A ggplot
object displaying:
For pars="logStrength"
:
Dynamic BTD: Faceted boxplots or density plots (including the 95% credible interval) of posterior log-strengths by team and period.
Static BTD: Boxplots or density plots (including the 95% credible interval) of posterior log-strengths for each team.
For pars="logTie"
or pars="home"
: A single boxplot or density plot with 95% credible interval.
Roberto Macrì Demartino roberto.macridemartino@deams.units.it.
## Not run:
if (instantiate::stan_cmdstan_exists()) {
library(dplyr)
# Load example data
data("italy")
# Prepare the data
italy_2020_2021_rank <- italy %>%
select(Season, home, visitor, hgoal, vgoal) %>%
filter(Season %in% c("2020", "2021")) %>%
mutate(match_outcome = case_when(
hgoal > vgoal ~ 1, # Home team wins
hgoal == vgoal ~ 2, # Draw
hgoal < vgoal ~ 3 # Away team wins
)) %>%
mutate(periods = case_when(
row_number() <= 190 ~ 1,
row_number() <= 380 ~ 2,
row_number() <= 570 ~ 3,
TRUE ~ 4
)) %>% # Assign periods based on match number
select(periods,
home_team = home,
away_team = visitor, match_outcome
)
# Fit the Bayesian Bradley-Terry-Davidson model with dynamic ranking
fit_rank_dyn <- btd_foot(
data = italy_2020_2021_rank,
dynamic_rank = TRUE,
rank_measure = "median",
iter_sampling = 1000,
parallel_chains = 2,
chains = 2
)
# Plot posterior distributions with default settings
plot_btdPosterior(fit_rank_dyn)
# Plot posterior distributions for specific teams with customized facets
plot_btdPosterior(
fit_rank_dyn,
teams = c("AC Milan", "AS Roma", "Juventus", "Inter"),
ncol = 2
)
plot_btdPosterior(
fit_rank_dyn,
plot_type = "density",
teams = c("AC Milan", "AS Roma", "Juventus", "Inter"),
ncol = 2
)
}
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.