简体   繁体   English

如何删除相关图矩阵中的无关紧要的变量

[英]How to remove insignificant variables in a correlogram matrix

I have a dataset with 29 variables and I have tried to see how they are correlated using cor() .我有一个包含 29 个变量的数据集,我尝试使用cor()查看它们是如何相关的。

This has given me a 29X29 matrix with the p-value for each product pair.这给了我一个 29X29 矩阵,其中包含每个产品对的 p 值。 Most of these correlations are insignificant, and I want only to retain the instances where the p-value is significant for 2 specific variables.这些相关性中的大多数都是微不足道的,我只想保留 p 值对 2 个特定变量显着的实例。

Here is a toy example, suppose that I want to maintain only the variables there are significant correlated with mpg , ig, cor_pmat(mpg, other_variables) < 0.05) .这是一个玩具示例,假设我只想维护与mpg , ig, cor_pmat(mpg, other_variables) < 0.05)显着相关的变量。

library(ggcorrplot)
p.mat <- cor_pmat(mtcars)
corr <- round(cor(mtcars), 2)

Any hint on how can I do that?关于我该怎么做的任何提示?

Here is a function to select on the data frame:这是数据帧上的 function 到 select:

library(dplyr)
library(rlang)
library(broom)

select_via_cor_sig <- function(.data, x, p.value, ...) {
  x <- rlang::ensym(x)

  .data %>%
    dplyr::select(-dplyr::all_of(x)) %>%
    names() %>%
    lapply(function(candidate) {
      c(rlang::as_string(x), candidate)
    }) -> ls_pairs
  
  ls_pairs %>% 
    lapply(function(vec_pair) {
      x <- .data[[vec_pair[1]]]
      y <- .data[[vec_pair[2]]]

      cor.test(x, y, ...) %>%
        broom::tidy() %>%
        dplyr::mutate(v1 = vec_pair[1], v2 = vec_pair[2]) %>%
        dplyr::select(v1, v2, dplyr::everything())
    }) %>%
    dplyr::bind_rows() -> tbl_tidy_cor_test
  
  tbl_tidy_cor_test %>% 
    dplyr::filter(p.value < {{p.value}}) %>%
    dplyr::pull(v2) %>% 
    c(rlang::as_string(x), .) -> keepers
  
  .data %>% 
    dplyr::select(dplyr::all_of(keepers))
}

# use it like so:
select_via_cor_sig(mtcars, mpg, 0.001)

If you want the p-value matrix, you could run it on the subset data frame produced by this function.如果你想要 p 值矩阵,你可以在这个 function 生成的子集数据帧上运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM