pca: Compute Principal Component Analysis

View source: R/pca.R

pcaR Documentation

Compute Principal Component Analysis

Description

Computes principal components from a numeric matrix or data frame with automatic scaling and zero-variance removal. Returns all principal components as a data frame. Wrapper for stats::prcomp().

Usage

pca(x = NULL, colnames.prefix = "pca_factor")

Arguments

x

Numeric matrix or data frame to decompose into principal components.

colnames.prefix

Character string used as prefix for column names in the output. Default: "pca_factor".

Details

This function performs Principal Component Analysis (PCA) to create uncorrelated linear combinations of the original variables. The PCA process:

  1. Removes columns with zero variance (constant values)

  2. Scales all remaining variables to mean = 0 and standard deviation = 1

  3. Computes principal components using singular value decomposition

  4. Returns all principal components ordered by decreasing variance explained

Usage in spatial analysis:

PCA is useful for dimension reduction when working with spatial distance matrices or multiple correlated spatial predictors. It creates orthogonal (uncorrelated) variables that capture the main patterns of variation while reducing dimensionality.

For spatial modeling with rf_spatial(), principal components of distance matrices can serve as alternative spatial predictors to Moran's Eigenvector Maps (MEMs). Use pca_multithreshold() to compute PCA across multiple distance thresholds for multi-scale spatial modeling.

Value

Data frame where each column is a principal component, ordered by decreasing variance explained. Columns are named with the pattern ⁠<prefix>_<number>⁠ (e.g., "pca_factor_1", "pca_factor_2"). The number of rows matches the number of rows in x.

See Also

pca_multithreshold(), mem(), stats::prcomp()

Other spatial_analysis: filter_spatial_predictors(), mem(), mem_multithreshold(), moran(), moran_multithreshold(), pca_multithreshold(), rank_spatial_predictors(), residuals_diagnostics(), residuals_test(), select_spatial_predictors_recursive(), select_spatial_predictors_sequential()

Examples

data(plants_distance)

# Compute principal components from distance matrix
pca_components <- pca(x = plants_distance)

# View structure
head(pca_components)
dim(pca_components)

# Check column names
colnames(pca_components)[1:5]

# Custom column prefix
pca_custom <- pca(
  x = plants_distance,
  colnames.prefix = "distance_pc"
)
colnames(pca_custom)[1:3]


spatialRF documentation built on Dec. 20, 2025, 1:07 a.m.

Related to pca in spatialRF...