Something mesmerizes me in R:
- Why are they differences in my correlations depending on the function/package I use and
- Which package::function should I choose in what circumstances
Consider the three following examples using the Iris Dataset:
stats::cor.test
cor.test(iris$petal.length, iris$petal.width, method='pearson')
#t = 43.32, df = 148, p-value < 2.2e-16
#cor = 0.9627571
stats::lm
summary(lm(iris$petal.length ~ iris$petal.width))
#(Intercept) 1.09057 0.07294 14.95 <2e-16 ***
#iris$petal.width 2.22589 0.05138 43.32 <2e-16 ***
#Multiple R-squared: 0.9269, Adjusted R-squared: 0.9264
#F-statistic: 1877 on 1 and 148 DF, p-value: < 2.2e-16
correlate(iris$petal.length, iris$petal.width, test=TRUE)
# Correlation
# y.var
#x.var 0.963***
#p-value
# y.var
# x.var 0.000
They all give similar values. For instance, the p-value in this example is always the same. The R is also really close ranging from 0.9264 to 0.963 and is in fact identical for stats::cor.test
and lsr::correlate
.