tests/testthat/test-pca-internal-irlba.R

test_that("pca runs without loading irlba package namespace", {
  pre_loaded <- "irlba" %in% loadedNamespaces()
  x <- matrix(rnorm(120), nrow = 30, ncol = 4)
  out <- pca(x, nv = 3)
  post_loaded <- "irlba" %in% loadedNamespaces()

  expect_s3_class(out, "prcomp")
  expect_equal(post_loaded, pre_loaded)
})

test_that("pca output dimensions are correct", {
  x <- matrix(rnorm(200), nrow = 25, ncol = 8)
  out <- pca(x, nv = 5)

  expect_equal(dim(out$x), c(25, 5))
  expect_equal(dim(out$rotation), c(8, 5))
  expect_equal(length(out$sdev), 5)
})

test_that("pca scores are consistent with truncated SVD up to sign", {
  set.seed(123)
  x <- matrix(rnorm(180), nrow = 30, ncol = 6)
  k <- 4
  out <- pca(x, nv = k)

  sv <- svd(x, nu = k, nv = k)
  ref_scores <- sweep(sv$u, 2, sv$d[seq_len(k)], "*")

  for (j in seq_len(k)) {
    cc <- suppressWarnings(cor(out$x[, j], ref_scores[, j]))
    expect_gt(abs(cc), 0.98)
  }
})

Try the KODAMA package in your browser

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

KODAMA documentation built on March 17, 2026, 5:07 p.m.