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

问题描述 投票:4回答:2

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

plot(IQ, isAtheist)
abline(lm(isAtheist~IQ))

IQ是numericisAtheist是布尔值,值为TRUEFALSE

在此输入图像描述

我试着写:

cor(IQ, isAtheist)

但它给了我一个错误:

Error in cor(IQ, isAtheist) : 'x' must be numeric

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

r statistics boolean numeric correlation
2个回答
5
投票

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


2
投票

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

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

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")

在此输入图像描述

© www.soinside.com 2019 - 2024. All rights reserved.