1 year ago

#355199

test-img

Louis Maiden

R ggplot error bars for 4th degree polynomial

I'm trying to reproduce the right side of this graph from Introduction to Statistical Learning using R (Figure 7.1, p267):

enter image description here

Here is the code I'm using, and while the curve seems on point, the error bars do not fly off wildly for high values in my graph, but conversely get more narrow:


library(tidyverse)
library(ISLR2)
library(broom)

wage <- Wage %>%
  as_tibble() %>%
  select(age, wage) %>%
  mutate(high_inc = wage > 250)

# 7.1.B Graph (ISLR page 267)
glm(high_inc ~ poly(age, 4), data = wage, family = binomial) %>%
  broom::augment(type.predict = 'response') %>%
  cbind(wage %>% select(age)) %>%
  as_tibble() %>%
  mutate(lower = .fitted - 1.96 * .se.fit,     
         upper = .fitted + 1.96 * .se.fit) %>% # POINTWISE STANDARD ERRORS
  ggplot(aes(age, .fitted)) +
  geom_line() +
  geom_ribbon(aes(ymin = lower, ymax = upper), color = "grey", alpha = .2)

enter image description here

Does anyone know why this is / what I'm doing wrong?

r

ggplot2

confidence-interval

polynomials

errorbar

0 Answers

Your Answer

Accepted video resources