spca_logrs: Function making a matrix of D(D-1) logratios and calculating...

View source: R/spca_logrs.R

spca_logrsR Documentation

Function making a matrix of D(D-1) logratios and calculating sparse PCA.

Description

Function making a matrix of D(D-1) logratios and calculating sparse PCA.

Usage

spca_logrs(X, alpha = 0.01, beta = 1e-04, k = (D - 1), draw = T)

Arguments

X

a matrix of raw compositional data with "n" rows and "D" columns/components

alpha

a sparsity parameter; the higher its value, the sparser the results; default is 0.01

beta

a tuning parameter resulting in shrinkage of the parameters towards zero; beta = 0 leads to lasso penalty; default is 1e-04

k

number of principal components (PCs) to be calculated; default is (D-1)

draw

a logical parameter stating whether a biplot should be drawn (TRUE) or not (FALSE); default is T

Details

The function creates a sparse PCA model where a matrix of pairwise logratios is taken as an input. The function spca from the library sparsepca is used for modelling, Erichson et al. (2020) for more details. For detailed information regarding sparse PCA with pairwise logratios, see Nesrstová et al. (2024).

Value

A list with the following components:

X.pairwise

A matrix of (D-1) pairwise logratios.

model

A sparse PCA model (using sparsepca::spca) where X.pairwise is the input.

loadings

A matrix of loadings.

model summary

A short summary of the model returning the explained variance by PCs.

expl.var

A proportion of variance of each PC.

number of zero logratios

States how many zero logratios (having zeros in all PCs) are in the model.

table of all zero

Returns the table of all zero logratios.

Author(s)

Viktorie Nesrstová

References

Erichson, N.B., Zheng, P. Manohar, K., Brunton, S.L., Kuntz, J.N., Aravkin, Y. (2020). Sparse principal component analysis via variable projection. SIAM J Appl Math. Available at: https://epubs.siam.org/doi/10.1137/18M1211350 DOI: \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1137/18M1211350")}

Nesrstová, V., Wilms, I., Hron, K., Filzmoser, P. (2024). Identifying Important Pairwise Logratios in Compositional Data with Sparse Principal Component Analysis. Mathematical Geosciences. Available at: https://link.springer.com/article/10.1007/s11004-024-10159-0 DOI: \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1007/s11004-024-10159-0")}

Examples

## Not run: 
  if (requireNamespace("MASS", quietly = TRUE)) {

# 1. Generate sample data
n <- 100              # observations
D <- 10               # parts/variables
Sig <- diag(D-1)      # positive-definite symmetric matrix -> covariance matrix
mu <- c(rep(0, D-1))  # means of variables

set.seed(1234)
# ilr coordinates
Z <- MASS::mvrnorm(n,mu,Sigma = Sig)

# Z -> CoDa X
V <- compositions::ilrBase(D = D)
X <- as.matrix(as.data.frame(acomp(exp(Z%*%t(V)))))

# 2. Apply sPCA to pairwise logratios
alpha_max <- 1 # specify max value of tuning parameter
alpha_nbr <- 50 # specify number of tuning parameters
alpha_ratio <- 1000 # specify ratio of largest to smallest tuning parameter
a <- sort(alpha_grid,decreasing=F)        # zero included

# Models for different values of alpha parameters, calculating PC1 and PC2
models <- list()
for(i in 1:length(a)){
 models[[i]] <- spca_logrs(X=X, alpha = a[i], k = 2, draw = F)
}

  }

## End(Not run)


robCompositions documentation built on Aug. 22, 2025, 5:11 p.m.