options pagesize=150 linesize=68;
filename grafout 'g05.ps';
goptions device=pslmono gsfname=grafout 
   rotate=landscape gsfmode=append;
/*********************************************************/
/* Mark.A Example of McNemar's test.                     */
/*********************************************************/
/************************************************************/
/* Purity data, example H of Cox and Snell (1981) Applied   */
/* Statistics.  Two manufacturing processes were applied to */
/* raw materials from 22 batches, yeilding 22 matched pairs.*/
/* First variable is covariate measuring purity of batch,   */
/* and next two variables reflect whether the result was    */
/* flawed.  Does one of the processes produce fewer flaws?  */
/************************************************************/
data purity ; infile 'purity.dat';
   input pure r1 $ r2 $ ; batch=_n_; run;
title 'McNemar test';
*The option agree below causes McNemar's test to be run,;
*and also outputs the kappa statistic, which is not relevant;
*in the current context.                                    ;
proc freq data=purity; 
   table r1*r2/norow nocol nopct agree; run;
* The same test may be run on the reformatted data using;
* the cmh test.  First, create separate data records for;
* each member of the pair.;
data purity2; set purity;
   proce=1; result=r1; output; proce=2; result=r2; output; run;
proc freq data=purity2; 
   table batch*proce*result/cmh noprint ; run;
/********************************************************/
/* Mark.B Create three fake data sets, one consistent   */
/* with H0 for CMH and McNemar, and two consistent with */
/* one H0 but not with the other.                       */
/********************************************************/
title 'Fake comparisons of McNemar and Pearson';
data fake; input a b we ex @@;
cards;
0 0 100 1 0 1  50 1 1 0  50 1 1 1  25 1 
0 0 100 2 0 1  50 2 1 0  50 2 1 1   0 2 
0 0 200 3 0 1  50 3 1 0 100 3 1 1  25 3
;
run;
proc freq data=fake; by ex; weight we;
   table a*b/chisq nopct nocol norow expect agree; run;
/********************************************************/
/* Mark.C Poisson regression example.                   */
/********************************************************/
/*******************************************************/
/* Number of accidental deaths from falls, 1979        */
/* World Almanac and Book of Facts 1984 (New York:     */
/* Newspaper Enterprise Association, 1984), and quoted */
/* in the Minitab Handbook, 3rd Edn. (Belmont, CA: Dux-*/
/* bury, 1994).  Falls are by month.  Columns are num- */
/* bers of falls, numbers of days in the month, and    */
/* month abbreviation.  Deaths may be influence by the */
/* weather; scorebelow scores months based on icyness. */
/*******************************************************/
title 'Falls data';
data falls; infile 'falls.dat'; input falls days month $; 
  score=0; _freq_=12;
  if _n_=1 or _n_=2 then score=1;
  if _n_=11 or _n_=12 or _n_=3 then score=2;
  run;
/*******************************************************/
/* Fit a Poisson regression model, without and with    */
/* accounting for number of days in each month.        */
/*******************************************************/
title 'Poisson regression model';
data falls; set falls; lt=log(days); run;
proc genmod data=falls; class month;
  model falls=month/dist=poisson type1; run;
title 'Poisson regression model with offset';
proc genmod data=falls; class month;
  model falls=month/dist=poisson offset=lt type1; run;
title1 'Poisson regression model to get goodness of fit';
title2 'Model ignores offset.';
proc genmod data=falls; model falls=/dist=poisson; run;
title 'Chi-square approach';
/*********************************************************/
/* Compare the Poisson regression approach to the Pearson*/
/* chi-square approach.                                  */
/*********************************************************/
proc freq data=falls; weight falls; 
   table month/ testp=( 0.08333333 0.08333333 0.08333333 
      0.08333333 0.08333333 0.08333333 0.08333333 0.08333333 
      0.08333333 0.08333333 0.08333333 0.08333333); run;