genpca: Generalized Principal Components Analysis

View source: R/gpca.R

genpcaR Documentation

Generalized Principal Components Analysis

Description

Compute a PCA in a inner-product space defined by row and coulmn constraint matrices.

Usage

genpca(
  X,
  A = NULL,
  M = NULL,
  ncomp = min(dim(X)),
  preproc = center(),
  deflation = FALSE,
  svd_init = TRUE,
  threshold = 1e-06,
  use_cpp = TRUE
)

Arguments

A

the column constraints. Can be a vector, symmetric matrix, or symmetric sparse matrix with ncol(X) rows and columns.

M

the row constraints. Can be a vector, symmetric matrix, or symmetric sparse matrix with nrow(X) rows and columns.

References

Abdi, H. (2007). Singular value decomposition (SVD) and generalized singular value decomposition. Encyclopedia of measurement and statistics, 907-912.

Allen, G. I., Grosenick, L., & Taylor, J. (2014). A generalized least-square matrix decomposition. Journal of the American Statistical Association, 109(505), 145-159.

Examples


N <- 10
coords <- expand.grid(x=seq(1,N), y=seq(1,N))
img <- apply(coords, 1, function(x) {
  x1 <- 1 - pnorm(abs(x[1] - N/2), sd=8)
  x2 <- 1 - pnorm(abs(x[2] - N/2), sd=8)
  x1*x2
})

mat <- matrix(img, N,N)
mlist <- replicate(10, as.vector(mat + rnorm(length(mat))*.02), simplify=FALSE)
X <- do.call(rbind, mlist)

## spatial smoother
S <- neighborweights:::spatial_smoother(coords, sigma=3, nnk=4)
S <- cov(as.matrix(S))
S <- S/eigen(S)$values[1]
T <- neighborweights:::spatial_smoother(as.matrix(1:10), sigma=3, nnk=3)
T <- cov(as.matrix(T))
T <- T/(eigen(T)$values[1])
gp1 <- genpca(X, A=S, ncomp=9)

gp1a <- genpca(X, A=S, M=T, ncomp=9)

Xs <- do.call(rbind, lapply(1:nrow(X), function(i) X[i,,drop=FALSE] %*% S))
gp2 <- genpca(as.matrix(Xs), ncomp=2)

## use an adjacency matrix to weight items sharing an index.

X <- matrix(rnorm(50*100), 50, 100)
colind <- rep(1:10, 10)
S <- neighborweights:::spatial_adjacency(as.matrix(colind), dthresh=4, sigma=1, nnk=27, normalized=TRUE, include_diagonal=TRUE, weight_mode="heat")
diag(S) <- 1
S <- S/RSpectra::svds(S,k=1)$d
gp1 <- genpca(X, A=S, ncomp=2)

bbuchsbaum/neuroca documentation built on April 22, 2022, 2:50 a.m.