Dear all,
For a project I need to calculate the conditional AIC of a mixed effects
model.
Luckily, I found a reference in the R help forum for a function to be used:
CAIC <- function(model) {
sigma <- attr(VarCorr(model), 'sc')
observed <- attr(model, 'y')
predicted <- fitted(model)
cond.loglik <- sum(dnorm(observed, predicted, sigma, log=TRUE))
rho <- hatTrace(model)
p <- length(fixef(model))
N <- nrow(attr(model, 'X'))
K.corr <- N*(N-p-1)*(rho+1)/((N-p)*(N-p-2)) +
N*(p+1)/((N-p)*(N-p-2))
CAIC <- -2*cond.loglik + 2*K.corr
return(CAIC)
}
Reference:
http://r.789695.n4.nabble.com/Using-Conditional-AIC-with-lmer-td847899.html
However, in this function the function hatTrace of the lme4 package was used
to calcuate rho
(line: rho <- hatTrace(model) )
Sadly, R tells me that this function could not be found. A search on the
internet revealed that this function was dropped out of the new package
release of lme4.
Can anyone help me with this? Is there an alternative function that gives me
the same result as the hatTrace function?
I searched the internet for solutions, but I could not find anything that is
helpful.
Thanks in advance,
Bye,
Inga
--
View this message in context:
http://r.789695.n4.nabble.com/Function-hatTrace-in-package-lme4-tp4646071.html
Sent from the R help mailing list archive at Nabble.com.
ingaschwabe <ingaschwabe <at> gmail.com> writes:> > Dear all, > > For a project I need to calculate the conditional AIC of a mixed effects > model. > Luckily, I found a reference in the R help forum for a function to be used: > > CAIC <- function(model) { > > sigma <- attr(VarCorr(model), 'sc') > observed <- attr(model, 'y') > predicted <- fitted(model) > cond.loglik <- sum(dnorm(observed, predicted, sigma, log=TRUE)) > > rho <- hatTrace(model) > p <- length(fixef(model)) > N <- nrow(attr(model, 'X')) > K.corr <- N*(N-p-1)*(rho+1)/((N-p)*(N-p-2)) + > N*(p+1)/((N-p)*(N-p-2)) > > CAIC <- -2*cond.loglik + 2*K.corr > > return(CAIC) > > } > > Reference: > http://r.789695.n4.nabble.com/Using-Conditional-AIC-with-lmer-td847899.html > > However, in this function the function hatTrace of the lme4 package was used > to calcuate rho > (line: rho <- hatTrace(model) ) > > Sadly, R tells me that this function could not be found. A search on the > internet revealed that this function was dropped out of the new package > release of lme4. > > Can anyone help me with this? Is there an alternative function that gives me > the same result as the hatTrace function?You're not the first to ask about this. Unfortunately, this disappeared from lme4 when the internal structures of the fitted models changed, and the computation is not trivially easy to re-implement. Searching with library(sos) findFn("{hat matrix} trace lme4") didn't turn up anything either. Details of the old calculation are available at http://sourcecodebrowser.com/lme4/0.99875-9 /lmer_8c.html#ac7ea98c698b5bd4fe72bb06da1bba62a followups to r-sig-mixed-models <at> r-project.org (broken URL for gmane length limits)