svgbf-models
Model Bayes factors
Which model of the factors do the data support, and by how much?
How to read it
- Read each bar as how much more the data support that model than the intercept-only model.
- Longest bar: the best-supported model.
- Read the posterior probability to weigh models against each other.
- Callout 1: The best-supported model.
- Callout 2: Its Bayes factor and posterior probability.
- Callout 3: BF = 1: no better than the baseline.
Watch out
- Posterior probabilities depend on equal prior odds across these models and on which models are listed.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
- Bars start at BF = 1 (no evidence); the strongest model is drawn solid. Percentages are posterior model probabilities with equal prior odds over the models shown and the intercept-only model.
# Model Bayes factors — Codesplash Chart Lab (recipe "bf-models")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("bf-models-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
M = "model"
B = "BF"
PP = "posterior"
def df_models():
d = df.dropna(subset=[M, B])
d = d[d[B] > 0].sort_values(B, ascending=False)
return pd.DataFrame({"model": d[M].astype(str), "bf": d[B], "posterior": d[PP] if PP else np.nan})
d = df_models()
fig, ax = plt.subplots(figsize=(7, 0.5 * len(d) + 1.2))
y = np.arange(len(d))[::-1]
ax.barh(y, d["bf"], left=1, color=PALETTE[0], alpha=0.7)
ax.set_xscale("log")
ax.set_yticks(y, d["model"])
ax.axvline(1, color="grey", linewidth=0.8)
for i, (bf, pp) in zip(y, zip(d["bf"], d["posterior"])):
ax.text(1 + bf, i, f" {bf:.3g}" + (f" · {pp:.0%}" if pd.notna(pp) else ""), va="center", fontsize=8)
ax.set_xlabel("Bayes factor against the intercept-only model (log scale)")
plt.show()Needs pandas, numpy, matplotlib · reads bf-models-demo.csv · app theme from the optional codesplash helper
# Model Bayes factors — Codesplash Chart Lab (recipe "bf-models")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("bf-models-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
M = "model"
B = "BF"
PP = "posterior"
def df_models():
d = df.dropna(subset=[M, B])
d = d[d[B] > 0].sort_values(B, ascending=False)
return pd.DataFrame({"model": d[M].astype(str), "bf": d[B], "posterior": d[PP] if PP else np.nan})
d = df_models()
fig = go.Figure(go.Bar(x=d["bf"], y=d["model"], orientation="h", marker_color=PALETTE[0], text=[f"{v:.3g}" + (f" · {q:.0%}" if pd.notna(q) else "") for v, q in zip(d["bf"], d["posterior"])]))
fig.update_layout(xaxis_type="log", xaxis_title="Bayes factor against the intercept-only model (log scale)", yaxis_autorange="reversed")
fig.show()Needs pandas, numpy, plotly · reads bf-models-demo.csv · app theme from the optional codesplash helper
# Model Bayes factors — Codesplash Chart Lab (recipe "bf-models")
# controls: defaults
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("bf-models-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["model"]] <- factor(df[["model"]])
M <- "model"
B <- "BF"
PP <- "posterior"
d <- data.frame(model = as.character(df[[M]]), bf = df[[B]], pp = if (is.null(PP)) NA_real_ else df[[PP]])
d <- d[is.finite(d$bf) & d$bf > 0, ]
d$model <- factor(d$model, levels = d$model[order(d$bf)])
p <- ggplot(d, aes(bf, model)) +
geom_segment(aes(x = 1, xend = bf, yend = model), colour = cs_cat[[1]], linewidth = 4, alpha = 0.7) +
geom_vline(xintercept = 1) +
geom_text(aes(label = ifelse(is.na(pp), sprintf("%.3g", bf), sprintf("%.3g · %.0f%%", bf, 100 * pp))), hjust = -0.1, size = 2.8) +
scale_x_log10(expand = expansion(mult = c(0.02, 0.25))) +
labs(x = "Bayes factor against the intercept-only model (log scale)", y = NULL) + theme_cs
print(p)Needs ggplot2 · reads bf-models-demo.csv · app theme from the optional codesplashr helper