Calculating AICc for regression with ARMA errors I am unable to manually calculate the AICc for a regression with ARMA errors.
I would appreciate any help, such as: (1) pointing out what I am doing wrong or not doing; (2) advice on a textbook that shows the correct method; (3) a corrected version of what I am trying to do.
Context
As part of my effort to understand the automated R functions I use, I am trying to manually reproduce an AICc test value from Hyndman and Athanasopoulos (2018, chapter 9.6).
The R code to reproduce Hyndman and Athanasopoulos's results is as follows:
# Lag and Arrange Predictor Variables
Advert <- cbind(
  AdLag0 = insurance[,"TV.advert"],
  AdLag1 = stats::lag(insurance[,"TV.advert"],-1)) %>%
  head(NROW(insurance))

# Arrange Regression Variables
y <- insurance[4:40,1] 
x1 <- Advert[4:40,1] # AdLag0
x2 <- Advert[4:40,2] # AdLag1

# Automated Regression with ARMA Errors
auto.arima(y, xreg=Advert[4:40,1:2],
           stationary = TRUE)

The above code produces the following test values:
AIC=58.09   AICc=60.02   BIC=66.14

The AICc is 60.02.
My Attempt to Calculate AICc Manually
First, I found an equation of an AICc for a linear regression:

Second, I reproduced the AICc equation in R:
# key AICc Equation Variables
n <- NROW(y)
sse <- fit$residuals^2
k <- length(fit$coefficients)

# AICc Elements:
# (1) Regression Approximation
reg_approx <- (n*log(sum(sse/n)))

# (2) Pi Element
pe <- ((n*(log(2*pi)))+n)

# (3) AICc Penalty Term
pt <- 2*k+((2*k*(k+1))/(n-k-1))

# Assemble AICc Equation:
man_aicc <- reg_approx + pt + pe

Third, I tested the “by hand” AICc equation using the same test data as above, but without ARMA errors:
y <- insurance[4:40,1] 
x1 <- Advert[4:40,1] # AdLag0
x2 <- Advert[4:40,2] # AdLag1

fit <- lm(y~x1+x2)

I used the residuals from fit to get a manual AICc test value of 101.5594.
To check my AICc test value, I used auto.arima with the same regression model, and tested for the best ARMA p and q values:
test <- auto.arima(y, xreg=Advert[4:40,1:2],
                   stationary=TRUE,
                   ic = "aicc", 
                   trace = TRUE)

The auto.arima AICc test value with ARIMA(0,0,0) is 102.4282, which is only 0.8688 greater than my AICc test result.
So, I concluded my manual version—which I think is essentially a regression with ARIMA(0,0,0)—was relatively comparable with auto.arima and was in working order.
Assumed Point of Failure
I tried to incorporate ARMA errors into my manual AICc equation with an ARIMA(1,0,1), which the Hyndman and Athanasopoulos example code used above:
fit <- lm(y~x1+x2)
arma_model <- Arima(fit$residuals, 
                    order = c(1,0,1), 
                    include.mean = FALSE)
# ARMA Errors:
arma_resid <- residuals(arma_model)

I constructed the same manual AICc as shown above, but replaced the sse var with the following:
sse <- arma_resid^2

The manual AICc test produced the following value: 74.53657.
This manual AICc value is much larger than the AICc=60.02 from the Hyndman and Athanasopoulos example above.
Conclusion
I assume there is some fundamental thing I am misunderstanding about regression with ARMA errors or the AICc function. Or, perhaps, it is something else entirely that I have missed.
Any help would be appreciated.

There are two things that stand out to me. Your definition of SSE and the Regression Approximation equations where you summed sse/n and not sse.

*

*The equation asks for SSE however you plugged in SSR.


*

*SSE = Sum of the squared errors

*SSR = Sum of the squared residuals

What you did was
sse <- fit$residuals^2

It should really be:
sse <- sum((y - mean(y))^2)



*Then

reg_approx <- n*log(sse/n)

Try that please
