Skip to content

tolerance_eigen error with rank 1 matrices #32

Description

@LukeMoraglia

Hey Derek,

I got an error when using tolerance_eigen on a rank-1 matrix. The error comes from the call to colSums and says

Error in colSums(eigen_res$vectors) : 
  'x' must be an array of at least two dimensions

In line 64 below, eigen_res$vectors gets converted to a vector when there is only one eigenvalue to keep.

GSVD/R/tolerance_eigen.R

Lines 64 to 68 in 0f41cf2

eigen_res$vectors <- eigen_res$vectors[,evs.to.keep]
rownames(eigen_res$vectors) <- colnames(x)
## new way inspired by FactoMineR but with some changes
vector_signs <- ifelse(colSums(eigen_res$vectors) < 0, -1, 1)

I converted back to matrix and that seems to have fixed the issue for me.
https://github.com/LukeMoraglia/GSVD/blob/a47ee067abe3a468137b34a4d5fd0984abca40ad/R/tolerance_eigen.R#L64

Code to reproduce error

library(GSVD)
set.seed(42)
X <- matrix(sample.int(20, 20*3, replace = TRUE), 20, 3)
R <- cor(X)

# Create a rank-1 matrix from R
eig_R <- tolerance_eigen(R)
R_rank1 <- as.matrix(eig_R$vectors[,1]) %*% eig_R$values[1] %*% t(as.matrix(eig_R$vectors[,1]))

tolerance_eigen(R_rank1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions