Linear Regression plot with normal curves for error (sideways)
[caption id="attachment\_755" align="aligncenter" width="480" caption="Linear Regression: Normal Errors"][/caption]
I remember my first encounter to the classical linear regression model with normally distributed errors by Dan Gillen in Stat 120c in spring 2005 at UCI. I mean, I've done linear regression in high school science classes: you get a best fit line out of your data. In the stat class, I remember seeing data drawn on the board and a line going through the data. To illustrate the normally distributed errors, he drew sideway normal curves.
I liked this presentation of the classical model. How do convey this in R? I couldn't find a package for it (searched for an hour I think). I always search for packages because I always think others could do a better job at it than me and because I don't like to re-invent things, even though it requires less time for me to do it myself in many cases. Well, here it is, written by me in R:
```{r}
<- function(where.x, where.y, values=seq(-4,4,.1), magnify=4, ...){
sideways.dnorm ###... consists of mean, sd, and log passed to dnorm function
<- dnorm(x=values, ...)
dens <- where.x + dens * magnify
x <- where.y + values
y return(cbind(x,y))
}
<- sideways.dnorm(where.x=3, where.y=3, values=seq(-2,2,.1))
temp plot(temp)
##jpeg("LinearRegressionNormalErrors.jpeg")
pdf("LinearRegressionNormalErros.pdf")
set.seed(123)
<- 200
n <- runif(n, -8,8)
x <- 1 + .5*x + rnorm(n, sd=1)
y <- lm(y~x)
fit plot(x,y, xlim=c(-10,10), ylim=c(-8, 10)
main="Classical Linear Regression\nwith Gaussian errors"
, ##, main=expression(paste("Classical Linear Regression\nwith N(0,", sigma\^2, ") errors", sep=''))
col="red"
,
)abline(fit, lwd=3, col="blue")
<- c(-4,0,4)
where.normal.x for(i in 1:length(where.normal.x)){
<- where.normal.x[i]
where.x <- predict(fit, newdata=data.frame(x=where.x))
where.y <- sideways.dnorm(where.x=where.x, where.y=where.y, magnify=4)
xy lines(xy)
abline(h=where.y, lty=2)
abline(v=where.x, lty=2)
}
dev.off()
```