Skip to content

Commit

Permalink
Use algebraic eigenvalues, and get rid of useNames = NA warning
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamoses committed Jul 4, 2023
1 parent 3b7224e commit 6e53cba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/SFEMethod-multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ multispati_rsp <- function(x, listw, nfposi = 30L, nfnega = 30L, scale = TRUE) {
if (scale) {
# Note that dudi.pca divides by n instead of n-1 when scaling data
n <- nrow(x)
x <- sweep(x, 2, sqrt(colVars(x)*(n-1)/n), FUN = "/")
x <- sweep(x, 2, sqrt(matrixStats::colVars(x)*(n-1)/n), FUN = "/")
}
if (inherits(listw, "Matrix") || is.matrix(listw))
W <- listw
Expand All @@ -65,9 +65,9 @@ multispati_rsp <- function(x, listw, nfposi = 30L, nfnega = 30L, scale = TRUE) {
stop("listw must be either a listw object or an adjacency matrix.")
covar <- t(x) %*% (W + t(W)) %*% x / (2*nrow(x))
if (nfnega == 0L) {
res <- eigs_sym(covar, k = nfposi, which = "LM")
res <- eigs_sym(covar, k = nfposi, which = "LA")
} else if (nfposi == 0L) {
res <- eigs_sym(covar, k = nfnega, which = "SM")
res <- eigs_sym(covar, k = nfnega, which = "SA")
} else {
nf <- max(nfposi, nfnega)
res <- eigs_sym(covar, k = 2*nf, which = "BE")
Expand Down
31 changes: 31 additions & 0 deletions tests/testthat/test-multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@ test_that("Correct output structure of multispati_rsp", {
expect_true(all(eigs[11:20] < 0))
})

test_that("Correct output structure of multispati_rsp, only positive", {
out <- multispati_rsp(t(mat), listw = g, nfposi = 10, nfnega = 0)
expect_equal(colnames(out), paste0("PC", 1:10))
expect_equal(rownames(out), colnames(sfe))
expect_true(is.numeric(out))
loadings <- attr(out, "rotation")
expect_equal(colnames(loadings), paste0("PC", 1:10))
expect_equal(rownames(loadings), rownames(mat))
expect_true(is.numeric(loadings))
eigs <- attr(out, "eig")
expect_equal(length(eigs), 10)
expect_true(is.numeric(eigs))
expect_true(all(eigs > 0))
})

test_that("Correct output structure of multispati_rsp, only negative", {
out <- multispati_rsp(t(mat), listw = g, nfposi = 0, nfnega = 10)
expect_equal(colnames(out), paste0("PC", 1:10))
expect_equal(rownames(out), colnames(sfe))
expect_true(is.numeric(out))
loadings <- attr(out, "rotation")
expect_equal(colnames(loadings), paste0("PC", 1:10))
expect_equal(rownames(loadings), rownames(mat))
expect_true(is.numeric(loadings))
eigs <- attr(out, "eig")
expect_equal(length(eigs), 10)
expect_true(is.numeric(eigs))
expect_true(all(diff(eigs) < 0))
expect_true(all(eigs < 0))
})

ref <- multispati_rsp(t(mat), listw = g, nfposi = 10, nfnega = 10)
test_that("CalculateMultivariate for matrix", {
out <- calculateMultivariate(mat, "multispati", listw = g,
Expand Down

0 comments on commit 6e53cba

Please sign in to comment.