| rpca | R Documentation |
Compute principal components of an optionally centered and scaled matrix using a randomized or deterministic singular value decomposition (SVD). Loadings describe the components, scores locate observations in those coordinates, and variances measure the contribution of each component.
rpca(
A,
k = NULL,
center = TRUE,
scale = TRUE,
retx = TRUE,
p = 10,
q = 2,
rand = TRUE
)
## S3 method for class 'rpca'
print(x, ...)
## S3 method for class 'rpca'
summary(object, ...)
## S3 method for class 'rpca'
predict(object, newdata, ...)
A |
A numeric matrix or data frame with observations in rows and variables
in columns. Dense real or complex data and real sparse matrices from
Matrix are supported. Rows containing missing values are omitted
with a warning; all remaining values must be finite. At least two rows
must remain. In the dimensions below, |
k |
A positive integer giving the number of principal components to return.
A fractional value of at least one is truncated toward zero.
Values greater than |
center |
Logical; subtract the training column means before computing the SVD
(default |
scale |
Logical; divide each processed column by its scale factor (default
|
retx |
Logical; return the fitted component scores in |
p |
A nonnegative integer oversampling parameter for |
q |
A nonnegative integer number of power iterations for
|
rand |
Logical; use the randomized SVD when |
... |
Additional arguments; currently ignored by the |
object, x |
An object returned by |
newdata |
A numeric matrix, data frame, or real sparse Matrix containing new observations. Supply the same variables in the same column order as the training data, preferably with no missing values; see Details. |
Principal components describe data along orthogonal directions in
the space of the variables. Each loading vector weights the original
variables to specify one direction. Retaining k components gives
a low-rank approximation to the processed data. For centered data, the
leading components describe directions of greatest sample variance.
With rand = TRUE and a supplied k, rpca uses
rsvd to approximate the dominant singular subspace. Finding
a small subspace first reduces the size of the subsequent SVD, with the
greatest savings for large matrices and small requested ranks. Increasing
p enlarges the sampled subspace, and increasing q spends
more work refining it to improve accuracy. Use set.seed before
fitting for reproducible random draws.
Centering makes the components describe deviations from the training
column means. Scaling adjusts the relative influence of variables with
different units or magnitudes. Both operations are enabled by default.
As in prcomp, center = FALSE leaves the column
means unchanged and accepts either already-centered or uncentered data.
Scaling remains enabled unless scale = FALSE; prcomp
disables scaling by default.
With scale = TRUE, each column is divided by the square root of
its sum of squared magnitudes divided by m-1, after any requested
centering. For centered data this is the sample standard deviation.
Scale factors below 1e-8 are replaced by one for compatibility,
so constant and nearly constant columns are retained. Complex columns
use squared magnitudes and therefore have real scale factors.
The total variance var includes every direction in the processed
data, including those omitted from the approximation. It is computed as
the squared Frobenius norm of the processed matrix divided by
m-1. For uncentered data this quantity measures squared energy
divided by m-1 and includes the contribution of the column means.
The proportion attributed to a retained component is its eigvals
entry divided by var, so the denominator stays the same as the
retained rank changes. At least two complete observations are required;
variance proportions are undefined when the processed matrix has zero
total variance.
Fitted scores are constructed from the SVD as U_k D_k.
Multiplying them by the conjugate transpose of the loadings reconstructs
the approximation in processed variable coordinates. Undo any scaling
and centering to express it in the original input coordinates.
predict(object, newdata) applies the stored training centering and
scaling, then multiplies the processed observations by
object$rotation. Reusing the training means and scale factors
places new observations in the same coordinate system. Supply complete
rows in the training column order; the method does not reorder columns
by name or omit incomplete new rows automatically.
Missing values generally propagate to the scores. For compatibility,
NaN entries produced or retained during scaling are replaced by zero.
Prediction also works when the fit was created with retx = FALSE.
For a randomized fit, projecting the training data with predict
can differ from the fitted scores in object$x because the randomized subspace omits
part of the data, represented by the range-finder residual.
The print and summary methods describe a fit.
plot.rpca draws the available scree and factor plots;
the individual plotting helpers can also be called directly.
Subtracting nonzero column means would fill in many entries of a sparse
matrix. With rand = TRUE and a supplied k, rpca
instead incorporates centering and scaling into its matrix products for
real sparse inputs. This avoids forming the entire centered matrix.
Storage includes the sparse entries and dense working factors of width
up to k + p, capped by the matrix dimensions. Loadings, fitted
scores, and predicted scores are dense. The computation estimates
ordinary PCA loadings and imposes no sparsity constraint on them.
With rand = FALSE or k = NULL, a sparse input is converted
to a dense matrix for the deterministic SVD.
rpca returns a list of class "rpca" with these components:
A dense n by k matrix of orthonormal
loadings (exact or approximate right singular vectors of the processed
data).
A numeric vector of length k containing the
retained squared singular values divided by m-1. These estimate
the leading eigenvalues of the processed cross-product matrix divided
by m-1, which is the sample covariance matrix for centered data.
A numeric vector of length k containing the square
roots of eigvals.
The squared Frobenius norm of the processed matrix divided
by m-1, including directions not returned. For centered data
this is the total sample variance.
A dense m by k matrix of fitted scores, or
NULL when retx = FALSE. Rows correspond to complete
observations retained from the input.
Numeric vectors containing the training column
means and scale factors, respectively, or FALSE when the
corresponding operation was disabled. Centers can be complex;
scale factors are real.
The predict method returns a dense matrix with one row per new
observation and one column per retained component. The summary
method returns a numeric matrix with rows for explained variance, standard
deviation, proportion of variance, and cumulative proportion. Its entries
are rounded to three decimal places. The print method displays
standard deviations, eigenvalues, and loadings, and invisibly returns the
displayed loading matrix.
Singular vectors are defined only up to sign (or a constant of modulus one for complex data). With repeated singular values, any orthonormal basis of the corresponding subspace is valid. Individual loading vectors can therefore differ between runs or implementations without changing the represented subspace.
N. Benjamin Erichson
N. B. Erichson, S. Voronin, S. L. Brunton and J. N. Kutz. 2019. Randomized Matrix Decompositions Using R. Journal of Statistical Software, 89(11), 1–48. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v089.i11")}.
N. Halko, P. G. Martinsson and J. A. Tropp. 2011. Finding Structure with Randomness: Probabilistic Algorithms for Constructing Approximate Matrix Decompositions. SIAM Review, 53(2), 217–288. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1137/090771806")}.
rsvd, prcomp,
plot.rpca, ggscreeplot,
ggindplot, ggcorplot,
rrpca for a low-rank plus sparse decomposition.
# Fit two components and project observations held out from the fit.
training <- log(as.matrix(iris[1:145, 1:4]))
new_data <- log(as.matrix(iris[146:150, 1:4]))
set.seed(42)
fit <- rpca(training, k = 2)
summary(fit)
predict(fit, new_data)
# Reconstruct in input coordinates (here, log measurements).
reconstructed <- fit$x %*% Conj(t(fit$rotation))
reconstructed <- sweep(reconstructed, 2, fit$scale, "*")
reconstructed <- sweep(reconstructed, 2, fit$center, "+")
# k = NULL uses the deterministic SVD and returns all components.
full <- rpca(training)
length(full$sdev)
# Sparse input: implicit preprocessing, with training scores omitted.
A <- Matrix::bdiag(outer(1:12, 1:6), outer(1:12, 6:1))
set.seed(42)
sparse_fit <- rpca(A, k = 2, retx = FALSE)
predict(sparse_fit, A[1:2, , drop = FALSE])
if (requireNamespace("ggplot2", quietly = TRUE)) {
plot(fit, type = "ratio", groups = iris$Species[1:145],
ind_labels = FALSE, ellipse = FALSE)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.