View source: R/S3_decompositions.R
| prcomp.HDF5Matrix | R Documentation |
Block-wise PCA entirely on disk, equivalent to prcomp().
Implements the same interface as stats::prcomp() but operates on
data stored in an HDF5 file without loading it into RAM.
## S3 method for class 'HDF5Matrix'
prcomp(
x,
retx = TRUE,
center = TRUE,
scale. = FALSE,
tol = NULL,
rank. = NULL,
ncomponents = 0L,
k = 2L,
q = 1L,
method = "auto",
rankthreshold = 0,
svdgroup = "SVD/",
overwrite = FALSE,
threads = -1L,
...
)
x |
An |
retx |
Logical. If |
center |
Logical. Subtract column means before PCA (default |
scale. |
Logical. Divide by column SDs before PCA (default |
tol |
Ignored (present for interface compatibility with |
rank. |
Integer. Number of principal components to compute. When
supplied (non- |
ncomponents |
Integer. Number of PCs to compute (0 = all, default). |
k |
Number of local SVDs per incremental level (default 2). |
q |
Number of incremental levels (default 1). |
method |
Computation method: |
rankthreshold |
Numeric in |
svdgroup |
HDF5 group for intermediate SVD storage (default |
overwrite |
Logical. Recompute even if PCA results exist (default |
threads |
Integer. OpenMP threads ( |
... |
Ignored (S3 compatibility). |
Constant columns and scale. = TRUE. As in
stats::prcomp(), a column with zero variance cannot be rescaled to
unit variance and the call stops with an error reporting how many columns
are affected and a few of their positions. Previously the division by zero
propagated silently and the whole decomposition degenerated to zeros. Either
leave scale. = FALSE (the default) or remove the constant columns
first.
An object of class c("HDF5PCA", "list") with elements:
sdevNumeric vector. Standard deviations of the PCs.
rotationHDF5Matrix. Variable loadings (rotation matrix).
xHDF5Matrix or NULL. Individual coordinates.
centerLogical. Whether columns were centered.
scaleLogical. Whether columns were scaled.
cumvarNumeric vector. Cumulative variance explained (percent).
lambdaNumeric vector. Eigenvalues.
var.cos2HDF5Matrix. Squared cosines for variables.
ind.cos2HDF5Matrix. Squared cosines for individuals.
ind.contribHDF5Matrix. Contributions of individuals to PCs.
fileCharacter. Path to the HDF5 file with all results.
The object also carries the attributes method, exact,
elements, auto_threshold, blocking and rank,
recording which decomposition path was actually taken; print()
shows them.
PCA runs on the same machinery as svd.HDF5Matrix and inherits
both of its silent approximations. Read that help page before interpreting a
large PCA. In particular, supplying ncomponents or rank.
below min(dim(x)) switches on per-block rank truncation, which is
the larger error source and can perturb even the first principal component
by a few percent; asking for a generously oversampled number of components
and discarding the extra ones is much more accurate than asking for exactly
the few you want. Check attr(pca, "exact") and
attr(pca, "truncated"); print() shows both.
tmp <- tempfile(fileext = ".h5")
X <- hdf5_create_matrix(tmp, "data/M", data = matrix(rnorm(1000), 100, 10))
pca <- prcomp(X, center = TRUE, scale. = FALSE)
cat("Variance explained (PC1-3):", pca$cumvar[1:3], "\n")
dim(pca$rotation) # 10 x nPC
dim(pca$x) # 100 x nPC
# rank. takes precedence over ncomponents when both are supplied.
# NOTE: this reuses the same output location as the call above, so it
# must come after pca's results have already been read/used -- once
# overwrite = TRUE recreates the output datasets, any earlier
# HDF5Matrix handle pointing at that location (here, pca$rotation and
# pca$x) becomes invalid by design (see HDF5Matrix lifecycle, Section
# 5.3.3): the package proactively clears stale external pointers when
# overwriting to convert what would otherwise be a memory-unsafe crash
# into a clean R-level error.
pca5 <- prcomp(X, rank. = 5, ncomponents = 10, overwrite = TRUE)
dim(pca5$rotation) # 10 x 5 -- rank. (5) was used, not ncomponents (10)
hdf5_close_all()
unlink(tmp)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.