Re: [R] extracting AIC scores from lmer and other objects

From: Ben Bolker <bolker_at_ufl.edu>
Date: Tue, 5 Feb 2008 20:14:55 +0000 (UTC)

Jarrett Byrnes <jebyrnes <at> ucdavis.edu> writes:

>
> I have a slight conundrum. I'm attempting to write a scrip that will
> take a number of objects (lm, glm, and lmer) and return AIC scores
> and weights. I've run into 3 problems, and was wondering if anyone
> had any pointers.
>
> 1) is there any convenient way to extract the name of the objects?
> Simply, if I have a vector of objects c(my.lm, my.lmer) and I want to
> get a character vector c("my.lm", "my.lmer"), how would one do this?
> I see this as far simpler than jumping into the ugly details of each
> object type and extracting coefficient lists and such - not to
> mention tidier.

  Hmm. Can you be a little more specific?

 If I do e.g.

> a = 1
> b = 2
> c = 3
> z <- c(a,b,c)

  then z is [1 2 3] and there's no longer any way to recover the names of the objects.

  On the other hand,

> f <- function(...) {

+ print(deparse(substitute(...)))
+ }
> f(c(a,b,c))

[1] "c(a, b, c)"

  and you could then use some combination of strsplit() or gsub() to get the pieces you wanted.

  I generally find it easier to work with named lists of model objects:
  L <- list(simplemodel=lm1,complexmodel=lm2)

and then names(L) gets you what you want.
>
> 2) I'm repeatedly getting the error
>
> Error in UseMethod("logLik") : no applicable method for "logLik"
>
> in a variety of different contexts. The first is if I have to get
> an AIC for an lmer object. AIC(my.lmer) give me the error above.
> However, I can circumvent this with a very silly solution -
>
> myAIC<-function(object) {a<-logLik(object)
> return(-2*a[1] +2*attr(a, 'df'))}
>
> I use this, and I do not get an error.
>

  This may (?) be related to the NEWS item for R 2.6.2 RC:

 o	The AIC() S4 generic in package stats4 no longer disables
	dispatch of S3 methods for AIC().

> 3) I do, however, get the above error if I have a vector of model
> objects. So, again, if I have something like model.list<-c(my.lm,
> my.lmer) or even just c(my.lm, my.lm2) and then call the following on
> the vector of models
>
> aiclist<-vector
> for(index in 1:length(model.list)){
> aiclist<-c(aiclist, myAIC(model.list[index]))
> }
>
> it again yields the Error in UseMethod("logLik"). Given that this is
> true either for lm, glm, or lmer objects, I'm guessing there's a more
> general issue here that I'm missing. Any pointers?
>
> Thanks!
>
> -Jarrett
>

  By the way: I would be careful using AIC to do model selection on lmer objects. In particular, the ugly "degrees of freedom" issue rears its ugly head here -- see Burnham and White 2002

Burnham, K. P., and G. C. White. 2002. Evaluation of some random effects methodology applicable to bird ringing data. Journal of Applied Statistics 29, no. 1:245-264.

  cheers
    Ben Bolker



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 05 Feb 2008 - 20:19:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Feb 2008 - 08:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive