R中点双列相关的P值

机器算法验证 r 相关性 p 值
2022-03-31 06:50:20

有人知道为点双序列相关产生 p 值的 R 包吗?

我已经尝试了所有我知道的主要软件包(在 Google 的帮助下),但没有找到。如果没有想到一个包,有什么方法可以直观地计算 p 值吗?

3个回答

点-双列相关相当于计算一个连续变量和一个二分变量之间的Pearson相关(后者需要用0和1编码)。因此,您可以只使用cor.testR 中的标准函数,它将输出相关性、95% 置信区间和具有相关p值的独立t检验:

set.seed(1)
x <- sample.int(100, 50, replace=TRUE)
y <- sample(c(0, 1), 50, replace=TRUE)
cor.test(x, y)

这产生了相关性r=0.202, 这不显着 (t=1.429,df=48,p=0.1595):

    Pearson's product-moment correlation

data:  x and y
t = 1.429, df = 48, p-value = 0.1595
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.08088534  0.45478598
sample estimates:
      cor 
0.2020105 

正如@sal-mangiafico 和@igor-p 指出的那样,包中的函数biserial.cor产生的ltm结果略有不同。这是因为cor.test使用总体标准差,而biserial.cor使用样本标准差。此外, 的结果与 的结果biserial.cor具有相反的符号cor.test这可以通过在 中指定参数来level=2调整biserial.cor

回应@user9413061,

我想我发现了问题的根源。

双列相关的标准定义中,使用总体标准差。

ltm::biserial.cor使用样本标准差。

在下文中,定义了一个函数来计算总体标准差。定义了函数biserial.cor.new,这ltm::biserial.corsd.pop使用 used 而不是相同sd

我认为biserial.cor.new会返回与cor.test.

sd.pop = function(x){sd(x)*sqrt((length(x)-1)/length(x))}

biserial.cor.new = 
function (x, y, use = c("all.obs", "complete.obs"), level = 1) 
{
    if (!is.numeric(x)) 
        stop("'x' must be a numeric variable.\n")
    y <- as.factor(y)
    if (length(levs <- levels(y)) > 2) 
        stop("'y' must be a dichotomous variable.\n")
    if (length(x) != length(y)) 
        stop("'x' and 'y' do not have the same length")
    use <- match.arg(use)
    if (use == "complete.obs") {
        cc.ind <- complete.cases(x, y)
        x <- x[cc.ind]
        y <- y[cc.ind]
    }
    ind <- y == levs[level]
    diff.mu <- mean(x[ind]) - mean(x[!ind])
    prob <- mean(ind)
    diff.mu * sqrt(prob * (1 - prob))/sd.pop(x)
}

还有一个例子:

x = c(3,4,5,6,7,5,6,7,8,9)
y = c(0,0,0,0,0,1,1,1,1,1)

library(ltm)

### DIFFERENT RESULTS WITH ltm::biserial.cor

biserial.cor(x,y, level=2)

   ### [1] 0.5477226

cor.test(x,y)

   ### Pearson's product-moment correlation
   ### sample estimates:
   ###       cor 
   ### 0.5773503

### SAME RESULTS WITH new function

biserial.cor.new(x,y, level=2)

    ### [1] 0.5773503

cor.test(x,y)

   ### Pearson's product-moment correlation
   ### sample estimates:
   ###       cor 
   ### 0.5773503

据我了解,您不必用 0 和 1 对二分法变量进行编码。因此,使用其他值会产生完全相同的输出。尝试例如:

x <- 1:100
y <- rep(c(0,1), 50)
y2 <- rep(c(-786,345), 50)
cor.test(x, y)
cor.test(x, y2)

两者都为您提供 0.01732137 的 r。通过对二分法变量进行不同编码可能发生的唯一事情是您得到-0.01732137,如果第一个数字大于第二个数字,就会出现这种情况,例如

y3 <- rep(c(0,1), 50)
cor.test(x, y3)

结果为-0.01732137。

此外,我在不同的页面上读到“点双列相关相当于计算连续变量和二分变量之间的 Pearson 相关”,但实际上,如果我对相同的变量进行 Pearson 和点双列相关,我会得到不同的结果数据。一个例子:

x <- 1:100
y <- rep(c(0,1), 50)

cor.test(x, y)

给我 0.01732137,但 biserial.cor(x, y)结果为 -0.01723455。

我理解得到正负值是可以的,但是绝对值应该是一样的,事实并非如此。如果我使用其他数据(例如,x <- rnorm(100, 100, 15)而不是x <- 1:100.

出于这个原因,我不确定是否可以使用cor.test()并报告您已经进行了点双序列相关。