0
$\begingroup$

In linear regression, suppose I have Residual Sum of Squares, how to calculate AIC from it?

set.seed(2019)
x = rnorm(1000,5,1)
e = rnorm(1000)
y = x * 2+e
m0 = lm(y~x)
RSS = sum(residuals(m0)^2) # 1029.991
AIC(m0) # 2873.427

How to calculate AIC = 2873.427 given there's 1 predictor, there's 1000 observations and the RSS is 1029.991?

$\endgroup$

1 Answer 1

1
$\begingroup$

Code:

res <- m0$residuals 
p <- m0$rank
N <- length(res)
w <- rep.int(1, N)
loglike.calc = .5* (sum(log(w)) - N * (log(2 * pi) + 1 - log(N) +log(sum(w*res^2))))
aic.calc = -2*as.numeric(loglike.calc)+2*(length(m0$coefficients)+1)

And then you get

> aic.calc
[1] 2873.427
$\endgroup$
1
  • 1
    $\begingroup$ If your answer is purely "how to do this in code" it's probably off topic. If your intent is to convey and explain the underlying formula, this would require more than just a line of code $\endgroup$
    – Glen_b
    Commented Mar 6, 2019 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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