Skip to content

Add Constructor to FisherExactTest #156

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/fisher.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ export FisherExactTest

"""
FisherExactTest(a::Integer, b::Integer, c::Integer, d::Integer)
FisherExactTest(x[, y])

Perform Fisher's exact test of the null hypothesis that the success probabilities ``a/c``
and ``b/d`` are equal, that is the odds ratio ``(a/c) / (b/d)`` is one, against the
alternative hypothesis that they are not equal.

If `x` is a matrix with at least two rows and columns, it is taken as a two-dimensional
contingency table. Otherwise, `x` and `y` must be vectors of the same length. The contingency
table is calculated using `counts` function from the `StatsBase` package.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
table is calculated using `counts` function from the `StatsBase` package.
table is calculated using the `counts` function from the `StatsBase` package.

Note that the entries of `x` (and `y` if provided) must be non-negative integers.

See [`pvalue(::FisherExactTest)`](@ref) and [`confint(::FisherExactTest)`](@ref) for details
about the computation of the default p-value and confidence interval, respectively.

Expand Down Expand Up @@ -73,6 +79,15 @@ struct FisherExactTest <: HypothesisTest
end
end

function FisherExactTest(x::AbstractMatrix{T}) where T<:Integer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function FisherExactTest(x::AbstractMatrix{T}) where T<:Integer
function FisherExactTest(x::AbstractMatrix{<:Integer})

FisherExactTest(x[1,1], x[1,2], x[2,1], x[2,2])
end

function FisherExactTest(x::AbstractVector{T}, y::AbstractVector{T}) where T<:Integer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function FisherExactTest(x::AbstractVector{T}, y::AbstractVector{T}) where T<:Integer
function FisherExactTest(x::AbstractVector{<:Integer}, y::AbstractVector{<:Integer})

d = counts(x, y)
FisherExactTest(d[1,1], d[1,2], d[2,1], d[2,2])
end

testname(::FisherExactTest) = "Fisher's exact test"
population_param_of_interest(x::FisherExactTest) = ("Odds ratio", 1.0, x.ω) # parameter of interest: name, value under h0, point estimate
default_tail(test::FisherExactTest) = :both
Expand Down
11 changes: 11 additions & 0 deletions test/fisher.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HypothesisTests, Test
using StatsBase
using HypothesisTests: default_tail

@testset "Fisher" begin
Expand Down Expand Up @@ -107,4 +108,14 @@ show(IOBuffer(), t)
t = HypothesisTests.FisherExactTest(1, 1, 1, 1)
@test HypothesisTests.pvalue(t, tail=:both) <= 1
show(IOBuffer(), t)

# constructor test for matrix and vectors
x=[1,1,1,0,0,0,0,0,1,1]
y=[1,1,0,0,1,0,1,1,1,1]

d = counts(x,y)

FisherExactTest(d)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check that results are the same for both calls, and that they are equal to what you get when passing a, b, c and d manually.

FisherExactTest(x,y)

end