\HeaderA{lmPerGene}{Fit linear model for each gene}{lmPerGene}
\keyword{methods}{lmPerGene}
\begin{Description}\relax
For each gene, \code{lmPerGene} fits the same, user-specified linear model.
It returns the estimates of the model parameters and their variances
for each fitted model. The function uses matrix algebra so it is much
faster than repeated calls to \code{lm}.
\end{Description}
\begin{Usage}
\begin{verbatim}
lmPerGene(eSet, formula, na.rm=TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{eSet}] An \code{ExpressionSet} object.
\item[\code{formula}] an object of class \code{\LinkA{formula}{formula}} (or one that can be coerced to
that class), specifying only the right-hand side starting with
the '~' symbol. The LHS is automatically set as the expression
levels provided in  \code{eSet}. The names of all predictors must
exist in the phenotypic data of \code{eSet}.
\item[\code{na.rm}] 
\end{ldescription}
 { Whether to remove missing observations. }
\end{Arguments}
\begin{Details}\relax
This function efficiently computes the least squares fit of a linear
regression to a set of gene expression values. We assume that there
are \code{G} genes, on \code{n} samples, and that there are \code{p} variables in
the regression equation.  So the result is that \code{G} different regressions
are computed, and various summary statistics are returned.

Since the independent variables are the same in each model fitting,
instead of repeatedly fitting linear model for each gene,
\code{lmPerGene} accelarates the fitting process by calculating the
hat matrix \code{X(X'X)\textasciicircum{}(-1)X'} first.  Then matrix multiplication, and
\code{solve} are to compute estimates of the model parameters.

Leaving the formula blank (the default) will calculate an 
intercept-only model, useful for generic pattern and outlier identification.
\end{Details}
\begin{Value}
A list with components:
\begin{ldescription}
\item[\code{Hmat}] The Hat matrix.
\item[\code{coefficients}] A matrix of dimension \code{p} by \code{G}
containing the estimated model parameters.
\item[\code{sigmaSqr}] A vector of length $G$ containing the mean square error
for that model, the sum of the residuals squared divided by \code{n - p}.
\item[\code{coef.var}] A matrix of dimension \code{p} by \code{G} containing
the estimated variances for the model parameters, for each regression.
\end{ldescription}
\end{Value}
\begin{Author}\relax
Robert Gentleman
\end{Author}
\begin{SeeAlso}\relax
\code{\LinkA{getResidPerGene}{getResidPerGene}} to extract row-by-row residuals; \code{\LinkA{gsealmPerm}{gsealmPerm}} for
code that utilizes  'lmPerGene' for gene-set-enrichment analysis (GSEA); and  \code{\LinkA{CooksDPerGene}{CooksDPerGene}} for diagnostic functions on
an object produced by 'lmPerGene'.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
data(sample.ExpressionSet)
layout(1)
lm1 = lmPerGene(sample.ExpressionSet,~sex)
qqnorm(lm1$coefficients[2,]/sqrt(lm1$coef.var[2,]),main="Sample Dataset: Sex Effect by Gene",ylab="Individual Gene t-statistic",xlab="Normal Quantile")
abline(0,1,col=2)
lm2 = lmPerGene(sample.ExpressionSet,~type+sex)
qqnorm(lm2$coefficients[2,]/sqrt(lm2$coef.var[2,]),main="Sample Dataset: Case vs. Control Effect by Gene, Adjusted for Sex",ylab="Individual Gene t-statistic",xlab="Normal Quantile")
abline(0,1,col=2)
\end{ExampleCode}
\end{Examples}

