You are on page 1of 2

Diagnostics in R Commander

First, lets assume you have the data set I named Copier, from Problem 1.20, with
explanatory variable named Copiers and response variable named Minutes. Assume
further that you have fit a linear model to the data, and that the model is named
Maintenance. Once you have fit a linear model, the residuals ei and the fitted values
are stored in the model. You can view them by typing
Maintenance$residuals
or
Maintenance$fitted.values
in the script window and hitting Submit. Its better to add them to the data as
additional variables. So go to Data -> Manage variables in active dataset -> Compute
new variable. Under New variable name choose an appropriate name like
Residuals or Fits. Then in the Expression to compute box type either
Maintenance$residuals or Maintenance$fitted.values to add these to your dataset.
Now if you click on the View data set button you will see that the residuals and/or
fitted values are now part of the data.
To prepare a stem-and-leaf plot of the residuals, go to Graphs -> Stem-and-leaf
display Under Variable pick Residuals (or whatever you named them), then I
would recommend changing Parts Per Stem from Automatic to 2 or 1. Under
Options I recommend you uncheck Trim outliers and Show depths. Then hit OK.
The stem-and-leaf plot of the residuals will appear in the Output Window. You cant save
it individually, but you could either copy-and-paste it or copy it by hand.
To plot the residuals against Xi, go to Graphs -> Scatterplot and choose the same xvariable youve been using, e.g., Copiers in this example, and choose Residuals (or
whatever you named them) as the y-variable. I recommend that you uncheck all the
boxes that were checked by default. Then hit OK. Your plot will appear in the R
interface on the graphics device. Refer back to Handout 4 regarding adding titles to your
plot. You would follow a similar procedure to plot the residuals against the fitted values.
To prepare a normal probability plot of the residuals, you have several options. First, you
can go to Models -> Graphs -> Basic diagnostic plots. In R, four plots will appear
together, including a normal probability plot of the (standardized residuals). Its the one
labeled Normal Q-Q. It indicates outliers with their observation index numbers. The
plot of the residuals against the fitted values is included in the output.
As a second option, you can go to Graphs -> Quantile-comparison plot, choose the
residuals as your variable, make sure Normal is selected, and hit OK. You get the plot,
along with the line through the quartiles, and an envelope of the tolerable range for the
points. This enables you to see outliers easily.
A third option is to type
qqnorm(Maintenance$residuals); qqline(Maintenance$residuals)

in the Script Window (using the name of your model in place of Maintenance), then hit
Submit. A normal probability plot of the residuals will appear in the R interface
graphics device.

To obtain the correlation between the ordered residuals and their expected values under
normality, you will need to carefully follow these steps:
1.
In the Script Window, type
StdErr = summary(Maintenance)$sigma
but use the name of your model in place of Maintenance. Then hit Submit.
2.
In the Script Window, type
n = 45
but use the size of your dataset instead of 45. Then hit Submit.
3.
In the Script Window, type
ExpVals = sapply(1:n, function(k) StdErr * qnorm((k-.375)/(n+.25)))

exactly as it appears. Then hit Submit.


4.
Finally, in the Script Window, type
cor(ExpVals,sort(Maintenance$residuals))
but use the name of your model in place of Maintenance. Then hit Submit.
If you were careful, then in the Output Window should appear the desired correlation,
which you can then compare to the critical values in Table B.6, p.1329 of the text.
To prepare a time plot of the residuals, go to Graphs -> Index plot, choose the
residuals as your variable, and change from Spikes to Points. Hit OK. The plot
appears in R in the graphics device.
To conduct the Breusch-Pagan test, go to Models -> Numerical diagnostics -> BreuschPagan test for heteroscedasticity Dont change anything in the dialog box; just hit
OK. In the output window you will see the value of the test statistic BP and the P-value
for the test. If the P-value is below your -level, you will reject H0 and conclude that the
error variance is not constant.

You might also like