The R Project SVN R

Rev

Rev 68948 | Rev 85904 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68948 Rev 70391
Line 1... Line 1...
1
% File src/library/stats/man/prcomp.Rd
1
% File src/library/stats/man/prcomp.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2012 R Core Team
3
% Copyright 1995-2016 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{prcomp}
6
\name{prcomp}
-
 
7
\title{Principal Components Analysis}
7
\alias{prcomp}
8
\alias{prcomp}
8
\alias{prcomp.formula}
9
\alias{prcomp.formula}
9
\alias{prcomp.default}
10
\alias{prcomp.default}
10
\alias{plot.prcomp}
11
\alias{plot.prcomp}
11
\alias{predict.prcomp}
12
\alias{predict.prcomp}
12
\alias{print.prcomp}
13
\alias{print.prcomp}
13
\alias{summary.prcomp}
14
\alias{summary.prcomp}
14
\alias{print.summary.prcomp}
15
\alias{print.summary.prcomp}
15
\concept{PCA}
16
\concept{PCA}
16
\title{Principal Components Analysis}
-
 
17
\description{
17
\description{
18
  Performs a principal components analysis on the given data matrix
18
  Performs a principal components analysis on the given data matrix
19
  and returns the results as an object of class \code{prcomp}.}
19
  and returns the results as an object of class \code{prcomp}.}
20
\usage{
20
\usage{
21
prcomp(x, \dots)
21
prcomp(x, \dots)
22
 
22
 
23
\method{prcomp}{formula}(formula, data = NULL, subset, na.action, \dots)
23
\method{prcomp}{formula}(formula, data = NULL, subset, na.action, \dots)
24
 
24
 
25
\method{prcomp}{default}(x, retx = TRUE, center = TRUE, scale. = FALSE,
25
\method{prcomp}{default}(x, retx = TRUE, center = TRUE, scale. = FALSE,
26
       tol = NULL, \dots)
26
       tol = NULL, rank. = NULL, \dots)
27
 
27
 
28
\method{predict}{prcomp}(object, newdata, \dots)
28
\method{predict}{prcomp}(object, newdata, \dots)
29
}
29
}
30
\arguments{
30
\arguments{
31
  \item{formula}{a formula with no response variable, referring only to
31
  \item{formula}{a formula with no response variable, referring only to
Line 39... Line 39...
39
  \item{na.action}{a function which indicates what should happen
39
  \item{na.action}{a function which indicates what should happen
40
    when the data contain \code{NA}s.  The default is set by
40
    when the data contain \code{NA}s.  The default is set by
41
    the \code{na.action} setting of \code{\link{options}}, and is
41
    the \code{na.action} setting of \code{\link{options}}, and is
42
    \code{\link{na.fail}} if that is unset. The \sQuote{factory-fresh}
42
    \code{\link{na.fail}} if that is unset. The \sQuote{factory-fresh}
43
    default is \code{\link{na.omit}}.}
43
    default is \code{\link{na.omit}}.}
44
  \item{\dots}{arguments passed to or from other methods. If \code{x} is
44
  \item{\dots}{arguments passed to or from other methods.  If \code{x} is
45
    a formula one might specify \code{scale.} or \code{tol}.}
45
    a formula one might specify \code{scale.} or \code{tol}.}
46
  \item{x}{a numeric or complex matrix (or data frame) which provides
46
  \item{x}{a numeric or complex matrix (or data frame) which provides
47
    the data for the principal components analysis.}
47
    the data for the principal components analysis.}
48
  \item{retx}{a logical value indicating whether the rotated variables
48
  \item{retx}{a logical value indicating whether the rotated variables
49
    should be returned.}
49
    should be returned.}
Line 51... Line 51...
51
    should be shifted to be zero centered. Alternately, a vector of
51
    should be shifted to be zero centered. Alternately, a vector of
52
    length equal the number of columns of \code{x} can be supplied.
52
    length equal the number of columns of \code{x} can be supplied.
53
    The value is passed to \code{scale}.}
53
    The value is passed to \code{scale}.}
54
  \item{scale.}{a logical value indicating whether the variables should
54
  \item{scale.}{a logical value indicating whether the variables should
55
    be scaled to have unit variance before the analysis takes
55
    be scaled to have unit variance before the analysis takes
56
    place. The default is \code{FALSE} for consistency with S, but
56
    place.  The default is \code{FALSE} for consistency with S, but
57
    in general scaling is advisable.  Alternatively, a vector of length
57
    in general scaling is advisable.  Alternatively, a vector of length
58
    equal the number of columns of \code{x} can be supplied.  The
58
    equal the number of columns of \code{x} can be supplied.  The
59
    value is passed to \code{\link{scale}}.}
59
    value is passed to \code{\link{scale}}.}
60
  \item{tol}{a value indicating the magnitude below which components
60
  \item{tol}{a value indicating the magnitude below which components
61
    should be omitted. (Components are omitted if their
61
    should be omitted. (Components are omitted if their
62
    standard deviations are less than or equal to \code{tol} times the
62
    standard deviations are less than or equal to \code{tol} times the
63
    standard deviation of the first component.)
63
    standard deviation of the first component.)  With the default null
64
    With the default null setting, no components
64
    setting, no components are omitted (unless \code{rank.} is specified
65
    are omitted.  Other settings for tol could be \code{tol = 0} or
65
    less than \code{min(dim(x))}.).  Other settings for tol could be
66
    \code{tol = sqrt(.Machine$double.eps)}, which would omit
66
    \code{tol = 0} or \code{tol = sqrt(.Machine$double.eps)}, which
67
    essentially constant components.}
67
    would omit essentially constant components.}
-
 
68
  \item{rank.}{optionally, a number specifying the maximal rank, i.e.,
-
 
69
    maximal number of principal components to be used.  Can be set as
-
 
70
    alternative or in addition to \code{tol}, useful notably when the
-
 
71
    desired rank is considerably smaller than the dimensions of the matrix.}
-
 
72
 
68
  \item{object}{Object of class inheriting from \code{"prcomp"}}
73
  \item{object}{object of class inheriting from \code{"prcomp"}}
69
  \item{newdata}{An optional data frame or matrix in which to look for
74
  \item{newdata}{An optional data frame or matrix in which to look for
70
    variables with which to predict.  If omitted, the scores are used.
75
    variables with which to predict.  If omitted, the scores are used.
71
    If the original fit used a formula or a data frame or a matrix with
76
    If the original fit used a formula or a data frame or a matrix with
72
    column names, \code{newdata} must contain columns with the same
77
    column names, \code{newdata} must contain columns with the same
73
    names. Otherwise it must contain the same number of columns, to be
78
    names. Otherwise it must contain the same number of columns, to be
Line 125... Line 130...
125
\seealso{
130
\seealso{
126
  \code{\link{biplot.prcomp}}, \code{\link{screeplot}},
131
  \code{\link{biplot.prcomp}}, \code{\link{screeplot}},
127
  \code{\link{princomp}}, \code{\link{cor}}, \code{\link{cov}},
132
  \code{\link{princomp}}, \code{\link{cor}}, \code{\link{cov}},
128
  \code{\link{svd}}, \code{\link{eigen}}.
133
  \code{\link{svd}}, \code{\link{eigen}}.
129
}
134
}
-
 
135
\examples{
-
 
136
C <- chol(S <- toeplitz(.9 ^ (0:31))) # Cov.matrix and its root
-
 
137
all.equal(S, crossprod(C))
-
 
138
set.seed(17)
-
 
139
X <- matrix(rnorm(32000), 1000, 32)
-
 
140
Z <- X \%*\% C  ## ==>  cov(Z) ~=  C'C = S
-
 
141
all.equal(cov(Z), S, tol = 0.08)
-
 
142
pZ <- prcomp(Z, tol = 0.1)
-
 
143
summary(pZ) # only ~14 PCs (out of 32)
130
\examples{\donttest{## signs are random
144
## or choose only 3 PCs more directly:
131
require(graphics)
145
pz3 <- prcomp(Z, rank. = 3)
-
 
146
summary(pz3) # same numbers as the first 3 above
-
 
147
stopifnot(ncol(pZ$rotation) == 14, ncol(pz3$rotation) == 3,
-
 
148
          all.equal(pz3$sdev, pZ$sdev, tol = 1e-15)) # exactly equal typically
132
 
149
 
-
 
150
\donttest{## signs are random
-
 
151
require(graphics)
133
## the variances of the variables in the
152
## the variances of the variables in the
134
## USArrests data vary by orders of magnitude, so scaling is appropriate
153
## USArrests data vary by orders of magnitude, so scaling is appropriate
135
prcomp(USArrests)  # inappropriate
154
prcomp(USArrests)  # inappropriate
136
prcomp(USArrests, scale = TRUE)
155
prcomp(USArrests, scale = TRUE)
137
prcomp(~ Murder + Assault + Rape, data = USArrests, scale = TRUE)
156
prcomp(~ Murder + Assault + Rape, data = USArrests, scale = TRUE)
138
plot(prcomp(USArrests))
157
plot(prcomp(USArrests))
139
summary(prcomp(USArrests, scale = TRUE))
158
summary(prcomp(USArrests, scale = TRUE))
140
biplot(prcomp(USArrests, scale = TRUE))
159
biplot(prcomp(USArrests, scale = TRUE))
-
 
160
}
141
}}
161
}
142
\keyword{multivariate}
162
\keyword{multivariate}