\section*{Bunches of Examples}
The following data are the survival times of groups of four animals
randomly allocated to three poisons and four treatments.  Then
experiment was part of an investigation to combat the effects of
certain toxic agents.  The is a 4-fold replication of a $3 \times 4$
factorial design.   Do an analysis of these data.
\ \\
\begin{center}
\begin{tabular}{c l l l l}
\hline
 &\multicolumn{4}{c}{Treatment} \\\hline
poison & A&B&C&D \\ \hline
I& 0.31& 0.82& 0.43& 0.45\\
&  0.45& 1.10& 0.45& 0.71\\
&  0.46& 0.88& 0.63& 0.66\\
&  0.43& 0.72& 0.76& 0.62\\
\\
II&  0.36& 0.92& 0.44& 0.56\\
&  0.29& 0.61& 0.35& 1.02\\
&  0.40& 0.49& 0.31& 0.71\\
&  0.23& 1.24& 0.40& 0.38\\
\\
III&  0.22& 0.30& 0.23& 0.30\\
&  0.21& 0.37& 0.25& 0.36\\
&  0.18& 0.38& 0.24& 0.31\\
&  0.23& 0.29& 0.22& 0.33\\\hline
\end{tabular}
\end{center}

As always the first step is to enter the data. This time, I'll just
enter the data and create the factor levels on the fly.

Here is the raw data, in a file called {\tt poison.dat}
\begin{verbatim}
 0.31 0.82 0.43 0.45
  0.45 1.10 0.45 0.71
  0.46 0.88 0.63 0.66
  0.43 0.72 0.76 0.62
  0.36 0.92 0.44 0.56
  0.29 0.61 0.35 1.02
  0.40 0.49 0.31 0.71
  0.23 1.24 0.40 0.38
  0.22 0.30 0.23 0.30
  0.21 0.37 0.25 0.36
  0.18 0.38 0.24 0.31
  0.23 0.29 0.22 0.33
\end{verbatim}

and here is my first pass at reading in the data and creating
appropriate factors (note the use of {\tt as.factor}.
\begin{verbatim}
> poison <- scan("poison.dat")
> poison
 [1] 0.31 0.82 0.43 0.45 0.45 1.10 0.45 0.71 0.46 0.88 0.63 0.66 0.43 0.72 0.76
[16] 0.62 0.36 0.92 0.44 0.56 0.29 0.61 0.35 1.02 0.40 0.49 0.31 0.71 0.23 1.24
[31] 0.40 0.38 0.22 0.30 0.23 0.30 0.21 0.37 0.25 0.36 0.18 0.38 0.24 0.31 0.23
[46] 0.29 0.22 0.33
> length(poison)
[1] 48
\end{verbatim}
Create a vector of the Tmt levels,
\begin{verbatim}
> rep(1:4, len=48)
 [1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2
[39] 3 4 1 2 3 4 1 2 3 4
> Tmt <- as.factor( rep(1:4, len=48 ) )
\end{verbatim}
and one of the Poison levels
\begin{verbatim}
> rep(1:3, rep(16,3))  
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3
[39] 3 3 3 3 3 3 3 3 3 3

> Poison <- as.factor(rep(1:3, rep(16,3)))
\end{verbatim}

and finally a data frame to hold all the data neatly together. (You
might not want to do this, but my data area is always a mess).
\begin{verbatim}

> poison.frame <- data.frame(poison, Tmt, Poison)
> poison.frame
   poison Tmt Poison 
 1   0.31   1      1
 2   0.82   2      1
 3   0.43   3      1
 4   0.45   4      1
> rm(poison, Tmt, Poison)                          
\end{verbatim}

So what sort of model {\em might\/} make sense here?  How about
$$
   Y_{ij} = \mu + \tau_i + \beta_j + \omega_{ij} + \epsilon_{ijk} $$

where $k$ implicitly indexes the replications? 

That model allows us to estimate main effects and interactions, and
still leaves room for random error?   First thing to do is look at
some pictures.

\begin{verbatim}
> plot.factor(poison.frame)
> attach(poison.frame, pos=1) 
> interaction.plot(Poison, Tmt, poison)
> interaction.plot(Tmt, Poison, poison)
\end{verbatim}
The plot of the RAW DATA is

\psfig{figure=poison.eps,angle=-90,width=6in}

It looks like there are treatment differences and some potential
interactions.  Why not let the ANOVA do its job and see where we go.

\begin{verbatim}
> anova(mymod <- aov(poison ~ Poison + Tmt))
Analysis of Variance Table

Response: poison

Terms added sequentially (first to last)
          Df Sum of Sq   Mean Sq  F Value        Pr(F) 
Poison     2  1.033012 0.5165062 20.64329 5.703730e-07
Tmt        3  0.921206 0.3070688 12.27267 6.696971e-06
Residuals 42  1.050862 0.0250205                      
\end{verbatim}

That analysis looks OK, except we forgot to add the interaction term.
Here is a version with interactions.
\begin{verbatim}
> anova(mymod <- aov(poison ~ Poison + Tmt + Poison*Tmt))
Analysis of Variance Table

Response: poison

Terms added sequentially (first to last)
           Df Sum of Sq   Mean Sq  F Value     Pr(F) 
Poison      2  1.033012 0.5165062 23.22174 0.0000003
Tmt         3  0.921206 0.3070688 13.80558 0.0000038
Poison:Tmt  6  0.250138 0.0416896  1.87433 0.1122506
Residuals  36  0.800725 0.0222424                   

\end{verbatim}

It appears there are strong main effects, but little or no
interaction.  

At the very least, we should now try a residual plot of these data.

\psfig{figure=poison2.eps,angle=-90,width=6in}

Those are not stellar looking plots.  The Box plots do not look
uniform, and there seems to be a relationship between residual and
fitted value.   What to do? 

One could go through a formal process of selecting a transformation,
perhaps from the Box-Cox family.  I've decided to just use an inverse,
and sqrt.

\begin{verbatim}
> anova(mymod <- aov( (poison)^(-1) ~ Poison + Tmt + Poison*Tmt))
Analysis of Variance Table

Response: (poison)^(-1)

Terms added sequentially (first to last)
           Df Sum of Sq  Mean Sq  F Value     Pr(F) 
Poison      2  34.87712 17.43856 72.63475 0.0000000
Tmt         3  20.41429  6.80476 28.34307 0.0000000
Poison:Tmt  6   1.57077  0.26180  1.09042 0.3867329
Residuals  36   8.64308  0.24009                   
> plot(1:10)
> plot.factor(data.frame(resid(mymod), Poison, Tmt))             
> plot(fitted(mymod),resid(mymod))                               
\end{verbatim}

\psfig{figure=poison3.eps,angle=-90,width=6in}

\begin{verbatim}
> anova(mymod <- aov( (poison)^(0.5) ~ Poison + Tmt + Poison*Tmt))
Analysis of Variance Table

Response: (poison)^(0.5)

Terms added sequentially (first to last)
           Df Sum of Sq   Mean Sq  F Value     Pr(F) 
Poison      2 0.5609264 0.2804632 34.38932 0.0000000
Tmt         3 0.4306422 0.1435474 17.60123 0.0000003
Poison:Tmt  6 0.0793987 0.0132331  1.62259 0.1692592
Residuals  36 0.2935992 0.0081555                   
> plot.factor(data.frame(resid(mymod), Poison, Tmt))
> plot(fitted(mymod),resid(mymod))                  
\end{verbatim}

\psfig{figure=poison4.eps,angle=-90,width=6in}

Note that the inverse transformation seems to be the ticket.  Note
particularly that any hint of interaction is completely missing on the
inverse scale.  


\section*{A Latin Square Design}
Suppose that 4 cars and four drivers are employed in a study of
possible differences between four gasoline additives.  Even though the
cars are identical models, slight systematic differences are likely to
occur in their performance, and even though each driver may do her
best to drive the car using the experimental protocol, slight
systematic differences can occur from driver to driver.  It would be
desirable to design (and analyze) an experiment that eliminates both
the driver-to-drive and automobile-to-automobile effects.  The Latin
Square arrangement below allows this to be done.

\begin{tabular}{c c| c| c| c| c|}
\multicolumn{2}{c}{}&\multicolumn{4}{c}{cars} \\
& & 1 & 2 & 3 & 4 \\\cline{3-6}
& I & A & B & D & C \\\cline{3-6}
drivers & II& D&C&A&B \\\cline{3-6}
&III&B&D&C&A\\\cline{3-6}
&IV&C&A&B&D \\\cline{3-6}
\end{tabular}

with cars (I,II,III, and IV), drivers (1, 2, 3, and 4) and additive
(A,B,C,and D).

Data will be assigned to each combination, so that data will be
indexed by three things (car, driver, and additive), each with 4
levels, but there will only be $16$ data points.  We do {\bf not} have
a complete replication of the experiment.

The model might be written
$$
Y_{ijk} = \mu + d_i + c_j + a_k + \epsilon_{ijk} $$

There is not much hope of estimating interactions.

Here is some data from this experiment.

\begin{tabular}{c c| c| c| c| c|}
\multicolumn{2}{c}{}&\multicolumn{4}{c}{cars} \\
& & 1 & 2 & 3 & 4 \\\cline{3-6}
& I & A 21 & B 26 & D 20  & C 25 \\\cline{3-6}
drivers & II& D 23&C 26&A 20&B 27 \\\cline{3-6}
&III&B 15&D 13&C 16&A 16\\\cline{3-6}
&IV&C 17 &A 15&B 20&D 20 \\\cline{3-6}
\end{tabular}

Lets do a quick analysis of these data, first entering them, and then
an ANOVA and plots.
Here is gas.dat
\begin{verbatim}
A 21 
B 26 
D 20 
C 25 
D 23 
C 26 
A 20 
B 27
B 15 
D 13 
C 16 
A 16
C 17 
A 15 
B 20 
D 20
\end{verbatim}

and here is the data being entered.
\begin{verbatim}
> delme <-read.table("gas.dat")
> delme
   V1 V2 
 1  A 21
 2  B 26
 3  D 20
 4  C 25
 5  D 23
 6  C 26
 7  A 20
 8  B 27
 9  B 15
10  D 13
11  C 16
12  A 16
13  C 17
14  A 15
15  B 20
16  D 20
> gas <- delme$V1
> resp <- delme$V2
> car <- rep(1:4, len=16)
> car
 [1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
> driver <- (1:4,rep(4,4))
Syntax error: "," used illegally at this point:
driver <- (1:4,
> driver <- rep(1:4,rep(4,4))
> driver
 [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
> gas.frame <- data.frame(resp, gas, car=as.factor(car), driver=as.factor(driver) )
\end{verbatim}

So here is my constructed data set
\begin{verbatim}
> gas.frame
   resp gas car driver 
 1   21   A   1      1
 2   26   B   2      1
 3   20   D   3      1
 4   25   C   4      1
 . . . . . .
\end{verbatim}
Now onto an analysis.

\begin{verbatim}
> anova(mymod <- aov(resp ~ gas + car + driver) )
Analysis of Variance Table

Response: resp

Terms added sequentially (first to last)
          Df Sum of Sq  Mean Sq F Value     Pr(F) 
gas        3        40 13.33333       5 0.0451975
car        3        24  8.00000       3 0.1169598
driver     3       216 72.00000      27 0.0006987
Residuals  6        16  2.66667                  
\end{verbatim}

\psfig{figure=gasplot.eps,angle=-90,width=6in}


Whoops, that was constructed data, look at those residuals!!!
% Local Variables: 
% mode: latex
% TeX-master: t
% TeX-master: t
% End: 
