I'm attempting to write a function that will standardize an n by d matrix, i.e. the matrix will be $N(0,I)$
From my understanding, a matrix Z can be "standardized" through the following operation: Z = $\Sigma^{-1/2}(X-a)\sim N(0,I)$, where a is a matrix of the row means of matrix X, and $\Sigma^{-1/2}$ is the square root of the inverse of the covariance matrix.
The function below attempts to take a matrix and standardize it:
standardize<-function(X){
Sigma<-cov(X)
S<-svd(Sigma)
R<-S$u%*%diag(sqrt(S$d))%*%t(S$v)
R.inverse<-solve(R)
Z<-X-rowMeans(X)
Z.final<-R.inverse%*%t(Z)
}
However, the resulting matrix usually has several NaNs values after attempting to compute the square root of the inverse of the covariance matrix.
Any insight would be much appreciated.