From 13d0e2986c4f794ea19732f25d1d0fc707559833 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 09:24:04 +0000 Subject: [PATCH 1/3] Add a figure of the expected squared error to the best-constant example Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_019xfc9ohb8bijGhgKPdUpzm --- _subfiles/_exm-best-constant-auto.qmd | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/_subfiles/_exm-best-constant-auto.qmd b/_subfiles/_exm-best-constant-auto.qmd index dbd24e48..644a06ba 100644 --- a/_subfiles/_exm-best-constant-auto.qmd +++ b/_subfiles/_exm-best-constant-auto.qmd @@ -51,6 +51,52 @@ stopifnot( ) ``` +@fig-best-constant-auto plots the expected squared error of every constant from 10 to 40 against $c$, +and marks the three constants in the table. + +```{r} +#| label: fig-best-constant-auto +#| fig-cap: >- +#| Expected squared error of a constant prediction c of fuel economy +#| for the 392 cars in the Auto data, for c from 10 to 40 miles per gallon. +#| The points mark the mean, the median and c = 20. +#| fig-alt: >- +#| A U-shaped curve of expected squared error against the constant c, +#| lowest at about 23.4 miles per gallon. +#| Points on the curve mark the mean, which is at the bottom, +#| the median, which sits just to the left of it, +#| and c = 20, which sits higher on the left arm. +#| fig-height: 3 +#| fig-width: 6 +#| code-fold: true +c_grid <- seq(10, 40, length.out = 301) +grid_deviations <- outer(mpg, c_grid, "-") +grid_squared <- grid_deviations^2 +grid_errors <- colMeans(grid_squared) +curve <- tibble::tibble(c = c_grid, expected_squared_error = grid_errors) +marked <- tibble::tibble( + constant = constants, + c = c_values, + expected_squared_error = errors, + label_hjust = c(0.5, 0.5, 1), + label_vjust = c(2.5, -2.5, -1) +) + +ggplot2::ggplot(curve, ggplot2::aes(x = c, y = expected_squared_error)) + + ggplot2::geom_line() + + ggplot2::geom_point(data = marked, size = 3) + + ggplot2::geom_text( + ggplot2::aes(label = constant, hjust = label_hjust, vjust = label_vjust), + data = marked + ) + + ggplot2::labs( + x = "constant prediction c (miles per gallon)", + y = "expected squared error" + ) + + ggplot2::expand_limits(y = 0) + + ggplot2::theme_minimal() +``` + The two columns agree, as @thm-constant-prediction-decompose says. The mean, `{r} round(mu, 2)`, has the smallest expected squared error, `{r} round(sigma2, 2)`, which is the [variance](#def-variance) of `mpg` over these cars (divisor the number of cars, not one fewer), From a259b2265d79c2d34455a2cea495ee82dfd45bf6 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 09:26:31 +0000 Subject: [PATCH 2/3] Build the figure's mappings as named objects and label the third point c = 20 Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_019xfc9ohb8bijGhgKPdUpzm --- _subfiles/_exm-best-constant-auto.qmd | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/_subfiles/_exm-best-constant-auto.qmd b/_subfiles/_exm-best-constant-auto.qmd index 644a06ba..43b02295 100644 --- a/_subfiles/_exm-best-constant-auto.qmd +++ b/_subfiles/_exm-best-constant-auto.qmd @@ -74,21 +74,27 @@ grid_deviations <- outer(mpg, c_grid, "-") grid_squared <- grid_deviations^2 grid_errors <- colMeans(grid_squared) curve <- tibble::tibble(c = c_grid, expected_squared_error = grid_errors) +point_labels <- c("mean", "median", "c = 20") +label_hjust <- c(0.5, 0.5, 1) +label_vjust <- c(2.5, -2.5, -2) marked <- tibble::tibble( - constant = constants, + point_label = point_labels, c = c_values, expected_squared_error = errors, - label_hjust = c(0.5, 0.5, 1), - label_vjust = c(2.5, -2.5, -1) + label_hjust = label_hjust, + label_vjust = label_vjust +) +curve_aes <- ggplot2::aes(x = c, y = expected_squared_error) +label_aes <- ggplot2::aes( + label = point_label, + hjust = label_hjust, + vjust = label_vjust ) -ggplot2::ggplot(curve, ggplot2::aes(x = c, y = expected_squared_error)) + +ggplot2::ggplot(curve, curve_aes) + ggplot2::geom_line() + ggplot2::geom_point(data = marked, size = 3) + - ggplot2::geom_text( - ggplot2::aes(label = constant, hjust = label_hjust, vjust = label_vjust), - data = marked - ) + + ggplot2::geom_text(label_aes, data = marked) + ggplot2::labs( x = "constant prediction c (miles per gallon)", y = "expected squared error" From 125f49304c627aec3eca3d3de046d0bd7cd73a0d Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 10:12:43 +0000 Subject: [PATCH 3/3] Pipe the curve data into ggplot() to satisfy ggplot_args_linter Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_019xfc9ohb8bijGhgKPdUpzm --- _subfiles/_exm-best-constant-auto.qmd | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/_subfiles/_exm-best-constant-auto.qmd b/_subfiles/_exm-best-constant-auto.qmd index 43b02295..59aa49c7 100644 --- a/_subfiles/_exm-best-constant-auto.qmd +++ b/_subfiles/_exm-best-constant-auto.qmd @@ -91,7 +91,9 @@ label_aes <- ggplot2::aes( vjust = label_vjust ) -ggplot2::ggplot(curve, curve_aes) + +curve |> + ggplot2::ggplot() + + curve_aes + ggplot2::geom_line() + ggplot2::geom_point(data = marked, size = 3) + ggplot2::geom_text(label_aes, data = marked) +