

LOG for Lecture 9 SAS Session
=============================  9/26/08

* THE TOPIC HERE IS FREQUENCY CROSS-TABULATION
 AND STATISTICS FOR TESTING RELATIONSHIPS IN A 
 BIVARIATE DISCRETE (CONTINGENCY-TABLE) SETTING ;

/* Now all of my saved datasets are in a directory "SASdat" */
 libname mydir "SASdat";
    data polynes;
       set mydir.polynest;
 
/* =======================================
   Comparisons between patterns of occurrence of 
       deforstn individually with elev latits rain age 
 ========================================  */

     proc univariate data=polynes;
       var deforstn elev latits  rain age ;
                 run;

* Learned from this that # missing values are as follows:

 deforstn  1
 elev      0
 LatitS    0
 rain      1     (occurs with missing age)
 age       4

NOTE that records with missing values are dropped by default
     in any statistical calculation ;

data newpoly (keep = deforstn ElevCat LatCat rainCat age);
    set polynes;
    LatCat = int((latitS + 25)/30);
    ElevCat = int((elev + 500)/1000);
    RainCat = int((rain + 500)/2000);


 options nodate nocenter nopage linesize=75;
 
proc print data=newpoly (obs=10); run;

                          Lat    Elev    Rain
Obs    Deforstn    Age    Cat     Cat     Cat

  1        2        2      1       2       2
  2        2        2      1       2       3
  3        2        3      0       2       1
  4        2        2      1       3       1
  5        2        3      1       1       1
  6        4        3      1       2       1
  7        2        3      1       2       2
  8        2        3      1       1       1
  9        2        3      1       1       1
 10        2        3      1       1       1

proc freq data=newpoly;
   tables  Deforstn*Age Deforstn*LatCat Deforstn*ElevCat
        Deforstn*RainCat  / nopercent norow nocol;
run;


Table of Deforstn by Age

Deforstn     Age

Frequency|     1.5|       2|     2.5|       3|  Total
---------+--------+--------+--------+--------+
       2 |      0 |     10 |      0 |      7 |     17
---------+--------+--------+--------+--------+
       3 |      1 |      5 |      4 |     18 |     28
---------+--------+--------+--------+--------+
     3.5 |      0 |      1 |      0 |      3 |      4
---------+--------+--------+--------+--------+
       4 |      1 |      1 |      3 |     17 |     22
---------+--------+--------+--------+--------+
       5 |      0 |      0 |      2 |      2 |      4
---------+--------+--------+--------+--------+
Total           2       17        9       47       75

Frequency Missing = 5



Table of Deforstn by LatCat

Deforstn     LatCat

Frequency|       0|       1|       2|  Total
---------+--------+--------+--------+
       2 |      1 |     18 |      1 |     20
---------+--------+--------+--------+
       3 |      6 |     23 |      0 |     29
---------+--------+--------+--------+
     3.5 |      1 |      3 |      0 |      4
---------+--------+--------+--------+
       4 |      5 |     16 |      1 |     22
---------+--------+--------+--------+
       5 |      3 |      1 |      0 |      4
---------+--------+--------+--------+
Total          16       61        2       79

Frequency Missing = 1



Table of Deforstn by ElevCat

Deforstn     ElevCat

Frequency|       0|       1|       2|       3|       4|  Total
---------+--------+--------+--------+--------+--------+
       2 |      4 |      8 |      6 |      2 |      0 |     20
---------+--------+--------+--------+--------+--------+
       3 |      4 |     19 |      4 |      1 |      1 |     29
---------+--------+--------+--------+--------+--------+
     3.5 |      0 |      4 |      0 |      0 |      0 |      4
---------+--------+--------+--------+--------+--------+
       4 |     12 |      4 |      3 |      1 |      2 |     22
---------+--------+--------+--------+--------+--------+
       5 |      3 |      1 |      0 |      0 |      0 |      4
---------+--------+--------+--------+--------+--------+
Total          23       36       13        4        3       79

Frequency Missing = 1

                                             

The FREQ Procedure

Table of Deforstn by RainCat

Deforstn     RainCat

Frequency|       0|       1|       2|       3|  Total
---------+--------+--------+--------+--------+
       2 |      1 |     13 |      4 |      2 |     20
---------+--------+--------+--------+--------+
       3 |      7 |     19 |      1 |      1 |     28
---------+--------+--------+--------+--------+
     3.5 |      3 |      1 |      0 |      0 |      4
---------+--------+--------+--------+--------+
       4 |      7 |     15 |      0 |      0 |     22
---------+--------+--------+--------+--------+
       5 |      4 |      0 |      0 |      0 |      4
---------+--------+--------+--------+--------+
Total          22       48        5        3       78

Frequency Missing = 2


* RE-DO by splitting each explanatory variable at median;

proc means data=polynes median;
   var elev latitS rain age; run;

Variable          Median
------------------------
Elev         998.0000000
LatitS        16.3000000
Rain             1967.00
Age            3.0000000
------------------------

data newpoly (keep = deforstn ElevCat LatCat rainCat age);
    set polynes;
    if (deforstn ne .) then deforstn = min(deforstn, 3.5);
    if (age ne .) then  age = (age ge 3);
    LatCat = (LatitS ge 16.3);
    ElevCat = (elev ge 1000);
    if (rain ne .) then RainCat = (rain ge 2000); 
         else rain=. ; run;

proc freq data=newpoly;
   tables  Deforstn*Age Deforstn*LatCat Deforstn*ElevCat
        Deforstn*RainCat  / nopercent norow nocol;
run;


Table of Deforstn by Age

Deforstn     Age

Frequency|       0|       1|  Total
---------+--------+--------+
       2 |     10 |      7 |     17
---------+--------+--------+
       3 |     10 |     18 |     28
---------+--------+--------+
     3.5 |      8 |     22 |     30
---------+--------+--------+
Total          28       47       75

Frequency Missing = 5



Table of Deforstn by LatCat

Deforstn     LatCat

Frequency|       0|       1|  Total
---------+--------+--------+
       2 |     15 |      5 |     20
---------+--------+--------+
       3 |     12 |     17 |     29
---------+--------+--------+
     3.5 |     12 |     18 |     30
---------+--------+--------+
Total          39       40       79

Frequency Missing = 1



Table of Deforstn by ElevCat

Deforstn     ElevCat

Frequency|       0|       1|  Total
---------+--------+--------+
       2 |      6 |     14 |     20
---------+--------+--------+
       3 |     14 |     15 |     29
---------+--------+--------+
     3.5 |     19 |     11 |     30
---------+--------+--------+
Total          39       40       79

Frequency Missing = 1



Table of Deforstn by RainCat

Deforstn     RainCat

Frequency|       0|       1|  Total
---------+--------+--------+
       2 |      2 |     18 |     20
---------+--------+--------+
       3 |     12 |     16 |     28
---------+--------+--------+
     3.5 |     26 |      4 |     30
---------+--------+--------+
Total          40       38       78

Frequency Missing = 2


*----------------------------------------
So far we examined the Tables visually only 
----------------------------------------- ;

data newpoly2;
    set polynes;
    if (age ne .) then  age = (age ge 3);
    LatCat = (LatitS ge 16.3);
    ElevCat = (elev ge 1000);
    if (rain ne .) then RainCat = (rain ge 2000); 
         else rain=. ; 

proc sort data=newpoly2; by RainCat; 
proc boxplot data=newpoly2;
  plot Deforstn* RainCat; run;


*-------------------------------------- ;

proc freq data=newpoly;
   tables  Deforstn*RainCat  / nopercent norow nocol expected;
run;

/* Gives same table as before along with "expected" cell counts
   under assumption of row-column independence 

Finally get chisq statistic values as well */

proc freq data=newpoly;
   tables  Deforstn*RainCat  / nopercent norow nocol expected chisq ;
run;



Statistics for Table of Deforstn by RainCat

Statistic                     DF       Value      Prob
------------------------------------------------------
Chi-Square                     2     29.4729    <.0001
Likelihood Ratio Chi-Square    2     33.2730    <.0001
Mantel-Haenszel Chi-Square     1     26.7475    <.0001
Phi Coefficient                       0.6147
Contingency Coefficient               0.5237
Cramer's V                            0.6147

Effective Sample Size = 78
Frequency Missing = 2









*#################################################
 For additional examples, see

http://www.math.umd.edu/~evs/s430/Scripts/FreqExmp.Log ;




