Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed Sep 3, 2020
1 parent 7fd83a3 commit 61614a7
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/var_equality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ struct VarianceEqualityTest{TD <: ContinuousUnivariateDistribution} <: Hypothesi
SSeᵢ::Vector{Float64}
DFt::Int
DFe::Int
description::Tuple{String, String, String} # test name, parameter of interest, test statistic name
# test name, parameter of interest, test statistic name
description::Tuple{String, String, String}
end

population_param_of_interest(t::VarianceEqualityTest) = (t.description[2], "all equal", NaN)
population_param_of_interest(t::VarianceEqualityTest) =
(t.description[2], "all equal", NaN)
testname(t::VarianceEqualityTest) = t.description[1]
teststatisticname(t::VarianceEqualityTest{TD}) where {TD <: ContinuousDistribution} =
length(t.description[3]) != 0 ? t.description[3] : (TD <: FDist ? "F" : "χ²")
Expand All @@ -25,9 +27,11 @@ function teststatistic(t::VarianceEqualityTest{Chisq})
y = sum(t.SStᵢ)/sum(t.SSeᵢ)
y*(t.DFe+t.DFt)/(1 + y) # sum(t.SStᵢ)/t.s²
end
pvalue(t::VarianceEqualityTest{TD}; tail=:right) where {TD <: ContinuousDistribution} = pvalue(TD(dof(t)...), teststatistic(t), tail=tail)
pvalue(t::VarianceEqualityTest{TD}; tail=:right) where {TD <: ContinuousDistribution} =
pvalue(TD(dof(t)...), teststatistic(t), tail=tail)

function show_params(io::IO, t::VarianceEqualityTest{TD}, indent="") where {TD <: ContinuousDistribution}
function show_params(io::IO, t::VarianceEqualityTest{TD},
indent="") where {TD <: ContinuousDistribution}
println(io, indent, "number of observations: ", nobs(t))
println(io, indent, rpad("$(teststatisticname(t)) statistic:", 24), teststatistic(t))
println(io, indent, "degrees of freedom: ", dof(t))
Expand All @@ -45,7 +49,8 @@ function Base.show(io::IOContext, t::VarianceEqualityTest)
MSe = SSe/t.DFe
println(io, "Source SS DF MS F P-value")
println(io, repeat("-", 55))
StatsBase.@printf(io, "Treatments %8.3f %4d %8.3f %8.3f %7.5f\n", SSt, t.DFt, MSt, MSt/MSe, pvalue(t))
StatsBase.@printf(io, "Treatments %8.3f %4d %8.3f %8.3f %7.5f\n",
SSt, t.DFt, MSt, MSt/MSe, pvalue(t))
StatsBase.@printf(io, "Error %8.3f %4d %8.3f\n", SSe, t.DFe, MSe)
println(io, repeat("-", 55))
StatsBase.@printf(io, "Total %8.3f %4d\n", SSt+SSe, t.DFt+t.DFe)
Expand All @@ -64,12 +69,15 @@ end
"""
OneWayANOVATest(groups::AbstractVector{<:Real}...)
Perform one-way analysis of variance test of the hypothesis that that the `groups` means are equal.
Perform one-way analysis of variance test of the hypothesis that that the `groups`
means are equal.
The one-way analysis of variance (one-way ANOVA) is a technique that can be used to compare means of two or more samples.
The ANOVA tests the null hypothesis, which states that samples in all groups are drawn from populations with the same mean values.
The one-way analysis of variance (one-way ANOVA) is a technique that can be used
to compare means of two or more samples. The ANOVA tests the null hypothesis, which states
that samples in all groups are drawn from populations with the same mean values.
To do this, two estimates are made of the population variance.
The ANOVA produces an F-statistic, the ratio of the variance calculated among the means to the variance within the samples.
The ANOVA produces an F-statistic, the ratio of the variance calculated among
the means to the variance within the samples.
Implements: [`pvalue`](@ref)
Expand All @@ -81,7 +89,8 @@ Implements: [`pvalue`](@ref)
function OneWayANOVATest(groups::AbstractVector{<:Real}...)
Nᵢ, SStᵢ, SSeᵢ = anova(groups...)
k = length(Nᵢ)
VarianceEqualityTest{FDist}(Nᵢ, SStᵢ, SSeᵢ, k-1, sum(Nᵢ)-k, ("One-way analysis of variance (ANOVA) test","Means","F"))
VarianceEqualityTest{FDist}(Nᵢ, SStᵢ, SSeᵢ, k-1, sum(Nᵢ)-k,
("One-way analysis of variance (ANOVA) test","Means","F"))
end

"""
Expand All @@ -96,7 +105,8 @@ other functions are accepted: x² or √|x|.
The test statistic, ``W``, is equivalent to the ``F`` statistic, and is defined as follows:
```math
W = \\frac{(N-k)}{(k-1)} \\cdot \\frac{\\sum_{i=1}^k N_i (Z_{i\\cdot}-Z_{\\cdot\\cdot})^2} {\\sum_{i=1}^k \\sum_{j=1}^{N_i} (Z_{ij}-Z_{i\\cdot})^2},
W = \\frac{(N-k)}{(k-1)} \\cdot \\frac{\\sum_{i=1}^k N_i (Z_{i\\cdot}-Z_{\\cdot\\cdot})^2}
{\\sum_{i=1}^k \\sum_{j=1}^{N_i} (Z_{ij}-Z_{i\\cdot})^2},
```
where
Expand All @@ -114,7 +124,8 @@ Implements: [`pvalue`](@ref)
# References
* Levene, Howard, "Robust tests for equality of variances". In Ingram Olkin; Harold Hotelling; et al. (eds.).
* Levene, Howard, "Robust tests for equality of variances".
In Ingram Olkin; Harold Hotelling; et al. (eds.).
Contributions to Probability and Statistics: Essays in Honor of Harold Hotelling.
Stanford University Press. pp. 278–292, 1960
Expand All @@ -129,15 +140,17 @@ function LeveneTest(groups::AbstractVector{<:Real}...; scorediff=abs, statistic=
# anova
Nᵢ, SStᵢ, SSeᵢ = anova(Zᵢⱼ...)
k = length(Nᵢ)
VarianceEqualityTest{FDist}(Nᵢ, SStᵢ, SSeᵢ, k-1, sum(Nᵢ)-k, ("Levene's test","Variances","W"))
VarianceEqualityTest{FDist}(Nᵢ, SStᵢ, SSeᵢ, k-1, sum(Nᵢ)-k,
("Levene's test","Variances","W"))
end

"""
BrownForsytheTest(groups::AbstractVector{<:Real}...)
The Brown–Forsythe test is a statistical test for the equality of `groups` variances.
The Brown–Forsythe test is a modification of the Levene's test with the median instead of the mean statistic for computing the spread within each group.
The Brown–Forsythe test is a modification of the Levene's test with the median instead
of the mean statistic for computing the spread within each group.
Implements: [`pvalue`](@ref)
Expand All @@ -161,7 +174,8 @@ Perform Fligner-Killeen median test of the null hypothesis that the `groups`
have equal variances, a test for homogeneity of variances.
This test is most robust against departures from normality, see references.
It is a ``k``-sample simple linear rank method that uses the ranks of the absolute values of the centered samples and weights
It is a ``k``-sample simple linear rank method that uses the ranks of the absolute values
of the centered samples and weights
```math
a_{N,i} = \\Phi^{-1}(1/2 + (i/2(N+1)))
```
Expand Down Expand Up @@ -191,5 +205,6 @@ function FlignerKilleenTest(groups::AbstractVector{<:Real}...)
# anova
Nᵢ, SStᵢ, SSeᵢ = anova(Qᵢⱼ...)
k = length(Nᵢ)
t3 = VarianceEqualityTest{Chisq}(Nᵢ, SStᵢ, SSeᵢ, k-1, sum(Nᵢ)-k, ("Fligner-Killeen test","Variances","FK"))
t3 = VarianceEqualityTest{Chisq}(Nᵢ, SStᵢ, SSeᵢ, k-1, sum(Nᵢ)-k,
("Fligner-Killeen test","Variances","FK"))
end

0 comments on commit 61614a7

Please sign in to comment.