Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multigroup permutations #198

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 117 additions & 0 deletions src/permutation.jl
Expand Up @@ -59,3 +59,120 @@ function show_params(io::IO, apt::PermutationTest, ident)
show(io, apt.samples)
println(io)
end

"""
ExactPermutationTest(data::Vector, f::Function)

Perform a permutation test (a.k.a. randomization test) of the null hypothesis
that `f(data)` is ??? across all groups (typically more than 2 groups). All
possible permutations are sampled.

The function `f` should reduce on the indices that comprise the groups in
the data using the statistics of interest.

# Examples
Say you want to test the difference in means between 3 groups (1,1), (2,2),
and (9,9,9,9), you'll first need to concatenate your data into a vector.
Then you'll need to create a function that accumulatively subtracts the
mean of the 1:2, 3:4, and 5:8 values of a given vector.

```julia-repl

julia> indices = [1:2, 3:4, 5:8] # indices to the vector
3-element Array{UnitRange{Int64},1}:
1:2
3:4
5:8

julia> data = [1,1,2,2,9,9,9,9] # the data vector
8-element Array{Int64,1}:
1
1
2
2
9
9
9
9

julia> f(x) = mapreduce(i -> mean(view(x, i)), -, indices) # calculate the cumalative
# difference between the means of the 1:2, 3:4, and 5:8 elements of vector `x`
f (generic function with 1 method)

julia> ExactPermutationTest(data, f)
Permutation Test
----------------
Population details:
parameter of interest: not implemented yet
value under h_0: NaN
point estimate: NaN

Test summary:
outcome with 95% confidence: fail to reject h_0
p-value: 0.2024

Details:
observation: -10.0
samples: [-10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0, -10.0 … -1.5, -1.5, -1.5, -1.5, -1.5, -1.5, -1.5, -1.5, -1.5, -1.5]
```
"""
function ExactPermutationTest(data::AbstractVector{T},
f::Function) where {T<:Real}
P = permutations(data)
PermutationTest(f(data), f.(P))
end

"""
ApproximatePermutationTest(data::Vector, f::Function, n::Int)

Perform a permutation test (a.k.a. randomization test) of the null hypothesis
that `f(data)` is ??? across all groups (typically more than 2 groups).
`n` of the `factorial(length(x)+length(y))` permutations are sampled at
random.

The function `f` should reduce on the indices that comprise the groups in
the data using the statistics of interest.

# Examples
Say you want to test the difference in means between 3 groups (1,1), (2,2),
and (9,9,9,9), you'll first need to concatenate your data into a vector.
Then you'll need to create a function that accumulatively subtracts the
mean of the 1:2, 3:4, and 5:8 values of a given vector.

```julia-repl
julia> indices = [1:20, 21:40, 41:80]
3-element Array{UnitRange{Int64},1}:
1:20
21:40
41:80

julia> data = [fill(1,20); fill(2, 20); fill(9, 40)];

julia> f(x) = mapreduce(i -> mean(view(x, i)), -, indices) # calculate the cumalative
# difference between the means of the 1:2, 3:4, and 5:8 elements of vector `x`

f (generic function with 1 method)

julia> ApproximatePermutationTest(data, f, 10^5)
Permutation Test
----------------
Population details:
parameter of interest: not implemented yet
value under h_0: NaN
point estimate: NaN

Test summary:
outcome with 95% confidence: reject h_0
p-value: <1e-4

Details:
observation: -10.0
samples: [-4.15, -5.85, -5.975, -5.949999999999999, -2.0749999999999993, -4.6, -4.6499999999999995, -6.05, -7.449999999999999, -5.0 … -6.475, -4.45, -8.575, -5.6000000000000005, -4.075, -5.425, -7.5, -2.825, -5.175, -6.625]
```
"""
function ApproximatePermutationTest(data::AbstractVector{T}, f::Function,
n::Int) where {T<:Real}
observation = f(data)
samples = [(shuffle!(data); f(data)) for _ in 1:n]
PermutationTest(observation, samples)
end
37 changes: 27 additions & 10 deletions test/permutation.jl
@@ -1,14 +1,31 @@
using HypothesisTests, Test

@testset "Permutation" begin
@test isapprox(pvalue(ExactPermutationTest([1,2,3],[4,5,6],mean), tail=:both), 0.1)
@test isapprox(pvalue(ExactPermutationTest([4,5,6],[1,2,3],mean), tail=:both), 0.1)
@test isapprox(pvalue(ExactPermutationTest([1,2,3],[4,5,6],mean), tail=:left), 0.05)
@test isapprox(pvalue(ExactPermutationTest([4,5,6],[1,2,3],mean), tail=:right), 0.05)

Random.seed!(12345)
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3],[4,5,6],mean,100), tail=:both), 0.09)
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6],[1,2,3],mean,100), tail=:both), 0.04)
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3],[4,5,6],mean,100), tail=:left), 0.08)
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6],[1,2,3],mean,100), tail=:right), 0.07)
@testset "Two groups" begin
@test isapprox(pvalue(ExactPermutationTest([1,2,3],[4,5,6],mean), tail=:both), 0.1)
@test isapprox(pvalue(ExactPermutationTest([4,5,6],[1,2,3],mean), tail=:both), 0.1)
@test isapprox(pvalue(ExactPermutationTest([1,2,3],[4,5,6],mean), tail=:left), 0.05)
@test isapprox(pvalue(ExactPermutationTest([4,5,6],[1,2,3],mean), tail=:right), 0.05)

Random.seed!(12345)
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3],[4,5,6],mean,100), tail=:both), 0.09)
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6],[1,2,3],mean,100), tail=:both), 0.04)
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3],[4,5,6],mean,100), tail=:left), 0.08)
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6],[1,2,3],mean,100), tail=:right), 0.07)
end

@testset "Multi groups" begin
indices = [1:3, 4:6]
f(x) = mapreduce(i -> mean(view(x, i)), -, indices)
@test isapprox(pvalue(ExactPermutationTest([1,2,3,4,5,6],f), tail=:both), 0.1)
@test isapprox(pvalue(ExactPermutationTest([4,5,6,1,2,3],f), tail=:both), 0.1)
@test isapprox(pvalue(ExactPermutationTest([1,2,3,4,5,6],f), tail=:left), 0.05)
@test isapprox(pvalue(ExactPermutationTest([4,5,6,1,2,3],f), tail=:right), 0.05)

Random.seed!(12345)
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3,4,5,6],f,100), tail=:both), 0.09)
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6,1,2,3],f,100), tail=:both), 0.04)
@test isapprox(pvalue(ApproximatePermutationTest([1,2,3,4,5,6],f,100), tail=:left), 0.08)
@test isapprox(pvalue(ApproximatePermutationTest([4,5,6,1,2,3],f,100), tail=:right), 0.07)
end
end