相关矩阵与R中的p值

时间:2018-05-20 13:31:10

标签: r dataframe dplyr

假设我想要行为相关矩阵

library(dplyr)

data(iris)
iris %>% 
  select_if(is.numeric) %>%
  cor(y =iris$Petal.Width, method = "spearman") %>%  round(2)

现在我们看到了

              [,1]
Sepal.Length  0.83
Sepal.Width  -0.29
Petal.Length  0.94
Petal.Width   1.00

我希望统计显着的相关性标记为* 其中

*<0,05
**<0,01
*** <0,001

怎么做?

3 个答案:

答案 0 :(得分:5)

使用的解决方案。我们可以将数据框转换为长格式,使用nest创建列表列,然后使用map为每个子集执行cor.test。之后,map_dbl可以通过指定名称"p.value"来提取P值。 dat1是最终输出。

library(tidyverse)

data(iris)
dat1 <- iris %>% 
  select_if(is.numeric) %>%
  gather(Column, Value, -Petal.Width) %>%
  group_by(Column) %>%
  nest() %>%
  mutate(Cor = map(data, ~cor.test(.x$Value, .x$Petal.Width, method = "spearman"))) %>%
  mutate(Estimate = round(map_dbl(Cor, "estimate"), 2), 
         P_Value = map_dbl(Cor, "p.value"))

dat1
# # A tibble: 3 x 5
#   Column       data               Cor         Estimate  P_Value
#   <chr>        <list>             <list>         <dbl>    <dbl>
# 1 Sepal.Length <tibble [150 x 2]> <S3: htest>    0.83  4.19e-40
# 2 Sepal.Width  <tibble [150 x 2]> <S3: htest>   -0.290 3.34e- 4
# 3 Petal.Length <tibble [150 x 2]> <S3: htest>    0.94  8.16e-70

如果您不需要列表列,可以使用select删除它们。

dat1 %>% select(-data, -Cor)
# # A tibble: 3 x 3
#   Column       Estimate  P_Value
#   <chr>           <dbl>    <dbl>
# 1 Sepal.Length    0.83  4.19e-40
# 2 Sepal.Width    -0.290 3.34e- 4
# 3 Petal.Length    0.94  8.16e-70

现在,我们可以使用mutatecase_when添加标签以显示重要性。

dat2 <- dat1 %>%
  select(-data, -Cor) %>%
  mutate(Significance = case_when(
    P_Value < 0.001  ~ "*** <0,001",
    P_Value < 0.01   ~ "** <0,01",
    P_Value < 0.05   ~ "*<0,05",
    TRUE             ~ "Not Significant"
  ))
dat2
# # A tibble: 3 x 4
#   Column       Estimate  P_Value Significance
#   <chr>           <dbl>    <dbl> <chr>       
# 1 Sepal.Length    0.83  4.19e-40 *** <0,001  
# 2 Sepal.Width    -0.290 3.34e- 4 *** <0,001  
# 3 Petal.Length    0.94  8.16e-70 *** <0,001 

答案 1 :(得分:3)

您可以根据自己的需要调整corstarsl()

corFun <- function (x) {
  library(Hmisc)
  x <- as.matrix(x)
  R <- rcorr(x, type="spearman")$r
  p <- rcorr(x, type="spearman")$P
  stars <- ifelse(p < 0.001, "***", ifelse(p < 0.01, "** ", 
                                             ifelse(p < 0.05, "* ", " ")))
  R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[, -1]
  Rnew <- matrix(paste(R, stars, sep = ""), ncol = ncol(x))
  diag(Rnew) <- paste(diag(R), " ", sep = "")
  rownames(Rnew) <- colnames(x)
  colnames(Rnew) <- paste(colnames(x), "", sep = "")
  Rnew <- as.matrix(Rnew)
  Rnew <- as.data.frame(Rnew)
  return(Rnew)
}

<强> 屈服

> data.frame(r=corFun(iris[, -5])[, 4])
                    r
Sepal.Length  0.83***
Sepal.Width  -0.29***
Petal.Length  0.94***
Petal.Width     1.00 

答案 2 :(得分:3)

以下两个tidyverse选项都使用tidy中的broom。使用tidy会提取估算值和p值,因此您无需手动执行此操作。我为你想要显示的不同显着性水平制作了一个休息向量,这样你就可以使用cut轻松地剪切和标记p值;将其保存在命名向量中也使其更具可重复性。

我第一次使用cor.test,它管道进入tidy.htest方法。我第二次使用来自rcorr的{​​{1}},其中包含了Hmisc方法。

在第一种情况下,我tidy.rcorr将数据框格式化为长格式,以便将每个度量与gather进行比较;在第二种情况下,需要一个矩阵,我使用了完整的数据集,然后过滤了包含Petal.Width的任一列。

Petal.Width

reprex package(v0.2.0)于2018-05-20创建。