spca: spca: Principal Component Analysis for Spatial Data

spcaR Documentation

spca: Principal Component Analysis for Spatial Data

Description

Performs PCA for a stack of raster layers.

Usage

spca(
  layers_stack,
  layers_to_proj = NULL,
  pca_obj = NULL,
  sv_dir = NULL,
  layers_format = ".asc",
  sv_proj_dir = NULL
)

Arguments

layers_stack

A RasterStack of environmental variables

layers_to_proj

A RasterStack of the environmental variables that will be projected (default NULL). If provided the function will project the PCA for those layers by using the PCA object computed for the layers_stack. It can also use the PCA object stored in pca_obj.

pca_obj

An object of class prcomp (default NULL). Usefull when the user already has done the PCa for layers_stack and just wants to project the PCA on the layers_to_proj object.

sv_dir

A directory where the PCs will be saved. If NULL the PCs will not be written.

layers_format

A raster format for writing PCA results (see writeFormats). Default = ".asc"

sv_proj_dir

A directory where the PCs projection will be saved. If NULL the PCs will be written inside sv_dir.

Details

spca uses the function prcomp of the stats package. If sv_dir is provided the PCs and pca_obj will be stored on it. The names of the layers in layers_to_proj need to be named with exactly the same names of those either inlayers_stack or the pca_obj.

Value

A list containing either the raster stack with Principal Components of layers_stack or layers_to_proj, a barplot of the cumulative and explained variance of each compoent of layers_stack and a prcomp object(pca_obj).

Examples

# -------------------------------------------------------------------
# A PCA on layers_stack  without saving.
layers_stack <- raster::stack(list.files(system.file("extdata",
                                    package = "ntbox"),
                                    pattern = "M_layers.tif$",
                                    full.names = TRUE))
pcs <- spca(layers_stack, sv_dir=NULL,layers_format=NULL)
raster::plot(pcs$pc_layers)

## Not run: 
# -------------------------------------------------------------------
# PCA projection without saving.

layers_to_proj <- raster::stack(list.files(system.file("extdata",
                                                      package = "ntbox"),
                                          pattern = "G_layers.tif$",
                                          full.names = TRUE))
pcs_with_proj <- spca(layers_stack =layers_stack,
                      layers_to_proj = layers_to_proj,
                      pca_obj = NULL,
                      sv_dir=NULL,layers_format=NULL)
# Barplot of the Cumulative and explained variance in each component,
print(pcs_with_proj$pca_plot)

## End(Not run)
# -------------------------------------------------------------------
# PCA projection and saving.
## Not run: 
pcs_with_proj_sv <- spca(layers_stack =layers_stack,
                         layers_to_proj = layers_to_proj,
                         pca_obj = NULL,
                         sv_dir=".",layers_format=".asc")


## End(Not run)
# -------------------------------------------------------------------
# PCA projection, saving and using the pca_obj.
## Not run: 

pca_obj <- base::readRDS(list.files(system.file("extdata",
                                                package = "ntbox"),
                                                pattern = ".rds$",
                                                full.names = TRUE))

pcs_with_proj_sv <- spca(layers_stack =NULL,
                         layers_to_proj = layers_to_proj,
                         pca_obj = pca_obj,
                         sv_dir=".",layers_format=".tif")

raster::plot(pcs_with_proj_sv$pcs_layers_projection)

## End(Not run)

luismurao/ntbox documentation built on April 3, 2024, 5:47 a.m.