天天看點

factoextra 主成分分析(1)

factoextra是一個R軟體包,可以輕松提取和可視化探索性多變量資料分析的輸出,其中包括:

主成分分析(PCA),用于通過在不丢失重要資訊的情況下減少資料的次元來總結連續(即定量)多變量資料中包含的資訊。

對應分析(CA),它是适用于分析由兩個定性變量(或分類資料)形成的大型列聯表的主成分分析的擴充。

多重對應分析(MCA),它是将CA改編為包含兩個以上分類變量的資料表格。

多因素分析(MFA)專用于資料集,其中變量按組(定性和/或定量變量)組織。

分層多因素分析(HMFA):在資料組織為分層結構的情況下,MFA的擴充。

混合資料因子分析(FAMD)是MFA的一個特例,緻力于分析包含定量和定性變量的資料集。

有許多R包實作主要元件方法。這些軟體包包括:FactoMineR,ade4,stats,ca,MASS和ExPosition。

  • 為什麼使用
  • R package factoextra具有靈活且易于使用的方法,可以用人類可讀的标準資料格式快速提取上述不同軟體包的分析結果。
  • 它以較少的輸入産生了基于ggplot2的優雅資料可視化。
  • 它還包含許多便于聚類分析和可視化的功能。
  • factoextra R軟體包可以處理來自多個軟體包的PCA,CA,MCA,MFA,FAMD和HMFA的結果,用于提取和可視化資料中包含的最重要資訊。
  • 在PCA,CA,MCA,MFA,FAMD和HMFA之後,可以使用以下高亮顯示最重要的行/列元素:

    它們的cos2值對應于它們在因子圖上的表示品質

    他們對主要次元定義的貢獻。

    如果你想這樣做,factoextra包提供了一個友善的解決方案。

  • 如果您想使用PCA / MCA進行預測并使用ggplot2可視化補充變量/個體在因子圖上的位置:那麼factoextra可以為您提供幫助。
  • 提取并可視化特征值/方差:
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))
           
factoextra 主成分分析(1)
# 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")
           
  • 提取和可視化變量的結果:
    factoextra 主成分分析(1)
  • 可以使用它們對主軸的貢獻(“contrib”)來控制變量顔色:
# Contributions of variables to PC1
fviz_contrib(res.pca, choice = "var", axes = 1, top = 10)

# Contributions of variables to PC2
fviz_contrib(res.pca, choice = "var", axes = 2, top = 10)
           
factoextra 主成分分析(1)

5d5b9846542399ad072923b856d2cf77_README-pca-variable-contributions-2.png

factoextra 主成分分析(1)

c5a5e79e032a4ebb2d386b8041694f47_README-pca-variable-contributions-1.png

  • 為每一個提取和可視化結果:
# 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)
             )
           
factoextra 主成分分析(1)
  • Color individuals by groups:
# 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
             )
           
factoextra 主成分分析(1)
相關資料:無監督學習 聚類分析① - 簡書 https://www.jianshu.com/p/921aa71afdf8 無監督學習 聚類分析② - 簡書 https://www.jianshu.com/p/514d771c243e 無監督學習 聚類分析③ - 簡書 https://www.jianshu.com/p/b8dcfc7c1d4c 無監督學習 聚類分析④ - 簡書 https://www.jianshu.com/p/e9ba1f5f5ae7 factoextra包 聚類分析(2) - 簡書 https://www.jianshu.com/p/18f14b6774f9

繼續閱讀