Skip to content

Commit 19c37bb

Browse files
authored
LikelihoodRatioTest extends StatsAPI.HypothesisTest (#814)
* LikelihoodRatioTest extends StatsAPI.HypothesisTest * version bump + news
1 parent 7b37191 commit 19c37bb

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

NEWS.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
MixedModels v4.33.0 Release Notes
2+
==============================
3+
- `LikelihoodRatioTest` now extends `StatsAPI.HypothesisTest` and provides a method for `StatsAPI.pvalue`. [#814]
4+
15
MixedModels v4.32.0 Release Notes
26
==============================
37
- Added `lmm` and `glmm` as convenience wrappers for `fit(LinearMixedModel, ...)` and `fit(GeneralizedLinearMixedModel, ...)` respectively [#810]
@@ -19,7 +23,7 @@ MixedModels v4.29.1 Release Notes
1923
MixedModels v4.29.0 Release Notes
2024
==============================
2125
- Testbed for experimental support for using PRIMA as an optimization backend introduced via the experimental `prfit!` function. [#799]
22-
- Julia compat bound raised to current 1.10, i.e. current LTS. [#799]
26+
- Julia compat bound raised to current 1.10, i.e. current LTS. [#799]
2327

2428
MixedModels v4.28.0 Release Notes
2529
==============================
@@ -612,3 +616,4 @@ Package dependencies
612616
[#801]: https://github.com/JuliaStats/MixedModels.jl/issues/801
613617
[#802]: https://github.com/JuliaStats/MixedModels.jl/issues/802
614618
[#810]: https://github.com/JuliaStats/MixedModels.jl/issues/810
619+
[#814]: https://github.com/JuliaStats/MixedModels.jl/issues/814

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MixedModels"
22
uuid = "ff71e718-51f3-5ec2-a782-8ffcbfa3c316"
33
author = ["Phillip Alday <me@phillipalday.com>", "Douglas Bates <dmbates@gmail.com>"]
4-
version = "4.32.0"
4+
version = "4.33.0"
55

66
[deps]
77
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"

src/MixedModels.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ using StaticArrays: StaticArrays, SVector
3131
using Statistics: Statistics, mean, quantile, std
3232
using StatsAPI: StatsAPI, aic, aicc, bic, coef, coefnames, coeftable, confint, deviance
3333
using StatsAPI: dof, dof_residual, fit, fit!, fitted, isfitted, islinear, leverage
34-
using StatsAPI: loglikelihood, meanresponse, modelmatrix, nobs, predict, r2, residuals
34+
using StatsAPI:
35+
loglikelihood, meanresponse, modelmatrix, nobs, pvalue, predict, r2, residuals
3536
using StatsAPI: response, responsename, stderror, vcov, weights
3637
using StatsBase: StatsBase, CoefTable, model_response, summarystats
3738
using StatsFuns: log2π, normccdf
@@ -129,6 +130,7 @@ export @formula,
129130
profileσ,
130131
profilesigma,
131132
profilevc,
133+
pvalue,
132134
pwrss,
133135
ranef,
134136
raneftables,

src/likelihoodratiotest.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Results of MixedModels.likelihoodratiotest
1515
* `pvalues`
1616
1717
"""
18-
struct LikelihoodRatioTest
18+
struct LikelihoodRatioTest <: StatsAPI.HypothesisTest
1919
formulas::AbstractVector{String}
2020
models::NamedTuple{(:dof, :deviance)}
2121
tests::NamedTuple{(:dofdiff, :deviancediff, :pvalues)}
@@ -26,6 +26,29 @@ function Base.propertynames(lrt::LikelihoodRatioTest, private::Bool=false)
2626
return (:deviance, :formulas, :models, :pvalues, :tests)
2727
end
2828

29+
"""
30+
pvalue(lrt::LikelihoodRatioTest)
31+
32+
Extract the p-value associated with a likelihood ratio test.
33+
34+
For `LikelihoodRatioTest`s containing more than one model comparison, i.e. more than two models,
35+
this throws an error because it is unclear which p-value is desired.
36+
37+
To get p-values for multiple tests, use `lrt.pvalues`.
38+
"""
39+
function StatsAPI.pvalue(lrt::LikelihoodRatioTest)
40+
pvalues = lrt.pvalues
41+
if length(pvalues) > 1
42+
throw(
43+
ArgumentError(
44+
"Cannot extract **only one** p-value from a multiple test result."
45+
),
46+
)
47+
end
48+
49+
return only(pvalues)
50+
end
51+
2952
function Base.getproperty(lrt::LikelihoodRatioTest, s::Symbol)
3053
if s == :dof
3154
lrt.models.dof

test/likelihoodratiotest.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ end
5858
@test [deviance(fm0), deviance(fm1)] == lrt.deviance
5959
@test deviance(fm0) - deviance(fm1) == only(lrt.tests.deviancediff)
6060
@test only(lrt.tests.dofdiff) == 1
61-
@test sum(map(length,lrt.tests)) == 3
62-
@test sum(map(length,lrt.pvalues)) == 1
63-
@test sum(map(length,lrt.models)) == 4
61+
@test sum(length,lrt.tests) == 3
62+
@test sum(length,lrt.pvalues) == 1
63+
@test sum(length,lrt.models) == 4
6464
@test length(lrt.formulae) == 2
6565
show(IOBuffer(),lrt);
6666
@test :pvalues in propertynames(lrt)
67+
@test only(lrt.pvalues) == pvalue(lrt)
6768

6869
lrt = likelihoodratiotest(lm1,fm1)
6970
@test lrt.deviance likelihoodratiotest(lm1.model,fm1).deviance
@@ -73,6 +74,9 @@ end
7374
@test occursin("-2 logLik", shown)
7475
@test !occursin("deviance", shown)
7576

77+
lrt = likelihoodratiotest(lm0, fm0, fm1)
78+
@test_throws ArgumentError pvalue(lrt)
79+
7680
# non nested FE between non-mixed and mixed
7781
@test_throws ArgumentError likelihoodratiotest(lm1, fm0)
7882

0 commit comments

Comments
 (0)