Skip to content

Commit

Permalink
Add correlation test (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
nklb authored Nov 1, 2020
1 parent e11d673 commit 010b551
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/src/multivariate.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This is equivalent to Box's ``M``-test for two groups.
BartlettTest
```

## Partial correlation test
## Correlation and partial correlation test

```@docs
CorrelationTest
Expand Down
17 changes: 15 additions & 2 deletions src/correlation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ using Statistics: clampcor
export CorrelationTest

"""
CorrelationTest(x, y)
Perform a t-test for the hypothesis that ``\\text{Cor}(x,y) = 0``, i.e. the correlation
of vectors `x` and `y` is zero.
CorrelationTest(x, y, Z)
Perform a t-test for the hypothesis that ``\\text{Cor}(x,y|Z=z) = 0``, i.e. the partial
Expand All @@ -30,6 +35,14 @@ struct CorrelationTest{T<:Real} <: HypothesisTest
k::Int
t::T

# Error checking is done in `cor`
function CorrelationTest(x::AbstractVector, y::AbstractVector)
r = cor(x, y)
n = length(x)
t = r * sqrt((n - 2) / (1 - r^2))
return new{typeof(r)}(r, n, 0, t)
end

# Error checking is done in `partialcor`
function CorrelationTest(x::AbstractVector, y::AbstractVector, Z::AbstractMatrix)
r = partialcor(x, y, Z)
Expand All @@ -47,10 +60,10 @@ struct CorrelationTest{T<:Real} <: HypothesisTest
end

testname(p::CorrelationTest) =
string("Test for nonzero ", p.k == 0 ? "partial " : "", " correlation")
string("Test for nonzero ", p.k != 0 ? "partial " : "", "correlation")

function population_param_of_interest(p::CorrelationTest)
param = p.k == 0 ? "Partial correlation" : "Correlation"
param = p.k != 0 ? "Partial correlation" : "Correlation"
(param, zero(p.r), p.r)
end

Expand Down
24 changes: 24 additions & 0 deletions test/correlation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ using Test
using DelimitedFiles
using StatsBase

@testset "Correlation" begin
# Columns are line number, calcium, iron
nutrient = readdlm(joinpath(@__DIR__, "data", "nutrient.txt"))[:, 1:3]
w = CorrelationTest(nutrient[:,2], nutrient[:,3])
let out = sprint(show, w)
@test occursin("reject h_0", out) && !occursin("fail to", out)
end
let ci = confint(w)
@test first(ci) 0.3327138 atol=1e-6
@test last(ci) 0.4546639 atol=1e-6
end
@test nobs(w) == 737
@test dof(w) == 735
@test pvalue(w) < 1e-25

x = CorrelationTest(nutrient[:,1], nutrient[:,2])
@test occursin("fail to reject", sprint(show, x))
let ci = confint(x)
@test first(ci) -0.1105478 atol=1e-6
@test last(ci) 0.0336730 atol=1e-6
end
@test pvalue(x) 0.2948405 atol=1e-6
end

@testset "Partial correlation" begin
# Columns are information, similarities, arithmetic, picture completion
wechsler = readdlm(joinpath(@__DIR__, "data", "wechsler.txt"))[:,2:end]
Expand Down

0 comments on commit 010b551

Please sign in to comment.