R ggplot 四次多项式的误差线

发布于 2025-01-18 00:21:31 字数 1067 浏览 1 评论 0原文

我正在尝试使用R的统计学习介绍来重现此图的右侧(图7.1,p267):

“在此处输入映像”

这是我正在使用的代码,而曲线似乎在点上,错误条不会在我的图表中的高值飞行,但相反会变得更窄:


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)

src =“ https://i.sstatic.net/vrxp4.jpg” alt =“在此处输入图像说明”>

有人知道为什么这是/我在做什么错?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文