options ls=80 ps=60 nodate;

data
cheese;
input taste acetic h2s lactic;
cards;
12.3 4.543 3.135 0.86
20.9 5.159 5.043 1.53
39.0 5.366 5.438 1.57
47.9 5.759 7.496 1.81
5.6 4.663 3.807 0.99
25.9 5.697 7.601 1.09
37.3 5.892 8.726 1.29
21.9 6.078 7.966 1.78
18.1 4.898 3.850 1.29
21.0 5.242 4.174 1.58
34.9 5.740 6.142 1.68
57.2 6.446 7.908 1.90
0.7 4.477 2.996 1.06
25.9 5.236 4.942 1.30
54.9 6.151 6.752 1.52
40.9 6.365 9.588 1.74
15.9 4.787 3.912 1.16
6.4 5.412 4.700 1.49
18.0 5.247 6.174 1.63
38.9 5.438 9.064 1.99
14.0 4.564 4.949 1.15
15.2 5.298 5.220 1.33
32.0 5.455 9.242 1.44
56.7 5.855 10.20 2.01
16.8 5.366 3.664 1.31
11.6 6.043 3.219 1.46
26.5 6.458 6.962 1.72
0.7 5.328 3.912 1.25
13.4 5.802 6.685 1.08
5.5 6.176 4.787 1.25
;

/* Example 7.1: standard residual plots*/
proc reg;
model taste = acetic h2s lactic/p r;
output out=diag p=yhat r=resid h=hat;
plot r.*p.;
run;

/* Printing out the leverages (i.e., diagonals of the hat matrix) */
proc print data=diag;
var hat;
title 'leverages--hat matrix diagonals';
run;

proc univariate plot;
var resid;
title 'univariate output';
run;

/* Example 7.2.
'student=student_residual' gives the internally studentised residuals
(i.e., the r_i's)
'rstudent=student_deleted_residual' gives the externally studentised residuals
(i.e., the t_i's)
Note: I'll create a new data set, diag, and then print the new data set. */

proc reg data=cheese;
model taste = acetic h2s lactic;
output out=diag2 p=yhat r=resid student=student_residual rstudent=student_deleted_residual;
run;
proc print data=diag2;
title 'fitted values, residuals and externally studentised residuals';
run;

/* Example 7.3: partial regression plots */
proc reg;
model taste = acetic h2s lactic/partial;
title 'partial regression plots';
run;

/* Example 7.5: VIF and Condition index */
proc reg;
model taste = acetic h2s lactic/vif collin;
title 'vif and condition index';
run;

/* Example 7.6: R^2, adj. R^2, C_p (and others) */
proc reg;
model taste = acetic h2s lactic / best=3
selection = rsquare adjrsq cp;
title 'model selection criteria';
run;

/* Example 7.7: Sequential variable selection procedures */
proc reg;
model taste = acetic h2s lactic/ details slentry=0.05 selection=forward;
model taste = acetic h2s lactic/ details slstay=0.05 selection=backward;
model taste = acetic h2s lactic/ details slentry=0.05 slstay=0.05 selection=stepwise;
title 'stepwise procedures';
run;