R ggplot 四次多项式的误差线
我正在尝试使用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):
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)
Does anyone know why this is / what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论