AIC(fit1)

Posted on
Picture of user. tbates Joined: 07/31/2009
would it be worth getting this to work for Mx as an accessor function for AIC, with the benefit of being consilient with other functions in R?
AIC(fit1)
Error in UseMethod("logLik") :
no applicable method for 'logLik' applied to an object of class "MxModel"
Replied on Thu, 12/06/2012 - 09:22
Picture of user. brandmaier Joined: 02/04/2010

I very much like the idea to place the class MxModel into the generic R framework for likelihood-based calculations. I hacked together the function and this seems to work ad hoc.

logLik.MxModel<-function(model)
{
ll <- NA
if (!is.null(model@output) & !is.null(model@output$Minus2LogLikelihood))
ll <- -0.5*model@output$Minus2LogLikelihood

if (!is.null(model@data))
attr(ll,"nobs") <- model@data@numObs
else
attr(ll,"nobs") <- NA
if (!is.null(model@output))
attr(ll,"df")<- length(model@output$estimate)
else
attr(ll,"df") <- NA
class(ll) <- "logLik"
return(ll);
}

This can be used to obtain log-likelihood and/or AIC, e.g., by typing


model <- mxRun(mxModel(...))

logLik(model)
AIC(model)

There is one more thing. As far as I understand, R defines the AIC based on the -2loglikelihood and degrees of freedom equal to the freely estimated parameters in the model, whereas OpenMx defines AIC based on the chi^2 and the degrees of freedom equal to the difference of parameters between the model and the saturated model. The AIC difference between any two models will come out the same, either way, but this has great potential to confuse users. So, if this function was integrated into OpenMx, the different ways to calculate AIC should be stated in the summary()-function.

Replied on Thu, 12/06/2012 - 11:38
Picture of user. brandmaier Joined: 02/04/2010

In reply to by brandmaier

I attach a patch to OpenMx (rev. 2295) that adds the capability as described above. With this patch applied (in trunk directory) and with adding the attached MxLogLik.R into subdirectory "R", I am able to obtain log-likelihood, AIC, and BIC via the generic accessors from the stats-package, e.g.:


model <- mxRun(mxModel(...))

logLik(model)
AIC(model)
BIC(model)