Reputation: 2485
I am looking for help to calculate the Pearson correlation coefficient with p-value by using Julia language. The analogous function in Python is scipy.stats.pearson
.
The Julia function below only gives me the correlation. Appreciate your help/hint about the p-value part.
using RDatasets, Statistics
iris = dataset("datasets", "iris");
Statistics.cor(iris.SepalLength, iris.SepalWidth)
Upvotes: 7
Views: 3533
Reputation: 1905
Now you can also use the function pvalue
from HypothesisTests
, for example:
using HypothesisTests
x = [1,2,3]; y = [2,3,5];
pvalue(CorrelationTest(x,y))
This example returns 0.1210377 which is the same than python's scipy.stats.pearsonr
and R's cor.test
.
Upvotes: 4
Reputation: 69949
I do not know about an existing implementation but here is a two-sided test with H0 equal to 0 using Fisher transformation:
using Distributions
cortest(x,y) =
if length(x) == length(y)
2 * ccdf(Normal(), atanh(abs(cor(x, y))) * sqrt(length(x) - 3))
else
error("x and y have different lengths")
end
or use the HypothesisTests.jl package, e.g.:
using HypothesisTests
OneSampleZTest(atanh(cor(iris.SepalLength, iris.SepalWidth)),
1, nrow(iris)-3)
Upvotes: 3