Last active
February 11, 2016 13:32
-
-
Save amchercashin/bf427997062f2fd5b21d to your computer and use it in GitHub Desktop.
Returns reduces data after PCA with desired percent of variance retained.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pca_var <- function(X, percent) { | |
#Normalize data before start | |
X = scale(X) | |
#Covariance matrix | |
Sigma = (1 / nrow(X)) * t(X) %*% X | |
#Singular value decomposition of covariance matrix | |
Sigma_svd <- svd(Sigma) | |
#Get minimum number of vectors to retain desired percent of variation | |
k = which(cumsum(Sigma_svd$d)/sum(Sigma_svd$d) >= percent)[1] | |
#Take this number of vectors | |
Ureduce <- Sigma_svd$u[, 1:k] | |
#Compute reduced version of data | |
Z = X %*% Ureduce | |
return(Z) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment