Skip to content

Instantly share code, notes, and snippets.

@sckott
Created October 13, 2011 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sckott/1284287 to your computer and use it in GitHub Desktop.
Save sckott/1284287 to your computer and use it in GitHub Desktop.
PGLMM reml
######################################
## Created by Matthew Helmus
######################################
PGLMM.reml<-function(sp)
{
#global tH tinvW tVV tX
sp<-abs(Re(sp))
Cd<-matrix(0,dim(tVV[[1]])[1],dim(tVV[[1]])[2])
for(i in 1:length(sp))
{
Cd=Cd + sp[i] * tVV[[i]]
}
V<- tinvW + Cd
invV<- solve(V,diag(x=1,nrow=dim(V)[1],ncol=dim(V)[2]))
# check to see if V is positive definite
if(all( eigen(V)$values >0 ))
{
cholV<-chol(V)
# ML
# LL=.5*(2*sum(log(diag(chol(V))))+tH'*invV*tH)
#REML
LL=.5 * (2 * sum(log(diag(cholV))) + t(tH) %*% invV %*% tH + log(det(t(tX) %*% invV %*% tX)))
} else {
LL<-10^10
}
LL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment