1
1
using DataFrames
2
2
using Distributions
3
3
using MixedModels
4
+ using PooledArrays
5
+ using StableRNGs
4
6
using Tables
5
7
using Test
6
8
7
9
using MixedModels: dataset
10
+ using GLM: linkinv
8
11
9
12
const gfms = Dict (
10
13
:cbpp => [@formula ((incid/ hsz) ~ 1 + period + (1 | herd))],
@@ -138,17 +141,28 @@ end
138
141
@test only (m11. θ) ≈ 1.838245201739852 atol= 1.e-5
139
142
end
140
143
144
+ @testset " dispersion parameter" begin
141
145
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)
150
165
151
- @testset " dispersion parameter" begin
152
166
@testset " Gaussian with non identity link" begin
153
167
dyestuff = MixedModels. dataset (:dyestuff )
154
168
gauss = fit (MixedModel, only (gfms[:dyestuff ]), dyestuff, Normal (), LogLink (), fast= true )
@@ -160,5 +174,18 @@ lme4deviance(x) = sum(x.resp.devresid)
160
174
@test lme4deviance (gauss) ≈ 115187.5
161
175
@test only (gauss. θ) ≈ 0.0
162
176
@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))
163
190
end
164
191
end
0 commit comments