From f32f4bd9788077b86817ef61bf01880b72ee5a5b Mon Sep 17 00:00:00 2001 From: Eric Weine Date: Mon, 11 Mar 2024 18:47:15 -0400 Subject: [PATCH 1/3] added support for dgTmatrix --- DESCRIPTION | 2 +- R/argument_checking.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 451543d..7daa299 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Encoding: UTF-8 Type: Package Package: fastglmpca -Version: 0.1-104 +Version: 0.1-105 Date: 2024-01-30 Title: Fast Algorithms for Generalized Principal Component Analysis Authors@R: c(person("Eric","Weine",role=c("aut","cre"), diff --git a/R/argument_checking.R b/R/argument_checking.R index c0c3642..7ab1390 100644 --- a/R/argument_checking.R +++ b/R/argument_checking.R @@ -1,7 +1,7 @@ # Return true if x is a compressed, sparse, column-oriented numeric # matrix. is.sparse.matrix <- function (x) - inherits(x,"dgCMatrix") && is.numeric(x@x) + (inherits(x,"dgCMatrix") || inherits(x,"dgTMatrix")) && is.numeric(x@x) # Verify that x is matrix with finite, numeric entries. verify.matrix <- function (x, arg.name = deparse(substitute(x))) { From 9877da0010ceebe1526c74a19ac43694f4a2ba61 Mon Sep 17 00:00:00 2001 From: Eric Weine Date: Mon, 11 Mar 2024 18:50:07 -0400 Subject: [PATCH 2/3] updated to more general sparse matrix class --- R/argument_checking.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/argument_checking.R b/R/argument_checking.R index 7ab1390..551d346 100644 --- a/R/argument_checking.R +++ b/R/argument_checking.R @@ -1,7 +1,7 @@ # Return true if x is a compressed, sparse, column-oriented numeric # matrix. is.sparse.matrix <- function (x) - (inherits(x,"dgCMatrix") || inherits(x,"dgTMatrix")) && is.numeric(x@x) + inherits(x,"dsparseMatrix") && is.numeric(x@x) # Verify that x is matrix with finite, numeric entries. verify.matrix <- function (x, arg.name = deparse(substitute(x))) { From 9e496fdec16438d72f137c52be4a703d30a1ff8b Mon Sep 17 00:00:00 2001 From: Eric Weine Date: Mon, 24 Jun 2024 14:50:04 -0400 Subject: [PATCH 3/3] added tests to confirm that different sparse matrix subclasses give the same results --- DESCRIPTION | 2 +- R/fit.R | 2 +- man/fit_glmpca_pois.Rd | 2 +- tests/testthat/test_fit_glmpca_pois.R | 41 +++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7daa299..55019bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -44,4 +44,4 @@ LazyData: true LazyDataCompression: xz NeedsCompilation: yes VignetteBuilder: knitr -RoxygenNote: 7.3.0 +RoxygenNote: 7.3.1 diff --git a/R/fit.R b/R/fit.R index ed9a55b..5747945 100644 --- a/R/fit.R +++ b/R/fit.R @@ -108,7 +108,7 @@ #' #' @param Y The n x m matrix of counts; all entries of \code{Y} should #' be non-negative. It can be a sparse matrix (class -#' \code{"dgCMatrix"}) or dense matrix (class \code{"matrix"}). +#' \code{"dsparseMatrix"}) or dense matrix (class \code{"matrix"}). #' #' @param K Integer 1 or greater specifying the rank of the matrix #' factorization. This should only be provided if the initial fit diff --git a/man/fit_glmpca_pois.Rd b/man/fit_glmpca_pois.Rd index ba8b756..12a512f 100644 --- a/man/fit_glmpca_pois.Rd +++ b/man/fit_glmpca_pois.Rd @@ -34,7 +34,7 @@ init_glmpca_pois( \arguments{ \item{Y}{The n x m matrix of counts; all entries of \code{Y} should be non-negative. It can be a sparse matrix (class -\code{"dgCMatrix"}) or dense matrix (class \code{"matrix"}).} +\code{"dsparseMatrix"}) or dense matrix (class \code{"matrix"}).} \item{K}{Integer 1 or greater specifying the rank of the matrix factorization. This should only be provided if the initial fit diff --git a/tests/testthat/test_fit_glmpca_pois.R b/tests/testthat/test_fit_glmpca_pois.R index 5d4adf9..50c12fb 100644 --- a/tests/testthat/test_fit_glmpca_pois.R +++ b/tests/testthat/test_fit_glmpca_pois.R @@ -282,3 +282,44 @@ test_that("Test fit works with input covariates",{ expect_equivalent(crossprod(fit_quick$U),diag(3),scale = 1,tolerance = 1e-8) expect_equivalent(crossprod(fit_quick$V),diag(3),scale = 1,tolerance = 1e-8) }) + +test_that("Results are the same with different classes of sparse matrices",{ + + # Simulate a 100 x 200 data set to factorize. + set.seed(1) + n <- 100 + m <- 200 + Y <- generate_glmpca_data_pois(n,m,K = 3)$Y + + Y_dgC <- as(Y, "CsparseMatrix") + Y_dgT <- as(Y, "dgTMatrix") + Y_dgR <- as(Y, "dgRMatrix") + + set.seed(1) + fit0_dgC <- init_glmpca_pois(Y_dgC,K = 3) + set.seed(1) + fit0_dgT <- init_glmpca_pois(Y_dgT,K = 3) + set.seed(1) + fit0_dgR <- init_glmpca_pois(Y_dgR,K = 3) + + suppressWarnings(capture.output( + fit_quick_dgC <- fit_glmpca_pois(Y_dgC,fit0 = fit0_dgC, + control = list(maxiter = 20)))) + + suppressWarnings(capture.output( + fit_quick_dgT <- fit_glmpca_pois(Y_dgT,fit0 = fit0_dgT, + control = list(maxiter = 20)))) + + suppressWarnings(capture.output( + fit_quick_dgR <- fit_glmpca_pois(Y_dgR,fit0 = fit0_dgR, + control = list(maxiter = 20)))) + + expect_equal(fit_quick_dgC$progress$loglik, fit_quick_dgR$progress$loglik) + expect_equal(fit_quick_dgC$progress$loglik, fit_quick_dgT$progress$loglik) + expect_equal(fit_quick_dgC$U, fit_quick_dgR$U) + expect_equal(fit_quick_dgC$d, fit_quick_dgR$d) + expect_equal(fit_quick_dgC$V, fit_quick_dgR$V) + expect_equal(fit_quick_dgC$U, fit_quick_dgT$U) + expect_equal(fit_quick_dgC$d, fit_quick_dgT$d) + expect_equal(fit_quick_dgC$V, fit_quick_dgT$V) +})