First time question asker here! Thanks in advance for any suggestions! So here's the issue. I start with a matrix Z which is samples by features. I create a covariance matrix $A = Z^T Z$. Then I perform eigenvalue decomposition on A, so $A = QDQ^T$. My goal is then to solve for Z in terms of Q and D. Here's the algebra showing how I solve for z which results in:
$$Z = \sqrt(D)Q^T$$
*This post (https://math.stackexchange.com/questions/2858299/solve-xtx-a-for-x/2859023) confirms that this math checks out, at least I think.
The problem comes when I try to code the above as a toy example in R
#1. Create toy data z <- matrix(c(1,2,3,4), nrow = 2) print(z) #2. Calculate symmetric matrix A A <- t(z) %*% z #3. Eigenvalue decomposition ev <- eigen(A) Q <- ev$vectors l <- ev$values D <- diag(l) #4. Re-multiply to obtain A again, as a sanity check A_new <- Q %*% D %*% t(Q) # Should equal A #5. Get expression for z z_new = sqrt(D) %*% t(Q) # Should equal z
A_new should equal A, and it does
A and A_new:
BUT I also think z_new should equal z, and it does not!
Why doesn't Z == Z_new?! Is the math wrong? Is the code wrong? I've never been so stumped!
Ultimately, what I'm trying to do is subtract out the variance that is explained by the first principal component from the original dataset Z. So my plan is to set the first eigenvalue (entry 0,0 of the D matrix) to 0, and then multiply back out and resolve for Z. If there's an easier way to do that directly, or if my method is
Thank you all