Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 2, 2023
1 parent 51b2b76 commit a258183
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion R/k_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ setMethod("k_means", signature(x="SpatRaster"),
function(x, centers=3, ..., maxcell=1000000, filename="", overwrite=FALSE, wopt=list()) {
stopifnot(maxcell > 0)
if (ncell(x) <= maxcell) {
v <- na.omit(as.matrix(x))
v <- unique(na.omit(as.matrix(x)))
omit <- as.vector(attr(v, "na.action"))
km <- stats::kmeans(v, centers=centers, ...)
out <- rast(x, nlyr=1)
Expand Down
6 changes: 3 additions & 3 deletions R/princomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ setMethod("princomp", signature(x="SpatRaster"),
function(x, cor=FALSE, fix_sign=TRUE) {
stopifnot(nlyr(x) > 1)
xcov <- layerCor(x, fun="cov", na.rm=TRUE)
model <- princomp(covmat = xcov$covariance, cor=cor, fix_sign=fix_sign)
model <- princomp(covmat=xcov$covariance, cor=cor, fix_sign=fix_sign)
model$center <- diag(xcov$mean)
n <- diag(xcov$n)
if (cor) {
## Calculate scale as population sd like in in princomp
## Scale as population sd, like in in princomp
S <- diag(xcov$covariance)
model$scale <- sqrt(S * (n-1) / n)
}
model$n <- n
model$n.obs <- as.dist(xcov$n)
model
}
)
Expand Down
14 changes: 7 additions & 7 deletions man/princomp.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
\title{SpatRaster PCA}

\description{
Compute principal components for SpatRaster layers.
The benefit of this method is that it uses all value to compute the principal components.
Alternatively, you can use \code{stats::\link[stats]{princomp}} or \code{stats::\link[stats]{prcomp}} directly, perhaps using a sample.
Compute principal components for SpatRaster layers. The benefit of this method is that it uses all values to compute the principal components, even for very large rasters. Alternatively, you can use \code{stats::\link[stats]{princomp}} or \code{stats::\link[stats]{prcomp}} directly, perhaps using a sample.
}

\usage{
Expand All @@ -17,7 +15,7 @@ Alternatively, you can use \code{stats::\link[stats]{princomp}} or \code{stats::

\arguments{
\item{x}{SpatRaster}
\item{cor}{logical. If \code{FALSE}, the covariance matrix is used. Otherwise the correlation matrix is used (should only be used if there are no constant variables}
\item{cor}{logical. If \code{FALSE}, the covariance matrix is used. Otherwise the correlation matrix is used}
\item{fix_sign}{logical. If \code{TRUE}, the signs of the loadings and scores are chosen so that the first element of each loading is non-negative}
}

Expand All @@ -27,6 +25,9 @@ princomp object

\author{Based on a similar method by Benjamin Leutner}

\note{
Use \code{mask(x, anyNA(x), maskvalue=TRUE)} if you want to assure that values from the same cells are used for all layers. Otherwise, the covariance matrix is computed for all cells in pairs of layers that are not \code{NA} in those two layers.
}

\seealso{ \code{\link[stats]{princomp}}}

Expand All @@ -37,11 +38,10 @@ pca <- princomp(r)
x <- predict(r, pca)

# use "index" to get a subset of the components
y <- predict(r, pca, index=1:2)
p <- predict(r, pca, index=1:2)

### use prcomp or princomp directly
### use princomp directly (or use prcomp instead)
pca <- princomp(r)

# may need to use sampling with a large raster
sr <- spatSample(r, 100000, "regular")
pca <- prcomp(sr)
Expand Down
3 changes: 2 additions & 1 deletion man/terra-package.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ Also see the methods in section VIII
\code{\link{interpolate}} \tab Predict a spatial model to a SpatRaster \cr
\code{\link{interpIDW}} \tab Inverse-distance-weighted interpolation \cr
\code{\link{interpNear}} \tab Nearest neighbor interpolation \cr
\code{\link{k_means}} \tab kmeans clustering \cr
\code{\link{k_means}} \tab k-means clustering of SpatRaster data \cr
\code{\link{princomp}} \tab Principal Component Analysis with SpatRaster data\cr

--------------------------- \tab ------------------------------------------------------------------------------------------ \cr
}
Expand Down

0 comments on commit a258183

Please sign in to comment.