Skip to content

Fix tpca() when there are more samples than features (n > p) - #13

Open
kimanh-lecao wants to merge 3 commits into
brendanlu:mainfrom
kimanh-lecao:pr/03-tpca-more-subjects-than-features
Open

kimanh-lecao wants to merge 3 commits into
brendanlu:mainfrom
kimanh-lecao:pr/03-tpca-more-subjects-than-features

Conversation

@kimanh-lecao

Copy link
Copy Markdown
Collaborator

Builds on #12; only the last commit is new.

tpca() fails whenever the number of samples exceeds the number of features:

set.seed(1)
tpca(array(rnorm(20 * 20 * 5), dim = c(20, 20, 5)), ncomp = 2)   # n = p: runs
tpca(array(rnorm(21 * 20 * 5), dim = c(21, 20, 5)), ncomp = 2)   # n > p
#> Error in .binary_facewise(a, b, bpparam = NULL) : pa == nb is not TRUE

This is a common shape for targeted panels, for example a few dozen metabolites or antibody titres
measured on more subjects.

Cause. tsvdm() returns the economy-size decomposition: uhat is n x k x t with
k = min(n, p). tpca() then builds the singular values tensor as n x p x t for
uhat %fp% S. When n <= p, k = n and the product conforms; when n > p, uhat has only p columns
against n rows of S, and .binary_facewise() stops.

Fix. Build the singular values tensor with k rows: dim = c(k, p, t) instead of c(n, p, t).
For n <= p this is the same call as before.

Checks.

  • n <= p is unchanged: 17 tpca() fits (n < p and n = p; matrix and tensor output; with and
    without centring; several ncomp) are identical() before and after, as are tpls(), tplsda(),
    block_tpls() and block_tplsda() results.
  • n > p now runs, and the projections are correct: for t = 5 and 6, with n x p of 21 x 20, 30 x 4,
    46 x 26 and 90 x 31, the tensor variates equal the transformed, centred data times vhat face by
    face to within 2e-14.
  • New test in test-tens.tpca.R, mirroring "tpca agrees with mixOmics pca" with n = 10 > p = 3:
    loadings and variates match mixOmics::pca(). It fails on the current main and passes with the
    fix.
  • The full test suite passes.

Closes #3.

Copilot AI balanced review requested due to automatic review settings October 16, 2026 00:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tpca() fails when there are more samples than features (n > p)

2 participants