Skip to contents

Function to extract the first n principal components explaining a predefined total amount of variance among climatic variables. These components can subsequently be used as synthetic climatic variables to reduce dimensionality in climate-analogue methods.

Usage

climPCA(
  climp,
  climf,
  trans = function(x) log(x),
  cen = TRUE,
  sc = TRUE,
  th = 0.8
)

Arguments

climp

raster.stack with one layer for each climatic variable with the values for present or baseline conditions.

climf

raster.stack with one layer for each climatic variable with the values for future conditions.

trans

function specifying the type of transformation to be applied prior to the PCA. Specify NA where no transformation is required (default log(x)).

cen

logical should the variables be centered prior to the PCA? (default TRUE).

sc

logical should the variables be scaled prior to the PCA? (default TRUE).

th

numeric threshold giving the minimum amount of total variance that should be explained by the principal components extracted.

Value

a list containing (i) the output from the PCA (call to 'prcomp'), and (ii) a table with the present/future cell values for the principal components accounting for the specified percentage of total variance (th).

See also

Author

Jorge Garcia Molinos

Examples

if (FALSE) { # \dontrun{
JapTC <- VoCC_get_data("JapTC.tif")

comp <- climPCA(JapTC[[c(1, 3, 5)]], JapTC[[c(2, 4, 6)]],
                trans = NA, cen = TRUE, sc = TRUE, th = 0.85)
summary(comp[[1]]) # first two components explain >90% of variance
# Create a data frame with the necessary variables in the required order (see climAna? for details)
clim <- comp[[2]][, c(2, 4, 3, 5, 1)]
clim[, c("x", "y")] <- terra::xyFromCell(JapTC[[1]], clim$cid)
} # }