数字和布尔变量之间的相关性

Edw*_*its 4 statistics boolean r numeric correlation

我在R中创建一个情节,使用:

plot(IQ, isAtheist)
abline(lm(isAtheist~IQ))
Run Code Online (Sandbox Code Playgroud)

IQ是numericisAtheist布尔值,有值TRUEFALSE.

在此输入图像描述

我试着写:

cor(IQ, isAtheist)
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误:

Error in cor(IQ, isAtheist) : 'x' must be numeric
Run Code Online (Sandbox Code Playgroud)

如何确定这两个变量之间的相关性?

Que*_*ann 5

在这种情况下,我真的不知道你想如何解释相关性,但你可以试试cor(IQ, as.numeric(isAtheist)).在这种情况下,TRUE将为1且为0.


42-*_*42- 2

这就是我认为您可能想要的(显示叠加在箱线图上的平均智商值的差异):

plot(IQ~isAtheist)
lines(x=c(1,2), y=predict( lm(IQ~isAtheist), 
                     newdata=list(isAtheist=c("NO","YES") ) ) ,
       col="red", type="b")
Run Code Online (Sandbox Code Playgroud)

plot.formula 默认情况下的 X 位置是as.numeric(factor(isAtheist)),即在 1 和 2 处,而不是在 0 和 1 处,这是您使用 时所假设的abline。超出这些值进行推断是没有意义的,因此我选择绘制为有界线段。我将添加一个有效的示例和输出。

set.seed(123)
 isAtheist=factor(c("NO","YES")[1+rep( c(0,1), 50 )])
 plot(IQ~isAtheist)
     lines(x=c(1,2), y=predict( lm(IQ~isAtheist), 
                          newdata=data.frame(isAtheist=c("NO","YES") ) ) ,
            col="red", type="b")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述