InstEval
data in lme4 contain evaluation scores, Y
, by student, S
, of instructor, D
, in department, P
. R
indicates a service course.
In [1]:
using HDF5,ReTerms,StatsBase
inst = h5open("/var/tmp/dat.h5","r") do io g2dict(io,"inst") end;
dump(inst)
In [2]:
X = hcat(ones(length(inst[:Y])),inst[:R] .- 1);
size(X)
Out[2]:
In [3]:
m1 = LMM(X,[reterm(inst[s]) for s in [:S,:D,:P]],inst[:Y]);
for t in m1.trms println(size(t)) end
The profiled log-likelihood, $$ -2\ell(\bf\theta|y)=\log\left|\bf\Lambda_\theta'Z'Z\Lambda_\theta+I\right|+ n\left(1+\log\left(\frac{2\pi\rho^2(\theta)}n\right)\right), $$ where $$ \rho^2(\bf\theta)= \min_{\bf\beta,u} \left\| \begin{bmatrix} \bf y\\ \bf 0 \end{bmatrix} - \begin{bmatrix} \bf Z\Lambda_\theta & \bf X \\ \bf I_q & \bf 0 \end{bmatrix} \begin{bmatrix} \bf u\\ \beta \end{bmatrix} \right\|^2, $$ can be evaluated from the Cholesky factor of $$ \begin{bmatrix} \bf\Lambda_\theta'Z'Z\Lambda_\theta+I & \bf\Lambda_\theta'Z'X & \bf\Lambda_\theta'Z'y\\ \bf X'Z\Lambda_\theta & \bf X'X & \bf X'y\\ \bf y'Z\Lambda_\theta & \bf y'X & \bf y'y \end{bmatrix} $$
hcat
)
In [4]:
size(m1.A) # saved products (lower triangle)
Out[4]:
In [5]:
for j in 1:4, i in j:4
println(i,",",j,": ",size(m1.A[i,j])," ",typeof(m1.A[i,j]))
end
In [6]:
for j in 1:4, i in j:4 println(i,",",j,": ",typeof(m1.L[i,j])) end
In [8]:
@doc "In-place lower Cholesky factor by blocks"->
function mycfactor!(A::AbstractMatrix)
n = Base.LinAlg.chksquare(A)
@inbounds begin
for k = 1:n
for j in 1:(k - 1)
downdate!(A[k,k],A[k,j]) # A[k,k] -= A[k,j]*A[k,j]'
end
cfactor!(A[k,k]) # (lower) Cholesky factor of A[k,k]
for i in (k + 1):n
for j in 1:(k - 1)
downdate!(A[i,k],A[i,j],A[k,j]) # A[i,k] -= A[i,j]*A[k,j]
end
Base.LinAlg.A_rdiv_Bc!(A[i,k],A[k,k])
end
end
end
return LowerTriangular(A)
end
Out[8]:
In [9]:
@doc "Negative twice the log-likelihood"->
function myobjective(lmm::LMM)
n = size(lmm.trms[1],1)
logdet(lmm) + n*(1.+log(2π*abs2(lmm.L[end,end][end,end])/n))
end
Out[9]:
In [11]:
@doc "Log-determinant of Λ'Z'ZΛ + I"->
mylogdet(lmm::LMM) = 2.*mapreduce(logdet,(+),diag(lmm.L)[1:end-1])
Out[11]:
downdate!
by writing methods.