clusteringRaster: Perform clustering on a SpatRaster object and calculate...

View source: R/SOptim_UnsupervisedClassification.R

clusteringRasterR Documentation

Perform clustering on a SpatRaster object and calculate internal clustering criteria

Description

Runs a clustering/unsupervised learning/classification algorithm on a multi-layer SpatRaster object. It also allows calculating internal clustering performance criteria based on function intCriteria using a stratified random sample of output pixels.

Usage

clusteringRaster(
  inRst,
  k,
  writeRasterData = TRUE,
  outRst,
  getSpatRaster = TRUE,
  method = "kmeans",
  calcIntCriteria = FALSE,
  crit = c("Silhouette"),
  intCritSampSize = 20000,
  verbose = TRUE,
  ...
)

Arguments

inRst

An input SpatRaster object. By default all layers/bands will be used to perform the k-means clustering.

k

An integer vector defining the number of clusters. If more than one value is given then kmeansRaster will run one time for each k value.

writeRasterData

Write clustered raster data to file (i.e., to outRst)? (default:TRUE)

outRst

Name of the output raster. For each value in k the file name will get a suffix in this form: "filename_method_nc_k.ext".

getSpatRaster

Get an output SpatRaster object with clustering solutions? (default: FALSE). If FALSE then some memory saving is expected.

method

A string defining the method used for clustering. Available options are:

  • "kmeans": for k-means partitioning clustering from stats package (default);

  • "hardcl": for hard-competitive learning;

  • "neuralgas": for neural-gas algorithm both from cclust package, and;

  • "clara": from cluster package.

calcIntCriteria

Calculate internal clustering criteria? (default: FALSE).

crit

A character vector containing the names of the indices to compute. Check available indices at intCriteria or running getCriteriaNames(TRUE).

intCritSampSize

Number of sample pixels to use for internal criteria calculation (default: 20000). Increasing this number will make estimations better however at the expense of more memory.

verbose

Print progress messages? (default: TRUE).

...

Further parameters passed to each of clustering functions kmeans, cclust or, clara (for clara, except the following paramaters which are pre-defined to: keep.data = FALSE, and medoids.x = FALSE to save memory and speed-up calculations).

Value

If getSpatRaster=TRUE then a SpatRaster object with all clustering solutions will be returned. If getSpatRaster=TRUE and calcIntCriteria=TRUE then an additional matrix named intClustCrit with one row by k value will be returned with the requested clustering indices. If getSpatRaster=FALSE then only the matrix with clustering indices will be returned.

See Also

See intCriteria, getCriteriaNames for more details on clustering criteria. Check out clustering functions at: kmeans, cclust, and, clara.

Examples


library(terra)

r1 <- rast(nrows=10, ncols=10, vals=rnorm(100))
r2 <- rast(nrows=10, ncols=10, vals=rnorm(100))
r3 <- rast(nrows=10, ncols=10, vals=rnorm(100))

rstStack <- c(r1, r2, r3)

clusteringRaster(rstStack, k = 2:10, writeRasterData = FALSE, getSpatRaster = TRUE, 
                 method="kmeans", calcIntCriteria = FALSE, crit = c("Silhouette"), 
                 intCritSampSize = 20000, verbose = TRUE)



joaofgoncalves/SegOptim documentation built on Feb. 5, 2024, 11:10 p.m.