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 @   歪歪ba  阅读(101)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
· 易语言 —— 开山篇
点击右上角即可分享
微信分享提示