tests/testthat/test-scale-consistency.R

library(vsp)

n <- 1000
A <- rsparsematrix(10, 10, density = 0.1)

# these tests confirm that the matrix reconstructions based on (U, D, V)
# and (Z, B, Y) match

test_that("SVD and factor reconstructions match", {
  fa <- vsp(A, rank = 5)

  udv <- as.matrix(fa$u %*% diag(fa$d) %*% t(fa$v))
  zby <- as.matrix(fa$Z %*% fa$B %*% t(fa$Y))

  expect_true(isTRUE(all.equal(udv, zby)))
})


test_that("SVD and factor reconstructions match, scaling", {
  fa <- vsp(A, rank = 5, degree_normalize = TRUE)

  udv <- as.matrix(fa$u %*% diag(fa$d) %*% t(fa$v))
  zby <- as.matrix(fa$Z %*% fa$B %*% t(fa$Y))

  expect_true(isTRUE(all.equal(udv, zby)))
})

rm(A, n)

Try the vsp package in your browser

Any scripts or data that you put into this service are public.

vsp documentation built on April 12, 2025, 1:41 a.m.