也许对某些人来说这很快,但是我想用plot()
表示回归的标准误差。
所以,如果我有这样的数据:
x1 <- 1:500
b0 <- 17
b1 <- 0.5
sigma <- 7
er <- rnorm(x1,0,sigma)
y <- b0 + b1*x1 + er
model1 <- lm(y~x1)
plot(x1,y)
abline(model1,col="red",lwd=5)
对于该回归,我如何用线表示标准误差?
谢谢!
答案 0 :(得分:0)
您可以手动计算回归而没有误差,然后将所有剩余误差的sd()
的常规行放入
x1 <- 1:500
b0 <- 17
b1 <- 0.5
sigma <- 7
er <- rnorm(x1,0,sigma)
y <- b0 + b1*x1 + er
GeneralStandardDev<-sd(model1$residuals)
UpperLine<- model1$coefficients[1]+model1$coefficients[2]*x1 + GeneralStandardDev
LowerLine<- model1$coefficients[1]+model1$coefficients[2]*x1 - GeneralStandardDev
model1 <- lm(y~x1)
plot(x1,y)
abline(model1,col="red",lwd=5)
lines(x1, UpperLine, col = "blue")
lines(x1, LowerLine, col = "blue")
答案 1 :(得分:0)
您可能要使用ggplot()
而不是plot()
。您的布局稍微现代一些,可能性更多。
在示例代码之后添加以下代码。为了提供更好的可见性,我对原始数据进行了一些更改(请参见第y <- b0 + b1*x1 + er*5
行。)
# ------ test case ggplot ----------------
library (ggplot2)
# --- change some data fields ------------
y <- b0 + b1*x1 + er*5
df <- data.frame(y,x1)
ggplot(data = df, aes(x1,y)) +
geom_point() +
geom_smooth(method="lm", color="red", fill = "blue")