简体   繁体   English

在回归线 R 中添加 +- 2 SE

[英]Add +- 2 SE in regression line R

I was wondering if it is possible to plot a regression line in R including +- 2* Standard Error.我想知道是否可以在 R 中绘制一条回归线,包括 +- 2* 标准误差。 For example, with iris :例如,使用iris

library(ggplot2)
ggplot(iris, aes(x = Petal.Length, y = Sepal.Width))+geom_smooth(method=lm, color="black")+ theme_bw()

the output is:输出是:

在此处输入图片说明

How would it possible (if possible) to expand the grey area for values twice the standard error?如何(如果可能)将灰色区域扩展为标准误差的两倍?

One option to achieve your desired result would be to manually add the confidence bands via stat_smooth and making use of after_stat :实现所需结果的一种选择是通过stat_smooth手动添加置信stat_smooth并使用after_stat

library(ggplot2)
ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) +
  geom_smooth(method = lm, color = "black", se = FALSE) +
  stat_smooth(aes(ymin = after_stat(y - 2 * se), ymax = after_stat(y + 2 * se)), geom = "ribbon", method = lm, fill = "grey60", alpha = .4) +
  theme_bw()
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM