11using DataFrames
22using Distributions
33using MixedModels
4+ using PooledArrays
5+ using StableRNGs
46using Tables
57using Test
68
79using MixedModels: dataset
10+ using GLM: linkinv
811
912const gfms = Dict (
1013 :cbpp => [@formula ((incid/ hsz) ~ 1 + period + (1 | herd))],
@@ -138,17 +141,28 @@ end
138141 @test only (m11. θ) ≈ 1.838245201739852 atol= 1.e-5
139142end
140143
144+ @testset " dispersion parameter" begin
141145
142- # NB: "deviance" is complicated in lme4
143- # there are several "deviances" defined:
144- # https://github.com/lme4/lme4/issues/375#issuecomment-214494445
145- # this is the way deviance(glmm) is computed in lme4
146- # also called devianceCondRel in
147- # https://github.com/lme4/lme4/blob/master/misc/logLikGLMM/logLikGLMM.R
148- lme4deviance (x) = sum (x. resp. devresid)
149-
146+ # NB: "deviance" is complicated in lme4
147+ # there are several "deviances" defined:
148+ # https://github.com/lme4/lme4/issues/375#issuecomment-214494445
149+ # this is the way deviance(glmm) is computed in lme4
150+ # also called devianceCondRel in
151+ # https://github.com/lme4/lme4/blob/master/misc/logLikGLMM/logLikGLMM.R
152+ lme4deviance (x) = sum (x. resp. devresid)
153+
154+ # # simulate some data ##
155+ rng = StableRNG (42 )
156+ ng = 25
157+ ns = 500
158+ # random effect
159+ σ = 1
160+ σre = 0.5
161+ u = repeat (randn (rng, ns) .* σre, ng)
162+ id = PooledArray (string .(repeat (1 : ns, ng)))
163+ # fixed effect
164+ x = rand (rng, ns* ng)
150165
151- @testset " dispersion parameter" begin
152166 @testset " Gaussian with non identity link" begin
153167 dyestuff = MixedModels. dataset (:dyestuff )
154168 gauss = fit (MixedModel, only (gfms[:dyestuff ]), dyestuff, Normal (), LogLink (), fast= true )
@@ -160,5 +174,18 @@ lme4deviance(x) = sum(x.resp.devresid)
160174 @test lme4deviance (gauss) ≈ 115187.5
161175 @test only (gauss. θ) ≈ 0.0
162176 @test only (gauss. beta) ≈ 7.331387691046023
177+ # this works, but starting from a clean fit doesn't
178+ # I'm not sure why the optimizer gets lost
179+ refit! (gauss, fast= false )
180+
181+ rng = StableRNG (42 )
182+ link = InverseLink ()
183+ offset = abs (minimum (u)) + 1
184+ y = map (d -> rand (rng, d), Normal .(linkinv .(link, u .+ offset), σ))
185+ dat = (u= u, id= id, x= x, y= y)
186+ gaussim = GeneralizedLinearMixedModel (@formula (y ~ 1 + (1 | id)), dat,
187+ Normal (), InverseLink ())
188+ fit! (gaussim, fast= true )
189+ @test only (gaussim. β) ≈ linkfun (link, mean (y))
163190 end
164191end
0 commit comments