Reputation: 7846
I can plot a linear regression line using abline:
x= runif(10)
y=runif(10)
fit1 <- lm(x ~ y )
sd2 <- sd(abs(fit1$residuals))*2
plot(x,y)
abline(fit1)
But how can I plot the standard deviation lines using abline. I tried:
abline(fit1+sd2)
abline(fit1-sd2)
but I get an error. Thank you for your help.
Upvotes: 1
Views: 5760
Reputation: 427
abline(fit)
works because it takes in intercept and slope from fit
to draw the line, it is identical to my second line of code. So abline with sd2
can work if we move the line upp sd2
units and down sd2
units. You can achieve it by my third and fourth lines.
plot(x,y)
abline(fit1$coefficients[1],fit1$coefficients[2])
abline(fit1$coefficients[1]+sd2,fit1$coefficients[2])
abline(fit1$coefficients[1]-sd2,fit1$coefficients[2])
Hope this explanation of abline
is helpful.
Upvotes: 3
Reputation: 6020
You can use ggplot
to achieve this:
x= runif(10)
y=runif(10)
fit1 <- lm(x ~ y )
sd2 <- sd(fit1$residuals)
library(ggplot2)
ggplot(data.frame(x,y), aes(x=x,y=y)) +
geom_point() +
geom_smooth(method="lm")
Upvotes: 2