【问题标题】:How to use Julia to compute the pearson correlation coefficient with p-value?如何使用 Julia 计算具有 p 值的 pearson 相关系数?
【发布时间】:2018-11-16 21:34:17
【问题描述】:

我正在寻求帮助,以使用 Julia 语言计算具有 p 值的 Pearson 相关系数。 Python中的类似函数是scipy.stats.pearson

下面的 Julia 函数只给出了相关性。感谢您对 p 值部分的帮助/提示。

using RDatasets, Statistics
iris = dataset("datasets", "iris");
Statistics.cor(iris.SepalLength, iris.SepalWidth)

【问题讨论】:

    标签: julia


    【解决方案1】:

    我不知道现有的实现,但这里有一个使用Fisher transformation 的 H0 等于 0 的双面测试:

    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
    

    或使用 HypothesisTests.jl 包,例如:

    using HypothesisTests
    
    OneSampleZTest(atanh(cor(iris.SepalLength, iris.SepalWidth)),
                   1, nrow(iris)-3)
    

    【讨论】:

      【解决方案2】:

      现在还可以使用HypothesisTests中的函数pvalue,例如:

       using HypothesisTests
       x = [1,2,3]; y = [2,3,5];
       pvalue(CorrelationTest(x,y))
      

      此示例返回 0.1210377,与 python 的 scipy.stats.pearsonr 和 R 的 cor.test 相同。

      【讨论】:

        猜你喜欢
        • 2012-11-19
        • 2020-08-18
        • 2022-11-27
        • 2019-12-10
        • 2021-10-30
        • 2016-05-09
        • 2019-02-01
        • 1970-01-01
        • 2019-04-22
        相关资源
        最近更新 更多