princomp | R Documentation |
Compute principal components for SpatRaster layers. This method can use all values to compute the principal components, even for very large rasters. This is because it computes the covariance matrix by processing the data in chunks, if necessary, using layerCor
. The population covariance is used (not the sample, with n-1
denominator, covariance).
Alternatively, you can specify maxcell
or sample raster values to a data.frame to speed up calculations for very large rasters (see the examples below).
See prcomp
for an alternative method that has higher numerical accuracy, but is slower, and for very large rasters can only be accomplished with a sample since all values must be read into memory.
## S4 method for signature 'SpatRaster'
princomp(x, cor=FALSE, fix_sign=TRUE, use="pairwise.complete.obs", maxcell=Inf)
x |
SpatRaster |
cor |
logical. If |
fix_sign |
logical. If |
use |
character. To decide how to handle missing values. This must be (an abbreviation of) one of the strings "everything", "complete.obs", "pairwise.complete.obs", or "masked.complete". With "pairwise.complete.obs", the covariance between a pair of layers is computed for all cells that are not |
maxcell |
positive integer. The maximum number of cells to be used. If this is smaller than ncell(x), a regular sample of |
princomp object
Alex Ilich and Robert Hijmans, based on a similar method by Benjamin Leutner
prcomp
princomp
f <- system.file("ex/logo.tif", package = "terra")
r <- rast(f)
pca <- princomp(r)
x <- predict(r, pca)
# use "index" to get a subset of the components
p <- predict(r, pca, index=1:2)
### use princomp directly
pca2 <- princomp(values(r), fix_sign = TRUE)
p2 <- predict(r, pca2)
### may need to use sampling with a large raster
### here with prcomp instead of princomp
sr <- spatSample(r, 100000, "regular")
pca3 <- prcomp(sr)
p3 <- predict(r, pca3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.