nipals code speedup

Issue #111 resolved
Former user created an issue

In the nipals function is this method of reconstructing X

       for (h in 1:ncomp) {
            X.hat = X.hat + eig[h] * t.mat[, h] %*% t(p[, h])
        }

I think this could be simplified by removing the loop over the h variable, and just multiplying the matrices together.

For example, the reconstructed matrix can be calculated by multiplying t, diag(eig), and p. Like this:

set.seed(42)
X <- matrix(rbinom(150, size=20, prob=.5), nrow=10)
nn = nipals(X, ncomp=10, reconst=TRUE)
Xh = nn$t %*% diag(nn$eig) %*% t(nn$p)
round(X - Xh,1)
round(Xh-nn$rec,1)

Kevin Wright

Comments (1)

  1. Log in to comment