How can I calculate the R-squared of a regression with arima errors using R?

If I have an arima object like a:

set.seed(100)
x1 <- cumsum(runif(100))
x2 <- c(rnorm(25, 20), rep(0, 75))
x3 <- x1 + x2

dummy = c(rep(1, 25), rep(0, 75))

a <- arima(x3, order=c(0, 1, 0), xreg=dummy)
print(a)


.

Series: x3
ARIMA(0,1,0)

Call: arima(x = x3, order = c(0, 1, 0), xreg = dummy)

Coefficients:
dummy
17.7665
s.e.   1.1434

sigma^2 estimated as 1.307:  log likelihood = -153.74
AIC = 311.48   AICc = 311.6   BIC = 316.67


How do calculate the R squared of this regression?

Once you have ARMA errors, it is not a simple linear regression any more. So you would have to define what you mean by $R^2$. Perhaps the squared correlation of fitted to actuals? In that case:

cor(fitted(a),x3)^2


The fitted() function will only work if you have loaded the forecast package, but it looks like you have already done that judging from the output in your question.

In your case, you don't have ARMA errors, but you do have differencing. So it is equivalent to the linear model

b <- lm(diff(x3) ~ diff(dummy) - 1)
summary(b)

Coefficients:
Estimate Std. Error t value Pr(>|t|)
diff(dummy)   17.766      1.149   15.46   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.149 on 98 degrees of freedom
Multiple R-squared: 0.7092,     Adjusted R-squared: 0.7062
F-statistic:   239 on 1 and 98 DF,  p-value: < 2.2e-16


Of course, that is a very different value of $R^2$ than just using the correlations as above because it is now being computed on the differences.

You will need define what you mean by $R^2$, and what you want to use it for. Once you move away from the usual regression set up with an intercept and iid errors, $R^2$ ceases to be uniquely defined and is not particularly useful.

• Okay, thanks for the useful answer. Perhaps I should have rephrased my question, but you answered it perfectly :) – fmark Mar 25 '11 at 8:13