Skip to content

Commit a64e165

Browse files
committed
fix some tests
1 parent 8d38410 commit a64e165

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

src/likelihoodratiotest.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ end
8484
_formula(x::MixedModel) = string(formula(x))
8585

8686

87+
Base.show(io::IO, lrt::LikelihoodRatioTest) = show(io, MIME("text/plain"), lrt)
8788
function Base.show(io::IO, ::MIME"text/plain", lrt::LikelihoodRatioTest{N}) where {N}
8889
println(io, "Likelihood-ratio test: $N models fitted on $(lrt.nobs) observations")
8990
println(io, "Model Formulae")
@@ -103,20 +104,20 @@ function Base.show(io::IO, ::MIME"text/plain", lrt::LikelihoodRatioTest{N}) wher
103104

104105
outrows[1, :] = ["",
105106
"DoF",
106-
"logLik",
107+
"-2 logLik",
107108
"χ²",
108109
"χ²-dof", "P(>χ²)"]
109110
outrows[2, :] = ["[1]",
110111
@sprintf("%.0d", lrt.dof[1]),
111-
@sprintf("%.4f", lrt.loglikelihood[1]),
112-
" ",
113-
" ",
114-
" "]
112+
@sprintf("%.4f", -2 * lrt.loglikelihood[1]),
113+
"",
114+
"",
115+
""]
115116

116117
for i in 2:nr
117118
outrows[i+1, :] = ["[$i]",
118119
@sprintf("%.0d", lrt.dof[i]),
119-
@sprintf("%.4f", lrt.loglikelihood[i]),
120+
@sprintf("%.4f", -2 * lrt.loglikelihood[i]),
120121
@sprintf("%.4f", chisq[i-1]),
121122
@sprintf("%.0d", Δdf[i-1]),
122123
string(StatsBase.PValue(lrt.pval[i]))]

test/likelihoodratiotest.jl

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,69 +71,69 @@ end
7171
lm0 = lm(@formula(reaction ~ 1), slp)
7272
lm1 = lm(@formula(reaction ~ 1 + days), slp)
7373

74-
# @test MixedModels._iscomparable(lm0, fm1)
75-
@test !MixedModels._iscomparable(lm1, fm0)
74+
@test MixedModels.isnested(lm0, fm1)
75+
@test !MixedModels.isnested(lm1, fm0)
7676

7777
lrt = likelihoodratiotest(fm0, fm1)
7878

7979
@test (deviance(fm0), deviance(fm1)) == lrt.deviance
80-
@test sprint(show, lrt) == "MixedModels.LikelihoodRatioTest{2}((\"reaction ~ 1 + (1 + days | subj)\", \"reaction ~ 1 + days + (1 + days | subj)\"), Likelihood-ratio test: 2 models fitted on 180 observations\n────────────────────────────────────────────────────────\n DOF ΔDOF LogLik Deviance Chisq p(>Chisq)\n────────────────────────────────────────────────────────\n[1] 5 -887.7379 1775.4759 \n[2] 6 1 -875.9697 1751.9393 23.5365 <1e-05\n────────────────────────────────────────────────────────, true)"
80+
@test sprint(show, lrt) == "Likelihood-ratio test: 2 models fitted on 180 observations\nModel Formulae\n1: reaction ~ 1 + (1 + days | subj)\n2: reaction ~ 1 + days + (1 + days | subj)\n────────────────────────────────────────────\n DoF -2 logLik χ² χ²-dof P(>χ²)\n────────────────────────────────────────────\n[1] 5 1775.4759 \n[2] 6 1751.9393 23.5365 1 <1e-05\n────────────────────────────────────────────"
8181
@test last(lrt.pvalues) == pvalue(lrt)
8282

8383
lrt = likelihoodratiotest(lm1, fm1)
8484
@test pvalue(lrt) 5.9e-32 atol=1e-16
8585

86-
# shown = sprint(show, lrt)
87-
# lrt = likelihoodratiotest(lm0, fm0, fm1)
88-
# @test_throws ArgumentError pvalue(lrt)
86+
lrt = likelihoodratiotest(lm0, fm0, fm1)
87+
@test_throws ArgumentError pvalue(lrt)
8988

90-
# # non nested FE between non-mixed and mixed
91-
# @test_throws ArgumentError likelihoodratiotest(lm1, fm0)
89+
# non nested FE between non-mixed and mixed
90+
@test_throws ArgumentError likelihoodratiotest(lm1, fm0)
9291

93-
# # mix of REML and ML
94-
# fm0 = fit(
95-
# MixedModel,
96-
# @formula(reaction ~ 1 + (1 + days | subj)),
97-
# slp;
98-
# REML=true,
99-
# progress=false,
100-
# )
101-
# @test_throws ArgumentError likelihoodratiotest(fm0, fm1)
102-
# @test_throws ArgumentError likelihoodratiotest(lm0, fm0)
92+
# mix of REML and ML
93+
fm0 = fit(
94+
MixedModel,
95+
@formula(reaction ~ 1 + (1 + days | subj)),
96+
slp;
97+
REML=true,
98+
progress=false,
99+
)
100+
@test_throws ArgumentError likelihoodratiotest(fm0, fm1)
101+
@test_throws ArgumentError likelihoodratiotest(lm0, fm0)
103102

104-
# # differing FE with REML
105-
# fm1 = fit(
106-
# MixedModel,
107-
# @formula(reaction ~ 1 + days + (1 + days | subj)),
108-
# slp;
109-
# REML=true,
110-
# progress=false,
111-
# )
103+
# differing FE with REML
104+
fm1 = fit(
105+
MixedModel,
106+
@formula(reaction ~ 1 + days + (1 + days | subj)),
107+
slp;
108+
REML=true,
109+
progress=false,
110+
)
112111

113-
# @test_throws ArgumentError likelihoodratiotest(fm0, fm1)
114-
# contra = MixedModels.dataset(:contra)
115-
# # glm doesn't like categorical responses, so we convert it to numeric ourselves
116-
# # TODO: upstream fix
117-
# cc = DataFrame(contra)
118-
# cc.usenum = ifelse.(cc.use .== "Y", 1, 0)
119-
# gmf = glm(@formula(usenum ~ 1 + age + urban + livch), cc, Bernoulli())
120-
# gmf2 = glm(@formula(usenum ~ 1 + age + abs2(age) + urban + livch), cc, Bernoulli())
121-
# gm0 = fit(
122-
# MixedModel,
123-
# @formula(use ~ 1 + age + urban + livch + (1 | urban & dist)),
124-
# contra,
125-
# Bernoulli();
126-
# fast=true,
127-
# progress=false,
128-
# )
129-
# gm1 = fit(
130-
# MixedModel,
131-
# @formula(use ~ 1 + age + abs2(age) + urban + livch + (1 | urban & dist)),
132-
# contra,
133-
# Bernoulli();
134-
# fast=true,
135-
# progress=false,
136-
# )
112+
@test_throws ArgumentError likelihoodratiotest(fm0, fm1)
113+
114+
contra = MixedModels.dataset(:contra)
115+
# glm doesn't like categorical responses, so we convert it to numeric ourselves
116+
# TODO: upstream fix
117+
cc = DataFrame(dataset(:contra))
118+
cc.usenum = ifelse.(cc.use .== "Y", 1, 0)
119+
gmf = glm(@formula(usenum ~ 1 + age + urban + livch), cc, Bernoulli())
120+
gmf2 = glm(@formula(usenum ~ 1 + age + abs2(age) + urban + livch), cc, Bernoulli())
121+
gm0 = fit(
122+
MixedModel,
123+
@formula(use ~ 1 + age + urban + livch + (1 | urban & dist)),
124+
dataset(:contra),
125+
Bernoulli();
126+
fast=true,
127+
progress=false,
128+
)
129+
gm1 = fit(
130+
MixedModel,
131+
@formula(use ~ 1 + age + abs2(age) + urban + livch + (1 | urban & dist)),
132+
dataset(:contra),
133+
Bernoulli();
134+
fast=true,
135+
progress=false,
136+
)
137137

138138
# lrt = likelihoodratiotest(gmf, gm1)
139139
# @test [-2 * loglikelihood(gmf), deviance(gm1)] ≈ lrt.deviance

0 commit comments

Comments
 (0)