【问题标题】:PCA - Extract variables falling below a certain threshold from the Communalities table in RPCA - 从 R 中的 Communalities 表中提取低于某个阈值的变量
【发布时间】:2021-09-08 10:10:22
【问题描述】:

我是新来的,还在学习如何正确使用 R,但我发现自己需要一些专家的帮助。我目前正在使用包EFA.dimensions 来做我的PCA。为此,我的脚本如下所示:

PCA(data, corkind='pearson', Nfactors=11, Ncases=NULL)

从出现在结果中的“Communalities”表(如下图所示)中,我想提取提取公共性低于 0.80 的变量列表。在显示的示例中,只有一个变量“ZLocomotionSocial”,但我有另一个数据集可能最终包含其中的许多变量,因此不必逐个查找它们会很棒。如果有帮助,最终目标是从“数据”中删除这些变量,然后重新运行 PCA。

Communalities table example

关于我可以使用哪些代码来解决这个问题有什么建议吗?

【问题讨论】:

    标签: r pca


    【解决方案1】:

    您可以在数据框中转换communalities 对象,然后使用dplyr 包进行一些基本过滤:

    library(tidyverse)
    library(EFA.dimensions)
    
    communalities <-
      data_Harman %>%
      PCA(Nfactors=3, corkind = "pearson") %>%
      pluck("communalities") %>%
      as_tibble(rownames = "variable")
    #> 
    #> Ncases must be provided when data is a correlation matrix.
    #> 
    #> 
    #> Principal Components Analysis
    #> 
    #> Specified kind of correlations for this analysis: from user
    #> 
    #> The specified number of factors to extract = 3
    #> 
    #> Model Fit Coefficients:
    #> 
    #> RMSR = 0.046
    #> 
    #> GFI = 0.993
    #> 
    #> CAF = 0.5
    #> 
    #> 
    #> Eigenvalues and factor proportions of variance:
    #>             Eigenvalues    Proportion of Variance    Cumulative Prop. Variance
    #> Factor 1           4.67                      0.58                         0.58
    #> Factor 2           1.77                      0.22                         0.81
    #> Factor 3           0.48                      0.06                         0.87
    #> Factor 4           0.42                      0.05                         0.92
    #> Factor 5           0.23                      0.03                         0.95
    #> Factor 6           0.19                      0.02                         0.97
    #> Factor 7           0.14                      0.02                         0.99
    #> Factor 8           0.10                      0.01                         1.00
    #> 
    #> Unrotated PCA Loadings:
    #>               Factor 1   Factor 2   Factor 3
    #> Height           -0.86      -0.37      -0.07
    #> Arm.span         -0.84      -0.44       0.08
    #> Forearm          -0.81      -0.46       0.01
    #> Leg.length       -0.84      -0.40      -0.10
    #> Weight           -0.76       0.52      -0.15
    #> Hips             -0.67       0.53      -0.05
    #> Chest.girth      -0.62       0.58      -0.29
    #> Chest.width      -0.67       0.42       0.59
    #> 
    #> Promax Rotation Pattern Matrix:
    #>               Factor 1   Factor 2   Factor 3
    #> Height           -0.92       0.10      -0.05
    #> Arm.span         -0.95      -0.10       0.12
    #> Forearm          -0.95      -0.07       0.02
    #> Leg.length       -0.93       0.10      -0.10
    #> Weight           -0.07       0.87       0.06
    #> Hips              0.01       0.75       0.17
    #> Chest.girth       0.06       0.99      -0.13
    #> Chest.width       0.00       0.07       0.94
    #> 
    #> Promax Rotation Structure Matrix:
    #>               Factor 1   Factor 2   Factor 3
    #> Height           -0.94       0.44       0.37
    #> Arm.span         -0.95       0.35       0.42
    #> Forearm          -0.93       0.33       0.35
    #> Leg.length       -0.93       0.42       0.32
    #> Weight           -0.45       0.93       0.61
    #> Hips             -0.36       0.85       0.62
    #> Chest.girth      -0.30       0.89       0.44
    #> Chest.width      -0.40       0.64       0.99
    #> 
    #> Eigenvalues and factor proportions of variance:
    #>             Eigenvalues    Proportion of Variance    Cumulative Prop. Variance
    #> Factor 1           3.51                      1.17                         1.17
    #> Factor 2           2.33                      0.78                         1.95
    #> Factor 3           0.96                      0.32                         2.27
    #> 
    #> Promax Rotation Factor Correlations:
    #>            Factor 1   Factor 2   Factor 3
    #> Factor 1       1.00      -0.41      -0.39
    #> Factor 2      -0.41       1.00       0.60
    #> Factor 3      -0.39       0.60       1.00
    communalities
    #> # A tibble: 8 x 2
    #>   variable    Communalities
    #>   <chr>               <dbl>
    #> 1 Height              0.882
    #> 2 Arm.span            0.909
    #> 3 Forearm             0.872
    #> 4 Leg.length          0.871
    #> 5 Weight              0.872
    #> 6 Hips                0.742
    #> 7 Chest.girth         0.803
    #> 8 Chest.width         0.975
    
    selected_communalities <-
      communalities %>%
      filter(Communalities < 0.8)
    selected_communalities
    #> # A tibble: 1 x 2
    #>   variable Communalities
    #>   <chr>            <dbl>
    #> 1 Hips             0.742
    
    selected_variables <- selected_communalities$variable
    selected_variables
    #> [1] "Hips"
    

    reprex package 创建于 2021-09-10 (v2.0.1)

    【讨论】:

    • 这太棒了!非常感谢danlooo!请问我可以再问一个问题吗?现在我有了“selected_variables”,你知道我可以如何使用“remove”命令吗?我正在尝试使用以下脚本,但到目前为止它没有工作:within(data, rm(selected_variables))
    • rm 是从计算机内存中删除 R 对象。我猜您只想过滤 行以查看所选变量及其在一个数据框中的共性。我相应地更新了答案。我建议阅读本章以获得基本技能:r4ds.had.co.nz/transform.html
    • 对不起,你是对的,我没有解释自己:我想做的是从我的原始数据中删除这些选定的变量(低于 0.8 的变量),这样我就可以重新 -在没有它们的情况下运行 PCA。
    • 我实际上只是设法用脚本做到了:within(data, rm(list = selected_variables)),以防它帮助任何人;-)
    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 2022-06-16
    • 2013-12-21
    • 2021-11-14
    • 2016-08-09
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多