Skip to content

Commit c36ad59

Browse files
authored
correlation of fixed effects (#274)
* correlation of fixed effects * corr and vcov properties, add names to GLMM getproperties
1 parent 3294bd8 commit c36ad59

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

src/MixedModels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export @formula,
6464
coefnames,
6565
coeftable,
6666
cond,
67-
describeblocks,
6867
condVar,
68+
describeblocks,
6969
deviance,
7070
dispersion,
7171
dispersion_parameter,

src/generalizedlinearmixedmodel.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ function Base.getproperty(m::GeneralizedLinearMixedModel, s::Symbol)
373373
m.β
374374
elseif s (, :sigma)
375375
sdest(m)
376-
elseif s (:A, :L, , :lowerbd, :PCA, :rePCA, :optsum, :X, :reterms, :feterms, :formula, :σs, :σρs)
376+
elseif s (:A, :L, , :lowerbd, :corr, :vcov, :PCA, :rePCA, :optsum, :X, :reterms, :feterms, :formula, :σs, :σρs)
377377
getproperty(m.LMM, s)
378378
elseif s == :y
379379
m.resp.y
@@ -416,6 +416,10 @@ Base.propertynames(m::GeneralizedLinearMixedModel, private = false) = (
416416
:lowerbd,
417417
:σρs,
418418
:σs,
419+
:corr,
420+
:vcov,
421+
:PCA,
422+
:rePCA,
419423
fieldnames(typeof(m))...,
420424
)
421425

src/linearmixedmodel.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ function Base.getproperty(m::LinearMixedModel, s::Symbol)
450450
filter(Base.Fix2(isa, FeMat), getfield(m, :allterms))
451451
elseif s == :objective
452452
objective(m)
453+
elseif s == :corr
454+
vcov(m, corr=true)
455+
elseif s == :vcov
456+
vcov(m, corr=false)
453457
elseif s == :PCA
454458
NamedTuple{fnames(m)}(PCA.(m.reterms))
455459
elseif s == :pvalues
@@ -537,6 +541,8 @@ Base.propertynames(m::LinearMixedModel, private = false) = (
537541
:lowerbd,
538542
:X,
539543
:y,
544+
:corr,
545+
:vcov,
540546
:PCA,
541547
:rePCA,
542548
:reterms,
@@ -851,23 +857,30 @@ end
851857
Returns the estimate of σ², the variance of the conditional distribution of Y given B.
852858
"""
853859
varest(m::LinearMixedModel) = pwrss(m) / dof_residual(m)
860+
"""
861+
vcov(m::LinearMixedModel)
854862
855-
function StatsBase.vcov(m::LinearMixedModel{T}) where {T}
863+
Returns the variance-covariance matrix of the fixed effects.
864+
If `corr=true`, then correlation of fixed effects is returned instead.
865+
"""
866+
function StatsBase.vcov(m::LinearMixedModel{T}; corr=false) where {T}
856867
Xtrm = first(m.feterms)
857868
iperm = invperm(Xtrm.piv)
858869
p = length(iperm)
859870
r = Xtrm.rank
860871
Linv = inv(feL(m))
861872
permvcov = varest(m) * (Linv'Linv)
862873
if p == Xtrm.rank
863-
permvcov[iperm, iperm]
874+
vv = permvcov[iperm, iperm]
864875
else
865876
covmat = fill(zero(T) / zero(T), (p, p))
866877
for j = 1:r, i = 1:r
867878
covmat[i, j] = permvcov[i, j]
868879
end
869-
covmat[iperm, iperm]
880+
vv = covmat[iperm, iperm]
870881
end
882+
883+
corr ? StatsBase.cov2cor!(vv,stderror(m)) : vv
871884
end
872885

873886
"""

test/pls.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ end
207207
@test last(values(first(σs))) 5.716827808126002 atol=0.0001
208208
show(IOBuffer(), fm)
209209

210+
@test fm.vcov [43.986843599069985 -1.370391879315539; -1.370391879315539 2.2567113314443645] atol=0.0001
211+
@test fm.corr [1.0 -0.1375451787621904; -0.1375451787621904 1.0] atol=0.0001
212+
210213
u3 = ranef(fm, uscale=true)
211214
@test length(u3) == 1
212215
@test size(first(u3)) == (2, 18)

0 commit comments

Comments
 (0)