Skip to content

Instantly share code, notes, and snippets.

@tbates
Last active December 15, 2015 08:09
Show Gist options
  • Save tbates/5229295 to your computer and use it in GitHub Desktop.
Save tbates/5229295 to your computer and use it in GitHub Desktop.
How does factor analysis work? eigenvalues and eigenvectors
x <- data.frame(matrix(ncol = 2, byrow = T, c(
-0.7, 0.2,
2.1, 2.7,
1.7, 2.3,
1.4, 0.8,
1.9, 2.0,
1.8, 1.0,
0.4, -0.4,
1.1, 0.3,
0.9, 0.4,
-0.6, 0.7))
)
names(x) = c("x","y")
cov_x = cov(x)
eigen(cov_x)$vectors
# Eigenvalues suggest 1 factor
eigen(cov_x)$values
# Let's keep a feature matrix of just 1 factor: column 1
feature_vector = eigen(cov_x)$vectors[, 1, drop = F]
round(t(feature_vector) %*% t(x), 2)
# > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# > [1,] -0.35 3.39 2.83 1.56 2.76 1.98 0 0.99 0.92 0.07
@tbates
Copy link
Author

tbates commented Mar 23, 2013

Based on: this lecture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment