title: AIC and BIC formula in R
body: <p>I am going to compute AIC and BIC of a linear model without using build in function AIC() and BIC() in R. But every time I compute AIC and BIC from formula and corresponding R function, I get different results. any idea?</p>
  
  <p>My R code is like this:</p>
  
  <pre><code>set.seed(123456)
     b = c ( 1:5 )  
     n=100
     nb=length(b)
     x = matrix ( rnorm ( nb*n) ,ncol = nb )
     y =  x %*% b + rnorm ( n) 
  
     l=lm(y~x+0)
     AIC(l)
     residual= (y-x %*% l$coef)
     2*nb-2*sum(dnorm(residual, 0, sd(residual), log=T))
  </code></pre>
  
  <p><strong>UPDATE: I have updated the question based on @nongkrong comment and it works well now.</strong></p>