Skip to content

Commit

Permalink
Fix bug in ranef!, change isempty(offset₀) branch in setβθ! (#63)
Browse files Browse the repository at this point in the history
CI failures are on nightlies only and not related to this change.
  • Loading branch information
dmbates committed Oct 20, 2016
1 parent 5f7df30 commit b3bc8c0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/PIRLS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ function setβθ!{T}(m::GeneralizedLinearMixedModel{T}, v::Vector{T})
β, lm, offset₀, X, rr = m.β, m.LMM, m.offset₀, m.X, m.resp
lb = length(β)
copy!(β, view(v, 1 : lb))
isempty(offset₀) ? BLAS.gemv!('N', one(T), X, β, zero(T), rr.offset) :
BLAS.gemv!('N', one(T), X, β, one(T), copy!(rr.offset, offset₀))
setθ!(m.LMM, copy!(m.θ, view(v, (lb + 1) : length(v))))
BLAS.gemv!('N', one(T), X, β, one(T), isempty(offset₀) ? fill!(rr.offset, 0) : copy!(rr.offset, offset₀))
m
end

Expand Down
24 changes: 15 additions & 9 deletions src/mixedmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,21 @@ function grplevels(m::MixedModel)
end

"""
ranef!{T}(v::Vector{Matrix{T}}, m::MixedModel{T}, uscale::Bool = false)
ranef!{T}(v::Vector{Matrix{T}}, m::MixedModel{T}, β, uscale::Bool)
Overwrites `v` with the conditional modes of the random effects for `m`.
If `uscale` is `true` the random effects are on the spherical (i.e. `u`) scale, otherwise on the
original scale
"""
function ranef!(v::Vector, m::MixedModel, uscale)
function ranef!{T}(v::Vector, m::LinearMixedModel{T}, β::AbstractArray{T}, uscale::Bool)
R, Λ = m.R, m.Λ
k = length(Λ) # number of random-effects terms
for j in 1:k
copy!(v[j], R[j, end])
end
= R[k + 1, end]
if !isempty(rβ) # in the pirls! function for GLMMs want to skip this
β = vec(feR(m) \ rβ)
kp1 = k + 1
kp1 = k + 1
if !isempty(β) # in the pirls! function for GLMMs want to skip this
for j in 1:k # subtract the fixed-effects contribution
BLAS.gemv!('N', -1.0, R[j, kp1], β, 1.0, vec(v[j]))
end
Expand All @@ -130,9 +128,13 @@ function ranef!(v::Vector, m::MixedModel, uscale)
Rjj = R[j, j]
uj = vec(v[j])
LinAlg.A_ldiv_B!(isa(Rjj, Diagonal) ? Rjj : UpperTriangular(Rjj), uj)
for i in 1:j - 1
ui = vec(v[i])
ui -= R[i, j] * uj
for i in 1:(j - 1)
Rij = R[i, j]
if isa(Rij, StridedMatrix{T})
BLAS.gemv!('N', -one(T), R[i, j], uj, one(T), vec(v[i]))
else
A_mul_B!(-one(T), R[i, j], uj, one(T), vec(v[i]))
end
end
end
if !uscale
Expand All @@ -143,6 +145,10 @@ function ranef!(v::Vector, m::MixedModel, uscale)
v
end

function ranef!(v::Vector, m::LinearMixedModel, uscale::Bool)
ranef!(v, m, feR(m) \ vec(m.R[end-1, end]), uscale)
end

"""
ranef{T}(m::MixedModel{T}, uscale=false)
Expand Down
4 changes: 2 additions & 2 deletions test/pls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ end
@test isapprox(objective(fm1), 237585.5534151694, atol = 1e-3)
ftd1 = fitted(fm1);
@test size(ftd1) == (73421,)
@test isapprox(ftd1[1], 3.1783062136367723, atol = 1e-5)
@test isapprox(ftd1[1], 3.19745, atol = 1e-4)
resid1 = residuals(fm1);
@test size(resid1) == (73421,)
@test isapprox(resid1[1], 1.8216937863632277, atol = 1e-5)
@test isapprox(resid1[1], 1.8025537831086234, atol = 1e-5)

fm2 = fit!(lmm(y ~ 1 + service + (1 | s) + (1 | d) + (1 | dept), InstEval));
@test size(fm2) == (73421,2,4114,3)
Expand Down

0 comments on commit b3bc8c0

Please sign in to comment.