Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

T-tests Request different degrees of freedom #188

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions src/t.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export OneSampleTTest, TwoSampleTTest, EqualVarianceTTest,
abstract type TTest <: HypothesisTest end
abstract type TwoSampleTTest <: TTest end

pvalue(x::TTest; tail=:both) = pvalue(TDist(x.df), x.t; tail=tail)
pvalue(x::TTest; tail=:both) = pvalue(TDist(x.dof), x.t; tail=tail)

default_tail(test::TTest) = :both

Expand All @@ -41,7 +41,7 @@ function StatsBase.confint(x::TTest; level::Float64=0.95, tail=:both)
elseif tail == :right
(StatsBase.confint(x, level=1-(1-level)*2)[1], Inf)
elseif tail == :both
q = quantile(TDist(x.df), 1-(1-level)/2)
q = quantile(TDist(x.dof), 1-(1-level)/2)
(x.xbar-q*x.stderr, x.xbar+q*x.stderr)
else
throw(ArgumentError("tail=$(tail) is invalid"))
Expand All @@ -54,7 +54,7 @@ end
struct OneSampleTTest <: TTest
n::Int # number of observations
xbar::Real # estimated mean
df::Int # degrees of freedom
dof::Int # degrees of freedom
stderr::Real # empirical standard error
t::Real # t-statistic
μ0::Real # mean under h_0
Expand All @@ -66,24 +66,24 @@ population_param_of_interest(x::OneSampleTTest) = ("Mean", x.μ0, x.xbar) # para
function show_params(io::IO, x::OneSampleTTest, ident="")
println(io, ident, "number of observations: $(x.n)")
println(io, ident, "t-statistic: $(x.t)")
println(io, ident, "degrees of freedom: $(x.df)")
println(io, ident, "degrees of freedom: $(x.dof)")
println(io, ident, "empirical standard error: $(x.stderr)")
end

"""
OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real = 0)
OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real = 0, dof::Int = n-1)

Perform a one sample t-test of the null hypothesis that `n` values with mean `xbar` and
sample standard deviation `stddev` come from a distribution with mean `μ0` against the
alternative hypothesis that the distribution does not have mean `μ0`.
alternative hypothesis that the distribution does not have mean `μ0`. Degrees of freedom
`dof` may be specified.

Implements: [`pvalue`](@ref), [`confint`](@ref)
"""
function OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real=0)
function OneSampleTTest(xbar::Real, stddev::Real, n::Int, μ0::Real=0, dof::Int=n-1)
stderr = stddev/sqrt(n)
t = (xbar-μ0)/stderr
df = n-1
OneSampleTTest(n, xbar, df, stderr, t, μ0)
OneSampleTTest(n, xbar, dof, stderr, t, μ0)
end

"""
Expand Down Expand Up @@ -119,7 +119,7 @@ struct EqualVarianceTTest <: TwoSampleTTest
n_x::Int # number of observations
n_y::Int # number of observations
xbar::Real # estimated mean difference
df::Int # degrees of freedom
dof::Int # degrees of freedom
stderr::Real # empirical standard error
t::Real # t-statistic
μ0::Real # mean difference under h_0
Expand All @@ -128,30 +128,31 @@ end
function show_params(io::IO, x::TwoSampleTTest, ident="")
println(io, ident, "number of observations: [$(x.n_x),$(x.n_y)]")
println(io, ident, "t-statistic: $(x.t)")
println(io, ident, "degrees of freedom: $(x.df)")
println(io, ident, "degrees of freedom: $(x.dof)")
println(io, ident, "empirical standard error: $(x.stderr)")
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

"""
EqualVarianceTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real})
EqualVarianceTTest(x::AbstractVector{T<:Real}, y::AbstractVector{T<:Real},
dof::Int = length(x)+length(y)-2)

Perform a two-sample t-test of the null hypothesis that `x` and `y` come from distributions
with equal means and variances against the alternative hypothesis that the distributions
have different means but equal variances.
have different means but equal variances. Degrees of freedom `dof` may be specified.

Implements: [`pvalue`](@ref), [`confint`](@ref)
"""
function EqualVarianceTTest(x::AbstractVector{T}, y::AbstractVector{S}, μ0::Real=0) where {T<:Real,S<:Real}
birm marked this conversation as resolved.
Show resolved Hide resolved
function EqualVarianceTTest(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, μ0::Real=0,
dof::Int=length(x)+length(y)-2)
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, dof, stderr, t, μ0)
end


Expand All @@ -161,7 +162,7 @@ struct UnequalVarianceTTest <: TwoSampleTTest
n_x::Int # number of observations
n_y::Int # number of observations
xbar::Real # estimated mean
df::Real # degrees of freedom
dof::Real # degrees of freedom
stderr::Real # empirical standard error
t::Real # t-statistic
μ0::Real # mean under h_0
Expand Down Expand Up @@ -192,6 +193,6 @@ function UnequalVarianceTTest(x::AbstractVector{T}, y::AbstractVector{S}, μ0::R
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)
dof = (varx / nx + vary / ny)^2 / ((varx / nx)^2 / (nx - 1) + (vary / ny)^2 / (ny - 1))
UnequalVarianceTTest(nx, ny, xbar, dof, stderr, t, μ0)
end
4 changes: 2 additions & 2 deletions test/t.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ end
a2 = [29.89, 29.93, 29.72, 29.98, 30.02, 29.98]

tst = EqualVarianceTTest(a1, a2)
@test tst.df == 10
@test tst.dof == 10
@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
show(IOBuffer(), tst)

tst = UnequalVarianceTTest(a1, a2)
@test abs(tst.df - 7.03) <= 0.01
@test abs(tst.dof - 7.03) <= 0.01
@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)
Expand Down