\HeaderA{prcomp}{Principal Components Analysis}{prcomp}
\aliasA{plot.prcomp}{prcomp}{plot.prcomp}
\methaliasA{prcomp.default}{prcomp}{prcomp.default}
\methaliasA{prcomp.formula}{prcomp}{prcomp.formula}
\aliasA{predict.prcomp}{prcomp}{predict.prcomp}
\aliasA{print.prcomp}{prcomp}{print.prcomp}
\aliasA{print.summary.prcomp}{prcomp}{print.summary.prcomp}
\aliasA{summary.prcomp}{prcomp}{summary.prcomp}
\keyword{multivariate}{prcomp}
\begin{Description}\relax
Performs a principal components analysis on the given data matrix
and returns the results as an object of class \code{prcomp}.
\end{Description}
\begin{Usage}
\begin{verbatim}
prcomp(x, ...)

## S3 method for class 'formula':
prcomp(formula, data = NULL, subset, na.action, ...)

## Default S3 method:
prcomp(x, retx = TRUE, center = TRUE, scale. = FALSE, tol = NULL, ...)

## S3 method for class 'prcomp':
predict(object, newdata, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{formula}] a formula with no response variable, referring only to
numeric variables.
\item[\code{data}] an optional data frame (or similar: see
\code{\LinkA{model.frame}{model.frame}}) containing the variables in the
formula \code{formula}.  By default the variables are taken from
\code{environment(formula)}.
\item[\code{subset}] an optional vector used to select rows (observations) of the
data matrix \code{x}.
\item[\code{na.action}] a function which indicates what should happen
when the data contain \code{NA}s.  The default is set by
the \code{na.action} setting of \code{\LinkA{options}{options}}, and is
\code{\LinkA{na.fail}{na.fail}} if that is unset. The \dQuote{factory-fresh}
default is \code{\LinkA{na.omit}{na.omit}}.
\item[\code{...}] arguments passed to or from other methods. If \code{x} is
a formula one might specify \code{scale.} or \code{tol}.
\item[\code{x}] a numeric or complex matrix (or data frame) which provides
the data for the principal components analysis.
\item[\code{retx}] a logical value indicating whether the rotated variables
should be returned.
\item[\code{center}] a logical value indicating whether the variables
should be shifted to be zero centered. Alternately, a vector of
length equal the number of columns of \code{x} can be supplied.
The value is passed to \code{scale}.
\item[\code{scale.}] a logical value indicating whether the variables should
be scaled to have unit variance before the analysis takes
place. The default is \code{FALSE} for consistency with S, but
in general scaling is advisable.  Alternatively, a vector of length
equal the number of columns of \code{x} can be supplied.  The
value is passed to \code{\LinkA{scale}{scale}}.
\item[\code{tol}] a value indicating the magnitude below which components
should be omitted. (Components are omitted if their
standard deviations are less than or equal to \code{tol} times the
standard deviation of the first component.)
With the default null setting, no components
are omitted.  Other settings for tol could be \code{tol = 0} or
\code{tol = sqrt(.Machine\$double.eps)}, which would omit
essentially constant components.
\item[\code{object}] Object of class inheriting from \code{"prcomp"}
\item[\code{newdata}] An optional data frame or matrix in which to look for
variables with which to predict.  If omitted, the scores are used.
If the original fit used a formula or a data frame or a matrix with
column names, \code{newdata} must contain columns with the same
names. Otherwise it must contain the same number of columns, to be
used in the same order.

\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The calculation is done by a singular value decomposition of the
(centered and possibly scaled) data matrix, not by using
\code{eigen} on the covariance matrix.  This
is generally the preferred method for numerical accuracy.  The
\code{print} method for the these objects prints the results in a nice
format and the \code{plot} method produces a scree plot.

Note that \code{scale = TRUE} cannot be used if there are zero or
constant (for \code{center = TRUE}) variables.
\end{Details}
\begin{Value}
\code{prcomp} returns a list with class \code{"prcomp"}
containing the following components:
\begin{ldescription}
\item[\code{sdev}] the standard deviations of the principal components
(i.e., the square roots of the eigenvalues of the
covariance/correlation matrix, though the calculation
is actually done with the singular values of the data matrix).
\item[\code{rotation}] the matrix of variable loadings (i.e., a matrix
whose columns contain the eigenvectors).  The function
\code{princomp} returns this in the element \code{loadings}.
\item[\code{x}] if \code{retx} is true the value of the rotated data (the
centred (and scaled if requested) data multiplied by the
\code{rotation} matrix) is returned.  Hence, \code{cov(x)} is the
diagonal matrix \code{diag(sdev\textasciicircum{}2)}.  For the formula method,
\code{napredict} is applied to handle the treatment of values
omitted by the \code{na.action}.
\item[\code{center, scale}] the centering and scaling used, or \code{FALSE}.
\end{ldescription}
\end{Value}
\begin{Note}\relax
The signs of the columns of the rotation matrix are arbitrary, and
so may differ between different programs for PCA, and even between
different builds of \R.
\end{Note}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.

Mardia, K. V., J. T. Kent, and J. M. Bibby (1979)
\emph{Multivariate Analysis}, London: Academic Press.

Venables, W. N. and B. D. Ripley (2002)
\emph{Modern Applied Statistics with S}, Springer-Verlag.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{biplot.prcomp}{biplot.prcomp}},
\code{\LinkA{princomp}{princomp}}, \code{\LinkA{cor}{cor}}, \code{\LinkA{cov}{cov}},
\code{\LinkA{svd}{svd}}, \code{\LinkA{eigen}{eigen}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
## the variances of the variables in the
## USArrests data vary by orders of magnitude, so scaling is appropriate
prcomp(USArrests)  # inappropriate
prcomp(USArrests, scale = TRUE)
prcomp(~ Murder + Assault + Rape, data = USArrests, scale = TRUE)
plot(prcomp(USArrests))
summary(prcomp(USArrests, scale = TRUE))
biplot(prcomp(USArrests, scale = TRUE))
\end{ExampleCode}
\end{Examples}

