Residuals

fjbjor<-c(5,2,9,7,3,3,4,5,8,3,5,5,6,7,1,4)
amagn<-c(0.1,0.03,0.19,0.095,0.07,0.02,0.07,0.085,0.12,0.04,0.06,0.05,0.1,0.09,0.01,0.05)
fit<-lm(amagn~fjbjor)

par(mar=c(4,4,2,1),mfrow=c(1,2))
plot(fjbjor,amagn,pch=16,col=1,cex=0.8,axes=F,xlim=c(0,10),ylim=c(0,0.2),
xaxs='i',yaxs='i',xlab='',ylab='',cex.lab=1.1)
abline(fit,col=colors()[309],lwd=1.5)
axis(1,cex.axis=1.1)
axis(2,cex.axis=1.1)
mtext('Number of beers',1,cex=1.3,2.5)
mtext('Alcohol level',2,cex=1.3,2.5)

resid<-resid(fit)
plot(fjbjor,resid,pch=16,col=1,cex=0.8,axes=F,xlim=c(0,10),ylim=c(-0.06,0.06),
xaxs='i',yaxs='i',xlab='',ylab='',cex.lab=1.1)
axis(1,cex.axis=1.1)
axis(2,cex.axis=1.1)
mtext('Number of beers',1,cex=1.3,2.5)
mtext('Residuals',2,cex=1.3,2.5)
segments(0,0,10,0,lty=2,col=colors()[309])
The first step in most diagnostic analyses is to
compute the residuals

\begin{block}{Residuals}
The vertical distance from our measurements to the regression line are called the \textbf{residuals} and are denoted with $\hat e$. The size of the residuals can be calculated with
\[
\hat e_i = y_i - \hat{y}_i
\]
Points above the regression line have a positive residue but points below it have a negative.
\end{block}
Examples
{\bf Example - beers}

We will continue to use the beer data.
\begin{verbatim}
beers<-c(5,2,9,7,3,3,4,5,8,3,5,5,6,7,1,4)
alcohol<-c(0.1,0.03,0.19,0.095,0.07,0.02,0.07,0.085,0.12,0.04,0.06,0.05,0.1,0.09,0.01,0.05)
\end{verbatim}

We use the lm() function to fit the data
\begin{verbatim}
fit<-lm(alcohol~beers)
\end{verbatim}
We use the residual() function to get the residuals:
\begin{verbatim}
residuals(fit)
\end{verbatim}