【问题标题】:Correlation in R - missing valuesR 中的相关性 - 缺失值
【发布时间】:2017-12-05 21:50:34
【问题描述】:

我想在以下数据集上运行相关性。我想要所有之间的相关性(V1 与 V2、V3、V4、V5;V2 与 V1、V3、V4、V5 等等......)。我想要相关系数和 p 值。

我的数据集

   Group    V1        V2          V3         V4       V5
   OH      0.3        5          -3.09      2.5      NA
   OH      0.5        1           NA        1.8      2.5 
   ON      2          2.5         NA       -3.11    -7.5
   OH      1.5       -3.35       -0.82       NA     -2.5
   ON      6.5       -2.85        2.5        NA      NA
   OH      3          0.5         1.8      -2.85     NA

我运行了这段代码

    correlations <- corr.test (mydataset, use = "pairwise"). 

我也跑过:

    correlations <- cor(mydataset, use = "complete.obs", method = "pearson")

我不知道如何处理缺失值。而且我没有得到任何输出。我不断收到此错误:

    Error in cor(x, use = use, method = method) : 'x' must be numeric

有什么可行的建议吗?

谢谢!

【问题讨论】:

  • 它也在尝试使用Group 列,因为这是您提供的。 (我假设您的其余列实际上是数字;您也可以检查一下。)
  • 只保留数据集中的数字列并执行corr.test (mydataset, use = "pairwise")。该命令将自动排除 NA。您可以在它将提供的示例表上进行检查。

标签: r correlation


【解决方案1】:

问题不在于 NA,而在于变量 Group 不是数字。所以试试

 cor(mydataset[sapply(mydataset, is.numeric)], use='pairwise')

这将只选择数值变量并排除 NA。

        V1         V2         V3         V4         V5
V1  1.0000000 -0.5917056  0.8907941 -0.9355822 -0.9819805
V2 -0.5917056  1.0000000 -0.6376181  0.4894776 -0.2468321
V3  0.8907941 -0.6376181  1.0000000 -1.0000000         NA
V4 -0.9355822  0.4894776 -1.0000000  1.0000000  1.0000000
V5 -0.9819805 -0.2468321         NA  1.0000000  1.0000000

在相关矩阵中,我们看到 V3V5 之间的相关性得到 NA,但那是因为 V3V5 并非仅在一次观察中同时 NA,而且您无法得到仅在一对点上相关。

【讨论】:

  • V1 _V2 V1 .0000000 0.5659588 V2 0.533333 0.65368 谢谢!我已经尝试过了,我得到了类似上面的东西。它给出了根本没有 NA 的变量的输出,但 R 根本没有拿起我的其他列!有什么建议吗??
  • 我得到了一个完整的相关矩阵。我得到的唯一 NA 结果是针对 V3、V5,但那是因为鉴于您提供的数据,V3 和 V5 仅在第 3 次观察中同时不是 NA。而且你不能只与一对有相关性。
【解决方案2】:

corr.test(test[2:6], use = 'pairwise') 是你想要的。您必须排除第一列,这是一个字符向量。下面是一个输出的代表。你得到系数和 p 值。


library('tidyverse')
#> ── Attaching packages ─────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1     ✔ purrr   0.2.4
#> ✔ tibble  1.3.4     ✔ dplyr   0.7.4
#> ✔ tidyr   0.7.2     ✔ stringr 1.2.0
#> ✔ readr   1.1.1     ✔ forcats 0.2.0
#> ── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
library('psych')
#> 
#> Attaching package: 'psych'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     %+%, alpha
test = tibble::tribble(
  ~Group, ~V1, ~V2, ~V3, ~V4, ~V5,
  'OH',      0.3,        5,          -3.09,      2.5,      NA,
  'OH',      0.5,        1,           NA,        1.8,      2.5,
  'ON',      2 ,         2.5,         NA,       -3.11,    -7.5,
  'OH',      1.5,       -3.35,       -0.82,       NA,     -2.5,
  'ON',      6.5,       -2.85,        2.5,        NA,      NA,
  'OH',      3,          0.5,         1.8,      -2.85,     NA
)
corr.test(test[2:6], use = 'pairwise')
#> Warning in sqrt(n - 2): NaNs produced
#> Warning in corr.test(test[2:6], use = "pairwise"): Number of subjects must
#> be greater than 3 to find confidence intervals.
#> Warning in sqrt(n[lower.tri(n)] - 3): NaNs produced
#> Call:corr.test(x = test[2:6], use = "pairwise")
#> Correlation matrix 
#>       V1    V2    V3    V4    V5
#> V1  1.00 -0.59  0.89 -0.94 -0.98
#> V2 -0.59  1.00 -0.64  0.49 -0.25
#> V3  0.89 -0.64  1.00 -1.00    NA
#> V4 -0.94  0.49 -1.00  1.00  1.00
#> V5 -0.98 -0.25    NA  1.00  1.00
#> Sample Size 
#>    V1 V2 V3 V4 V5
#> V1  6  6  4  4  3
#> V2  6  6  4  4  3
#> V3  4  4  4  2  1
#> V4  4  4  2  4  2
#> V5  3  3  1  2  3
#> Probability values (Entries above the diagonal are adjusted for multiple tests.) 
#>      V1   V2   V3   V4   V5
#> V1 0.00 0.86 0.66 0.45 0.66
#> V2 0.22 0.00 1.00 1.00 1.00
#> V3 0.11 0.36 0.00  NaN   NA
#> V4 0.06 0.51  NaN 0.00  NaN
#> V5 0.12 0.84   NA  NaN 0.00
#> 
#>  To see confidence intervals of the correlations, print with the short=FALSE option

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-18
    • 2021-07-24
    • 1970-01-01
    • 2019-08-31
    • 2012-08-10
    • 2019-12-06
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多