R 处理、可视化 多变量数据

factoextra  包

1 PCA  Principal Component Analysis

2 CA Correspondence Analysis

3 MCA Multiple corespondence Analysis

4  MFA Multiple Factor Analysis

5 HMFA Hierachical Multiple Factor Analysis

6. FAMD Factor Analysis of Mixed Data

 

如 1 PCA 部分

library("FactoMineR")
library("factoextra")
data("decathlon2") # 加载数据框
glimpse(decathlon2)
df<-decathlon2[1:23,1:10]
df

 

res.pca<-PCA(df,graph = FALSE)
get_eig(res.pca)
fviz_screeplot(res.pca,addlables=TRUE,ylim=c(0,50))

 

 

 

 

 

 

 

 

 var<-get_pca_var(res.pca) # 提取变量结果
 var
 head(var$coord) 
 head(var$contrib)
fviz_pca_var(res.pca,col.var = "black")

 

 

 

 

fviz_pca_var(res.pca,col.var = "contrib",gradient.cols=c("#00AFBB", "#E7B800", "#FC4E07"),repel = TRUE) # 按变量的contributions 给他们上色

  

 

# 变量在不同主成分水平的贡献
fviz_contrib(res.pca,choice = "var",axes=1,top = 10)
fviz_contrib(res.pca,choice="var",axes=2,top=10)

 

 

# 提取、可视化个体的pca结果
 ind<-get_pca_ind(res.pca)
 ind
head(ind$coord)
fviz_pca_ind(res.pca,col.ind = "cos2",gradient.cols=c("#00AFBB", "#E7B800", "#FC4E07"),repel = TRUE)

  

 

 fviz_pca_biplot(res.pca,repel = TRUE) ## Biplot of individuals and variables

  

 

# 按组别给个体上色 
iris.pca<-PCA(iris[,-5],graph=FALSE)
 fviz_pca_ind(iris.pca,lable="none",habillage = iris$Species,palette =  c("#00AFBB", "#E7B800", "#FC4E07"),addEllipses = TRUE)

  

 

参考 https://rpkgs.datanovia.com/factoextra/

posted on 2022-02-07 13:39  BioinformaticsMaster  阅读(186)  评论(0编辑  收藏  举报

导航