Skip to content

Commit f7c2820

Browse files
add corrmat function that generates full correlation matrix of ReMat (#277)
* add corrmat function that generates full correlation matrix of ReMat * Work with the factor instead of the product Co-authored-by: Douglas Bates <[email protected]>
1 parent 9d84925 commit f7c2820

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/remat.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,43 @@ function σρs(A::ReMat{T}, sc::T) where {T}
550550
NamedTuple{(:σ,:ρ)}((σs, ntuple(i -> ρ(i,λ,im,indpairs,σs,sc), (k * (k - 1)) >> 1)))
551551
end
552552

553+
"""
554+
corrmat(A::ReMat)
555+
556+
Return the estimated correlation matrix for `A`. The diagonal elements are 1
557+
and the off-diagonal elements are the correlations between those random effect
558+
terms
559+
560+
# Example
561+
562+
```jldoctest
563+
julia> using MixedModels
564+
565+
julia> mod = fit(MixedModel,
566+
@formula(rt_trunc ~ 1 + spkr + prec + load + (1 + spkr + prec | subj)),
567+
MixedModels.dataset(:kb07));
568+
569+
julia> VarCorr(mod)
570+
Variance components:
571+
Column Variance Std.Dev. Corr.
572+
subj (Intercept) 136593.752 369.58592
573+
spkr: old 22923.238 151.40422 0.21
574+
prec: maintain 32348.431 179.85669 -0.98 -0.03
575+
Residual 642324.227 801.45133
576+
577+
julia> MixedModels.corrmat(mod.reterms[1])
578+
3×3 Array{Float64,2}:
579+
1.0 0.214843 -0.982951
580+
0.214843 1.0 -0.0316047
581+
-0.982951 -0.0316047 1.0
582+
```
583+
"""
584+
function corrmat(A::ReMat{T}) where {T}
585+
λ = A.λ
586+
λnorm = rownormalize!(copy!(zeros(T, size(λ)), λ))
587+
Symmetric(λnorm * λnorm', :L)
588+
end
589+
553590
vsize(A::ReMat{T,S}) where {T,S} = S
554591

555592
function zerocorr!(A::ReMat{T}) where {T}

0 commit comments

Comments
 (0)