PCA图_绘图(一)

使用的R包:FactoMineR factoextra

FactoMineR提供PCA算法以及(原始)结果;factoextra则extract and visualize the output

Principal Component Analysis (PCA), which is used to summarize the
information contained in a continuous (i.e, quantitative)
multivariate data by reducing the dimensionality of the data
without loosing important information.

library("FactoMineR")
library("factoextra")
data("decathlon2")
df <- decathlon2[1:23, 1:10]  # loading data

pcaResult = PCA(df,  graph = FALSE)  # PCA来自FactoMineR,只要result而不graph

get_eig(pcaResult)  # eigenvalue 特征值
# get_eigenvalue(pcaResult)  
# get_pca(pcaResult)
# fviz_screeplot(pcaResult, addlabels = TRUE, ylim = c(0, 50))

var = get_pca_var(pcaResult)
# var
# head(var$coord)
# head(var$contrib)
fviz_pca_var(pcaResult, col.var = "black")
# Control variable colors using their contributions
fviz_pca_var(pcaResult, col.var="contrib",
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE # Avoid text overlapping
             )

ind = get_pca_ind(pcaResult)
# ind
# head(ind$coord)  
fviz_pca_ind(pcaResult, col.ind = "cos2", 
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
             repel = TRUE # Avoid text overlapping (slow if many points)
             )

# Biplot of individuals and variables
fviz_pca_biplot(pcaResult, repel = TRUE)  

所以该怎么理解variables和individuals…

pcaResult

换熟悉的iris数据集看看

# Compute PCA on the iris data set
# The variable Species (index = 5) is removed
# before PCA analysis
iris.pca <- PCA(iris[,-5], graph = FALSE)

# Visualize
# Use habillage to specify groups for coloring
fviz_pca_ind(iris.pca,
             label = "none", # hide individual labels
             habillage = iris$Species, # color by groups
             palette = c("#00AFBB", "#E7B800", "#FC4E07"),
             addEllipses = TRUE # Concentration ellipses
             )

参考资料

FactoMineR: Exploratory Multivariate Data Analysis with R

factoextra : Extract and Visualize the Results of Multivariate Data Analyses

posted @ 2023-03-05 23:03  歪歪ba  阅读(69)  评论(0编辑  收藏  举报