TransWikia.com

Reproduce Figure 3.2 in Introduction to Statistical Learning

Data Science Asked by Andy Draper on April 26, 2021

Has anyone reproduced Figure 3.2 in Introduction to Statistical Learning (James et al)?

https://trevorhastie.github.io/ISLR/ISLR%20Seventh%20Printing.pdf

They have a contour plot with circles.

Here is my code, which produces a contour plot with ellipses. Some of the numbers appear close, but some others are very different.

library(tidyverse)

# dataset can be found here: https://trevorhastie.github.io/ISLR/data.html
advert <- read_csv('Advertising.csv')

calc_ssr <- function(b0, b1){
  r <- advert$sales - (b0 + b1 * advert$TV)
  sum(r^2)
}

b0_range <- seq(5, 9, length.out = 25)
b1_range <- seq(0.03, 0.065, length.out = 25)
params <- crossing(b0 = b0_range, b1 = b1_range)
params$ssr <- map2_dbl(params$b0, params$b1, calc_ssr)

params %>% 
  ggplot(aes(x = b0, y = b1, z = ssr)) + 
  geom_contour_filled(breaks = c(2100, 2150, 2200, 2300, 2500, 3000))
```

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP