7

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)

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.