Skip to content

Warning: family()$aic is not what you think!

April 27, 2013

Or at least not what I thought. Some background: R has this thing called family objects. They are meant to provide model fitting functions (e.g. glm, glmer) information about the assumed error distribution (e.g. binomial, Poisson, Gaussian). They have several components that return functions or expressions for computing various values that depend on the chosen family. One of those functions is for computing AIC, and you get it, for the binomial family say, by doing this:

> binomial()$aic
function (y, n, mu, wt, dev) 
    m  1)) 
    else wt
    -2 * sum(ifelse(m > 0, (wt/m), 0) * dbinom(round(m * y), 
        round(m), mu, log = TRUE))

OK, so what’s my problem? Well, if you look carefully at this function…you’ll find that it just returns minus twice the log-likelihood. Well minus twice the log-likelihood is not AIC, its deviance. The AIC is the deviance + twice the number of parameters. To confirm my suspicion, here’s some code from glm:

aic <- family$aic

So aic contains the AIC function for the particular family being assumed. OK, a bit later the AIC of the model is calculated:

## calculate AIC
    aic.model <-
	aic(y, n, mu, weights, dev) + 2*rank

Note how they have to add the 2*rank bit (rank is just a fancy word for number of parameters…basically). So the AIC function from family objects don’t return AIC! OK this is very confusing to me. There’s probably a good reason for this, but I don’t know…its a little fishy.

Just to avoid people further confusion, the AIC function does actually compute AIC, thankfully. And this is the function that most non-power-users will use. But as a power-user, I’m frustrated that family()$aic doesn’t return a function that returns AIC!

Trying to remember the rule that if you rate poorly the code of someone smarter than you, you’re probably missing something. I do believe this, its just frustrating that family()$aic doesn’t return a function that returns AIC!

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: