5

I am creating a plot in R, using:

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

IQ is numeric and isAtheist is boolean, having values TRUE or FALSE.

enter image description here

I have tried to write:

cor(IQ, isAtheist)

But it is gives me an error:

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

How can I determine the correlation between these two variables?

Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86

2 Answers2

5

I do not really know how you want to interpret correlation in this case, but you can try cor(IQ, as.numeric(isAtheist)). In this case TRUE will be 1 and FALSE 0.

Quentin Geissmann
  • 2,240
  • 1
  • 21
  • 36
2

This is what I think you may want (to show the differences in mean IQ value superimposed on the boxplot):

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

The X-position in the default of plot.formula is as.numeric(factor(isAtheist)), i.e. at 1 and 2 rather than at 0 and 1 which was what you were assuming with your use of abline. Makes no sense to extrapolate beyond those values, so I chose to plot as a bounded segment. I will add a worked example and output.

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

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487