由于点双列相关只是流行的 Peason 积矩系数的一个特例,您可以使用它cor.test
来近似(稍后将详细介绍)连续 X 和二分 Y 之间的相关性。例如,给定以下数据:
set.seed(23049)
x <- rnorm(1e3)
y <- sample(0:1, 1e3, replace = TRUE)
跑步cor.test(x, y)
会给你你想要的信息。
Pearson's product-moment correlation
data: x and y
t = -1.1971, df = 998, p-value = 0.2316
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.09962497 0.02418410
sample estimates:
cor
-0.03786575
作为系数之间相似性的指示,请注意计算出的相关性与给您的-0.03786575
相似性:ltm::biserial.cor
> library(ltm)
> biserial.cor(x, y, level = 2)
[1] -0.03784681
差异在于基于biserial.cor
总体计算的事实,标准偏差除以,n
其中计算样本的标准偏差,除以。cor
cor.test
n - 1
正如 cgage 所指出的,您还可以使用该polyserial()
函数,在我的示例中会产生
> polyserial(x, y, std.err = TRUE)
Polyserial Correlation, 2-step est. = -0.04748 (0.03956)
Test of bivariate normality: Chisquare = 1.891, df = 5, p = 0.864
在这里,我认为计算出的相关性(-0.04748)的差异是由于polyserial
使用优化算法来近似计算(除非 Y 具有两个以上的水平,否则这是不必要的)。