/**********************************************************************/


   /* DATA Step Method--Three Chi-square Tests            */
   /* for Testing Equal Cell Probabilities                */

   /* create data set of cell counts                      */
proc freq data=defects;                     
   tables type / noprint out=cells;         
   run;                                     
proc summary;                               
   var count;
                               
      /* data set SUMMARY contains                        */    
      /* total count and number of cells                  */    
   output out=summary sum=total n=ncells;   
   run;                                     
data _null_;                                
   set cells nobs=ncells end=eof;           
   if _n_=1 then set summary; 
              
         /* equal expected cell counts                    */    
      cellexp=total/ncells;                 
                                            
   /* PEARSON'S CHI-SQUARE                                */
    
         /* cell contribution to chi-square               */    
      cellchsq=(count-cellexp)**2 / cellexp;

         /* sum is Pearson chi-square                     */    
      chisq+cellchsq;                       
                                            
   /* NEYMAN CHI-SQUARE                                   */
    
         /* cell contribution to chi-square               */    
      ncellcsq=(count-cellexp)**2 / count;
  
         /* sum is Neyman chi-square                      */     
      nchisq+ncellcsq;                      
                                            
   /* LIKELIHOOD RATIO CHI-SQUARE                         */
    
         /* cell contribution to G**2                     */    
      cellg2=count*log(count/cellexp);

         /* sum is one-half G**2                          */                
     g2+cellg2;                                         
                                                        
  if eof then do;                                       
     g2=2*g2;                                           
     df=ncells-1;                                       
     pchisq=1-probchi(chisq,df);                        
     pnchisq=1-probchi(nchisq,df);                      
     pg2=1-probchi(g2,df);                              
     put  // @21 'Pearson chi-square = ' chisq;         
     put     @21 'Degrees of freedom = ' df;            
     put     @21 'Pr > chi-square    = ' pchisq 6.4 / ; 
     put     @21 'Neyman chi-square  = ' nchisq;        
     put     @21 'Degrees of freedom = ' df;            
     put     @21 'Pr > chi-square    = ' pnchisq 6.4 / ;
     put @17 'Likelihood ratio chi-square = ' g2;       
     put @17 'Degrees of freedom          = ' df;       
     put @17 'Pr > chi-square             = ' pg2 6.4;  
   end;                                                 
   run;


/**********************************************************************/


   /* PROC CATMOD Method--Neyman's Chi-square             */
   /* for Testing Equal Cell Probabilities                */

proc catmod data=defects;           
   response marginals;              
   model type=;                     
   restrict b1=.2 b2=.2 b3=.2 b4=.2;
run; 


/**********************************************************************/


proc catmod data=cells;             
   weight count;                    
   response marginals;              
   model type=;                     
   restrict b1=.2 b2=.2 b3=.2 b4=.2;
run;


/**********************************************************************/


proc catmod data=defects;        
   response +  -.2, -.2, -.2, -.2
            *  1 0 0 0 0,        
               0 1 0 0 0,        
               0 0 1 0 0,        
               0 0 0 1 0;        
   model type= / noint;          
run;                             


/**********************************************************************/


response marginals;


/**********************************************************************/


   /* PROC CATMOD Method--The Likelihood Ratio Chi-Square */
   /* for Testing Equal Cell Probabilities                */

proc catmod data=defects;      
  model type= / noint ml nogls;
run;


/**********************************************************************/


proc catmod data=cells;        
  weight count;                
  model type= / noint ml nogls;
run;                                                      


/**********************************************************************/


   /* PROC CAPABILITY Method--Pearson's Chi-square        */
   /* for Testing Equal Cell Probabilities                */

proc capability data=defects noprint;                     
   histogram type / midpoints=1 to 5                       
                    beta(alpha=1 beta=1 theta=0.5 sigma=5);
   title 'Pearson''s chi-square test for equal probabilities';     
run;                                                      


/**********************************************************************/


   /* DATA Step Method--Three Chi-square Tests            */ 
   /* for Testing for a Specified Distribution            */

proc freq data=defects;
                                
      /* create data set of cell counts                   */                
   tables type / noprint out=cells;                     
   run;                                                 
data cells;
                                            
      /* update data set CELLS with expected cell counts  */
   set cells;                                           
   input cellexp @@;                                    
   cards;                                               
   12.5 12.5 50 12.5 12.5                               
run;                                                 
proc summary;                                          
   var count;
                                           
      /* data set SUMMARY contains                        */   
      /* total count and number of cells                  */   
   output out=summary sum=total n=ncells;  
run;                                    
data _null_;                              
   set cells nobs=ncells end=eof;          
   if _n_=1 then set summary;              
                                          
   /* PEARSON'S CHI-SQUARE                                */
   
         /* cell contribution to chi-square               */   
      cellchsq=(count-cellexp)**2 / cellexp

         /* sum is Pearson chi-square                     */   
      chisq+cellchsq;                      
                                          
   /* NEYMAN CHI-SQUARE                                   */
   
         /* cell contribution to chi-square               */   
      ncellcsq=(count-cellexp)**2 / count; 

         /* sum is Neyman chi-square                      */    
      nchisq+ncellcsq;                     
                                          
   /* LIKELIHOOD RATIO CHI-SQUARE                         */
   
         /* cell contribution to G**2                     */   
      cellg2=count*log(count/cellexp); 
     
         /* sum is one-half G**2                          */   
      g2+cellg2;                           
                                          
   if eof then do;                         
      g2=2*g2;                             
      df=ncells-1;                         
      pchisq=1-probchi(chisq,df);          
      pnchisq=1-probchi(nchisq,df);                      
      pg2=1-probchi(g2,df);                              
      put  // @21 'Pearson chi-square = ' chisq;         
      put     @21 'Degrees of freedom = ' df;            
      put     @21 'Pr > chi-square    = ' pchisq 6.4 / ; 
      put     @21 'Neyman chi-square  = ' nchisq;        
      put     @21 'Degrees of freedom = ' df;            
      put     @21 'Pr > chi-square    = ' pnchisq 6.4 / ;
      put @17 'Likelihood ratio chi-square = ' g2;       
      put @17 'Degrees of freedom          = ' df;       
      put @17 'Pr > chi-square             = ' pg2 6.4;  
   end;                                                  
run;                                                  


/**********************************************************************/


   /* PROC CATMOD Method--Neyman's Chi-square             */
   /* for Testing for a Specified Distribution            */

proc catmod data=defects;                 
   response marginals;                    
   model type=;                           
   restrict b1=.125 b2=.125 b3=.5 b4=.125;
run;                                      


/**********************************************************************/


proc catmod data=defects;              
   response +  -.125, -.125, -.5, -.125
            *  1 0 0 0 0,              
               0 1 0 0 0,              
               0 0 1 0 0,              
               0 0 0 1 0;              
   model type= / noint;                
run;                                   


/**********************************************************************/


   /* PROC CATMOD Method--The Likelihood Ratio Chi-Square */
   /* for Testing for a Specified Distribution            */

proc catmod data=defects;            
   model type= / ml;                 
   restrict b1=0 b2=0 b3=1.3863 b4=0;
run;                                 


/**********************************************************************/