options ls=65 ps=54;              
title 'Chemical Yield example in Figure 7-1 pg. 291';

* Read data into SAS from an external file;
* LOW levels of a factor are -1 and HIGH levels are +1;
filename raw '~/Montgomery/Data/fig7-1.dat';
data raw;
  infile raw;
  input factorA factorB response;     
run;                                  

* Print the data;
proc print data=raw;         
run;

* Statistical analysis for 2^2 factorial design with interaction;
* Factor effects are not output here;
proc glm data=raw;              
  class factorB factorA;
  model response=factorA factorB factorA*factorB / p;
run;

* Statistical analysis for 2^2 factorial design with interaction;
* using a regression model;
* Factor effects (divided by 2) are output here;
proc glm data=raw;
  model response=factorA factorB factorA*factorB / p;
run;

* Statistical analysis for 2^2 factorial design (refined model);
proc glm data=raw;        
  class factorB factorA;
  model response=factorA factorB / p;
  means factorA factorB / lsd alpha=0.05;
  output out=temp p=fit r=resid student=stdresid;
run;

* Model adequacy checking (Residual Analysis);        
proc rank data=temp out=checkass normal=vw;
  var resid;
  ranks expected;
run;

proc plot data=checkass vpct=50 hpct=50;
* Normal Probability Plot;
  plot expected*resid = '*' / box;
* HOV, model adecuacy, and outlier detection;
  plot stdresid*fit = '*' / vref=-2 2 vrefchar='-' box; 
* HOV among factorA levels and outlier detection; 
  plot stdresid*factorA = '*' / vref=-2 2 vrefchar='-' box;   
* HOV among factorB levels and outlier detection;
  plot stdresid*factorB = '*' / vref=-2 2 vrefchar='-' box;
run;

proc univariate data=checkass plot normal;
  var resid;
run;