Skip to content

Commit dee41c2

Browse files
authored
Add missing methods for isnested for Table(Statistical|Regression)Model (#245)
* add isnested method for TableRegressionModel * also statistical model * bugfix bump patch * add test, one method
1 parent 7a377cd commit dee41c2

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StatsModels"
22
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
3-
version = "0.6.26"
3+
version = "0.6.27"
44

55
[deps]
66
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"

src/lrtest.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ julia> model = glm(@formula(Result ~ 1 + Treatment), dat, Binomial(), LogitLink(
5050
julia> bigmodel = glm(@formula(Result ~ 1 + Treatment + Other), dat, Binomial(), LogitLink());
5151
5252
julia> lrtest(nullmodel, model, bigmodel)
53-
┌ Warning: Could not check whether models are nested as model type TableRegressionModel does not implement isnested: results may not be meaningful
54-
└ @ StatsModels ~/.julia/dev/StatsModels/src/lrtest.jl:108
5553
Likelihood-ratio test: 3 models fitted on 12 observations
5654
──────────────────────────────────────────────
5755
DOF ΔDOF Deviance ΔDeviance p(>Chisq)
@@ -62,8 +60,6 @@ Likelihood-ratio test: 3 models fitted on 12 observations
6260
──────────────────────────────────────────────
6361
6462
julia> lrtest(bigmodel, model, nullmodel)
65-
┌ Warning: Could not check whether models are nested as model type TableRegressionModel does not implement isnested: results may not be meaningful
66-
└ @ StatsModels ~/.julia/dev/StatsModels/src/lrtest.jl:108
6763
Likelihood-ratio test: 3 models fitted on 12 observations
6864
──────────────────────────────────────────────
6965
DOF ΔDOF Deviance ΔDeviance p(>Chisq)

src/statsmodel.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ StatsBase.r2(mm::TableRegressionModel, variant::Symbol) = r2(mm.model, variant)
144144
StatsBase.adjr2(mm::TableRegressionModel, variant::Symbol) = adjr2(mm.model, variant)
145145
StatsBase.loglikelihood(mm::TableModels, c::Colon) = loglikelihood(mm.model, c)
146146

147+
isnested(m1::TableModels, m2::TableModels; kwargs...) = isnested(m1.model, m2.model; kwargs...)
148+
147149
function _return_predictions(T, yp::AbstractVector, nonmissings, len)
148150
out = Vector{Union{eltype(yp),Missing}}(missing, len)
149151
out[nonmissings] = yp

test/statsmodel.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ end
106106
StatsBase.fit(::Type{DummyModTwo}, ::Matrix, ::Vector) = DummyModTwo("hello!")
107107
Base.show(io::IO, m::DummyModTwo) = println(io, m.msg)
108108

109+
109110
@testset "stat model types" begin
110111

111112
## Test fitting
@@ -264,6 +265,19 @@ end
264265
[2] 2 1 3.2600 -10.7400 0.0010
265266
──────────────────────────────────────────────"""
266267

268+
@testset "isnested with TableRegressionModel" begin
269+
d = DataFrame(y=y, x1=x1, x2=x2)
270+
271+
m0 = fit(DummyMod, @formula(y ~ 1), d)
272+
m1 = fit(DummyMod, @formula(y ~ 1 + x1), d)
273+
m2 = fit(DummyMod, @formula(y ~ 1 + x1 * x2), d)
274+
275+
@test StatsModels.isnested(m0, m1)
276+
@test StatsModels.isnested(m1, m2)
277+
@test StatsModels.isnested(m0, m2)
278+
end
279+
280+
267281
m0 = DummyModNoIntercept(Float64[], ones(4, 0), y)
268282
m1 = DummyModNoIntercept([0.3], reshape(x1, :, 1), y)
269283
m2 = DummyModNoIntercept([0.25, 0.05, 0.04], [x1 x2 x1.*x2], y)

0 commit comments

Comments
 (0)