Skip to content

Commit

Permalink
added tests to confirm that different sparse matrix subclasses give t…
Browse files Browse the repository at this point in the history
…he same results
  • Loading branch information
Eric Weine committed Jun 24, 2024
1 parent 9877da0 commit 9e496fd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ LazyData: true
LazyDataCompression: xz
NeedsCompilation: yes
VignetteBuilder: knitr
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
2 changes: 1 addition & 1 deletion R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion man/fit_glmpca_pois.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/testthat/test_fit_glmpca_pois.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 9e496fd

Please sign in to comment.