两个皮尔逊相关系数是否不同?

机器算法验证 r 假设检验 相关性 皮尔逊-r
2022-03-16 06:26:40

我在这里知道这个问题,我的问题被列为重复项,但它并没有完全回答我的问题。然而,它确实帮助我进步了一点,所以谢谢,我以前找不到它。上述答案的在线计算器也与vassarstats.net计算器不同意(尽管略有不同),所以我认为这进一步支持了我不使用黑匣子的理由。

所以我会重新解释我的问题,并希望这能得到答案:

我有两个要比较的 Pearson 相关系数。每个来自 2 组 40 个基因相同的系,并且是男性和女性特征值之间的相关性。

我已经做到了zR-在使用该函数时对我的相关系数进行转换,atanh()并使用自制函数对其进行复制RtoZ <- function (r) 0.5*log((1+r)/(1-r))

问题是接下来要做什么:我如何实际测试R两个相关性是否不同?

3个回答

以防万一某人(其他人)必须对多对变量的相关系数进行比较,这里有一个基于 rg255 对复制有用的回复的函数:

cor.diff.test = function(x1, x2, y1, y2, method="pearson") {
  cor1 = cor.test(x1, x2, method=method)
  cor2 = cor.test(y1, y2, method=method)

  r1 = cor1$estimate
  r2 = cor2$estimate
  n1 = sum(complete.cases(x1, x2))
  n2 = sum(complete.cases(y1, y2))
  fisher = ((0.5*log((1+r1)/(1-r1)))-(0.5*log((1+r2)/(1-r2))))/((1/(n1-3))+(1/(n2-3)))^0.5

  p.value = (2*(1-pnorm(abs(fisher))))

  result= list(
    "cor1" = list(
      "estimate" = as.numeric(cor1$estimate),
      "p.value" = cor1$p.value,
      "n" = n1
    ),
    "cor2" = list(
      "estimate" = as.numeric(cor2$estimate),
      "p.value" = cor2$p.value,
      "n" = n2
    ),
    "p.value.twosided" = as.numeric(p.value),
    "p.value.onesided" = as.numeric(p.value) / 2
  )
  cat(paste(sep="",
          "cor1: r=", format(result$cor1$estimate, digits=3), ", p=", format(result$cor1$p.value, digits=3), ", n=", result$cor1$n, "\n",
          "cor2: r=", format(result$cor2$estimate, digits=3), ", p=", format(result$cor2$p.value, digits=3), ", n=", result$cor2$n, "\n",
          "diffence: p(one-sided)=", format(result$p.value.onesided, digits=3), ", p(two-sided)=", format(result$p.value.twosided, digits=3), "\n"
  ))
  return(result);
}

一旦完成 Fisher 的 z 变换,这只是获得 p 值的一个例子

# Correlations    
cor.test (df1$a, df1$b, method = "p")
cor.test (df2$a, df2$b, method = "p")

# function to do fisher transformations 
fisher.z<- function (r1,r2,n1,n2) ((0.5*log((1+r1)/(1-r1)))-(0.5*log((1+r2)/(1-r2))))/((1/(n1-3))+(1/(n2-3)))^0.5

# or this (either version will suffice) 
fisher.z<- function (r1,r2,n1,n2) (atanh(r1) - atanh(r2)) / ((1/(n1-3))+(1/(n2-3)))^0.5

#input n and r from correlations manually (two tailed test)
2*(1-pnorm(abs(fisher.z(r1= ,r2= ,n1= ,n2= ))))

请参阅本演示文稿的最后四张幻灯片pnorm()r 中的函数。

如果人们仍在寻找一种简单的方法来比较两者r皮尔逊相关性。Rpaired.r的包中有一个专门为此调用的函数。psych

用法:

paired.r(xy, xz, yz=NULL, n, n2=NULL,twotailed=TRUE)

或者作为一个简单的例子: For r1and r2and a sample size of njust do:

paired.r(r1,r2,n=n)