From ef60cb7fe2753e6db21fd13bf782da39c348bf37 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 2 Jun 2017 18:01:26 +0200 Subject: [PATCH 01/18] Subtypes of HypothesisTest have an optional tail field --- src/HypothesisTests.jl | 12 ++++++++++-- src/t.jl | 27 +++++++++++++++------------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index 670b961a..7d9c318b 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -68,6 +68,14 @@ function check_alpha(alpha::Float64) end end +function get_tail(test::HypothesisTest) + if :tail in fieldnames(test) + getfield(test, :tail) + else + default_tail(test) + end +end + # Pretty-print function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, testname(test)) @@ -86,9 +94,9 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io) # test summary - p = pvalue(test) + tail = get_tail(test) + p = pvalue(test, tail=tail) outcome = if p > 0.05 "fail to reject" else "reject" end - tail = default_tail(test) println(io, "Test summary:") println(io, " outcome with 95% confidence: $outcome h_0") if tail == :both diff --git a/src/t.jl b/src/t.jl index 313ccbd7..92578ae6 100644 --- a/src/t.jl +++ b/src/t.jl @@ -33,13 +33,13 @@ pvalue(x::TTest; tail=:both) = pvalue(TDist(x.df), x.t; tail=tail) default_tail(test::TTest) = :both # confidence interval by inversion -function StatsBase.confint(x::TTest, alpha::Float64=0.05; tail=:both) +function StatsBase.confint(x::TTest, alpha::Float64=0.05; tail=get_tail(x)) check_alpha(alpha) if tail == :left - (-Inf, StatsBase.confint(x, alpha*2)[2]) + (-Inf, StatsBase.confint(x, alpha*2, tail=:both)[2]) # tail=:both required as recursive anchor elseif tail == :right - (StatsBase.confint(x, alpha*2)[1], Inf) + (StatsBase.confint(x, alpha*2, tail=:both)[1], Inf) elseif tail == :both q = quantile(TDist(x.df), 1-alpha/2) (x.xbar-q*x.stderr, x.xbar+q*x.stderr) @@ -58,6 +58,7 @@ immutable OneSampleTTest <: TTest stderr::Real # empirical standard error t::Real # t-statistic μ0::Real # mean under h_0 + tail::Symbol # :left, :right, or :both end testname(::OneSampleTTest) = "One sample t-test" @@ -70,19 +71,19 @@ function show_params(io::IO, x::OneSampleTTest, ident="") println(io, ident, "empirical standard error: $(x.stderr)") end -function OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real=0) +function OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real=0; tail::Symbol=:both) stderr = stddev/sqrt(n) t = (xbar-μ0)/stderr df = n-1 - OneSampleTTest(n, xbar, df, stderr, t, μ0) + OneSampleTTest(n, xbar, df, stderr, t, μ0, tail) end -OneSampleTTest{T<:Real}(v::AbstractVector{T}, μ0::Real=0) = OneSampleTTest(mean(v), std(v), length(v), μ0) +OneSampleTTest{T<:Real}(v::AbstractVector{T}, μ0::Real=0; tail::Symbol=:both) = OneSampleTTest(mean(v), std(v), length(v), μ0, tail=tail) -function OneSampleTTest{T<:Real, S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0) +function OneSampleTTest{T<:Real, S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; tail::Symbol=:both) check_same_length(x, y) - OneSampleTTest(x - y, μ0) + OneSampleTTest(x - y, μ0, tail=tail) end @@ -96,6 +97,7 @@ immutable EqualVarianceTTest <: TwoSampleTTest stderr::Real # empirical standard error t::Real # t-statistic μ0::Real # mean difference under h_0 + tail::Symbol # :left, :right, or :both end function show_params(io::IO, x::TwoSampleTTest, ident="") @@ -108,14 +110,14 @@ end testname(::EqualVarianceTTest) = "Two sample t-test (equal variance)" population_param_of_interest(x::TwoSampleTTest) = ("Mean difference", x.μ0, x.xbar) # parameter of interest: name, value under h0, point estimate -function EqualVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0) +function EqualVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; tail::Symbol=:both) nx, ny = length(x), length(y) xbar = mean(x) - mean(y) stddev = sqrt(((nx - 1) * var(x) + (ny - 1) * var(y)) / (nx + ny - 2)) stderr = stddev * sqrt(1/nx + 1/ny) t = (xbar - μ0) / stderr df = nx + ny - 2 - EqualVarianceTTest(nx, ny, xbar, df, stderr, t, μ0) + EqualVarianceTTest(nx, ny, xbar, df, stderr, t, μ0, tail) end @@ -129,16 +131,17 @@ immutable UnequalVarianceTTest <: TwoSampleTTest stderr::Real # empirical standard error t::Real # t-statistic μ0::Real # mean under h_0 + tail::Symbol # :left, :right, or :both end testname(::UnequalVarianceTTest) = "Two sample t-test (unequal variance)" -function UnequalVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0) +function UnequalVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; tail::Symbol=:both) nx, ny = length(x), length(y) xbar = mean(x)-mean(y) varx, vary = var(x), var(y) stderr = sqrt(varx/nx + vary/ny) t = (xbar-μ0)/stderr df = (varx / nx + vary / ny)^2 / ((varx / nx)^2 / (nx - 1) + (vary / ny)^2 / (ny - 1)) - UnequalVarianceTTest(nx, ny, xbar, df, stderr, t, μ0) + UnequalVarianceTTest(nx, ny, xbar, df, stderr, t, μ0, tail) end From 5f82c9cd815e48eb9d8634ab9205cd79e2dd3f3d Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 2 Jun 2017 18:19:21 +0200 Subject: [PATCH 02/18] Subtypes of HypothesisTest have an optional alpha field --- src/HypothesisTests.jl | 16 +++++++++++++--- src/t.jl | 27 +++++++++++++++++---------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index 7d9c318b..e4e0dcca 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -76,10 +76,20 @@ function get_tail(test::HypothesisTest) end end +function get_alpha(test::HypothesisTest) + if :alpha in fieldnames(test) + getfield(test, :alpha) + else + 0.05 + end +end + # Pretty-print function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, testname(test)) println(io, repeat("-", length(testname(test)))) + + conf_string = string((1 - get_alpha(test)) * 100) * "%" # population details has_ci = applicable(StatsBase.confint, test) @@ -89,16 +99,16 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, " value under h_0: $param_under_h0") println(io, " point estimate: $param_estimate") if has_ci - println(io, " 95% confidence interval: $(StatsBase.confint(test))") + println(io, " $conf_string confidence interval: $(StatsBase.confint(test))") # TODO indent all details like this one end println(io) # test summary tail = get_tail(test) p = pvalue(test, tail=tail) - outcome = if p > 0.05 "fail to reject" else "reject" end + outcome = if p > get_alpha(test) "fail to reject" else "reject" end println(io, "Test summary:") - println(io, " outcome with 95% confidence: $outcome h_0") + println(io, " outcome with $conf_string confidence: $outcome h_0") # TODO indent all details like this one if tail == :both println(io, " two-sided p-value: $p") elseif tail == :left || tail == :right diff --git a/src/t.jl b/src/t.jl index 92578ae6..dcadfbf8 100644 --- a/src/t.jl +++ b/src/t.jl @@ -33,7 +33,7 @@ pvalue(x::TTest; tail=:both) = pvalue(TDist(x.df), x.t; tail=tail) default_tail(test::TTest) = :both # confidence interval by inversion -function StatsBase.confint(x::TTest, alpha::Float64=0.05; tail=get_tail(x)) +function StatsBase.confint(x::TTest, alpha::Float64=get_alpha(x); tail=get_tail(x)) check_alpha(alpha) if tail == :left @@ -59,6 +59,7 @@ immutable OneSampleTTest <: TTest t::Real # t-statistic μ0::Real # mean under h_0 tail::Symbol # :left, :right, or :both + alpha::Real # alpha value end testname(::OneSampleTTest) = "One sample t-test" @@ -71,19 +72,21 @@ function show_params(io::IO, x::OneSampleTTest, ident="") println(io, ident, "empirical standard error: $(x.stderr)") end -function OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real=0; tail::Symbol=:both) +function OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real=0; tail::Symbol=:both, alpha::Real=0.05) stderr = stddev/sqrt(n) t = (xbar-μ0)/stderr df = n-1 - OneSampleTTest(n, xbar, df, stderr, t, μ0, tail) + OneSampleTTest(n, xbar, df, stderr, t, μ0, tail, alpha) end -OneSampleTTest{T<:Real}(v::AbstractVector{T}, μ0::Real=0; tail::Symbol=:both) = OneSampleTTest(mean(v), std(v), length(v), μ0, tail=tail) +OneSampleTTest{T<:Real}(v::AbstractVector{T}, μ0::Real=0; tail::Symbol=:both, alpha::Real=0.05) = + OneSampleTTest(mean(v), std(v), length(v), μ0, tail=tail, alpha=alpha) -function OneSampleTTest{T<:Real, S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; tail::Symbol=:both) +function OneSampleTTest{T<:Real, S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; + tail::Symbol=:both, alpha::Real=0.05) check_same_length(x, y) - OneSampleTTest(x - y, μ0, tail=tail) + OneSampleTTest(x - y, μ0, tail=tail, alpha=alpha) end @@ -98,6 +101,7 @@ immutable EqualVarianceTTest <: TwoSampleTTest t::Real # t-statistic μ0::Real # mean difference under h_0 tail::Symbol # :left, :right, or :both + alpha::Real # alpha value end function show_params(io::IO, x::TwoSampleTTest, ident="") @@ -110,14 +114,15 @@ end testname(::EqualVarianceTTest) = "Two sample t-test (equal variance)" population_param_of_interest(x::TwoSampleTTest) = ("Mean difference", x.μ0, x.xbar) # parameter of interest: name, value under h0, point estimate -function EqualVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; tail::Symbol=:both) +function EqualVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; + tail::Symbol=:both, alpha::Real=0.05) nx, ny = length(x), length(y) xbar = mean(x) - mean(y) stddev = sqrt(((nx - 1) * var(x) + (ny - 1) * var(y)) / (nx + ny - 2)) stderr = stddev * sqrt(1/nx + 1/ny) t = (xbar - μ0) / stderr df = nx + ny - 2 - EqualVarianceTTest(nx, ny, xbar, df, stderr, t, μ0, tail) + EqualVarianceTTest(nx, ny, xbar, df, stderr, t, μ0, tail, alpha) end @@ -132,16 +137,18 @@ immutable UnequalVarianceTTest <: TwoSampleTTest t::Real # t-statistic μ0::Real # mean under h_0 tail::Symbol # :left, :right, or :both + alpha::Real # alpha value end testname(::UnequalVarianceTTest) = "Two sample t-test (unequal variance)" -function UnequalVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; tail::Symbol=:both) +function UnequalVarianceTTest{T<:Real,S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0; + tail::Symbol=:both, alpha::Real=0.05) nx, ny = length(x), length(y) xbar = mean(x)-mean(y) varx, vary = var(x), var(y) stderr = sqrt(varx/nx + vary/ny) t = (xbar-μ0)/stderr df = (varx / nx + vary / ny)^2 / ((varx / nx)^2 / (nx - 1) + (vary / ny)^2 / (ny - 1)) - UnequalVarianceTTest(nx, ny, xbar, df, stderr, t, μ0, tail) + UnequalVarianceTTest(nx, ny, xbar, df, stderr, t, μ0, tail, alpha) end From 14f15d1e9ab5dc21897fad8fa176b637163997d1 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 2 Jun 2017 18:50:47 +0200 Subject: [PATCH 03/18] Indent all labels of details in Base.show by the same length --- src/HypothesisTests.jl | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index e4e0dcca..3f42cffa 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -84,6 +84,9 @@ function get_alpha(test::HypothesisTest) end end +# Utility for pretty-printing: Append white space so that length(with_trailing_whitespace(s)) = max(len, length(s)) +with_trailing_whitespace(s::String, len::Int) = s * join(repeat([" "], outer=max(len - length(s), 0)), "") + # Pretty-print function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, testname(test)) @@ -93,13 +96,14 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) # population details has_ci = applicable(StatsBase.confint, test) + label_len = has_ci ? length(conf_string) + 21 : 22 # 21 is length of ' confidence interval:', 22 that of 'parameter of interest:' (param_name, param_under_h0, param_estimate) = population_param_of_interest(test) println(io, "Population details:") - println(io, " parameter of interest: $param_name") - println(io, " value under h_0: $param_under_h0") - println(io, " point estimate: $param_estimate") + println(io, " $(with_trailing_whitespace("parameter of interest:", label_len)) $param_name") + println(io, " $(with_trailing_whitespace("value under h_0", label_len)) $param_under_h0") + println(io, " $(with_trailing_whitespace("point estimate", label_len)) $param_estimate") if has_ci - println(io, " $conf_string confidence interval: $(StatsBase.confint(test))") # TODO indent all details like this one + println(io, " $conf_string confidence interval: $(StatsBase.confint(test))") end println(io) @@ -108,13 +112,14 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) p = pvalue(test, tail=tail) outcome = if p > get_alpha(test) "fail to reject" else "reject" end println(io, "Test summary:") - println(io, " outcome with $conf_string confidence: $outcome h_0") # TODO indent all details like this one + label_len = length(conf_string) + 25 # 25 is length of 'outcome with confidence:' + println(io, " outcome with $conf_string confidence: $outcome h_0") if tail == :both - println(io, " two-sided p-value: $p") + println(io, " $(with_trailing_whitespace("two-sided p-value:", label_len)) $p") elseif tail == :left || tail == :right - println(io, " one-sided p-value: $p") + println(io, " $(with_trailing_whitespace("one-sided p-value:", label_len)) $p") else - println(io, " p-value: $p") + println(io, " $(with_trailing_whitespace("p-value:", label_len)) $p") end println(io) From c3363a1966941df73ff233fd2b9345baf3341f9a Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 2 Jun 2017 19:00:29 +0200 Subject: [PATCH 04/18] get_tail and get_alpha reuse the same function getting a field value with default --- src/HypothesisTests.jl | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index 3f42cffa..d7f36cb3 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -68,21 +68,16 @@ function check_alpha(alpha::Float64) end end -function get_tail(test::HypothesisTest) - if :tail in fieldnames(test) - getfield(test, :tail) +# Utility to get an optional field (e.g., :tail and :alpha) +getfield(value::HypothesisTest, name::Symbol, default::Any) = + if name in fieldnames(value) + Base.getfield(value, name) else - default_tail(test) + default end -end -function get_alpha(test::HypothesisTest) - if :alpha in fieldnames(test) - getfield(test, :alpha) - else - 0.05 - end -end +get_tail(test::HypothesisTest) = getfield(test, :tail, default_tail(test)) +get_alpha(test::HypothesisTest) = getfield(test, :alpha, 0.05) # Utility for pretty-printing: Append white space so that length(with_trailing_whitespace(s)) = max(len, length(s)) with_trailing_whitespace(s::String, len::Int) = s * join(repeat([" "], outer=max(len - length(s), 0)), "") From 596444fd8ec92e54f6968f44c324b8d4633a580c Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 2 Jun 2017 19:52:49 +0200 Subject: [PATCH 05/18] Fixed test run by providing optional tail parameter to all pvalue functions --- src/augmented_dickey_fuller.jl | 3 ++- src/box_test.jl | 4 ++-- src/breusch_godfrey.jl | 2 +- src/circular.jl | 5 ++++- src/jarque_bera.jl | 2 +- src/kruskal_wallis.jl | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/augmented_dickey_fuller.jl b/src/augmented_dickey_fuller.jl index 3282964e..cdd3dc46 100644 --- a/src/augmented_dickey_fuller.jl +++ b/src/augmented_dickey_fuller.jl @@ -207,6 +207,7 @@ end testname(::ADFTest) = "Augmented Dickey-Fuller unit root test" population_param_of_interest(x::ADFTest) = ("coefficient on lagged non-differenced variable", 0, x.coef) +default_tail(test::ADFTest) = :left function show_params(io::IO, x::ADFTest, ident) println(io, ident, "sample size in regression: ", x.n) @@ -215,4 +216,4 @@ function show_params(io::IO, x::ADFTest, ident) println(io, ident, "Critical values at 1%, 5%, and 10%: ", x.cv') end -pvalue(x::ADFTest) = HypothesisTests.pvalue(Normal(0, 1), adf_pv_aux(x.stat, x.deterministic); tail=:left) +pvalue(x::ADFTest; tail=default_tail(x)) = HypothesisTests.pvalue(Normal(0, 1), adf_pv_aux(x.stat, x.deterministic); tail=tail) diff --git a/src/box_test.jl b/src/box_test.jl index c3f43380..32b09571 100644 --- a/src/box_test.jl +++ b/src/box_test.jl @@ -71,7 +71,7 @@ function show_params(io::IO, x::BoxPierceTest, ident) println(io, ident, "Q statistic: ", x.Q) end -pvalue(x::BoxPierceTest) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=:right) +pvalue(x::BoxPierceTest; tail=default_tail(x)) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) #Ljung-Box test @@ -120,4 +120,4 @@ function show_params(io::IO, x::LjungBoxTest, ident) println(io, ident, "Q statistic: ", x.Q) end -pvalue(x::LjungBoxTest) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=:right) +pvalue(x::LjungBoxTest; tail=default_tail(x)) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) diff --git a/src/breusch_godfrey.jl b/src/breusch_godfrey.jl index 9d0d2411..556b643d 100644 --- a/src/breusch_godfrey.jl +++ b/src/breusch_godfrey.jl @@ -76,4 +76,4 @@ function show_params(io::IO, x::BreuschGodfreyTest, ident) println(io, ident, "T*R^2 statistic: ", x.BG) end -pvalue(x::BreuschGodfreyTest) = pvalue(Chisq(x.lag), x.BG; tail=:right) +pvalue(x::BreuschGodfreyTest; tail=default_tail(x)) = pvalue(Chisq(x.lag), x.BG; tail=tail) diff --git a/src/circular.jl b/src/circular.jl index 51cbdbcf..f5bc3424 100644 --- a/src/circular.jl +++ b/src/circular.jl @@ -60,7 +60,10 @@ function show_params(io::IO, x::RayleighTest, ident="") println(io, ident, "test statistic: $(x.Rbar^2 * x.n)") end -function pvalue(x::RayleighTest) +function pvalue(x::RayleighTest; tail=default_tail(x)) + if tail != default_tail(x) # warn that tail is not used here + warn("tail=$tail has no effect on the computation of the p value") + end Z = x.Rbar^2 * x.n x.n > 1e6 ? exp(-Z) : exp(-Z)*(1+(2*Z-Z^2)/(4*x.n)-(24*Z - 132*Z^2 + 76*Z^3 - 9*Z^4)/(288*x.n^2)) diff --git a/src/jarque_bera.jl b/src/jarque_bera.jl index c55abfe8..2bb19716 100644 --- a/src/jarque_bera.jl +++ b/src/jarque_bera.jl @@ -84,4 +84,4 @@ function show_params(io::IO, x::JarqueBeraTest, ident) println(io, ident, "JB statistic: ", x.JB) end -pvalue(x::JarqueBeraTest) = pvalue(Chisq(2), x.JB; tail=:right) +pvalue(x::JarqueBeraTest; tail=default_tail(x)) = pvalue(Chisq(2), x.JB; tail=tail) diff --git a/src/kruskal_wallis.jl b/src/kruskal_wallis.jl index 677e0013..f0c3f236 100644 --- a/src/kruskal_wallis.jl +++ b/src/kruskal_wallis.jl @@ -53,7 +53,7 @@ function show_params(io::IO, x::KruskalWallisTest, ident) println(io, ident, "adjustment for ties: ", x.tie_adjustment) end -pvalue(x::KruskalWallisTest) = pvalue(Chisq(x.df), x.H; tail=:right) +pvalue(x::KruskalWallisTest; tail=default_tail(x)) = pvalue(Chisq(x.df), x.H; tail=tail) ## helper From 15282b550da4ca328f1c3efc41b9a34cc59be998 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Tue, 6 Jun 2017 15:54:10 +0200 Subject: [PATCH 06/18] Confidence interval in Base.show respects alpha parameter --- src/HypothesisTests.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index d7f36cb3..4e9c6fec 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -87,7 +87,9 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, testname(test)) println(io, repeat("-", length(testname(test)))) - conf_string = string((1 - get_alpha(test)) * 100) * "%" + alpha = get_alpha(test) + tail = get_tail(test) + conf_string = string((1 - alpha) * 100) * "%" # population details has_ci = applicable(StatsBase.confint, test) @@ -98,14 +100,13 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, " $(with_trailing_whitespace("value under h_0", label_len)) $param_under_h0") println(io, " $(with_trailing_whitespace("point estimate", label_len)) $param_estimate") if has_ci - println(io, " $conf_string confidence interval: $(StatsBase.confint(test))") + println(io, " $conf_string confidence interval: $(StatsBase.confint(test, alpha, tail=tail))") end println(io) # test summary - tail = get_tail(test) p = pvalue(test, tail=tail) - outcome = if p > get_alpha(test) "fail to reject" else "reject" end + outcome = if p > alpha "fail to reject" else "reject" end println(io, "Test summary:") label_len = length(conf_string) + 25 # 25 is length of 'outcome with confidence:' println(io, " outcome with $conf_string confidence: $outcome h_0") From 8007b1e01c5fdd95de18d81b320d05400663adea Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Wed, 14 Jun 2017 15:59:22 +0200 Subject: [PATCH 07/18] Optional tail parameter of pvalue is initialized with hard-coded default instead of calling default_tail --- src/augmented_dickey_fuller.jl | 3 ++- src/box_test.jl | 4 ++-- src/breusch_godfrey.jl | 2 +- src/circular.jl | 6 +++--- src/jarque_bera.jl | 2 +- src/kruskal_wallis.jl | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/augmented_dickey_fuller.jl b/src/augmented_dickey_fuller.jl index cdd3dc46..28709827 100644 --- a/src/augmented_dickey_fuller.jl +++ b/src/augmented_dickey_fuller.jl @@ -216,4 +216,5 @@ function show_params(io::IO, x::ADFTest, ident) println(io, ident, "Critical values at 1%, 5%, and 10%: ", x.cv') end -pvalue(x::ADFTest; tail=default_tail(x)) = HypothesisTests.pvalue(Normal(0, 1), adf_pv_aux(x.stat, x.deterministic); tail=tail) +pvalue(x::ADFTest; tail=:left) = # :left is default tail + HypothesisTests.pvalue(Normal(0, 1), adf_pv_aux(x.stat, x.deterministic); tail=tail) diff --git a/src/box_test.jl b/src/box_test.jl index 32b09571..8601e99b 100644 --- a/src/box_test.jl +++ b/src/box_test.jl @@ -71,7 +71,7 @@ function show_params(io::IO, x::BoxPierceTest, ident) println(io, ident, "Q statistic: ", x.Q) end -pvalue(x::BoxPierceTest; tail=default_tail(x)) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) +pvalue(x::BoxPierceTest; tail=:right) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) #Ljung-Box test @@ -120,4 +120,4 @@ function show_params(io::IO, x::LjungBoxTest, ident) println(io, ident, "Q statistic: ", x.Q) end -pvalue(x::LjungBoxTest; tail=default_tail(x)) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) +pvalue(x::LjungBoxTest; tail=:right) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) diff --git a/src/breusch_godfrey.jl b/src/breusch_godfrey.jl index 556b643d..faf178fb 100644 --- a/src/breusch_godfrey.jl +++ b/src/breusch_godfrey.jl @@ -76,4 +76,4 @@ function show_params(io::IO, x::BreuschGodfreyTest, ident) println(io, ident, "T*R^2 statistic: ", x.BG) end -pvalue(x::BreuschGodfreyTest; tail=default_tail(x)) = pvalue(Chisq(x.lag), x.BG; tail=tail) +pvalue(x::BreuschGodfreyTest; tail=:right) = pvalue(Chisq(x.lag), x.BG; tail=tail) diff --git a/src/circular.jl b/src/circular.jl index f5bc3424..21756df9 100644 --- a/src/circular.jl +++ b/src/circular.jl @@ -60,9 +60,9 @@ function show_params(io::IO, x::RayleighTest, ident="") println(io, ident, "test statistic: $(x.Rbar^2 * x.n)") end -function pvalue(x::RayleighTest; tail=default_tail(x)) - if tail != default_tail(x) # warn that tail is not used here - warn("tail=$tail has no effect on the computation of the p value") +function pvalue(x::RayleighTest; tail=:both) # :both is default tail + if tail != :both # warn that tail is not used here + throw(ArgumentError(":both is the only valid value for the tail of a RayleighTest")) end Z = x.Rbar^2 * x.n x.n > 1e6 ? exp(-Z) : diff --git a/src/jarque_bera.jl b/src/jarque_bera.jl index 2bb19716..b4d5403b 100644 --- a/src/jarque_bera.jl +++ b/src/jarque_bera.jl @@ -84,4 +84,4 @@ function show_params(io::IO, x::JarqueBeraTest, ident) println(io, ident, "JB statistic: ", x.JB) end -pvalue(x::JarqueBeraTest; tail=default_tail(x)) = pvalue(Chisq(2), x.JB; tail=tail) +pvalue(x::JarqueBeraTest; tail=:right) = pvalue(Chisq(2), x.JB; tail=tail) diff --git a/src/kruskal_wallis.jl b/src/kruskal_wallis.jl index f0c3f236..aca9eeec 100644 --- a/src/kruskal_wallis.jl +++ b/src/kruskal_wallis.jl @@ -53,7 +53,7 @@ function show_params(io::IO, x::KruskalWallisTest, ident) println(io, ident, "adjustment for ties: ", x.tie_adjustment) end -pvalue(x::KruskalWallisTest; tail=default_tail(x)) = pvalue(Chisq(x.df), x.H; tail=tail) +pvalue(x::KruskalWallisTest; tail=:right) = pvalue(Chisq(x.df), x.H; tail=tail) ## helper From e63c378f103dd7f224b839f7dbd6d80e459ac97a Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Wed, 14 Jun 2017 16:28:15 +0200 Subject: [PATCH 08/18] Pretty-printing with fixed detail line length --- src/HypothesisTests.jl | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index d7f36cb3..c836ed67 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -79,26 +79,25 @@ getfield(value::HypothesisTest, name::Symbol, default::Any) = get_tail(test::HypothesisTest) = getfield(test, :tail, default_tail(test)) get_alpha(test::HypothesisTest) = getfield(test, :alpha, 0.05) -# Utility for pretty-printing: Append white space so that length(with_trailing_whitespace(s)) = max(len, length(s)) -with_trailing_whitespace(s::String, len::Int) = s * join(repeat([" "], outer=max(len - length(s), 0)), "") - # Pretty-print function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, testname(test)) println(io, repeat("-", length(testname(test)))) - conf_string = string((1 - get_alpha(test)) * 100) * "%" + # utilities for pretty-printing + conf_string = string(floor((1 - get_alpha(test)) * 100, 6)) # limit to 6 decimals in % + prettify_detail(label::String, value::Any, len::Int) = # len is max length of label + " " * label * " "^max(len - length(label), 0) * string(value) # population details has_ci = applicable(StatsBase.confint, test) - label_len = has_ci ? length(conf_string) + 21 : 22 # 21 is length of ' confidence interval:', 22 that of 'parameter of interest:' (param_name, param_under_h0, param_estimate) = population_param_of_interest(test) println(io, "Population details:") - println(io, " $(with_trailing_whitespace("parameter of interest:", label_len)) $param_name") - println(io, " $(with_trailing_whitespace("value under h_0", label_len)) $param_under_h0") - println(io, " $(with_trailing_whitespace("point estimate", label_len)) $param_estimate") + println(io, prettify_detail("parameter of interest:", param_name, 32)) + println(io, prettify_detail("value under h_0:", param_under_h0, 32)) + println(io, prettify_detail("point estimate:", param_estimate, 32)) if has_ci - println(io, " $conf_string confidence interval: $(StatsBase.confint(test))") + println(io, prettify_detail(conf_string*"% confidence interval:", StatsBase.confint(test), 32)) end println(io) @@ -106,16 +105,13 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) tail = get_tail(test) p = pvalue(test, tail=tail) outcome = if p > get_alpha(test) "fail to reject" else "reject" end + tailvalue = + if tail == :both "two-sided p-value:" + elseif tail == :left || tail == :right "one-sided p-value ($(string(tail)) tail):" + else "p-value:" end println(io, "Test summary:") - label_len = length(conf_string) + 25 # 25 is length of 'outcome with confidence:' - println(io, " outcome with $conf_string confidence: $outcome h_0") - if tail == :both - println(io, " $(with_trailing_whitespace("two-sided p-value:", label_len)) $p") - elseif tail == :left || tail == :right - println(io, " $(with_trailing_whitespace("one-sided p-value:", label_len)) $p") - else - println(io, " $(with_trailing_whitespace("p-value:", label_len)) $p") - end + println(io, prettify_detail("outcome with "*conf_string*"% confidence:", outcome*" h_0", 36)) + println(io, prettify_detail(tailvalue, p, 36)) println(io) # further details From c882e25d1f7e16184f554c4ca66313c70f942743 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Wed, 14 Jun 2017 17:07:11 +0200 Subject: [PATCH 09/18] Fallback-function approach to tail and alpha. The default_tail function was renamed to tail and overloaded for the different defaults or field access (in the case of t-tests). --- src/HypothesisTests.jl | 22 ++++++---------------- src/anderson_darling.jl | 4 ++-- src/augmented_dickey_fuller.jl | 2 +- src/binomial.jl | 4 ++-- src/box_test.jl | 4 ++-- src/breusch_godfrey.jl | 2 +- src/circular.jl | 6 +++--- src/fisher.jl | 2 +- src/jarque_bera.jl | 2 +- src/kolmogorov_smirnov.jl | 2 +- src/kruskal_wallis.jl | 2 +- src/mann_whitney.jl | 4 ++-- src/power_divergence.jl | 2 +- src/t.jl | 8 ++++---- src/wilcoxon.jl | 4 ++-- src/z.jl | 2 +- test/anderson_darling.jl | 6 +++--- test/binomial.jl | 6 +++--- test/box_test.jl | 6 +++--- test/breusch_godfrey.jl | 4 ++-- test/circular.jl | 8 ++++---- test/fisher.jl | 4 ++-- test/jarque_bera.jl | 6 +++--- test/kolmogorov_smirnov.jl | 6 +++--- test/kruskal_wallis.jl | 4 ++-- test/mann_whitney.jl | 6 +++--- test/power_divergence.jl | 4 ++-- test/t.jl | 8 ++++---- test/wilcoxon.jl | 8 ++++---- test/z.jl | 6 +++--- 30 files changed, 72 insertions(+), 82 deletions(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index c836ed67..4896edcc 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -68,24 +68,13 @@ function check_alpha(alpha::Float64) end end -# Utility to get an optional field (e.g., :tail and :alpha) -getfield(value::HypothesisTest, name::Symbol, default::Any) = - if name in fieldnames(value) - Base.getfield(value, name) - else - default - end - -get_tail(test::HypothesisTest) = getfield(test, :tail, default_tail(test)) -get_alpha(test::HypothesisTest) = getfield(test, :alpha, 0.05) - # Pretty-print function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io, testname(test)) println(io, repeat("-", length(testname(test)))) # utilities for pretty-printing - conf_string = string(floor((1 - get_alpha(test)) * 100, 6)) # limit to 6 decimals in % + conf_string = string(floor((1 - alpha(test)) * 100, 6)) # limit to 6 decimals in % prettify_detail(label::String, value::Any, len::Int) = # len is max length of label " " * label * " "^max(len - length(label), 0) * string(value) @@ -102,9 +91,9 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) println(io) # test summary - tail = get_tail(test) + tail = HypothesisTests.tail(test) p = pvalue(test, tail=tail) - outcome = if p > get_alpha(test) "fail to reject" else "reject" end + outcome = if p > alpha(test) "fail to reject" else "reject" end tailvalue = if tail == :both "two-sided p-value:" elseif tail == :left || tail == :right "one-sided p-value ($(string(tail)) tail):" @@ -122,8 +111,9 @@ end # parameter of interest: name, value under h0, point estimate population_param_of_interest{T<:HypothesisTest}(test::T) = ("not implemented yet", NaN, NaN) -# is the test one- or two-sided -default_tail(test::HypothesisTest) = :undefined +# is the test one- or two-sided? +tail(test::HypothesisTest) = :undefined # overloaded for defaults or field access +alpha(test::HypothesisTest) = 0.05 function show_params{T<:HypothesisTest}(io::IO, test::T, ident="") fieldidx = find(Bool[t<:Number for t in T.types]) diff --git a/src/anderson_darling.jl b/src/anderson_darling.jl index 14bda655..33e89f75 100644 --- a/src/anderson_darling.jl +++ b/src/anderson_darling.jl @@ -35,7 +35,7 @@ function OneSampleADTest{T<:Real}(x::AbstractVector{T}, d::UnivariateDistributio end testname(::OneSampleADTest) = "One sample Anderson-Darling test" -default_tail(test::OneSampleADTest) = :right +tail(test::OneSampleADTest) = :right function show_params(io::IO, x::OneSampleADTest, ident="") println(io, ident, "number of observations: $(x.n)") @@ -78,7 +78,7 @@ function KSampleADTest{T<:Real}(xs::AbstractVector{T}...; modified=true) end testname(::KSampleADTest) = "k-sample Anderson-Darling test" -default_tail(test::KSampleADTest) = :right +tail(test::KSampleADTest) = :right function show_params(io::IO, x::KSampleADTest, ident="") println(io, ident, "number of samples: $(x.k)") diff --git a/src/augmented_dickey_fuller.jl b/src/augmented_dickey_fuller.jl index 28709827..dafe1563 100644 --- a/src/augmented_dickey_fuller.jl +++ b/src/augmented_dickey_fuller.jl @@ -207,7 +207,7 @@ end testname(::ADFTest) = "Augmented Dickey-Fuller unit root test" population_param_of_interest(x::ADFTest) = ("coefficient on lagged non-differenced variable", 0, x.coef) -default_tail(test::ADFTest) = :left +tail(test::ADFTest) = :left function show_params(io::IO, x::ADFTest, ident) println(io, ident, "sample size in regression: ", x.n) diff --git a/src/binomial.jl b/src/binomial.jl index 1cac9669..cb8cf3c6 100644 --- a/src/binomial.jl +++ b/src/binomial.jl @@ -46,7 +46,7 @@ Returns the string value. E.g. "Binomial test", "Sign Test" """ testname(::BinomialTest) = "Binomial test" population_param_of_interest(x::BinomialTest) = ("Probability of success", x.p, x.x/x.n) # parameter of interest: name, value under h0, point estimate -default_tail(test::BinomialTest) = :both +tail(test::BinomialTest) = :both function show_params(io::IO, x::BinomialTest, ident="") println(io, ident, "number of observations: $(x.n)") @@ -157,7 +157,7 @@ SignTest{T<:Real, S<:Real}(x::AbstractVector{T}, y::AbstractVector{S}) = SignTes testname(::SignTest) = "Sign Test" population_param_of_interest(x::SignTest) = ("Median", x.median, median(x.data)) # parameter of interest: name, value under h0, point estimate -default_tail(test::SignTest) = :both +tail(test::SignTest) = :both function show_params(io::IO, x::SignTest, ident="") text1 = "number of observations:" diff --git a/src/box_test.jl b/src/box_test.jl index 8601e99b..91f81cdb 100644 --- a/src/box_test.jl +++ b/src/box_test.jl @@ -62,7 +62,7 @@ end testname(::BoxPierceTest) = "Box-Pierce autocorrelation test" population_param_of_interest(x::BoxPierceTest) = ("autocorrelations up to lag k", "all zero", NaN) -default_tail(test::BoxPierceTest) = :right +tail(test::BoxPierceTest) = :right function show_params(io::IO, x::BoxPierceTest, ident) println(io, ident, "number of observations: ", x.n) @@ -111,7 +111,7 @@ end testname(::LjungBoxTest) = "Ljung-Box autocorrelation test" population_param_of_interest(x::LjungBoxTest) = ("autocorrelations up to lag k", "all zero", NaN) -default_tail(test::LjungBoxTest) = :right +tail(test::LjungBoxTest) = :right function show_params(io::IO, x::LjungBoxTest, ident) println(io, ident, "number of observations: ", x.n) diff --git a/src/breusch_godfrey.jl b/src/breusch_godfrey.jl index faf178fb..71902be3 100644 --- a/src/breusch_godfrey.jl +++ b/src/breusch_godfrey.jl @@ -68,7 +68,7 @@ end testname(::BreuschGodfreyTest) = "Breusch-Godfrey autocorrelation test" population_param_of_interest(x::BreuschGodfreyTest) = ("coefficients on lagged residuals up to lag p", "all zero", NaN) -default_tail(test::BreuschGodfreyTest) = :right +tail(test::BreuschGodfreyTest) = :right function show_params(io::IO, x::BreuschGodfreyTest, ident) println(io, ident, "number of observations: ", x.n) diff --git a/src/circular.jl b/src/circular.jl index 21756df9..543522bb 100644 --- a/src/circular.jl +++ b/src/circular.jl @@ -53,7 +53,7 @@ end testname(::RayleighTest) = "Rayleigh test" population_param_of_interest(x::RayleighTest) = ("Mean resultant length", 0, x.Rbar) # parameter of interest: name, value under h0, point estimate -default_tail(test::RayleighTest) = :both +tail(test::RayleighTest) = :both function show_params(io::IO, x::RayleighTest, ident="") println(io, ident, "number of observations: $(x.n)") @@ -102,7 +102,7 @@ FisherTLinearAssociation{S <: Real, T <: Real}(theta::Vector{S}, testname(::FisherTLinearAssociation) = "T-linear test of circular-circular association" population_param_of_interest(x::FisherTLinearAssociation) = ("Circular correlation coefficient", 0, x.rho_t) # parameter of interest: name, value under h0, point estimate -default_tail(test::FisherTLinearAssociation) = :both +tail(test::FisherTLinearAssociation) = :both function show_params(io::IO, x::FisherTLinearAssociation, ident="") println(io, ident, "number of observations: [$(length(x.theta)),$(length(x.phi))]") @@ -215,7 +215,7 @@ end testname(::JammalamadakaCircularCorrelation) = "Jammalamadaka circular correlation" population_param_of_interest(x::JammalamadakaCircularCorrelation) = ("Circular-circular correlation coefficient", 0, x.r) # parameter of interest: name, value under h0, point estimate -default_tail(test::JammalamadakaCircularCorrelation) = :both +tail(test::JammalamadakaCircularCorrelation) = :both function show_params(io::IO, x::JammalamadakaCircularCorrelation, ident="") println(io, ident, "test statistic: $(x.Z)") diff --git a/src/fisher.jl b/src/fisher.jl index 86b76e25..d98e40bb 100644 --- a/src/fisher.jl +++ b/src/fisher.jl @@ -45,7 +45,7 @@ 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 +tail(test::FisherExactTest) = :both # The sizing argument to print_matrix was removed during the 0.5 dev period if VERSION < v"0.5.0-dev+1936" diff --git a/src/jarque_bera.jl b/src/jarque_bera.jl index b4d5403b..ce2f641c 100644 --- a/src/jarque_bera.jl +++ b/src/jarque_bera.jl @@ -77,7 +77,7 @@ end testname(::JarqueBeraTest) = "Jarque-Bera normality test" population_param_of_interest(x::JarqueBeraTest) = ("skewness and kurtosis", "0 and 3", "$(x.skew) and $(x.kurt)") -default_tail(test::JarqueBeraTest) = :right +tail(test::JarqueBeraTest) = :right function show_params(io::IO, x::JarqueBeraTest, ident) println(io, ident, "number of observations: ", x.n) diff --git a/src/kolmogorov_smirnov.jl b/src/kolmogorov_smirnov.jl index 8c0472be..7ddf3de1 100644 --- a/src/kolmogorov_smirnov.jl +++ b/src/kolmogorov_smirnov.jl @@ -31,7 +31,7 @@ export @compat abstract type ExactKSTest <: KSTest end population_param_of_interest(x::KSTest) = ("Supremum of CDF differences", 0.0, x.δ) # parameter of interest: name, value under h0, point estimate -default_tail(test::KSTest) = :both +tail(test::KSTest) = :both ## ONE SAMPLE KS-TEST diff --git a/src/kruskal_wallis.jl b/src/kruskal_wallis.jl index aca9eeec..3f82a06c 100644 --- a/src/kruskal_wallis.jl +++ b/src/kruskal_wallis.jl @@ -43,7 +43,7 @@ end testname(::KruskalWallisTest) = "Kruskal-Wallis rank sum test (chi-square approximation)" population_param_of_interest(x::KruskalWallisTest) = ("Location parameters", "all equal", NaN) # parameter of interest: name, value under h0, point estimate -default_tail(test::KruskalWallisTest) = :right +tail(test::KruskalWallisTest) = :right function show_params(io::IO, x::KruskalWallisTest, ident) println(io, ident, "number of observation in each group: ", x.n_i) diff --git a/src/mann_whitney.jl b/src/mann_whitney.jl index 78557525..0653f852 100644 --- a/src/mann_whitney.jl +++ b/src/mann_whitney.jl @@ -66,7 +66,7 @@ ExactMannWhitneyUTest{S<:Real,T<:Real}(x::AbstractVector{S}, y::AbstractVector{T testname(::ExactMannWhitneyUTest) = "Exact Mann-Whitney U test" population_param_of_interest(x::ExactMannWhitneyUTest) = ("Location parameter (pseudomedian)", 0, x.median) # parameter of interest: name, value under h0, point estimate -default_tail(test::ExactMannWhitneyUTest) = :both +tail(test::ExactMannWhitneyUTest) = :both function show_params(io::IO, x::ExactMannWhitneyUTest, ident) println(io, ident, "number of observations in each group: ", [x.nx, x.ny]) @@ -153,7 +153,7 @@ ApproximateMannWhitneyUTest{S<:Real,T<:Real}(x::AbstractVector{S}, y::AbstractVe testname(::ApproximateMannWhitneyUTest) = "Approximate Mann-Whitney U test" population_param_of_interest(x::ApproximateMannWhitneyUTest) = ("Location parameter (pseudomedian)", 0, x.median) # parameter of interest: name, value under h0, point estimate -default_tail(test::ApproximateMannWhitneyUTest) = :both +tail(test::ApproximateMannWhitneyUTest) = :both function show_params(io::IO, x::ApproximateMannWhitneyUTest, ident) println(io, ident, "number of observations in each group: ", [x.nx, x.ny]) diff --git a/src/power_divergence.jl b/src/power_divergence.jl index ab09fd7f..5c28f80e 100644 --- a/src/power_divergence.jl +++ b/src/power_divergence.jl @@ -40,7 +40,7 @@ end # parameter of interest: name, value under h0, point estimate population_param_of_interest(x::PowerDivergenceTest) = ("Multinomial Probabilities", x.theta0, x.thetahat) -default_tail(test::PowerDivergenceTest) = :right +tail(test::PowerDivergenceTest) = :right pvalue(x::PowerDivergenceTest; tail=:right) = pvalue(Chisq(x.df),x.stat; tail=tail) diff --git a/src/t.jl b/src/t.jl index dcadfbf8..a4b06d11 100644 --- a/src/t.jl +++ b/src/t.jl @@ -28,12 +28,12 @@ export OneSampleTTest, TwoSampleTTest, EqualVarianceTTest, @compat abstract type TTest <: HypothesisTest end @compat abstract type TwoSampleTTest <: TTest end -pvalue(x::TTest; tail=:both) = pvalue(TDist(x.df), x.t; tail=tail) - -default_tail(test::TTest) = :both +pvalue(x::TTest; tail=x.tail) = pvalue(TDist(x.df), x.t; tail=tail) +tail(x::TTest) = x.tail # defaults set by constructors +alpha(x::TTest) = x.alpha # confidence interval by inversion -function StatsBase.confint(x::TTest, alpha::Float64=get_alpha(x); tail=get_tail(x)) +function StatsBase.confint(x::TTest, alpha::Float64=x.alpha; tail::Symbol=x.tail) check_alpha(alpha) if tail == :left diff --git a/src/wilcoxon.jl b/src/wilcoxon.jl index c1cfe6a2..e5e4f51f 100644 --- a/src/wilcoxon.jl +++ b/src/wilcoxon.jl @@ -69,7 +69,7 @@ ExactSignedRankTest{S<:Real,T<:Real}(x::AbstractVector{S}, y::AbstractVector{T}) testname(::ExactSignedRankTest) = "Exact Wilcoxon signed rank test" population_param_of_interest(x::ExactSignedRankTest) = ("Location parameter (pseudomedian)", 0, x.median) # parameter of interest: name, value under h0, point estimate -default_tail(test::ExactSignedRankTest) = :both +tail(test::ExactSignedRankTest) = :both function show_params(io::IO, x::ExactSignedRankTest, ident) println(io, ident, "number of observations: ", x.n) @@ -160,7 +160,7 @@ ApproximateSignedRankTest{S<:Real,T<:Real}(x::AbstractVector{S}, y::AbstractVect testname(::ApproximateSignedRankTest) = "Approximate Wilcoxon signed rank test" population_param_of_interest(x::ApproximateSignedRankTest) = ("Location parameter (pseudomedian)", 0, x.median) # parameter of interest: name, value under h0, point estimate -default_tail(test::ApproximateSignedRankTest) = :both +tail(test::ApproximateSignedRankTest) = :both function show_params(io::IO, x::ApproximateSignedRankTest, ident) println(io, ident, "number of observations: ", x.n) diff --git a/src/z.jl b/src/z.jl index 86828f5c..f921f611 100644 --- a/src/z.jl +++ b/src/z.jl @@ -30,7 +30,7 @@ export OneSampleZTest, TwoSampleZTest, EqualVarianceZTest, pvalue(x::ZTest; tail=:both) = pvalue(Normal(0.0, 1.0), x.z; tail=tail) -default_tail(test::ZTest) = :both +tail(test::ZTest) = :both # confidence interval by inversion function StatsBase.confint(x::ZTest, alpha::Float64=0.05; tail=:both) diff --git a/test/anderson_darling.jl b/test/anderson_darling.jl index e48dbe10..b8966cd7 100644 --- a/test/anderson_darling.jl +++ b/test/anderson_darling.jl @@ -1,5 +1,5 @@ using HypothesisTests, Distributions, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail # One sample test n = 1000 @@ -9,7 +9,7 @@ x = rand(Normal(), n) t = OneSampleADTest(x, Normal()) @test isapprox(t.A², 0.2013, atol=0.1^4) @test isapprox(pvalue(t), 0.8811, atol=0.1^4) -@test default_tail(t) == :right +@test tail(t) == :right x = rand(DoubleExponential(), n) t = OneSampleADTest(x, Normal()) @@ -36,7 +36,7 @@ t = KSampleADTest(samples...) @test isapprox(t.A²k, 8.3926, atol=0.1^4) @test isapprox(t.σ, 1.2038, atol=0.1^4) @test isapprox(pvalue(t), 0.0020, atol=0.1^4) -@test default_tail(t) == :right +@test tail(t) == :right t = KSampleADTest(samples..., modified = false) @test isapprox(t.A²k, 8.3559, atol=0.1^4) diff --git a/test/binomial.jl b/test/binomial.jl index 9f80631e..562b1862 100644 --- a/test/binomial.jl +++ b/test/binomial.jl @@ -1,11 +1,11 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail t = BinomialTest(26, 78) @test pvalue(t) ≈ 0.004334880883507431 @test pvalue(t, tail=:left) ≈ 0.002167440441753716 @test pvalue(t, tail=:right) ≈ 0.9989844298129187 -@test default_tail(t) == :both +@test tail(t) == :both @test_ci_approx confint(t) (0.23058523962930383, 0.4491666887959782) @test_ci_approx confint(t, tail=:left) (0.0, 0.4313047758370174) @test_ci_approx confint(t, tail=:right) (0.2451709633730693, 1.0) @@ -58,7 +58,7 @@ x = [55, 58, 61, 61, 62, 62, 62, 63, 63, 64, 66, 68, 68, 69, 69, 69, 70, 71, 72, @test pvalue(SignTest(x, 70)) ≈ 0.004425048828125003 @test pvalue(SignTest(x, 70), tail=:left) ≈ 0.0022125244140625013 @test pvalue(SignTest(x, 70), tail=:right) ≈ 0.9996356964111328 -@test default_tail(SignTest(x)) == :both +@test tail(SignTest(x)) == :both @test_ci_approx confint(SignTest(x, 70)) (62, 69) @test_ci_approx confint(SignTest(x, 70), 0.0002) (61, 71) show(IOBuffer(), SignTest(x, 70)) diff --git a/test/box_test.jl b/test/box_test.jl index 364ab1ee..34401952 100644 --- a/test/box_test.jl +++ b/test/box_test.jl @@ -1,5 +1,5 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail sim_data_h0=[ 0.297287984535462;0.382395967790608;-0.597634476728231;-0.0104452446373756; @@ -36,7 +36,7 @@ t = HypothesisTests.BoxPierceTest(sim_data_h0,2,1) @test t.dof == 1 @test t.Q ≈ 1.233942980734545 @test pvalue(t) ≈ 0.2666415904008932 -@test default_tail(t) == :right +@test tail(t) == :right show(IOBuffer(), t) t = HypothesisTests.LjungBoxTest(sim_data_h0,5,2) @@ -46,7 +46,7 @@ t = HypothesisTests.LjungBoxTest(sim_data_h0,5,2) @test t.dof == 2 @test t.Q ≈ 3.2090126519163626 @test pvalue(t) ≈ 0.36050846449240337 -@test default_tail(t) == :right +@test tail(t) == :right show(IOBuffer(), t) sim_data_h1 = [ diff --git a/test/breusch_godfrey.jl b/test/breusch_godfrey.jl index b439524f..e32c049e 100644 --- a/test/breusch_godfrey.jl +++ b/test/breusch_godfrey.jl @@ -1,5 +1,5 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail # data simulated under H_1 data_h1 = [ @@ -114,7 +114,7 @@ t = BreuschGodfreyTest(data_h1[:,2:end],res_vec,4) @test t.lag == 4 @test t.BG ≈ 31.39810637185552 @test pvalue(t) ≈ 2.5390992557054064e-6 -@test default_tail(t) == :right +@test tail(t) == :right show(IOBuffer(), t) t = BreuschGodfreyTest(data_h1[:,2:end],res_vec,2,false) diff --git a/test/circular.jl b/test/circular.jl index 3eb1b515..1c189485 100644 --- a/test/circular.jl +++ b/test/circular.jl @@ -1,5 +1,5 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail # Fisher, 1995 example 4.11 @test abs(pvalue(RayleighTest(0.2370, 60)) - 0.034) <= 0.001 @@ -13,7 +13,7 @@ t = RayleighTest( 285, 292, 305, 315, 325, 328, 329, 343, 354, 359] *pi/180) @test abs(pvalue(t) - 0.20) <= 0.01 -@test default_tail(t) == :both +@test tail(t) == :both show(IOBuffer(), t) # Fisher, 1995 example 6.8 @@ -28,12 +28,12 @@ wind_direction_12pm = t = FisherTLinearAssociation(wind_direction_6am, wind_direction_12pm) @test abs(t.rho_t- 0.191) < 0.001 @test abs(pvalue(t) - 0.01) < 0.01 -@test default_tail(t) == :both +@test tail(t) == :both show(IOBuffer(), t) # Jammaladak, 2001 example 8.1 t = JammalamadakaCircularCorrelation(wind_direction_6am, wind_direction_12pm) @test abs(t.r - 0.2704648) < 1e-7 @test abs(pvalue(t) - 0.2247383) < 1e-7 -@test default_tail(t) == :both +@test tail(t) == :both show(IOBuffer(), t) diff --git a/test/fisher.jl b/test/fisher.jl index 9652a9b1..2799cabd 100644 --- a/test/fisher.jl +++ b/test/fisher.jl @@ -1,5 +1,5 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail t = HypothesisTests.FisherExactTest(1, 1, 1, 1) @test t.ω ≈ 1.0 @@ -7,7 +7,7 @@ t = HypothesisTests.FisherExactTest(1, 1, 1, 1) @test pvalue(t; tail=:right) ≈ 0.8333333333333337 @test pvalue(t; method=:central) ≈ 1.0 @test pvalue(t; method=:minlike) ≈ 1.0 -@test default_tail(t) == :both +@test tail(t) == :both @test_ci_approx confint(t; tail=:left) (0.0, 76.24918299781056) @test_ci_approx confint(t; tail=:right) (0.013114894621608135, Inf) @test_ci_approx confint(t; method=:central) (0.006400016357911029, 156.2496006379585) diff --git a/test/jarque_bera.jl b/test/jarque_bera.jl index df208c91..99a13073 100644 --- a/test/jarque_bera.jl +++ b/test/jarque_bera.jl @@ -1,5 +1,5 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail sim_data_h0 = 1.0 .+ [ 0.2972879845354616, 0.3823959677906078, -0.5976344767282311, -0.01044524463737564, @@ -37,7 +37,7 @@ t = JarqueBeraTest(sim_data_h0) @test t.skew ≈ -0.020527653857777352 @test t.kurt ≈ 2.5117242352057993 @test pvalue(t) ≈ 0.6003695680393418 -@test default_tail(t) == :right +@test tail(t) == :right show(IOBuffer(), t) sim_data_h1 = [ @@ -54,5 +54,5 @@ t = JarqueBeraTest(sim_data_h1) @test t.skew ≈ 0.11785113019775637 @test t.kurt ≈ 1.0138888888888888 @test pvalue(t) ≈ 0.00020338498134114293 -@test default_tail(t) == :right +@test tail(t) == :right show(IOBuffer(), t) diff --git a/test/kolmogorov_smirnov.jl b/test/kolmogorov_smirnov.jl index 66fb1170..754b0842 100644 --- a/test/kolmogorov_smirnov.jl +++ b/test/kolmogorov_smirnov.jl @@ -14,7 +14,7 @@ t = ApproximateOneSampleKSTest(x, Uniform()) @test pvalue(t) ≈ 0.6777349664784745 @test pvalue(t; tail=:left) ≈ 0.849573771973747 @test pvalue(t; tail=:right) ≈ 0.3545875485608989 -@test default_tail(t) == :both +@test tail(t) == :both show(IOBuffer(), t) t = ApproximateTwoSampleKSTest(x, [(0:24)/25...]) @@ -24,7 +24,7 @@ t = ApproximateTwoSampleKSTest(x, [(0:24)/25...]) @test pvalue(t) ≈ 0.993764859699076 @test pvalue(t; tail=:left) ≈ 0.8521437889662113 @test pvalue(t; tail=:right) ≈ 0.697676326071031 -@test default_tail(t) == :both +@test tail(t) == :both show(IOBuffer(), t) t = ExactOneSampleKSTest(x, Uniform()) @@ -34,7 +34,7 @@ t = ExactOneSampleKSTest(x, Uniform()) @test pvalue(t) ≈ 0.6263437768244742 @test pvalue(t; tail=:left) ≈ 0.8195705417998183 @test pvalue(t; tail=:right) ≈ 0.32350648882777194 -@test default_tail(t) == :both +@test tail(t) == :both show(IOBuffer(), t) ## check fit to normal distribution diff --git a/test/kruskal_wallis.jl b/test/kruskal_wallis.jl index b23ecfbe..9e1d2512 100644 --- a/test/kruskal_wallis.jl +++ b/test/kruskal_wallis.jl @@ -1,5 +1,5 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail # www.uni-siegen.de/phil/sozialwissenschaften/soziologie/mitarbeiter/ludwig-mayerhofer/statistik/statistik_downloads/statistik_ii_7.pdf u5 = [620, 5350, 7220] @@ -14,7 +14,7 @@ t = HypothesisTests.KruskalWallisTest(u5, u250, u2500, more) @test t.H ≈ 1.5803174603174597 @test t.tie_adjustment == 1 @test pvalue(t) ≈ 0.6638608922384397 -@test default_tail(t) == :right +@test tail(t) == :right show(IOBuffer(), t) # http://www.brightstat.com/index.php?option=com_content&task=view&id=41&Itemid=1&limit=1&limitstart=2 diff --git a/test/mann_whitney.jl b/test/mann_whitney.jl index fae53656..adfda4a0 100644 --- a/test/mann_whitney.jl +++ b/test/mann_whitney.jl @@ -1,12 +1,12 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail # Basic exact test @test abs(pvalue(ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) - 0.0232) <= 1e-4 @test abs(pvalue(ExactMannWhitneyUTest([2.1:2:21;], [1:10;])) - 0.0232) <= 1e-4 @test abs(pvalue(ExactMannWhitneyUTest([1.5:10:100;], [2.1:2:21;])) - 0.0068) <= 1e-4 @test abs(pvalue(ExactMannWhitneyUTest([2.1:2:21;], [1.5:10:100;])) - 0.0068) <= 1e-4 -@test default_tail(ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both +@test tail(ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both show(IOBuffer(), ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) # Exact with ties @@ -28,7 +28,7 @@ show(IOBuffer(), ExactMannWhitneyUTest([1:10;], [2:2:24;])) @test abs(pvalue(ApproximateMannWhitneyUTest([2.1:2:21;], [1:10;])) - 0.0257) <= 1e-4 @test abs(pvalue(ApproximateMannWhitneyUTest([1.5:10:100;], [2.1:2:21;])) - 0.0091) <= 1e-4 @test abs(pvalue(ApproximateMannWhitneyUTest([2.1:2:21;], [1.5:10:100;])) - 0.0091) <= 1e-4 -@test default_tail(ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both +@test tail(ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both show(IOBuffer(), ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) # Approximate with ties diff --git a/test/power_divergence.jl b/test/power_divergence.jl index 87f1687f..9b8249e2 100644 --- a/test/power_divergence.jl +++ b/test/power_divergence.jl @@ -1,6 +1,6 @@ using HypothesisTests, Base.Test using StatsBase -using HypothesisTests: default_tail +using HypothesisTests: tail #Example 1 in R #Agresti (2007) p. 39 @@ -26,7 +26,7 @@ for i = 1:length(c) end @test pvalue(m) ≈ 2.9535891832117357e-7 -@test default_tail(m) == :right +@test tail(m) == :right @test m.stat ≈ 30.070149095754687 @test m.df ≈ 2 @test m.n ≈ 2757 diff --git a/test/t.jl b/test/t.jl index efc73583..af5b9555 100644 --- a/test/t.jl +++ b/test/t.jl @@ -1,5 +1,5 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail ## ONE SAMPLE T-TEST @@ -10,7 +10,7 @@ tst = OneSampleTTest(-5:10) @test abs(pvalue(tst) - 0.0530) <= 1e-4 @test abs(pvalue(tst; tail=:left) - 0.9735) <= 1e-4 @test abs(pvalue(tst; tail=:right) - 0.0265) <= 1e-4 -@test default_tail(tst) == :both +@test tail(tst) == :both show(IOBuffer(), tst) tst = OneSampleTTest(mean(-5:10), std(-5:10), 16) @@ -50,7 +50,7 @@ tst = EqualVarianceTTest(a1, a2) @test abs(tst.t - 1.959) <= 1e-3 @test abs(pvalue(tst) - 0.078) <= 1e-3 @test all(abs.([confint(tst)...] - [-0.0131, 0.2031]) .<= 1e-4) -@test default_tail(tst) == :both +@test tail(tst) == :both show(IOBuffer(), tst) tst = UnequalVarianceTTest(a1, a2) @@ -58,5 +58,5 @@ tst = UnequalVarianceTTest(a1, a2) @test abs(tst.t - 1.959) <= 1e-3 @test abs(pvalue(tst) - 0.091) <= 1e-3 @test all(abs.([confint(tst)...] - [-0.0196, 0.2096]) .<= 1e-4) -@test default_tail(tst) == :both +@test tail(tst) == :both show(IOBuffer(), tst) diff --git a/test/wilcoxon.jl b/test/wilcoxon.jl index 953ea1f7..9dfeabe9 100644 --- a/test/wilcoxon.jl +++ b/test/wilcoxon.jl @@ -1,12 +1,12 @@ using HypothesisTests, Base.Test -using HypothesisTests: default_tail +using HypothesisTests: tail # Basic exact test @test abs(pvalue(ExactSignedRankTest([1:10;], [2:2:20;])) - 0.0020) <= 1e-4 @test abs(pvalue(ExactSignedRankTest([2:2:20;], [1:10;])) - 0.0020) <= 1e-4 @test abs(pvalue(ExactSignedRankTest([1:10;], [2:2:16; -1; 1])) - 0.4316) <= 1e-4 @test abs(pvalue(ExactSignedRankTest([2:2:16; -1; 1], [1:10;])) - 0.4316) <= 1e-4 -@test default_tail(ExactSignedRankTest([1:10;], [2:2:20;])) == :both +@test tail(ExactSignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), ExactSignedRankTest([1:10;], [2:2:20;])) # Exact with ties @@ -22,7 +22,7 @@ show(IOBuffer(), ExactSignedRankTest([1:10;], [1:10;])) @test abs(pvalue(ApproximateSignedRankTest([2:2:20;], [1:10;])) - 0.005922) <= 1e-6 @test abs(pvalue(ApproximateSignedRankTest([1:10;], [2:2:16; -1; 1])) - 0.4148) <= 1e-4 @test abs(pvalue(ApproximateSignedRankTest([2:2:16; -1; 1], [1:10;])) - 0.4148) <= 1e-4 -@test default_tail(ApproximateSignedRankTest([1:10;], [2:2:20;])) == :both +@test tail(ApproximateSignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), ApproximateSignedRankTest([1:10;], [2:2:20;])) # Approximate with ties @@ -36,7 +36,7 @@ show(IOBuffer(), ApproximateSignedRankTest([1:10;], [1:10;])) # # Tests for automatic selection @test abs(pvalue(SignedRankTest([1:10;], [2:2:20;])) - 0.0020) <= 1e-4 @test abs(pvalue(SignedRankTest([1:10;], [2:11;])) - 0.0020) <= 1e-4 -@test default_tail(SignedRankTest([1:10;], [2:2:20;])) == :both +@test tail(SignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), SignedRankTest([1:10;], [2:2:20;])) # One Sample tests diff --git a/test/z.jl b/test/z.jl index d83be510..d9c2b653 100644 --- a/test/z.jl +++ b/test/z.jl @@ -1,6 +1,6 @@ using HypothesisTests, Base.Test using Distributions -using HypothesisTests: default_tail +using HypothesisTests: tail # This is always the null in our tests. null = Normal(0.0, 1.0) @@ -20,7 +20,7 @@ tst = OneSampleZTest(x) @test pvalue(tst) ≈ 2 * min(cdf(null, z), ccdf(null, z)) @test pvalue(tst; tail=:left) ≈ cdf(null, z) @test pvalue(tst; tail=:right) ≈ ccdf(null, z) -@test default_tail(tst) == :both +@test tail(tst) == :both show(IOBuffer(), tst) tst = OneSampleZTest(m, s, n) @@ -83,7 +83,7 @@ z = xbar / se @test pvalue(tst) ≈ 2 * min(cdf(null, z), ccdf(null, z)) @test pvalue(tst; tail=:left) ≈ cdf(null, z) @test pvalue(tst; tail=:right) ≈ ccdf(null, z) -@test default_tail(tst) == :both +@test tail(tst) == :both @test confint(tst)[1] ≈ xbar + quantile(null, 0.05 / 2) * se @test confint(tst)[2] ≈ xbar + cquantile(null, 0.05 / 2) * se show(IOBuffer(), tst) From c1ac852702dbfbf89b81d4a71dcf24f4a75448b9 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 16 Jun 2017 16:43:43 +0200 Subject: [PATCH 10/18] Redirect deprecated default_tail to tail --- src/deprecated.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/deprecated.jl b/src/deprecated.jl index a44bc037..117ee3f5 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -1,3 +1,4 @@ using Base: @deprecate @deprecate ci(args...) confint(args...) +@deprecate default_tail(test::HypothesisTest) tail(test) From 36a9e76a252769f3003ca675ff302e334e1cbe8b Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 16 Jun 2017 18:37:56 +0200 Subject: [PATCH 11/18] Fixed Base.show for the case of tail being no keyword argument of the pvalue function --- src/HypothesisTests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index 4896edcc..de7739ed 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -92,7 +92,7 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) # test summary tail = HypothesisTests.tail(test) - p = pvalue(test, tail=tail) + p = pvalue(test) # obeys value of HypothesisTests.tail(test) if applicable outcome = if p > alpha(test) "fail to reject" else "reject" end tailvalue = if tail == :both "two-sided p-value:" From e6bc7a62443b4d5a01fe010ddc5ce51b19e7cde8 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 16 Jun 2017 18:46:24 +0200 Subject: [PATCH 12/18] Added comments describing how to test deprecation forwarding. All tests pass but the deprecation warnings are too annoying to actually run the tests every time. --- test/anderson_darling.jl | 2 ++ test/binomial.jl | 2 ++ test/box_test.jl | 2 ++ test/breusch_godfrey.jl | 1 + test/circular.jl | 3 +++ test/fisher.jl | 1 + test/jarque_bera.jl | 2 ++ test/kolmogorov_smirnov.jl | 3 +++ test/kruskal_wallis.jl | 1 + test/mann_whitney.jl | 2 ++ test/power_divergence.jl | 1 + test/wilcoxon.jl | 3 +++ test/z.jl | 2 ++ 13 files changed, 25 insertions(+) diff --git a/test/anderson_darling.jl b/test/anderson_darling.jl index b8966cd7..06658f9b 100644 --- a/test/anderson_darling.jl +++ b/test/anderson_darling.jl @@ -10,6 +10,7 @@ t = OneSampleADTest(x, Normal()) @test isapprox(t.A², 0.2013, atol=0.1^4) @test isapprox(pvalue(t), 0.8811, atol=0.1^4) @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right x = rand(DoubleExponential(), n) t = OneSampleADTest(x, Normal()) @@ -37,6 +38,7 @@ t = KSampleADTest(samples...) @test isapprox(t.σ, 1.2038, atol=0.1^4) @test isapprox(pvalue(t), 0.0020, atol=0.1^4) @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right t = KSampleADTest(samples..., modified = false) @test isapprox(t.A²k, 8.3559, atol=0.1^4) diff --git a/test/binomial.jl b/test/binomial.jl index 562b1862..d9661325 100644 --- a/test/binomial.jl +++ b/test/binomial.jl @@ -6,6 +6,7 @@ t = BinomialTest(26, 78) @test pvalue(t, tail=:left) ≈ 0.002167440441753716 @test pvalue(t, tail=:right) ≈ 0.9989844298129187 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both @test_ci_approx confint(t) (0.23058523962930383, 0.4491666887959782) @test_ci_approx confint(t, tail=:left) (0.0, 0.4313047758370174) @test_ci_approx confint(t, tail=:right) (0.2451709633730693, 1.0) @@ -59,6 +60,7 @@ x = [55, 58, 61, 61, 62, 62, 62, 63, 63, 64, 66, 68, 68, 69, 69, 69, 70, 71, 72, @test pvalue(SignTest(x, 70), tail=:left) ≈ 0.0022125244140625013 @test pvalue(SignTest(x, 70), tail=:right) ≈ 0.9996356964111328 @test tail(SignTest(x)) == :both +# @test HypothesisTests.default_tail(SignTest(x)) == :both @test_ci_approx confint(SignTest(x, 70)) (62, 69) @test_ci_approx confint(SignTest(x, 70), 0.0002) (61, 71) show(IOBuffer(), SignTest(x, 70)) diff --git a/test/box_test.jl b/test/box_test.jl index 34401952..afb10cf8 100644 --- a/test/box_test.jl +++ b/test/box_test.jl @@ -37,6 +37,7 @@ t = HypothesisTests.BoxPierceTest(sim_data_h0,2,1) @test t.Q ≈ 1.233942980734545 @test pvalue(t) ≈ 0.2666415904008932 @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) t = HypothesisTests.LjungBoxTest(sim_data_h0,5,2) @@ -47,6 +48,7 @@ t = HypothesisTests.LjungBoxTest(sim_data_h0,5,2) @test t.Q ≈ 3.2090126519163626 @test pvalue(t) ≈ 0.36050846449240337 @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) sim_data_h1 = [ diff --git a/test/breusch_godfrey.jl b/test/breusch_godfrey.jl index e32c049e..6840f8ae 100644 --- a/test/breusch_godfrey.jl +++ b/test/breusch_godfrey.jl @@ -115,6 +115,7 @@ t = BreuschGodfreyTest(data_h1[:,2:end],res_vec,4) @test t.BG ≈ 31.39810637185552 @test pvalue(t) ≈ 2.5390992557054064e-6 @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) t = BreuschGodfreyTest(data_h1[:,2:end],res_vec,2,false) diff --git a/test/circular.jl b/test/circular.jl index 1c189485..72a35dd7 100644 --- a/test/circular.jl +++ b/test/circular.jl @@ -14,6 +14,7 @@ t = RayleighTest( *pi/180) @test abs(pvalue(t) - 0.20) <= 0.01 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) # Fisher, 1995 example 6.8 @@ -29,6 +30,7 @@ t = FisherTLinearAssociation(wind_direction_6am, wind_direction_12pm) @test abs(t.rho_t- 0.191) < 0.001 @test abs(pvalue(t) - 0.01) < 0.01 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) # Jammaladak, 2001 example 8.1 @@ -36,4 +38,5 @@ t = JammalamadakaCircularCorrelation(wind_direction_6am, wind_direction_12pm) @test abs(t.r - 0.2704648) < 1e-7 @test abs(pvalue(t) - 0.2247383) < 1e-7 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) diff --git a/test/fisher.jl b/test/fisher.jl index 2799cabd..1ccb6e62 100644 --- a/test/fisher.jl +++ b/test/fisher.jl @@ -8,6 +8,7 @@ t = HypothesisTests.FisherExactTest(1, 1, 1, 1) @test pvalue(t; method=:central) ≈ 1.0 @test pvalue(t; method=:minlike) ≈ 1.0 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both @test_ci_approx confint(t; tail=:left) (0.0, 76.24918299781056) @test_ci_approx confint(t; tail=:right) (0.013114894621608135, Inf) @test_ci_approx confint(t; method=:central) (0.006400016357911029, 156.2496006379585) diff --git a/test/jarque_bera.jl b/test/jarque_bera.jl index 99a13073..9e4698cb 100644 --- a/test/jarque_bera.jl +++ b/test/jarque_bera.jl @@ -38,6 +38,7 @@ t = JarqueBeraTest(sim_data_h0) @test t.kurt ≈ 2.5117242352057993 @test pvalue(t) ≈ 0.6003695680393418 @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) sim_data_h1 = [ @@ -55,4 +56,5 @@ t = JarqueBeraTest(sim_data_h1) @test t.kurt ≈ 1.0138888888888888 @test pvalue(t) ≈ 0.00020338498134114293 @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) diff --git a/test/kolmogorov_smirnov.jl b/test/kolmogorov_smirnov.jl index 754b0842..8d3b1f2a 100644 --- a/test/kolmogorov_smirnov.jl +++ b/test/kolmogorov_smirnov.jl @@ -15,6 +15,7 @@ t = ApproximateOneSampleKSTest(x, Uniform()) @test pvalue(t; tail=:left) ≈ 0.849573771973747 @test pvalue(t; tail=:right) ≈ 0.3545875485608989 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) t = ApproximateTwoSampleKSTest(x, [(0:24)/25...]) @@ -25,6 +26,7 @@ t = ApproximateTwoSampleKSTest(x, [(0:24)/25...]) @test pvalue(t; tail=:left) ≈ 0.8521437889662113 @test pvalue(t; tail=:right) ≈ 0.697676326071031 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) t = ExactOneSampleKSTest(x, Uniform()) @@ -35,6 +37,7 @@ t = ExactOneSampleKSTest(x, Uniform()) @test pvalue(t; tail=:left) ≈ 0.8195705417998183 @test pvalue(t; tail=:right) ≈ 0.32350648882777194 @test tail(t) == :both +# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) ## check fit to normal distribution diff --git a/test/kruskal_wallis.jl b/test/kruskal_wallis.jl index 9e1d2512..dafafa15 100644 --- a/test/kruskal_wallis.jl +++ b/test/kruskal_wallis.jl @@ -15,6 +15,7 @@ t = HypothesisTests.KruskalWallisTest(u5, u250, u2500, more) @test t.tie_adjustment == 1 @test pvalue(t) ≈ 0.6638608922384397 @test tail(t) == :right +# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) # http://www.brightstat.com/index.php?option=com_content&task=view&id=41&Itemid=1&limit=1&limitstart=2 diff --git a/test/mann_whitney.jl b/test/mann_whitney.jl index adfda4a0..5dccd28b 100644 --- a/test/mann_whitney.jl +++ b/test/mann_whitney.jl @@ -7,6 +7,7 @@ using HypothesisTests: tail @test abs(pvalue(ExactMannWhitneyUTest([1.5:10:100;], [2.1:2:21;])) - 0.0068) <= 1e-4 @test abs(pvalue(ExactMannWhitneyUTest([2.1:2:21;], [1.5:10:100;])) - 0.0068) <= 1e-4 @test tail(ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both +# @test HypothesisTests.default_tail(ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both show(IOBuffer(), ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) # Exact with ties @@ -29,6 +30,7 @@ show(IOBuffer(), ExactMannWhitneyUTest([1:10;], [2:2:24;])) @test abs(pvalue(ApproximateMannWhitneyUTest([1.5:10:100;], [2.1:2:21;])) - 0.0091) <= 1e-4 @test abs(pvalue(ApproximateMannWhitneyUTest([2.1:2:21;], [1.5:10:100;])) - 0.0091) <= 1e-4 @test tail(ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both +# @test HypothesisTests.default_tail(ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both show(IOBuffer(), ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) # Approximate with ties diff --git a/test/power_divergence.jl b/test/power_divergence.jl index 9b8249e2..07dbef9f 100644 --- a/test/power_divergence.jl +++ b/test/power_divergence.jl @@ -27,6 +27,7 @@ end @test pvalue(m) ≈ 2.9535891832117357e-7 @test tail(m) == :right +# @test HypothesisTests.default_tail(m) == :right @test m.stat ≈ 30.070149095754687 @test m.df ≈ 2 @test m.n ≈ 2757 diff --git a/test/wilcoxon.jl b/test/wilcoxon.jl index 9dfeabe9..f882e44e 100644 --- a/test/wilcoxon.jl +++ b/test/wilcoxon.jl @@ -7,6 +7,7 @@ using HypothesisTests: tail @test abs(pvalue(ExactSignedRankTest([1:10;], [2:2:16; -1; 1])) - 0.4316) <= 1e-4 @test abs(pvalue(ExactSignedRankTest([2:2:16; -1; 1], [1:10;])) - 0.4316) <= 1e-4 @test tail(ExactSignedRankTest([1:10;], [2:2:20;])) == :both +# @test HypothesisTests.default_tail(ExactSignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), ExactSignedRankTest([1:10;], [2:2:20;])) # Exact with ties @@ -23,6 +24,7 @@ show(IOBuffer(), ExactSignedRankTest([1:10;], [1:10;])) @test abs(pvalue(ApproximateSignedRankTest([1:10;], [2:2:16; -1; 1])) - 0.4148) <= 1e-4 @test abs(pvalue(ApproximateSignedRankTest([2:2:16; -1; 1], [1:10;])) - 0.4148) <= 1e-4 @test tail(ApproximateSignedRankTest([1:10;], [2:2:20;])) == :both +# @test HypothesisTests.default_tail(ApproximateSignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), ApproximateSignedRankTest([1:10;], [2:2:20;])) # Approximate with ties @@ -37,6 +39,7 @@ show(IOBuffer(), ApproximateSignedRankTest([1:10;], [1:10;])) @test abs(pvalue(SignedRankTest([1:10;], [2:2:20;])) - 0.0020) <= 1e-4 @test abs(pvalue(SignedRankTest([1:10;], [2:11;])) - 0.0020) <= 1e-4 @test tail(SignedRankTest([1:10;], [2:2:20;])) == :both +# @test HypothesisTests.default_tail(SignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), SignedRankTest([1:10;], [2:2:20;])) # One Sample tests diff --git a/test/z.jl b/test/z.jl index d9c2b653..3847fe41 100644 --- a/test/z.jl +++ b/test/z.jl @@ -21,6 +21,7 @@ tst = OneSampleZTest(x) @test pvalue(tst; tail=:left) ≈ cdf(null, z) @test pvalue(tst; tail=:right) ≈ ccdf(null, z) @test tail(tst) == :both +# @test HypothesisTests.default_tail(tst) == :both show(IOBuffer(), tst) tst = OneSampleZTest(m, s, n) @@ -84,6 +85,7 @@ z = xbar / se @test pvalue(tst; tail=:left) ≈ cdf(null, z) @test pvalue(tst; tail=:right) ≈ ccdf(null, z) @test tail(tst) == :both +# @test HypothesisTests.default_tail(tst) == :both @test confint(tst)[1] ≈ xbar + quantile(null, 0.05 / 2) * se @test confint(tst)[2] ≈ xbar + cquantile(null, 0.05 / 2) * se show(IOBuffer(), tst) From bda80a7896150def8ceedfaeb58399379fa1a393 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 16 Jun 2017 19:14:15 +0200 Subject: [PATCH 13/18] Define IO Buffer once to ease temporary redirection to STDOUT --- test/t.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/t.jl b/test/t.jl index af5b9555..80c41648 100644 --- a/test/t.jl +++ b/test/t.jl @@ -1,6 +1,8 @@ using HypothesisTests, Base.Test using HypothesisTests: tail +iobuffer = IOBuffer() # define once to ease temporary redirection to STDOUT + ## ONE SAMPLE T-TEST # One sample @@ -11,7 +13,7 @@ tst = OneSampleTTest(-5:10) @test abs(pvalue(tst; tail=:left) - 0.9735) <= 1e-4 @test abs(pvalue(tst; tail=:right) - 0.0265) <= 1e-4 @test tail(tst) == :both -show(IOBuffer(), tst) +show(iobuffer, tst) tst = OneSampleTTest(mean(-5:10), std(-5:10), 16) @test abs(pvalue(tst) - 0.0530) <= 1e-4 @@ -24,7 +26,7 @@ c = confint(tst; tail=:left) c = confint(tst; tail=:right) @test abs(c[1] - 0.4135) .<= 1e-4 @test c[2] == Inf -show(IOBuffer(), tst) +show(iobuffer, tst) tst = OneSampleTTest(-10:5) @test abs(pvalue(tst) - 0.0530) <= 1e-4 @@ -33,7 +35,7 @@ tst = OneSampleTTest(-10:5) @test all(abs.([confint(tst)...] - [-5.0369, 0.0369]) .<= 1e-4) @test abs.(confint(tst; tail=:left)[2] - (-0.4135)) .<= 1e-4 @test abs.(confint(tst; tail=:right)[1] - (-4.5865)) .<= 1e-4 -show(IOBuffer(), tst) +show(iobuffer, tst) # Paired samples @test abs(pvalue(OneSampleTTest([1, 1, 2, 1, 0], [0, 1, 1, 1, 0])) - 0.1778) <= 1e-4 @@ -51,7 +53,7 @@ tst = EqualVarianceTTest(a1, a2) @test abs(pvalue(tst) - 0.078) <= 1e-3 @test all(abs.([confint(tst)...] - [-0.0131, 0.2031]) .<= 1e-4) @test tail(tst) == :both -show(IOBuffer(), tst) +show(iobuffer, tst) tst = UnequalVarianceTTest(a1, a2) @test abs(tst.df - 7.03) <= 0.01 @@ -59,4 +61,4 @@ tst = UnequalVarianceTTest(a1, a2) @test abs(pvalue(tst) - 0.091) <= 1e-3 @test all(abs.([confint(tst)...] - [-0.0196, 0.2096]) .<= 1e-4) @test tail(tst) == :both -show(IOBuffer(), tst) +show(iobuffer, tst) From 480e152681594da4d8a7b1c0578cda2a602c2fa1 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 16 Jun 2017 19:25:44 +0200 Subject: [PATCH 14/18] Added unit tests for tail parameter --- test/t.jl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/t.jl b/test/t.jl index 80c41648..8d2ecf53 100644 --- a/test/t.jl +++ b/test/t.jl @@ -15,6 +15,20 @@ tst = OneSampleTTest(-5:10) @test tail(tst) == :both show(iobuffer, tst) +tst = OneSampleTTest(-5:10, tail=:left) # specify the tail for the test +@test abs(pvalue(tst; tail=:both) - 0.0530) <= 1e-4 +@test abs(pvalue(tst) - 0.9735) <= 1e-4 # now tail=:left is the default in pvalue(tst) +@test abs(pvalue(tst; tail=:right) - 0.0265) <= 1e-4 +@test tail(tst) == :left +show(iobuffer, tst) + +tst = OneSampleTTest(-5:10, tail=:right) # specify the tail for the test +@test abs(pvalue(tst; tail=:both) - 0.0530) <= 1e-4 +@test abs(pvalue(tst; tail=:left) - 0.9735) <= 1e-4 +@test abs(pvalue(tst) - 0.0265) <= 1e-4 # now tail=:right is the default in pvalue(tst) +@test tail(tst) == :right +show(iobuffer, tst) + tst = OneSampleTTest(mean(-5:10), std(-5:10), 16) @test abs(pvalue(tst) - 0.0530) <= 1e-4 @@ -37,6 +51,16 @@ tst = OneSampleTTest(-10:5) @test abs.(confint(tst; tail=:right)[1] - (-4.5865)) .<= 1e-4 show(iobuffer, tst) +tst = OneSampleTTest(-10:5, tail=:left) +@test abs.(confint(tst)[2] - (-0.4135)) .<= 1e-4 +@test abs.(confint(tst; tail=:right)[1] - (-4.5865)) .<= 1e-4 +show(iobuffer, tst) + +tst = OneSampleTTest(-10:5, tail=:right) +@test abs.(confint(tst; tail=:left)[2] - (-0.4135)) .<= 1e-4 +@test abs.(confint(tst)[1] - (-4.5865)) .<= 1e-4 +show(iobuffer, tst) + # Paired samples @test abs(pvalue(OneSampleTTest([1, 1, 2, 1, 0], [0, 1, 1, 1, 0])) - 0.1778) <= 1e-4 @@ -55,6 +79,12 @@ tst = EqualVarianceTTest(a1, a2) @test tail(tst) == :both show(iobuffer, tst) +tst = EqualVarianceTTest(a1, a2, tail=:left) +@test abs(pvalue(tst) - 0.960) <= 1e-3 +@test abs(pvalue(tst, tail=:both) - 0.078) <= 1e-3 +@test tail(tst) == :left +show(iobuffer, tst) + tst = UnequalVarianceTTest(a1, a2) @test abs(tst.df - 7.03) <= 0.01 @test abs(tst.t - 1.959) <= 1e-3 @@ -62,3 +92,9 @@ tst = UnequalVarianceTTest(a1, a2) @test all(abs.([confint(tst)...] - [-0.0196, 0.2096]) .<= 1e-4) @test tail(tst) == :both show(iobuffer, tst) + +tst = UnequalVarianceTTest(a1, a2, tail=:right) +@test abs(pvalue(tst) - 0.045) <= 1e-3 +@test abs(pvalue(tst, tail=:both) - 0.091) <= 1e-3 +@test tail(tst) == :right +show(iobuffer, tst) From 96eb8cf5d3f60908a349645583a43caa2eccd9ef Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 16 Jun 2017 19:46:08 +0200 Subject: [PATCH 15/18] Updated doc of t-tests --- doc/api/confint.rst | 7 ++++--- doc/api/pvalue.rst | 4 ++-- doc/parametric/test_t.rst | 27 +++++++++++++++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/doc/api/confint.rst b/doc/api/confint.rst index 9ba8102d..05ca9b4a 100644 --- a/doc/api/confint.rst +++ b/doc/api/confint.rst @@ -3,11 +3,12 @@ Confidence Interval ============================================== -.. function:: confint(test::HypothesisTest, alpha=0.05; tail=:both) +.. function:: confint(test::HypothesisTest, alpha=alpha(test); tail=tail(test)) - Compute a confidence interval C with coverage 1-``alpha``. + Compute a confidence interval C with coverage 1-``alpha`` + (``alpha=0.05`` is the default for all tests). - If ``tail`` is ``:both`` (default), then a two-sided confidence + If ``tail`` is ``:both`` (default for most tests), then a two-sided confidence interval is returned. If ``tail`` is ``:left`` or ``:right``, then a one-sided confidence interval is returned diff --git a/doc/api/pvalue.rst b/doc/api/pvalue.rst index 00e30618..5c9a4cd0 100644 --- a/doc/api/pvalue.rst +++ b/doc/api/pvalue.rst @@ -3,11 +3,11 @@ p-value ============================================== -.. function:: pvalue(test::HypothesisTest; tail=:both) +.. function:: pvalue(test::HypothesisTest; tail=tail(test)) Compute the p-value for a given significance test. - If ``tail`` is ``:both`` (default), then the p-value for the + If ``tail`` is ``:both`` (default for most hypothesis tests), then the p-value for the two-sided test is returned. If ``tail`` is ``:left`` or ``:right``, then a one-sided test is performed. diff --git a/doc/parametric/test_t.rst b/doc/parametric/test_t.rst index ba96b6db..611df58b 100644 --- a/doc/parametric/test_t.rst +++ b/doc/parametric/test_t.rst @@ -1,45 +1,57 @@ T-test ============================================= -.. function:: OneSampleTTest(v::AbstractVector{T<:Real}, mu0::Real=0) +.. function:: OneSampleTTest(v::AbstractVector{T<:Real}, mu0::Real=0; tail::Symbol=:both, alpha::Real=0.05) Perform a one sample t-test of the null hypothesis that the data in vector ``v`` comes from a distribution with mean ``mu0`` against the alternative hypothesis that the distribution does not have mean ``mu0``. + + ``tail`` and ``alpha`` specify the defaults for the application of + :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` -.. function:: OneSampleTTest(xbar::Real, stdev::Real, n::Int, mu0::Real=0) +.. function:: OneSampleTTest(xbar::Real, stdev::Real, n::Int, mu0::Real=0; tail::Symbol=:both, alpha::Real=0.05) Perform a one sample t-test of the null hypothesis that ``n`` values with mean ``xbar`` and sample standard deviation ``stdev`` come from a distribution with ``mu0`` against the alternative hypothesis that the distribution does not have mean ``mu0``. + + ``tail`` and ``alpha`` specify the defaults for the application of + :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` -.. function:: OneSampleTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real}, mu0::Real=0) +.. function:: OneSampleTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real}, mu0::Real=0; tail::Symbol=:both, alpha::Real=0.05) Perform a paired sample t-test of the null hypothesis that the differences between pairs of values in vectors ``x`` and ``y`` come from a distribution with ``mu0`` against the alternative hypothesis that the distribution does not have mean ``mu0``. + + ``tail`` and ``alpha`` specify the defaults for the application of + :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` -.. function:: EqualVarianceTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real}) +.. function:: EqualVarianceTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real}; tail::Symbol=:both, alpha::Real=0.05) Perform a two-sample t-test of the null hypothesis that ``x`` and ``y`` come from a distributions with the same mean and equal variances against the alternative hypothesis that the distributions have different means and but equal variances. + + ``tail`` and ``alpha`` specify the defaults for the application of + :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` -.. function:: UnequalVarianceTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real}) +.. function:: UnequalVarianceTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real}; tail::Symbol=:both, alpha::Real=0.05) Perform an unequal variance two-sample t-test of the null hypothesis that ``x`` and ``y`` come from a distributions with @@ -54,5 +66,8 @@ T-test .. math:: \nu_{\chi'} \approx \frac{\left(\sum_{i=1}^n k_i s_i^2\right)^2} {\sum_{i=1}^n \frac{(k_i s_i^2)^2}{\nu_i}} - + + ``tail`` and ``alpha`` specify the defaults for the application of + :ref:`pvalue` and :ref:`confint`. + Implements: :ref:`pvalue`, :ref:`confint` From 86626ec4df549abe89c6d1ed0a98c2c9872fcea4 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Sat, 17 Jun 2017 12:43:18 +0200 Subject: [PATCH 16/18] Uncommented julia:nightly because not even specified by REQUIRE file --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 89d0304a..450e1afc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ os: - osx julia: - 0.5 - - nightly +# - nightly # The REQUIRE file states that only julia 0.5 is supported notifications: email: false # uncomment the following lines to override the default test script From 1938ffb311e3e6ca4dd9547c8aa708b26cf827a1 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Sun, 18 Jun 2017 09:10:38 +0200 Subject: [PATCH 17/18] Removed named tail parameter from the remaining pvalue functions --- src/augmented_dickey_fuller.jl | 3 +-- src/box_test.jl | 4 ++-- src/breusch_godfrey.jl | 2 +- src/circular.jl | 5 +---- src/jarque_bera.jl | 2 +- src/kruskal_wallis.jl | 2 +- 6 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/augmented_dickey_fuller.jl b/src/augmented_dickey_fuller.jl index dafe1563..8e568e00 100644 --- a/src/augmented_dickey_fuller.jl +++ b/src/augmented_dickey_fuller.jl @@ -216,5 +216,4 @@ function show_params(io::IO, x::ADFTest, ident) println(io, ident, "Critical values at 1%, 5%, and 10%: ", x.cv') end -pvalue(x::ADFTest; tail=:left) = # :left is default tail - HypothesisTests.pvalue(Normal(0, 1), adf_pv_aux(x.stat, x.deterministic); tail=tail) +pvalue(x::ADFTest) = HypothesisTests.pvalue(Normal(0, 1), adf_pv_aux(x.stat, x.deterministic); tail=:left) diff --git a/src/box_test.jl b/src/box_test.jl index 91f81cdb..7dbbe3a2 100644 --- a/src/box_test.jl +++ b/src/box_test.jl @@ -71,7 +71,7 @@ function show_params(io::IO, x::BoxPierceTest, ident) println(io, ident, "Q statistic: ", x.Q) end -pvalue(x::BoxPierceTest; tail=:right) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) +pvalue(x::BoxPierceTest) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=:right) #Ljung-Box test @@ -120,4 +120,4 @@ function show_params(io::IO, x::LjungBoxTest, ident) println(io, ident, "Q statistic: ", x.Q) end -pvalue(x::LjungBoxTest; tail=:right) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=tail) +pvalue(x::LjungBoxTest) = pvalue(Chisq(x.lag-x.dof), x.Q; tail=:right) diff --git a/src/breusch_godfrey.jl b/src/breusch_godfrey.jl index 71902be3..7dec9d00 100644 --- a/src/breusch_godfrey.jl +++ b/src/breusch_godfrey.jl @@ -76,4 +76,4 @@ function show_params(io::IO, x::BreuschGodfreyTest, ident) println(io, ident, "T*R^2 statistic: ", x.BG) end -pvalue(x::BreuschGodfreyTest; tail=:right) = pvalue(Chisq(x.lag), x.BG; tail=tail) +pvalue(x::BreuschGodfreyTest) = pvalue(Chisq(x.lag), x.BG; tail=:right) diff --git a/src/circular.jl b/src/circular.jl index 543522bb..555297c2 100644 --- a/src/circular.jl +++ b/src/circular.jl @@ -60,10 +60,7 @@ function show_params(io::IO, x::RayleighTest, ident="") println(io, ident, "test statistic: $(x.Rbar^2 * x.n)") end -function pvalue(x::RayleighTest; tail=:both) # :both is default tail - if tail != :both # warn that tail is not used here - throw(ArgumentError(":both is the only valid value for the tail of a RayleighTest")) - end +function pvalue(x::RayleighTest) Z = x.Rbar^2 * x.n x.n > 1e6 ? exp(-Z) : exp(-Z)*(1+(2*Z-Z^2)/(4*x.n)-(24*Z - 132*Z^2 + 76*Z^3 - 9*Z^4)/(288*x.n^2)) diff --git a/src/jarque_bera.jl b/src/jarque_bera.jl index ce2f641c..c92e9694 100644 --- a/src/jarque_bera.jl +++ b/src/jarque_bera.jl @@ -84,4 +84,4 @@ function show_params(io::IO, x::JarqueBeraTest, ident) println(io, ident, "JB statistic: ", x.JB) end -pvalue(x::JarqueBeraTest; tail=:right) = pvalue(Chisq(2), x.JB; tail=tail) +pvalue(x::JarqueBeraTest) = pvalue(Chisq(2), x.JB; tail=:right) diff --git a/src/kruskal_wallis.jl b/src/kruskal_wallis.jl index 3f82a06c..f4f6f182 100644 --- a/src/kruskal_wallis.jl +++ b/src/kruskal_wallis.jl @@ -53,7 +53,7 @@ function show_params(io::IO, x::KruskalWallisTest, ident) println(io, ident, "adjustment for ties: ", x.tie_adjustment) end -pvalue(x::KruskalWallisTest; tail=:right) = pvalue(Chisq(x.df), x.H; tail=tail) +pvalue(x::KruskalWallisTest) = pvalue(Chisq(x.df), x.H; tail=:right) ## helper From 5d38a50e31273bee2532e8ac3c8f2600283d7bb4 Mon Sep 17 00:00:00 2001 From: Mirko Bunse Date: Fri, 30 Jun 2017 17:53:04 +0200 Subject: [PATCH 18/18] Realised requested changes. Only missing change: tail and alpha as field of every test. --- .travis.yml | 2 +- doc/api/confint.rst | 7 ++++--- doc/api/pvalue.rst | 2 +- doc/parametric/test_t.rst | 22 +++++++++++----------- src/HypothesisTests.jl | 35 +++++++++++++++++++---------------- src/t.jl | 2 +- test/anderson_darling.jl | 2 -- test/binomial.jl | 2 -- test/box_test.jl | 2 -- test/breusch_godfrey.jl | 1 - test/circular.jl | 3 --- test/fisher.jl | 1 - test/jarque_bera.jl | 2 -- test/kolmogorov_smirnov.jl | 3 --- test/kruskal_wallis.jl | 1 - test/mann_whitney.jl | 2 -- test/power_divergence.jl | 1 - test/wilcoxon.jl | 3 --- test/z.jl | 2 -- 19 files changed, 37 insertions(+), 58 deletions(-) diff --git a/.travis.yml b/.travis.yml index 450e1afc..89d0304a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ os: - osx julia: - 0.5 -# - nightly # The REQUIRE file states that only julia 0.5 is supported + - nightly notifications: email: false # uncomment the following lines to override the default test script diff --git a/doc/api/confint.rst b/doc/api/confint.rst index 05ca9b4a..85cdc47b 100644 --- a/doc/api/confint.rst +++ b/doc/api/confint.rst @@ -6,11 +6,12 @@ Confidence Interval .. function:: confint(test::HypothesisTest, alpha=alpha(test); tail=tail(test)) Compute a confidence interval C with coverage 1-``alpha`` - (``alpha=0.05`` is the default for all tests). + (``alpha=0.05`` is the default for all tests, unless alpha is overridden when the test + object is created). - If ``tail`` is ``:both`` (default for most tests), then a two-sided confidence + If ``tail`` is ``:both``, then a two-sided confidence interval is returned. If ``tail`` is ``:left`` or - ``:right``, then a one-sided confidence interval is returned + ``:right``, then a one-sided confidence interval is returned. .. note:: Most of the implemented confidence intervals are *strongly consistent*, that is, the diff --git a/doc/api/pvalue.rst b/doc/api/pvalue.rst index 5c9a4cd0..db53e896 100644 --- a/doc/api/pvalue.rst +++ b/doc/api/pvalue.rst @@ -7,7 +7,7 @@ p-value Compute the p-value for a given significance test. - If ``tail`` is ``:both`` (default for most hypothesis tests), then the p-value for the + If ``tail`` is ``:both``, then the p-value for the two-sided test is returned. If ``tail`` is ``:left`` or ``:right``, then a one-sided test is performed. diff --git a/doc/parametric/test_t.rst b/doc/parametric/test_t.rst index 611df58b..a9b21d08 100644 --- a/doc/parametric/test_t.rst +++ b/doc/parametric/test_t.rst @@ -7,8 +7,8 @@ T-test in vector ``v`` comes from a distribution with mean ``mu0`` against the alternative hypothesis that the distribution does not have mean ``mu0``. - - ``tail`` and ``alpha`` specify the defaults for the application of + + ``tail`` and ``alpha`` specify the defaults when calling :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` @@ -20,8 +20,8 @@ T-test ``stdev`` come from a distribution with ``mu0`` against the alternative hypothesis that the distribution does not have mean ``mu0``. - - ``tail`` and ``alpha`` specify the defaults for the application of + + ``tail`` and ``alpha`` specify the defaults when calling :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` @@ -33,8 +33,8 @@ T-test ``y`` come from a distribution with ``mu0`` against the alternative hypothesis that the distribution does not have mean ``mu0``. - - ``tail`` and ``alpha`` specify the defaults for the application of + + ``tail`` and ``alpha`` specify the defaults when calling :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` @@ -45,8 +45,8 @@ T-test ``x`` and ``y`` come from a distributions with the same mean and equal variances against the alternative hypothesis that the distributions have different means and but equal variances. - - ``tail`` and ``alpha`` specify the defaults for the application of + + ``tail`` and ``alpha`` specify the defaults when calling :ref:`pvalue` and :ref:`confint`. Implements: :ref:`pvalue`, :ref:`confint` @@ -66,8 +66,8 @@ T-test .. math:: \nu_{\chi'} \approx \frac{\left(\sum_{i=1}^n k_i s_i^2\right)^2} {\sum_{i=1}^n \frac{(k_i s_i^2)^2}{\nu_i}} - - ``tail`` and ``alpha`` specify the defaults for the application of + + ``tail`` and ``alpha`` specify the defaults when calling :ref:`pvalue` and :ref:`confint`. - + Implements: :ref:`pvalue`, :ref:`confint` diff --git a/src/HypothesisTests.jl b/src/HypothesisTests.jl index de7739ed..d75f9573 100644 --- a/src/HypothesisTests.jl +++ b/src/HypothesisTests.jl @@ -32,7 +32,7 @@ using Rmath: pwilcox, psignrank import StatsBase.confint -export testname, pvalue, confint +export testname, pvalue, confint, tail, alpha @compat abstract type HypothesisTest end check_same_length(x::AbstractVector, y::AbstractVector) = if length(x) != length(y) @@ -75,32 +75,35 @@ function Base.show{T<:HypothesisTest}(io::IO, test::T) # utilities for pretty-printing conf_string = string(floor((1 - alpha(test)) * 100, 6)) # limit to 6 decimals in % - prettify_detail(label::String, value::Any, len::Int) = # len is max length of label + format_detail(label::String, value::Any, len::Int) = # len is max length of label " " * label * " "^max(len - length(label), 0) * string(value) # population details - has_ci = applicable(StatsBase.confint, test) + has_ci = applicable(confint, test) (param_name, param_under_h0, param_estimate) = population_param_of_interest(test) println(io, "Population details:") - println(io, prettify_detail("parameter of interest:", param_name, 32)) - println(io, prettify_detail("value under h_0:", param_under_h0, 32)) - println(io, prettify_detail("point estimate:", param_estimate, 32)) + println(io, format_detail("parameter of interest:", param_name, 32)) + println(io, format_detail("value under h_0:", param_under_h0, 32)) + println(io, format_detail("point estimate:", param_estimate, 32)) if has_ci - println(io, prettify_detail(conf_string*"% confidence interval:", StatsBase.confint(test), 32)) + println(io, format_detail(conf_string*"% confidence interval:", confint(test), 32)) end println(io) # test summary - tail = HypothesisTests.tail(test) - p = pvalue(test) # obeys value of HypothesisTests.tail(test) if applicable - outcome = if p > alpha(test) "fail to reject" else "reject" end - tailvalue = - if tail == :both "two-sided p-value:" - elseif tail == :left || tail == :right "one-sided p-value ($(string(tail)) tail):" - else "p-value:" end + testtail = tail(test) + p = pvalue(test) # obeys value of tail(test) if applicable + outcome = p > alpha(test) ? "fail to reject" : "reject" + if testtail == :both + plabel ="two-sided p-value:" + elseif testtail == :left || testtail == :right + plabel = "one-sided p-value ($(string(testtail)) tail):" + else + plabel = "p-value:" + end println(io, "Test summary:") - println(io, prettify_detail("outcome with "*conf_string*"% confidence:", outcome*" h_0", 36)) - println(io, prettify_detail(tailvalue, p, 36)) + println(io, format_detail("outcome with "*conf_string*"% confidence:", outcome*" h_0", 36)) + println(io, format_detail(plabel, p, 36)) println(io) # further details diff --git a/src/t.jl b/src/t.jl index a4b06d11..9bcc7432 100644 --- a/src/t.jl +++ b/src/t.jl @@ -37,7 +37,7 @@ function StatsBase.confint(x::TTest, alpha::Float64=x.alpha; tail::Symbol=x.tail check_alpha(alpha) if tail == :left - (-Inf, StatsBase.confint(x, alpha*2, tail=:both)[2]) # tail=:both required as recursive anchor + (-Inf, StatsBase.confint(x, alpha*2, tail=:both)[2]) # tail=:both blocks infinite recursion elseif tail == :right (StatsBase.confint(x, alpha*2, tail=:both)[1], Inf) elseif tail == :both diff --git a/test/anderson_darling.jl b/test/anderson_darling.jl index 06658f9b..b8966cd7 100644 --- a/test/anderson_darling.jl +++ b/test/anderson_darling.jl @@ -10,7 +10,6 @@ t = OneSampleADTest(x, Normal()) @test isapprox(t.A², 0.2013, atol=0.1^4) @test isapprox(pvalue(t), 0.8811, atol=0.1^4) @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right x = rand(DoubleExponential(), n) t = OneSampleADTest(x, Normal()) @@ -38,7 +37,6 @@ t = KSampleADTest(samples...) @test isapprox(t.σ, 1.2038, atol=0.1^4) @test isapprox(pvalue(t), 0.0020, atol=0.1^4) @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right t = KSampleADTest(samples..., modified = false) @test isapprox(t.A²k, 8.3559, atol=0.1^4) diff --git a/test/binomial.jl b/test/binomial.jl index d9661325..562b1862 100644 --- a/test/binomial.jl +++ b/test/binomial.jl @@ -6,7 +6,6 @@ t = BinomialTest(26, 78) @test pvalue(t, tail=:left) ≈ 0.002167440441753716 @test pvalue(t, tail=:right) ≈ 0.9989844298129187 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both @test_ci_approx confint(t) (0.23058523962930383, 0.4491666887959782) @test_ci_approx confint(t, tail=:left) (0.0, 0.4313047758370174) @test_ci_approx confint(t, tail=:right) (0.2451709633730693, 1.0) @@ -60,7 +59,6 @@ x = [55, 58, 61, 61, 62, 62, 62, 63, 63, 64, 66, 68, 68, 69, 69, 69, 70, 71, 72, @test pvalue(SignTest(x, 70), tail=:left) ≈ 0.0022125244140625013 @test pvalue(SignTest(x, 70), tail=:right) ≈ 0.9996356964111328 @test tail(SignTest(x)) == :both -# @test HypothesisTests.default_tail(SignTest(x)) == :both @test_ci_approx confint(SignTest(x, 70)) (62, 69) @test_ci_approx confint(SignTest(x, 70), 0.0002) (61, 71) show(IOBuffer(), SignTest(x, 70)) diff --git a/test/box_test.jl b/test/box_test.jl index afb10cf8..34401952 100644 --- a/test/box_test.jl +++ b/test/box_test.jl @@ -37,7 +37,6 @@ t = HypothesisTests.BoxPierceTest(sim_data_h0,2,1) @test t.Q ≈ 1.233942980734545 @test pvalue(t) ≈ 0.2666415904008932 @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) t = HypothesisTests.LjungBoxTest(sim_data_h0,5,2) @@ -48,7 +47,6 @@ t = HypothesisTests.LjungBoxTest(sim_data_h0,5,2) @test t.Q ≈ 3.2090126519163626 @test pvalue(t) ≈ 0.36050846449240337 @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) sim_data_h1 = [ diff --git a/test/breusch_godfrey.jl b/test/breusch_godfrey.jl index 6840f8ae..e32c049e 100644 --- a/test/breusch_godfrey.jl +++ b/test/breusch_godfrey.jl @@ -115,7 +115,6 @@ t = BreuschGodfreyTest(data_h1[:,2:end],res_vec,4) @test t.BG ≈ 31.39810637185552 @test pvalue(t) ≈ 2.5390992557054064e-6 @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) t = BreuschGodfreyTest(data_h1[:,2:end],res_vec,2,false) diff --git a/test/circular.jl b/test/circular.jl index 72a35dd7..1c189485 100644 --- a/test/circular.jl +++ b/test/circular.jl @@ -14,7 +14,6 @@ t = RayleighTest( *pi/180) @test abs(pvalue(t) - 0.20) <= 0.01 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) # Fisher, 1995 example 6.8 @@ -30,7 +29,6 @@ t = FisherTLinearAssociation(wind_direction_6am, wind_direction_12pm) @test abs(t.rho_t- 0.191) < 0.001 @test abs(pvalue(t) - 0.01) < 0.01 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) # Jammaladak, 2001 example 8.1 @@ -38,5 +36,4 @@ t = JammalamadakaCircularCorrelation(wind_direction_6am, wind_direction_12pm) @test abs(t.r - 0.2704648) < 1e-7 @test abs(pvalue(t) - 0.2247383) < 1e-7 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) diff --git a/test/fisher.jl b/test/fisher.jl index 1ccb6e62..2799cabd 100644 --- a/test/fisher.jl +++ b/test/fisher.jl @@ -8,7 +8,6 @@ t = HypothesisTests.FisherExactTest(1, 1, 1, 1) @test pvalue(t; method=:central) ≈ 1.0 @test pvalue(t; method=:minlike) ≈ 1.0 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both @test_ci_approx confint(t; tail=:left) (0.0, 76.24918299781056) @test_ci_approx confint(t; tail=:right) (0.013114894621608135, Inf) @test_ci_approx confint(t; method=:central) (0.006400016357911029, 156.2496006379585) diff --git a/test/jarque_bera.jl b/test/jarque_bera.jl index 9e4698cb..99a13073 100644 --- a/test/jarque_bera.jl +++ b/test/jarque_bera.jl @@ -38,7 +38,6 @@ t = JarqueBeraTest(sim_data_h0) @test t.kurt ≈ 2.5117242352057993 @test pvalue(t) ≈ 0.6003695680393418 @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) sim_data_h1 = [ @@ -56,5 +55,4 @@ t = JarqueBeraTest(sim_data_h1) @test t.kurt ≈ 1.0138888888888888 @test pvalue(t) ≈ 0.00020338498134114293 @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) diff --git a/test/kolmogorov_smirnov.jl b/test/kolmogorov_smirnov.jl index 8d3b1f2a..754b0842 100644 --- a/test/kolmogorov_smirnov.jl +++ b/test/kolmogorov_smirnov.jl @@ -15,7 +15,6 @@ t = ApproximateOneSampleKSTest(x, Uniform()) @test pvalue(t; tail=:left) ≈ 0.849573771973747 @test pvalue(t; tail=:right) ≈ 0.3545875485608989 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) t = ApproximateTwoSampleKSTest(x, [(0:24)/25...]) @@ -26,7 +25,6 @@ t = ApproximateTwoSampleKSTest(x, [(0:24)/25...]) @test pvalue(t; tail=:left) ≈ 0.8521437889662113 @test pvalue(t; tail=:right) ≈ 0.697676326071031 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) t = ExactOneSampleKSTest(x, Uniform()) @@ -37,7 +35,6 @@ t = ExactOneSampleKSTest(x, Uniform()) @test pvalue(t; tail=:left) ≈ 0.8195705417998183 @test pvalue(t; tail=:right) ≈ 0.32350648882777194 @test tail(t) == :both -# @test HypothesisTests.default_tail(t) == :both show(IOBuffer(), t) ## check fit to normal distribution diff --git a/test/kruskal_wallis.jl b/test/kruskal_wallis.jl index dafafa15..9e1d2512 100644 --- a/test/kruskal_wallis.jl +++ b/test/kruskal_wallis.jl @@ -15,7 +15,6 @@ t = HypothesisTests.KruskalWallisTest(u5, u250, u2500, more) @test t.tie_adjustment == 1 @test pvalue(t) ≈ 0.6638608922384397 @test tail(t) == :right -# @test HypothesisTests.default_tail(t) == :right show(IOBuffer(), t) # http://www.brightstat.com/index.php?option=com_content&task=view&id=41&Itemid=1&limit=1&limitstart=2 diff --git a/test/mann_whitney.jl b/test/mann_whitney.jl index 5dccd28b..adfda4a0 100644 --- a/test/mann_whitney.jl +++ b/test/mann_whitney.jl @@ -7,7 +7,6 @@ using HypothesisTests: tail @test abs(pvalue(ExactMannWhitneyUTest([1.5:10:100;], [2.1:2:21;])) - 0.0068) <= 1e-4 @test abs(pvalue(ExactMannWhitneyUTest([2.1:2:21;], [1.5:10:100;])) - 0.0068) <= 1e-4 @test tail(ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both -# @test HypothesisTests.default_tail(ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both show(IOBuffer(), ExactMannWhitneyUTest([1:10;], [2.1:2:21;])) # Exact with ties @@ -30,7 +29,6 @@ show(IOBuffer(), ExactMannWhitneyUTest([1:10;], [2:2:24;])) @test abs(pvalue(ApproximateMannWhitneyUTest([1.5:10:100;], [2.1:2:21;])) - 0.0091) <= 1e-4 @test abs(pvalue(ApproximateMannWhitneyUTest([2.1:2:21;], [1.5:10:100;])) - 0.0091) <= 1e-4 @test tail(ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both -# @test HypothesisTests.default_tail(ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) == :both show(IOBuffer(), ApproximateMannWhitneyUTest([1:10;], [2.1:2:21;])) # Approximate with ties diff --git a/test/power_divergence.jl b/test/power_divergence.jl index 07dbef9f..9b8249e2 100644 --- a/test/power_divergence.jl +++ b/test/power_divergence.jl @@ -27,7 +27,6 @@ end @test pvalue(m) ≈ 2.9535891832117357e-7 @test tail(m) == :right -# @test HypothesisTests.default_tail(m) == :right @test m.stat ≈ 30.070149095754687 @test m.df ≈ 2 @test m.n ≈ 2757 diff --git a/test/wilcoxon.jl b/test/wilcoxon.jl index f882e44e..9dfeabe9 100644 --- a/test/wilcoxon.jl +++ b/test/wilcoxon.jl @@ -7,7 +7,6 @@ using HypothesisTests: tail @test abs(pvalue(ExactSignedRankTest([1:10;], [2:2:16; -1; 1])) - 0.4316) <= 1e-4 @test abs(pvalue(ExactSignedRankTest([2:2:16; -1; 1], [1:10;])) - 0.4316) <= 1e-4 @test tail(ExactSignedRankTest([1:10;], [2:2:20;])) == :both -# @test HypothesisTests.default_tail(ExactSignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), ExactSignedRankTest([1:10;], [2:2:20;])) # Exact with ties @@ -24,7 +23,6 @@ show(IOBuffer(), ExactSignedRankTest([1:10;], [1:10;])) @test abs(pvalue(ApproximateSignedRankTest([1:10;], [2:2:16; -1; 1])) - 0.4148) <= 1e-4 @test abs(pvalue(ApproximateSignedRankTest([2:2:16; -1; 1], [1:10;])) - 0.4148) <= 1e-4 @test tail(ApproximateSignedRankTest([1:10;], [2:2:20;])) == :both -# @test HypothesisTests.default_tail(ApproximateSignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), ApproximateSignedRankTest([1:10;], [2:2:20;])) # Approximate with ties @@ -39,7 +37,6 @@ show(IOBuffer(), ApproximateSignedRankTest([1:10;], [1:10;])) @test abs(pvalue(SignedRankTest([1:10;], [2:2:20;])) - 0.0020) <= 1e-4 @test abs(pvalue(SignedRankTest([1:10;], [2:11;])) - 0.0020) <= 1e-4 @test tail(SignedRankTest([1:10;], [2:2:20;])) == :both -# @test HypothesisTests.default_tail(SignedRankTest([1:10;], [2:2:20;])) == :both show(IOBuffer(), SignedRankTest([1:10;], [2:2:20;])) # One Sample tests diff --git a/test/z.jl b/test/z.jl index 3847fe41..d9c2b653 100644 --- a/test/z.jl +++ b/test/z.jl @@ -21,7 +21,6 @@ tst = OneSampleZTest(x) @test pvalue(tst; tail=:left) ≈ cdf(null, z) @test pvalue(tst; tail=:right) ≈ ccdf(null, z) @test tail(tst) == :both -# @test HypothesisTests.default_tail(tst) == :both show(IOBuffer(), tst) tst = OneSampleZTest(m, s, n) @@ -85,7 +84,6 @@ z = xbar / se @test pvalue(tst; tail=:left) ≈ cdf(null, z) @test pvalue(tst; tail=:right) ≈ ccdf(null, z) @test tail(tst) == :both -# @test HypothesisTests.default_tail(tst) == :both @test confint(tst)[1] ≈ xbar + quantile(null, 0.05 / 2) * se @test confint(tst)[2] ≈ xbar + cquantile(null, 0.05 / 2) * se show(IOBuffer(), tst)