Skip to contents

Calculate and rasterize principal components from a metric raster

Usage

calculate_pcomp(
  mraster,
  nComp,
  center = TRUE,
  scale = TRUE,
  maxcells = Inf,
  plot = FALSE,
  details = FALSE,
  filename = NULL,
  overwrite = FALSE,
  ...
)

Arguments

mraster

spatRaster. ALS metrics raster.

nComp

Numeric. Value indicating number of principal components to be rasterized.

center

Logical. Value indicating whether the variables should be shifted to be zero centered.

scale

Logical. Value indicating whether the variables should be scaled to have unit variance.

maxcells

Numeric. Maximum number of samples to use to generate principal components. For objects that are too large to feed into prcomp.

plot

Logical. Plots output strata raster with samples.

details

Logical. If FALSE (default) output is only stratification raster. If TRUE return a list where $details is additional stratification information and $raster is the output stratification spatRaster.

filename

Character. Path to write output samples.

overwrite

Logical. Choice to overwrite existing filename if it exists.

...

Additional arguments to be passed to prcomp.

Value

Output raster with specified number of principal components as layers.

Author

Tristan R.H. Goodbody

Examples

#--- Load raster ---#
r <- system.file("extdata", "mraster.tif", package = "sgsR")
mr <- terra::rast(r)

calculate_pcomp(
  mraster = mr,
  nComp = 2
)
#> class       : SpatRaster 
#> dimensions  : 277, 373, 2  (nrow, ncol, nlyr)
#> resolution  : 20, 20  (x, y)
#> extent      : 431100, 438560, 5337700, 5343240  (xmin, xmax, ymin, ymax)
#> coord. ref. : UTM Zone 17, Northern Hemisphere 
#> source(s)   : memory
#> varnames    : mraster 
#>               mraster 
#> names       :       PC1,       PC2 
#> min values  : -4.402269, -2.155242 
#> max values  :  5.282663,  5.357801 

pcomp <- calculate_pcomp(
  mraster = mr,
  nComp = 3,
  details = TRUE
)

#--- Display principal component details ---#
# pcomp$pca

#--- Display importance of components ---#
# summary(pcomp$pca)