options ls=65 ps=54;              
title 'Battery Life Example in Table 6-1 pg. 235';

* Read data into SAS from an external file;
filename raw '~/Montgomery/Data/tble6-1.dat';
data raw;
  infile raw;
  input factorA factorB response;     
run;                                  

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

* Statistical analysis for two-factor factorial design with
interaction;
proc glm data=raw;              
  class factorB factorA;
  model response=factorA factorB factorA*factorB / p;
  means factorA factorB factorA*factorB / lsd alpha=0.05;
  lsmeans factorA*factorB / stderr tdiff pdiff;
  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;