簡體   English   中英

在回歸線 R 中添加 +- 2 SE

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

我想知道是否可以在 R 中繪制一條回歸線,包括 +- 2* 標准誤差。 例如,使用iris

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

輸出是:

在此處輸入圖片說明

如何(如果可能)將灰色區域擴展為標准誤差的兩倍?

實現所需結果的一種選擇是通過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