/* Filename: GrayonandDelpecheX2.sas Purpose: SAS code to fit a two-way table of frequencies. PROC FREQ is invoked for a chi-square test. Then we use IML to compute the adjusted residuals (see Agresti 1990 _Categorical Data Analysis_ p. 224. The data are from Grayson and Delpeche 1998, JAS 25:1119-1129. Last Update: FDN 3.30.02 */ data flageoletfrags; input group $ couche $ shaft proxdist; cards; high c06 158 66 high c08 220 70 high c09 362 72 low c04 92 18 low c05 863 119 low c07 563 489 low c11 327 84 ; proc sort data =flageoletfrags; by group couche; proc transpose out=frags (rename=(col1=count _name_=type)); by group couche; var shaft proxdist; proc print; run; proc freq data=frags; weight count; table couche*type / chisq cellchi2; run; proc iml; use flageoletfrags; * give the name of the input 2x2 table here; read all var _all_ into x [colname=vars rowname=couche]; * give the variable that contains row names after: rowname= ; rowmarg= x[,+]/sum(x); * get the marginal row probs; colmarg= x[+,]/sum(x); * get the marginal col probs; expected= (rowmarg*colmarg)#sum(x); * the expected values--note matrix multiplication; notexpected = (1-rowmarg)*(1-colmarg); * 1- the expected probs; se= (expected#notexpected)##.5; * the standard error; adjres = (x-expected)/se; * the adjusted residuals; create adjres from adjres [colname=vars rowname=couche]; * give the variable that contains row names; append from adjres [colname=vars rowname=couche]; * give the variable that contains row names; proc print; title2 'the adjusted residuals'; run;