Skip to content

Instantly share code, notes, and snippets.

@amchercashin
Last active February 11, 2016 13:32
Show Gist options
  • Save amchercashin/bf427997062f2fd5b21d to your computer and use it in GitHub Desktop.
Save amchercashin/bf427997062f2fd5b21d to your computer and use it in GitHub Desktop.
Returns reduces data after PCA with desired percent of variance retained.
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