factoextra是一个R软件包,可以轻松提取和可视化探索性多变量数据分析的输出,其中包括: 主成分分析(PCA),用于通过在不丢失重要信息的情况下减少数据的维度来总结连续(即定量)多变量数据中包含的信息。 对应分析(CA),它是适用于分析由两个定性变量(或分类数据)形成的大型列联表的主成分分析的扩展。 多重对应分析(MCA),它是将CA改编为包含两个以上分类变量的数据表格。 多因素分析(MFA)专用于数据集,其中变量按组(定性和/或定量变量)组织。 分层多因素分析(HMFA):在数据组织为分层结构的情况下,MFA的扩展。 混合数据因子分析(FAMD)是MFA的一个特例,致力于分析包含定量和定性变量的数据集。 有许多R包实现主要组件方法。这些软件包包括:FactoMineR,ade4,stats,ca,MASS和ExPosition。
library("factoextra")
data("decathlon2")
df <- decathlon2[1:23, 1:10]
library("FactoMineR")
res.pca <- PCA(df, graph = T)
get_eig(res.pca)
fviz_screeplot(res.pca, addlabels = TRUE, ylim = c(0, 50))
# Extract the results for variables
var <- get_pca_var(res.pca)
var
#> Principal Component Analysis Results for variables
#> ===================================================
#> Name Description
#> 1 "$coord" "Coordinates for the variables"
#> 2 "$cor" "Correlations between variables and dimensions"
#> 3 "$cos2" "Cos2 for the variables"
#> 4 "$contrib" "contributions of the variables"
# Coordinates of variables
head(var$coord)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> X100m -0.8506257 -0.17939806 0.3015564 0.03357320 -0.1944440
#> Long.jump 0.7941806 0.28085695 -0.1905465 -0.11538956 0.2331567
#> Shot.put 0.7339127 0.08540412 0.5175978 0.12846837 -0.2488129
#> High.jump 0.6100840 -0.46521415 0.3300852 0.14455012 0.4027002
#> X400m -0.7016034 0.29017826 0.2835329 0.43082552 0.1039085
#> X110m.hurdle -0.7641252 -0.02474081 0.4488873 -0.01689589 0.2242200
# Contribution of variables
head(var$contrib)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> X100m 17.544293 1.7505098 7.338659 0.13755240 5.389252
#> Long.jump 15.293168 4.2904162 2.930094 1.62485936 7.748815
#> Shot.put 13.060137 0.3967224 21.620432 2.01407269 8.824401
#> High.jump 9.024811 11.7715838 8.792888 2.54987951 23.115504
#> X400m 11.935544 4.5799296 6.487636 22.65090599 1.539012
#> X110m.hurdle 14.157544 0.0332933 16.261261 0.03483735 7.166193
# Graph of variables: default plot
fviz_pca_var(res.pca, col.var = "black")
# Control variable colors using their contributions
fviz_pca_var(res.pca, col.var="contrib",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE # Avoid text overlapping
)
# Extract the results for individuals
ind <- get_pca_ind(res.pca)
ind
#> Principal Component Analysis Results for individuals
#> ===================================================
#> Name Description
#> 1 "$coord" "Coordinates for the individuals"
#> 2 "$cos2" "Cos2 for the individuals"
#> 3 "$contrib" "contributions of the individuals"
# Coordinates of individuals
head(ind$coord)
#> Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
#> SEBRLE 0.1955047 1.5890567 0.6424912 0.08389652 1.16829387
#> CLAY 0.8078795 2.4748137 -1.3873827 1.29838232 -0.82498206
#> BERNARD -1.3591340 1.6480950 0.2005584 -1.96409420 0.08419345
#> YURKOV -0.8889532 -0.4426067 2.5295843 0.71290837 0.40782264
#> ZSIVOCZKY -0.1081216 -2.0688377 -1.3342591 -0.10152796 -0.20145217
#> McMULLEN 0.1212195 -1.0139102 -0.8625170 1.34164291 1.62151286
# Graph of individuals
# 1\. Use repel = TRUE to avoid overplotting
# 2\. Control automatically the color of individuals using the cos2
# cos2 = the quality of the individuals on the factor map
# Use points only
# 3\. Use gradient color
fviz_pca_ind(res.pca, col.ind = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE # Avoid text overlapping (slow if many points)
)
[图片上传失败...(image-6a6139-1529602238838)]
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有