Rev 38105 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{princomp}\alias{princomp}\alias{princomp.formula}\alias{princomp.default}\alias{plot.princomp}\alias{print.princomp}\alias{predict.princomp}\title{Principal Components Analysis}\concept{PCA}\usage{princomp(x, \dots)\method{princomp}{formula}(formula, data = NULL, subset, na.action, \dots)\method{princomp}{default}(x, cor = FALSE, scores = TRUE, covmat = NULL,subset = rep(TRUE, nrow(as.matrix(x))), \dots)\method{predict}{princomp}(object, newdata, \dots)}\arguments{\item{formula}{a formula with no response variable, referring only tonumeric variables.}\item{data}{an optional data frame (or similar: see\code{\link{model.frame}}) containing the variables in theformula \code{formula}. By default the variables are taken from\code{environment(formula)}.}\item{subset}{an optional vector used to select rows (observations) of thedata matrix \code{x}.}\item{na.action}{a function which indicates what should happenwhen the data contain \code{NA}s. The default is set bythe \code{na.action} setting of \code{\link{options}}, and is\code{\link{na.fail}} if that is unset. The \dQuote{factory-fresh}default is \code{\link{na.omit}}.}\item{x}{a numeric matrix or data frame which provides the data for theprincipal components analysis.}\item{cor}{a logical value indicating whether the calculation shoulduse the correlation matrix or the covariance matrix. (Thecorrelation matrix can only be used if there are no constant variables.)}\item{scores}{a logical value indicating whether the score on eachprincipal component should be calculated.}\item{covmat}{a covariance matrix, or a covariance list as returned by\code{\link{cov.wt}} (and \code{\link[MASS]{cov.mve}} or\code{\link[MASS]{cov.mcd}} from package \pkg{MASS}).If supplied, this is used rather than the covariance matrix of\code{x}.}\item{\dots}{arguments passed to or from other methods. If \code{x} isa formula one might specify \code{cor} or \code{scores}.}\item{object}{Object of class inheriting from \code{"princomp"}}\item{newdata}{An optional data frame or matrix in which to look forvariables with which to predict. If omitted, the scores are used.If the original fit used a formula or a data frame or a matrix withcolumn names, \code{newdata} must contain columns with the samenames. Otherwise it must contain the same number of columns, to beused in the same order.}}\description{\code{princomp} performs a principal components analysis on the givennumeric data matrix and returns the results as an object of class\code{princomp}.}\value{\code{princomp} returns a list with class \code{"princomp"}containing the following components:\item{sdev}{the standard deviations of the principal components.}\item{loadings}{the matrix of variable loadings (i.e., a matrixwhose columns contain the eigenvectors). This is of class\code{"loadings"}: see \code{\link{loadings}} for its \code{print}method.}\item{center}{the means that were subtracted.}\item{scale}{the scalings applied to each variable.}\item{n.obs}{the number of observations.}\item{scores}{if \code{scores = TRUE}, the scores of the supplieddata on the principal components. These are non-null only if\code{x} was supplied, and if \code{covmat} was also supplied if itwas a covariance list. For the formula method, \code{napredict} isapplied to handle the treatment of values omitted by the \code{na.action}.}\item{call}{the matched call.}\item{na.action}{If relevant.}}\details{\code{princomp} is a generic function with \code{"formula"} and\code{"default"} methods.The calculation is done using \code{\link{eigen}} on the correlation orcovariance matrix, as determined by \code{\link{cor}}. This is done forcompatibility with the S-PLUS result. A preferred method ofcalculation is to use \code{\link{svd}} on \code{x}, as is done in\code{prcomp}.Note that the default calculation uses divisor \code{N} for thecovariance matrix.The \code{\link{print}} method for the these objects prints theresults in a nice format and the \code{\link{plot}} method producesa scree plot (\code{\link{screeplot}}). There is also a\code{\link{biplot}} method.If \code{x} is a formula then the standard NA-handling is applied tothe scores (if requested): see \code{\link{napredict}}.\code{princomp} only handles so-called R-mode PCA, that is featureextraction of variables. If a data matrix is supplied (possibly via aformula) it is required that there are at least as many units asvariables. For Q-mode PCA use \code{\link{prcomp}}.}\note{The signs of the columns of the loadings and scores are arbitrary, andso may differ between different programs for PCA, and even betweendifferent builds of \R.}\references{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.}\seealso{\code{\link{summary.princomp}}, \code{\link{screeplot}},\code{\link{biplot.princomp}},\code{\link{prcomp}}, \code{\link{cor}}, \code{\link{cov}},\code{\link{eigen}}.}\examples{## The variances of the variables in the## USArrests data vary by orders of magnitude, so scaling is appropriate(pc.cr <- princomp(USArrests)) # inappropriateprincomp(USArrests, cor = TRUE) # =^= prcomp(USArrests, scale=TRUE)## Similar, but different:## The standard deviations differ by a factor of sqrt(49/50)summary(pc.cr <- princomp(USArrests, cor = TRUE))loadings(pc.cr) ## note that blank entries are small but not zeroplot(pc.cr) # shows a screeplot.biplot(pc.cr)## Formula interfaceprincomp(~ ., data = USArrests, cor = TRUE)# NA-handlingUSArrests[1, 2] <- NApc.cr <- princomp(~ Murder + Assault + UrbanPop,data = USArrests, na.action=na.exclude, cor = TRUE)pc.cr$scores}\keyword{multivariate}