是否可以为 R 中相关包的 correlation() 函数中的每个成对比较删除 NA?
Is it possible to remove NAs for each pairwise comparison in correlation() function of correlation package in R?
我想知道如何在 R[=32= 中的 correlation
包的 correlation()
函数中删除 NA
以进行成对比较]. 欢迎使用其他替代方案。我知道 Hmisc
包中的 rcorr()
,但我需要长(整洁)格式的输出。
这相当于 cor(x, use = 'pairwise.complete.obs')
。
因为我同时需要 p.value
和 estimate
,所以 cor()
不合适,痛苦的是 cor.test()
没有 use = 'pairwise.complete.obs'
作为参数.
具体来说,由于数据量大 cor.test(x, na.action = 'na.omit')
从 Pearson 相关性分析中过度删除了条目,这就是为什么我希望根据成对比较而不是跨整个数据集来执行此操作。
好吧,只是为了好玩。使用 corrr
包将为您提供一些整洁的数据选项,即您可以获得 a) 整洁格式的相关性和 b) 长格式的相关性。它还可以为您提供成对完整 obs 的数量 (pair_n
).
从那里开始,a) 计算不为零的相关性的 t 值和 b) 相应的 p 值相对容易。请注意,在我上面的评论中,我假设您想计算两个相关性之间的差异。但是,我认为您只需要相关性的正常 p 值。
1。创建一个包含缺失的玩具数据集:
set.seed(1)
mtcars_NA <- mtcars %>%
mutate(across(everything(), ~ if_else(row_number() %in% sample(1:32, 5), NA_real_, .)))
2。计算相关性,附加样本大小并获得 t/p 个值
library(tidyverse)
library(corrr)
mtcars_NA %>%
correlate() %>%
shave() %>%
stretch() %>%
filter(!is.na(r)) %>%
left_join(mtcars_NA %>%
pair_n %>%
as.data.frame() %>%
rownames_to_column("x") %>%
pivot_longer(-x,
values_to = "n",
names_to = "y"),
by = c("x", "y")) %>%
mutate(t_value = r / sqrt((1 - r^2) / (n -2)),
p_value = 2*pt(q = abs(t_value), df = n-2, lower.tail = FALSE))
给出:
# A tibble: 55 x 6
x y r n t_value p_value
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 mpg cyl -0.851 22 -7.23 0.000000534
2 mpg disp -0.864 23 -7.87 0.000000107
3 mpg hp -0.785 23 -5.80 0.00000929
4 mpg drat 0.684 22 4.19 0.000449
5 mpg wt -0.882 24 -8.78 0.0000000122
6 mpg qsec 0.434 23 2.21 0.0385
7 mpg vs 0.742 23 5.07 0.0000511
8 mpg am 0.549 22 2.94 0.00814
9 mpg gear 0.476 23 2.48 0.0218
10 mpg carb -0.640 23 -3.81 0.00101
# ... with 45 more rows
3。让我们将第一个相关性与 cor.test 函数
进行比较
cor.test(mtcars_NA$cyl, mtcars_NA$mpg)
给出:
Pearson's product-moment correlation
data: mtcars_NA$cyl and mtcars_NA$mpg
t = -7.2326, df = 20, p-value = 5.337e-07
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.9363704 -0.6687359
sample estimates:
cor
-0.8505393
结果是一样的。
我想知道如何在 R[=32= 中的 correlation
包的 correlation()
函数中删除 NA
以进行成对比较]. 欢迎使用其他替代方案。我知道 Hmisc
包中的 rcorr()
,但我需要长(整洁)格式的输出。
这相当于 cor(x, use = 'pairwise.complete.obs')
。
因为我同时需要 p.value
和 estimate
,所以 cor()
不合适,痛苦的是 cor.test()
没有 use = 'pairwise.complete.obs'
作为参数.
具体来说,由于数据量大 cor.test(x, na.action = 'na.omit')
从 Pearson 相关性分析中过度删除了条目,这就是为什么我希望根据成对比较而不是跨整个数据集来执行此操作。
好吧,只是为了好玩。使用 corrr
包将为您提供一些整洁的数据选项,即您可以获得 a) 整洁格式的相关性和 b) 长格式的相关性。它还可以为您提供成对完整 obs 的数量 (pair_n
).
从那里开始,a) 计算不为零的相关性的 t 值和 b) 相应的 p 值相对容易。请注意,在我上面的评论中,我假设您想计算两个相关性之间的差异。但是,我认为您只需要相关性的正常 p 值。
1。创建一个包含缺失的玩具数据集:
set.seed(1)
mtcars_NA <- mtcars %>%
mutate(across(everything(), ~ if_else(row_number() %in% sample(1:32, 5), NA_real_, .)))
2。计算相关性,附加样本大小并获得 t/p 个值
library(tidyverse)
library(corrr)
mtcars_NA %>%
correlate() %>%
shave() %>%
stretch() %>%
filter(!is.na(r)) %>%
left_join(mtcars_NA %>%
pair_n %>%
as.data.frame() %>%
rownames_to_column("x") %>%
pivot_longer(-x,
values_to = "n",
names_to = "y"),
by = c("x", "y")) %>%
mutate(t_value = r / sqrt((1 - r^2) / (n -2)),
p_value = 2*pt(q = abs(t_value), df = n-2, lower.tail = FALSE))
给出:
# A tibble: 55 x 6
x y r n t_value p_value
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 mpg cyl -0.851 22 -7.23 0.000000534
2 mpg disp -0.864 23 -7.87 0.000000107
3 mpg hp -0.785 23 -5.80 0.00000929
4 mpg drat 0.684 22 4.19 0.000449
5 mpg wt -0.882 24 -8.78 0.0000000122
6 mpg qsec 0.434 23 2.21 0.0385
7 mpg vs 0.742 23 5.07 0.0000511
8 mpg am 0.549 22 2.94 0.00814
9 mpg gear 0.476 23 2.48 0.0218
10 mpg carb -0.640 23 -3.81 0.00101
# ... with 45 more rows
3。让我们将第一个相关性与 cor.test 函数
进行比较cor.test(mtcars_NA$cyl, mtcars_NA$mpg)
给出:
Pearson's product-moment correlation
data: mtcars_NA$cyl and mtcars_NA$mpg
t = -7.2326, df = 20, p-value = 5.337e-07
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.9363704 -0.6687359
sample estimates:
cor
-0.8505393
结果是一样的。