Skip to content

Instantly share code, notes, and snippets.

@slchangtw
Last active January 25, 2018 10:32
Show Gist options
  • Save slchangtw/733757f963d2bba9b01ce3f0840c4e1c to your computer and use it in GitHub Desktop.
Save slchangtw/733757f963d2bba9b01ce3f0840c4e1c to your computer and use it in GitHub Desktop.
iris2 <- as.matrix(iris[, 1:4])
# cetralize data
centered_iris2 <- scale(iris2, center = TRUE, scale = FALSE)
# covariance matrix
cov_iris2 <- (t(centered_iris2) %*% centered_iris2) / (dim(centered_iris2)[1] - 1)
# perform eigendecomposition on covariance matrix
eig_cov <- eigen(cov_iris2)
# Compare the results
pca <- prcomp(iris2)
pca$sdev - sqrt(eig_cov$values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment