Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I'll use an example so that you can reproduce the results

# mortality 
mort = ts(scan("http://www.stat.pitt.edu/stoffer/tsa2/data/cmort.dat"),start=1970, frequency=52)

# temperature
temp = ts(scan("http://www.stat.pitt.edu/stoffer/tsa2/data/temp.dat"), start=1970, frequency=52)

#pollutant particulates
part = ts(scan("http://www.stat.pitt.edu/stoffer/tsa2/data/part.dat"), start=1970, frequency=52)

temp = temp-mean(temp)
temp2 = temp^2
trend = time(mort)

Now, fit a model for mortality data

fit = lm(mort ~ trend + temp + temp2 + part, na.action=NULL)

What I want now is to reproduce the result of the AIC command

AIC(fit)
[1] 3332.282

According to R's help file for AIC, AIC = -2 * log.likelihood + 2 * npar. If I'm correct I think that log.likelihood is given using the following formula:

n = length(mort)
RSS = anova(fit)[length(anova(fit)[,2]),2] # there must be better ways to get this, anyway
(log.likelihood <- -n/2*(log(2*pi)+log(RSS/n)+1))

 [1] -1660.135

This is approximately equal to

logLik(fit)
'log Lik.' -1660.141 (df=6)

As far as I can tell, the number of parameters in the model are 5 (how can I get this number programmatically ??). So AIC should be given by:

-2 * log.likelihood + 2 * 5
[1] 3330.271

Ooops, it seems like I should have used 6 instead of 5 as the number of parameters. What is wrong with those calculations?

share|improve this question

1 Answer

up vote 11 down vote accepted
> -2*logLik(fit)+2*(length(fit$coef)+1)
[1] 3332.282

(you forgot; you have 6 parameter because $\sigma_{\epsilon}$ also has to be estimated! 

share|improve this answer
Aww, you've beat me. +1 – mbq Jul 31 '10 at 10:44
Thanks! Is it the same as the df=6 that LogLik returns? Could I have used -2*logLik(fit)+2*(attr(logLik(fit),"df")) ?? – gd047 Jul 31 '10 at 10:58
@user603 Also, how did you "draw" that indexed sigma? – gd047 Jul 31 '10 at 15:48
1  
@gd047. LaTeX code: $\sigma_{\epsilon}$ – Rob Hyndman Aug 3 '10 at 13:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.