#| label: plot-lrt-concept
#| echo: false
#| fig-cap: "Conceptual plot of Deviance versus Number of Parameters, illustrating the Likelihood Ratio Test statistic (Λ) and Residual Deviance. Λ is the drop from the Null Model to the Full Model. The Saturated Model represents perfect fit (Deviance = 0)."
library(ggplot2)
## 1. Create conceptual data for the plot
## These are just for illustration
n_obs <- 60 # Number of observations
p0 <- 1 # Parameters in null model (intercept)
p1 <- 21 # Parameters in full model (e.g., intercept + 7 predictors)
psat <- n_obs # Parameters in saturated model (1 per observation)
D0 <- 41 # Null deviance
D1 <- 20 # Full model deviance (residual deviance of M1)
D_sat <- 0 # Saturated model deviance
## Data frame for the three points
plot_data <- data.frame(
model = c("M_0 (Null)", "M_1 (Full)", "M_Sat (Saturated)"),
params = c(p0, p1, psat),
deviance = c(D0, D1, D_sat),
## Add custom justification and nudges for labels
hjust_val = c(0.5, 0.5, 1.1), # Right-align the last label
nudge_x_val = c(0, 0, 0)
)
## 2. Create the ggplot
ggplot(plot_data, aes(x = params, y = deviance)) +
## Draw dashed guide lines for D0 and D1
geom_segment(aes(x = p0, y = D0, xend = p1, yend = D0), linetype = "dashed", color = "grey70") +
geom_segment(aes(x = p1, y = D1, xend = psat, yend = D1), linetype = "dashed", color = "grey70") +
## Connect the points with lines
geom_line(color = "black", linetype = "solid", linewidth = 0.5) +
## Draw the main points
geom_point(size = 4, aes(color = model)) +
## --- MODIFIED LABEL PLACEMENT ---
## Label the points using custom nudge/justification
geom_text(
aes(label = model, hjust = hjust_val, nudge_x = nudge_x_val),
nudge_y = 2.5, # Use a much smaller vertical nudge
size = 4
) +
## --- ADDED BACK D0 and D1 ANNOTATIONS ---
## D0 (Null Deviance)
geom_segment(
aes(x = p0 - 2, y = D0, xend = p0 - 2, yend = D_sat), # Nudged left
arrow = arrow(ends = "both", length = unit(0.1, "inches")),
color = "darkgreen",
linewidth = 1
) +
annotate(
"text",
x = p0 - 3, y = D0 / 2, # Nudged left
label = "D[0]", parse = TRUE,
color = "darkgreen", hjust = 0.5, size = 5
) +
## D1 (Residual Deviance of Full Model)
geom_segment(
aes(x = psat + 2, y = D1, xend = psat + 2, yend = D_sat), # Nudged right
arrow = arrow(ends = "both", length = unit(0.1, "inches")),
color = "darkblue",
linewidth = 1
) +
annotate(
"text",
x = psat + 3, y = D1 / 2, # Nudged right
label = "D[1]", parse = TRUE,
color = "darkblue", hjust = 0.5, size = 5
) +
## LRT statistic Λ = D0 - D1
geom_segment(
aes(x = p1 + 2, y = D0, xend = p1 + 2, yend = D1), # Nudged right
arrow = arrow(ends = "both", length = unit(0.1, "inches")),
color = "red",
linewidth = 1
) +
annotate(
"text",
x = p1 + 3, y = D1 + (D0 - D1) / 2, # Nudged right
label = "Lambda == D[0] - D[1]",
parse = TRUE,
color = "red", hjust = 0, size = 5
) +
## Customize axes to show the symbolic labels
scale_x_continuous(
breaks = c(p0, p1, psat),
labels = c(expression(p[0]), expression(p[1]), expression(n)),
expand = expansion(mult = 0.1) # Add some padding
) +
scale_y_continuous(
breaks = c(D_sat, D1, D0),
labels = c(expression(0), expression(D[1]), expression(D[0]))
) +
## Labels and Title
labs(
title = "Relationship between Deviance and Model Complexity",
x = "Number of Parameters",
y = "Model Deviance"
) +
## Clean theme
theme_minimal(base_size = 14) +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "none" # Remove legend, as points are labeled
) +
scale_color_manual(values = c("M_0 (Null)" = "blue", "M_1 (Full)" = "blue", "M_Sat (Saturated)" = "red"))