Skip to content

Instantly share code, notes, and snippets.

@espio999
Created September 12, 2021 01:32
Show Gist options
  • Save espio999/cb949f1559e9d52bae28dc7f883df630 to your computer and use it in GitHub Desktop.
Save espio999/cb949f1559e9d52bae28dc7f883df630 to your computer and use it in GitHub Desktop.
PCA (Principal Component Analysis) - plot, analysis, and prediction
library(FactoMineR)
library(factoextra)
library(corrplot)
mydata = LifeCycleSavings
mypca = PCA(mydata, scale.unit = TRUE, graph = FALSE)
mypca$eig
fviz_pca_var(mypca, col.var = "cos2", gradient.cols = c("blue", "red"), repel = TRUE)
fviz_pca_var(mypca, col.var = "contrib", gradient.cols = c("blue", "red"), repel = TRUE)
corrplot(mypca$var$cos2, addCoef.col = "gray")
corrplot(mypca$var$contrib, is.corr = FALSE, addCoef.col = "gray")
fviz_pca_ind(mypca, repel = TRUE)
fviz_cos2(mypca, choice = "ind", axes = 1:2)
fviz_pca_ind(mypca, repel = TRUE, select.ind = list(cos2 = 0.8))
fviz_pca_biplot(
mypca,
col.var = "blue",
col.ind = "cos2",
gradient.cols = c("blue", "red"),
repel = TRUE,
select.ind = list(cos2 = 0.8))
mypca = PCA(mydata, ind.sup = 23, quanti.sup = 1 ,scale.unit = TRUE, graph = FALSE)
mypca$ind.sup
mypca$quanti.sup
myplot = fviz_pca_biplot(
mypca,
col.var = "orange",
col.ind = "cos2",
gradient.cols = c("gray", "green"),
repel = TRUE,
select.ind = list(cos2 = 0.8))
myplot = fviz_add(myplot, mypca$ind.sup$cos2, color = "blue", repel = TRUE)
myplot|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment