
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% algorithm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\section{Basic Algorithms}

I will briefly introduce the core 
algorithms implemented
in {\tt R/maanova}. For the details please read related
references. 

%for ANOVA model
\subsection{ANOVA model for microarray experiment}

\subsubsection{Model}
The ANOVA model for microarray experiment was first proposed 
in Kerr {\it et al} (2000). Wolfinger {\it et al} (2001)
proposed the 2-stage ANOVA model, which is the one used
in {\tt R/maanova}. 

Basically an ANOVA model for 
microarray experiment can be specified in two stages.
The first stage is the normalization model
\begin{equation}
y_{ijkgr} = \mu + A_i + D_j + AD_{ij} + r_{ijkgr}
\end{equation}
The term $\mu$ captures the overall mean. The rest of the terms
capture the overall effects 
due to arrays, dyes, and labeling reactions. The residual 
of the first stage will be used as the inputs for the second stage.

The second stage models gene-specific effects:
\begin{equation}
r_{ijkr} = G + AG_i + DG_j + VG_k + \epsilon_{ijkr}
\end{equation}
Here $G$ captures the average effect of the gene. $AG$ captures
the array by gene variation. $DG$ captures the dye by gene variation. 
For one dye system there will be no $DG$ effect in the model.
$VG$ captures the effects for the experimental varieties. In the
multiple factor experiment such like Paigen's 28-array experiment, 
$VG$ should be further decoupled into several factors. 

\subsubsection{Fixed model versus Mixed model}
The fixed effect model assumes independence among all 
observations and only one source of random variation.  
Although it is applicable to many microarray experiments, 
the fixed effects model does not allow for multiple sources
of variation nor does it account for correlation among 
the observations that arise as a consequence of different 
layers of variation. 

The mixed model treats some of the factors in an experimental 
design as random samples from a population.   
In other words, we assume that if the experiment were 
to be repeated, the same effects would not be exactly 
reproduced, but that similar effects would be drawn 
from a hypothetical population of effects.  Therefore, 
we model these factors as sources of variance. 
Usually Array effect ($AG$) should be treated as random
factor in ANOVA model. If you have biological replicates,
it should be treated as random as well. 


%for F-test
\subsection{Hypothesis tests}
For statistical test in the fixed ANOVA model, 
there is only one variance term 
and all factors in the model are tested against this variance.
In mixed model ANOVA, there are multiple levels 
of variances (biological, array, residual, etc.). 
The test statistics need to be constructed based on 
the proper variations. I will skip the details here and 
the interested reader can read Searle {\it et al}. 

\subsubsection{F test in matrix notation}
Mathematically speaking, 
a Microarray ANOVA model for a gene
can be expressed as the following mixed effect linear model 
(without losing generality):
\begin{equation}
\label{eq:mme}
y = X\beta + Zu + e
\end{equation}
Here, $\beta$ and $u$ are vectors for fixed-effects and random-effects 
parameters. $X$ and $Z$ are design matrices. Note that fixed effects
model is a special case for mixed effects model with $Z$ and $u$ empty.
An assumption is that $u$ and $e$ are normally distributed with 
\begin{equation}
% e[u,e] = [0,0]
E\left[\begin{array}{c}u\\e\end{array}\right] 
 = \left[\begin{array}{c}0\\0\end{array}\right]
\end{equation}
\begin{equation}
%Var[u e] = [G 0;0 R]
Var\left[\begin{array}{c}u\\e\end{array}\right] 
 = \left[\begin{array}{cc}G & 0\\0 & R\end{array}\right]
\end{equation}
$G$ and $R$ are unknown variance components and are estimated 
using restricted maximum liklihood (REML) method. The estimated 
$\hat{\beta}$ and $\hat{u}$ are called 
{\bf best linear unbiased estimator} (BLUE) and 
{\bf best linear unbiased predictor} (BLUP) respectively.
Using estimated $G$ and $R$, the variance-covariance 
matrix of $\hat{\beta}$ and $\hat{u}$ can be expressed as
\begin{equation}
\hat{C}=\left[\begin{array}{cc}X'\hat{R}^{-1}X & X'\hat{R}^{-1}Z \\
Z'\hat{R}^{-1}X & Z'\hat{R}^{-1}Z+\hat{G}^{-1}\end{array}\right]^-
\end{equation}
where $^{-}$ denotes generalized inverse. After getting $\hat{C}$,
test statistics can be obtained through the following formula. 
For a given hypothesis
\begin{equation}
H: L\left[\begin{array}{c}\beta \\ u\end{array}\right]=0
\end{equation}
F-statistic is:
\begin{equation}
F=\frac
  {\left[\begin{array}{c}\hat{\beta}\\u\end{array}\right]'L'
   (L\hat{C}L')^{-}L\left[\begin{array}{c}\hat{\beta}\\u\end{array}\right]}
  {q}
\label{eq:f}
\end{equation}
Here $q$ is the rank of $L$ matrix. 

Notice that for a fixed effect model where $Z$ and $u$ 
are empty, equation \ref{eq:f} will become 
\begin{equation}
F=\frac
  {(L\hat{\beta})'(L(X'X)^{-}L')^{-1}L\hat{\beta}}
  {q\sigma_e^{2}}
\end{equation}
Here $\sigma_e^2$ is the error variance.

F approximately follows F-distribution
with $q$ numerator degrees 
of freedom. The calculation of denominator degrees of
freedom can be tricky and I will skip it here. If there is 
not enough degree of freedom, rely on tabulated P-values
can be risky and a permutation test is recommended.  

%contrast matrix
\subsubsection{Building $L$ matrix}
In equation \ref{eq:f}, $L$ must be a matrix that $L\beta$
is estimable. Estimability requires that the rows of $L$
must be the linear combinations of the rows of $X$. 
In {\tt R/maanova}, the program will automatically 
build $L$ matrix for the term(s) to be tested.
Normally in a mixed effects model, only the fixed
effect terms can be tested so the $u$ portion of
$L$ contains all 0s.

Building L matrix can be difficult. If the term to be
tested is orthogonal to all other terms, $L$ should contain
all pairwise comparision for this term. That is, if the term
has $N$ levels, L should be a $(N-1)$ by $N$ matrix.
If the tested term is confounded with any other term, 
things will become complicated. {\tt R/maanova} 
does the following to compute L matrix:
\begin{enumerate}
\item calculate a generalized inverse of $X'X$ in such
a way that the dependent columns in X for the tested term
are set to zero. 
\item calculate $(X'X)^-(X'X)$. The result matrix span 
the same linear space as $X$ and it contains a lot of zeros.
\item Take the part of $(X'X)^-(X'X)$ corresponding to 
the tested term, remove the rows with all zeros and
the left part is $L$.
\end{enumerate}

% four F-tests
\subsubsection{Four flavors of F-tests}
{\tt R/maanova} offers four F statistics 
(called $F_1$, $F_2$, $F_3$ and $F_s$). Users have the option to 
turn on or off any of them. The difference among them
is the calculation of $\hat{C}$ matrix in equation \ref{eq:f}.
Or in another word, the way to estimate the variance components
$\hat{R}$ and $\hat{G}$.

Briefly speaking, 
$F_1$ computes $\hat{C}$ matrix based on the variance components
of a single gene. It does not assume the common variance 
among the genes. $F_3$ assumes the common variance among the genes. 
The $\hat{C}$ matrix will be calculated based on the 
global variance (mean variance of all genes). 
$F_2$ test is a hybrid of $F_1$ and $F_3$. It uses 
a weighted combination of global and gene-specific variance 
to compute the $\hat{C}$ matrix. $F_s$ uses the James-Stein
estimator for $\hat{R}$ and $\hat{G}$ and computes $\hat{C}$ 
matrix. For details about four F-tests, please read Cui (2003b)
and Cui (2003c).

%data shuffling
\subsection{Data shuffling in permutation test}
Choosing proper data shuffling method is crucial
to permutation test. {\tt R/maanova} offers two types
of data shuffling, residual shuffling and sample 
shuffling. 

Residual shuffling works only for fixed effects models. 
It is based on the assumption of homogeneous error variance. 
It does the following:
\begin{enumerate}
\item Fit a null hypothesis ANOVA model and get the residuals
\item Shuffle residuals globally and make a new data set
\item Compute test statistics on the new data set
\item Repeat step 2 and 3 for a certain times
\end{enumerate}

Residual shuffling is incorrect for mixed effect models, 
where you have multiple random terms. For a mixed effects
model, {\tt R/maanova} will automatically choose sample
shuffling, which does the following:
\begin{enumerate}
\item For the given tested term, check if it is nested 
within any random term. If not, go to step 4.
\item Choose the lowest random term (among the ones 
nesting with the tested term) as the base for shuffling.
If there is only one random term nesting with the tested
term, this random term will be the shuffle base.
\item Shuffle the sample names for the tested term in such a way
that the same shuffle base will corresponds to the same 
sample name. 
\item If there is no nesting, shuffle the sample names for 
the tested term freely, keeping all other terms unchanged.
For multiple dye arrays, if Array and/or Dye effects are
included in the model, the array structure should be perserved,
e.g., the sample names on the same array should be shuffled
together. 
\item repeat step 3 or 4 for a certain times
\end{enumerate}

Note that if the experiment size is small, the number of 
possible permutations will not be sufficient. In that case,
users will have to rely on the tabulated values.
