\documentstyle[11pt,fullpage,psfig]{article}

\input info.tex

\newcommand{\Var}{\mbox{Var}\,}

\newcommand{\eq}[1]{(\ref{#1})}

\newcommand{\combos}[2]{\left(\begin{array}{c}#1\\#2\end{array}\right)}

%\slides

\begin{document}

\begin{center}\bf
Note Set \#1 for Thursday March 5, 1998
\end{center}

\section*{Log-Linear Models in SAS}

There are four SAS procedures that can be used almost immediately to
fit and evaluate log-linear models.  They are
\begin{center}
\begin{tabular}{|l|p{3in}|}
\hline
Proc Name & Descripion \\
\hline
PROC FREQ & produces tables similar to {\tt crosstabs()} in SPLUS \\
\hline
PROC CATMOD & fits a variety of models (including log-linear) to
categorical data \\
\hline
PROC GENMOD & essentially equivalent to {\tt glm()} in SPLUS \\
\hline
PROC INSIGHT & does a variety of interactive graphics and model
fitting. \\
\hline
\end{tabular}
\end{center}

To illustrate these different procedures I will do partial analyses of
the automobile accident data from last time in SAS.

\subsection*{Running an analysis in SAS}

To run a SAS job, create the command file, let's say you call it 
{\tt bozo.sas}, in a text editor.  Now there are three things you can
do:
\begin{enumerate}

\item Say {\tt sas bozo.sas} at the UNIX command prompt, and then after
the command finishes running, look for newly created files in your 
directory, like {\tt bozo.log} and {\tt bozo.lst}.  

Or...

\item In the interactive windows version of SAS, find the small 
window labelled {\sf SAS: PROGRAM EDITOR--Untitled} and use the
File/Open... pulldown menu to read in your progam to the editor.  
Once it has been read in, find the small horizontal toolbar labelled 
{\sf Toolbox: PROGRAM EDITOR} again and click on the running man
in the middle of the toolbar to run the job.  The ``bozo.log'' file will 
appear in the {\sf SAS: LOG} window, and the ``bozo.lst'' 
file will appear in the {\sf SAS: OUTPUT} window.

{\bf Or...}

\item Even more interesting is to use PROC INSIGHT to do an entire
analysis.  You can get help on PROC INSIGHT by selecting \ul{MODELING
AND ANALYSIS TOOLS} and then \ul{Interactive} \ul{Data Analysis}.  

\end{enumerate}

To quit the interactive version of SAS, find the {\sf SAS: Session
Management} window (usually minimized, so you'll have to restore it from
its screen icon) and click on the ``Terminate'' button. 

I have combined all the illustrative analyses in a single file here;
however you might want to consider doing each one separately.  Sample
files for the separate analyses can be found in the files {\tt
  freq.sas}, {\tt catmod.sas}, {\tt genmod.sas} and {\tt insight.sas},
in the class data directory.

\begin{verbatim}
/* bozo.sas */

data accident;
input Cell Count Type $ Severity $ Eject $ Weight $ ;
datalines;
 1  350 Type1 Sev1 Ejct1  Wght1
 2  150 Type2 Sev1 Ejct1  Wght1
 3   60 Type1 Sev2 Ejct1  Wght1
 4  112 Type2 Sev2 Ejct1  Wght1
 5   26 Type1 Sev1 Ejct2  Wght1
 6   23 Type2 Sev1 Ejct2  Wght1
 7   19 Type1 Sev2 Ejct2  Wght1
 8   80 Type2 Sev2 Ejct2  Wght1
 9 1878 Type1 Sev1 Ejct1  Wght2
10 1022 Type2 Sev1 Ejct1  Wght2
11  148 Type1 Sev2 Ejct1  Wght2
12  404 Type2 Sev2 Ejct1  Wght2
13  111 Type1 Sev1 Ejct2  Wght2
14  161 Type2 Sev1 Ejct2  Wght2
15   22 Type1 Sev2 Ejct2  Wght2
16  265 Type2 Sev2 Ejct2  Wght2
;

proc freq data=accident;
  weight Count;
  tables Type*Severity*Eject*Weight / chisq;
  /* this lets us check conditional independence of Eject and Weight */
  /* given fixed values of Type and Severity (see output) */
run;

proc catmod data=accident;
  weight Count;
  model Type*Severity*Eject*Weight = _response_ / 
    ml noresponse noparm pred=freq;
  loglin Type|Severity|Eject|Weight @ 2;
  /* this is like (Type+Severity+Eject+Weight)^2 in SPLUS */
run;

proc genmod data=accident;
   class Type Severity Eject Weight;
   model Count = Type|Severity|Eject|Weight @ 2 /       dist = poisson
                                                        link = log
   /* specify analysis-of-deviance table like SPLUS */  type1;
run;

proc insight data=accident;
run;

\end{verbatim}

\subsection*{PROC FREQ}

Here is an extract of the output from PROC FREQ:
\begin{verbatim}
                               The SAS System                             25
                                              11:29 Wednesday, March 4, 1998

                         TABLE 1 OF EJECT BY WEIGHT
                  CONTROLLING FOR TYPE=Type1 SEVERITY=Sev1

                    EJECT     WEIGHT

                    Frequency|
                    Percent  |
                    Row Pct  |
                    Col Pct  |Wght1   |Wght2   |  Total
                    ---------+--------+--------+
                    Ejct1    |    350 |   1878 |   2228
                             |  14.80 |  79.41 |  94.21
                             |  15.71 |  84.29 |
                             |  93.09 |  94.42 |
                    ---------+--------+--------+
                    Total         376     1989     2365
                                15.90    84.10   100.00

                    EJECT     WEIGHT

                    Frequency|
                    Percent  |
                    Row Pct  |
                    Col Pct  |Wght1   |Wght2   |  Total
                    ---------+--------+--------+
                    Ejct2    |     26 |    111 |    137
                             |   1.10 |   4.69 |   5.79
                             |  18.98 |  81.02 |
                             |   6.91 |   5.58 |
                    ---------+--------+--------+
                    Total         376     1989     2365
                                15.90    84.10   100.00

           Statistic                     DF     Value        Prob
           ------------------------------------------------------
           Chi-Square                     1     1.031       0.310

[ much more output omitted ... ]
\end{verbatim}

\subsection*{PROC CATMOD}
Here is a part of the output from PROC CATMOD:
\begin{verbatim}
               MAXIMUM-LIKELIHOOD ANALYSIS-OF-VARIANCE TABLE

             Source                   DF   Chi-Square      Prob
             --------------------------------------------------
             TYPE                      1        93.33    0.0000
             SEVERITY                  1       155.36    0.0000
             TYPE*SEVERITY             1       391.60    0.0000
             EJECT                     1       764.26    0.0000
             TYPE*EJECT                1       108.57    0.0000
             SEVERITY*EJECT            1       220.36    0.0000
             WEIGHT                    1       706.50    0.0000
             TYPE*WEIGHT               1        15.33    0.0001
             SEVERITY*WEIGHT           1        59.17    0.0000
             EJECT*WEIGHT              1         1.72    0.1901

             LIKELIHOOD RATIO          5         7.33    0.1974
\end{verbatim}
The Chi-squared values here are approximately the change in deviance
for adding the corresponding term to the model {\em last}\footnote{You
  can get exactly these deviances by requesting a ``type3'' analysis
  in PROC GENMOD.}, i.e. this tells you how significant each term
remains after accounting for variability due to all other terms in the
model.

\subsection*{PROC GENMOD}

With the ``type1'' option on the MODEL line, GENMOD produces exactly
the same analysis of deviance table as {\tt glm()} in SPLUS.
\begin{verbatim}
                     LR Statistics For Type 1 Analysis

           Source             Deviance    DF   ChiSquare  Pr>Chi

           INTERCEPT         7686.3582     0           .       .
           TYPE              7653.6969     1     32.6613  0.0001
           SEVERITY          6164.2646     1   1489.4323  0.0001
           TYPE*SEVERITY     5562.8450     1    601.4196  0.0001
           EJECT             2888.1315     1   2674.7135  0.0001
           TYPE*EJECT        2602.1353     1    285.9962  0.0001
           SEVERITY*EJECT    2372.8023     1    229.3331  0.0001
           WEIGHT              76.3569     1   2296.4453  0.0001
           TYPE*WEIGHT         75.5996     1      0.7573  0.3842
           SEVERITY*WEIGHT      9.0200     1     66.5797  0.0001
           EJECT*WEIGHT         7.3281     1      1.6918  0.1934
\end{verbatim}

\subsection*{PROC INSIGHT}

To set this up you need a {\tt bozo.sas} file like this:
\begin{verbatim}
data accident;
input Cell Count Severity $ Eject $ Weight $ ;
datalines;
 1  350 Type1 Sev1 Ejct1  Wght1
 2  150 Type2 Sev1 Ejct1  Wght1
 3   60 Type1 Sev2 Ejct1  Wght1
 4  112 Type2 Sev2 Ejct1  Wght1
 5   26 Type1 Sev1 Ejct2  Wght1
 6   23 Type2 Sev1 Ejct2  Wght1
 7   19 Type1 Sev2 Ejct2  Wght1
 8   80 Type2 Sev2 Ejct2  Wght1
 9 1878 Type1 Sev1 Ejct1  Wght2
10 1022 Type2 Sev1 Ejct1  Wght2
11  148 Type1 Sev2 Ejct1  Wght2
12  404 Type2 Sev2 Ejct1  Wght2
13  111 Type1 Sev1 Ejct2  Wght2
14  161 Type2 Sev1 Ejct2  Wght2
15   22 Type1 Sev2 Ejct2  Wght2
16  265 Type2 Sev2 Ejct2  Wght2
;
proc insight data=accident;
run;
\end{verbatim}
When you run this in interactive SAS, a new window will pop up with
the data in a spreadsheet format.
\begin{itemize}
\item You can use the {\sf Edit/Variables...} pulldown menu to recode 
variables if you wish, or use the {\sf Analyze} pulldown menu to make 
a variety of exploratory plots (e.g. scatter plots, box plots, and so forth.
\item More interestingly, you can use the {\sf Analyze/Fit(Y X)...} pulldown
menu to perform linear regressions, log-linear model fits, etc.  (after
selecting the Y and X variables in the dialog box for {\sf Analyze/Fit(Y
X)...}, be sure to click on the Method button and select {\sf Poisson}
for the response distribution and {\sf log} for the link function, from
the menu of radio buttons that appears.)
\end{itemize}

\subsection*{Getting help in SAS}

After starting the interactive version of SAS by typing ``sas'' at the
UNIX prompt on Andrew, find the small horizontal toolbar labelled
{\sf Toolbox: PROGRAM EDITOR--Untitled} and click the help button on the
far right.

In the help window that pops up, you can 
\begin{itemize}

\item Click \ul{MODELING AND ANALYSIS TOOLS}, then \ul{Data Analysis} to
get a list of PROC's that you can get help on, including CATMOD, GENMOD
and FREQ.

\item Click \ul{SAMPLE PROGRAMS}, then \ul{SAS/STAT}, then e.g.
\ul{Documentation Examples from Proc} \ul{Catmod} (examples 4 and 5
illustrate fitting log-linear models) or \ul{Documentation Examples}
\ul{from Proc Genmod: 1} (look for the Poisson regression example), etc.,  
to get examples of how to set up SAS procedure files to use these commands.  

\item Graphics aren't all that easy to figure out in SAS.  Probably the
best thing to do is to try PROC INSIGHT.  There are a couple of nice
examples of using PROC INSIGHT for EDA and residual analysis in the
36-462/745 web pages this semester:
\begin{itemize}
\item[$*$] 
{\tt http://lib.stat.cmu.edu/\~{}giovanni/745/sas\_eda/sas\_eda.html}
\item[$*$] 
{\tt http://lib.stat.cmu.edu/\~{}giovanni/745/sas\_reg/sas\_reg.html}
\end{itemize}
\end{itemize}

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



