【问题标题】:Dplyr and tidyverse -- correlation between pattern of variables (Same variable VAR1_time1 and VAR1_time2)Dplyr 和 tidyverse - 变量模式之间的相关性(相同变量 VAR1_time1 和 VAR1_time2)
【发布时间】:2018-11-25 17:10:45
【问题描述】:

我从Code Review 开始这个话题并要求交叉发布,因为(与 StackOverflow 相比),Code Review 的 dplyrtidyverse 用户很少。如果这个话题对这个社区来说还不够,请随意压制这个话题!但如果没问题,看看这个数据集。 “a”和“b”用于在测量相同变量时进行区分。在这种情况下,X1a 和 X1b 访问同一个变量,但“a”是(假设..)去年,“b”是今年。

我有一个明确的目标!我想关联“a”和“b”并绘制它。 X 轴具有变量名称(1、2、3 等),y 轴具有相关结果。

我为帮助我澄清这个问题而创建的虚假数据以及我开发的代码如下:

 all_items <- data.frame("1a" = sample(1:5), 
                            "2a" = sample(1:5),
                            "3a" = sample(1:5),
                            "1b" = sample(1:5),
                            "2b" = sample(1:5),
                            "3b" = rep(sample(1:5),10))


    #matrix with correlation
    all_correlation <- cor(all_items, method = "spearman") %>% 
      as.data.frame()

    #filter
    all_correlation <- all_correlation %>% select(-c(ends_with("a"))) #columns

    #create a colum with the now name
    all_correlation <- all_correlation %>% 
      mutate(item = row.names(.)) %>% select(item, everything())

    #supress some rows
    all_correlation <- all_correlation %>%  filter(!grepl("b", item))

    #filter(stringr::str_detect(row.names(.), "b"))
    #get only the diagonal
    all_correlation <- data.frame(item=1:3,Result=diag(as.matrix(all_correlation[, -1])))

    #P Value
    all_correlation_p_value <- Hmisc::rcorr(as.matrix(all_items))$P %>% as.data.frame()

    #filter
    all_correlation_p_value <- all_correlation_p_value %>% select(-c(ends_with("a")))
    all_correlation_p_value <- all_correlation_p_value %>% mutate(item = row.names(.)) %>% select(item, everything())
    all_correlation_p_value <- all_correlation_p_value %>%  filter(!grepl("b", item))
    all_correlation_p_value <- data.frame(item=1:3,P_Valor=diag(as.matrix(all_correlation_p_value[, -1])))

    #General table with the correlation  results
    all_correlation <- right_join(all_correlation,all_correlation_p_value, by = "item")


#Plot
ggplot(all_correlation, aes(x=item, y=Result)) + 
  geom_point(aes(color=Result)) +
  geom_line() +
  annotate("text", x = all_correlation$item, 
           y=all_correlation$Result, 
           label = paste("P-value =",round(all_correlation$P_Valor,3)), hjust = -0.1, colour = "red") +
  scale_x_continuous(breaks = seq(1,3,1)) 

谢谢。

【问题讨论】:

  • 据我所知,您提供的链接上的问题已被接受?
  • @Tjebo,是的!谢谢。我会更新

标签: r ggplot2 dplyr correlation tidyverse


【解决方案1】:

我在code review 上得到了答案,我把它放在这里,每个人都可以访问。感谢flodel

这里有两种可能的重写。对于第一个,请注意 Hmisc::rcorr 已经计算了 correlations ($r) 和 p-values ($P) 矩阵,因此您可以从中进行所有工作,前提是您提取权限行 (a_vars) 和列 (b_vars) 并且只保留对角线价值观:

correlations <- rcorr(as.matrix(all_items), type = "spearman")
a_vars <- ends_with("a", vars = names(all_items))
b_vars <- ends_with("b", vars = names(all_items))
all_correlation <- data.frame(
  item    = seq_along(a_vars),
  Result  = diag(correlations$r[a_vars, b_vars]),
  P_Valor = diag(correlations$P[a_vars, b_vars])
)

我不太喜欢这种方法的是它只使用了 rcorr 计算的 36 个相关性中的 3 个。在第二种方法中,我首先将数据拆分为两个表(一个用于 a,一个用于 b),以便通过 Map 仅计算 3 个相关性。我还从 rcorr 切换到了基础 cor.test,它具有(IMO)更直观的行为:当给定两个向量作为输入时,它计算一个相关性,而不是四个相关性。

a <- all_items %>% select(ends_with("a"))
b <- all_items %>% select(ends_with("b"))
cor_test <- Map(cor.test, a, b)
all_correlation <- data.frame(
  item    = seq_along(a),
  Result  = sapply(cor_test, `[[`, "estimate"),
  P_Valor = sapply(cor_test, `[[`, "p.value")
)

【讨论】:

    猜你喜欢
    • 2015-06-12
    • 2021-01-19
    • 2019-02-12
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多