美文网首页
数据集间向量相关性计算

数据集间向量相关性计算

作者: 倪桦 | 来源:发表于2022-05-23 21:01 被阅读0次

library(dplyr)

数据集相关性

library(dplyr);library(tidyverse)

p_value <- corrplot::cor.mtest(bind_cols(df1,df2), conf.level = 0.95)
p_value <- p_value$p %>% data.frame() %>% rownames_to_column("rowname") %>% gather("var", "p", -rowname)

cor_matrix <- corrr::correlate(bind_cols(df1,df2),method = "spearman") %>%
    tidyr::pivot_longer(cols=-term,names_to="var",values_to = 'value') %>%
    data.frame() %>% 
    dplyr::rename(rowname=term)

cor_pval_matrix <- plyr::join(cor_matrix,p_value)

cor_matrix <- cor_pval_matrix %>% 
    # mutate(value = ifelse(p > 0.05, NA_integer_, value)) %>%
    select(-p) %>%
    spread(var, value) %>% 
    filter(rowname %in% colnames(df1)) %>% 
    select_if(names(.) %in% c("rowname",colnames(df2))) %>% data.frame() %>%
    column_to_rownames("rowname") %>% as.matrix()


pval_matrix <- cor_pval_matrix %>% 
    # mutate(value = ifelse(p > 0.05, NA_integer_, value)) %>%
    select(-value) %>%
    spread(var, p) %>% 
    filter(rowname %in% colnames(df1)) %>% 
    select_if(names(.) %in% c("rowname",colnames(df2))) %>% data.frame() %>%
    column_to_rownames("rowname") %>% as.matrix()


COL1 <- colorRampPalette(c("#053061","#2166AC","#4393C3","#92C5DE","#D1E5F0","#FFFFFF","#FDDBC7","#F4A582","#D6604D","#B2182B","#67001F"))


corrplot::corrplot(cor_matrix, 
                   p.mat = pval_matrix,
                   # order = 'hclust',
                   # hclust.method = "complete",
                   type = 'full',
                   method = 'color', 
                   is.corr=FALSE,
                   col = COL1(200),
                   cl.pos = "n",
                   col.lim = c(min(cor_matrix),max(cor_matrix)),
                   sig.level = c(0.001, 0.01, 0.05),pch.cex = 0.9,insig = 'label_sig',pch.col = 'grey20',diag = T,
                   tl.col = "black")



相关文章

网友评论

      本文标题:数据集间向量相关性计算

      本文链接:https://www.haomeiwen.com/subject/nzmrprtx.html