How to calculate (by hand) the R-squared of a VAR time series model built using R?

I have created a VAR model in R (using the command VAR) and my model reports an R-squared of about 0.8 for the variable I'm most interested in. I'm trying to replicate that result by calculating the R-squared by hand. I have extracted the fitted values (using the command fitted(model_name)) and then I have used the following equation:

$$R^2 = 1- { \sum_{} (y-\hat y)^2 \over \sum_{}(y - \overline y)^2}.$$

With $$y$$ = actual value, $$\hat y$$ = predicted (fitted) value and $$\overline y$$ = the average of the actual value.

I am taking into account the fact that my model uses two lags, so I have two fitted values less than the total amount of points used to train. I'm making sure to pair the actual and predicted values correctly.

I have tried using this equation before in regular linear regression and I am able to replicate the value I get from R, however I cannot do it with this VAR model. Is it because when the R-squared is calculated for time series, something different is done? I have tried googling this and cannot find any reference to a different equation for the case of time series.

Any comments or suggestions as to what could be wrong will be greatly appreciated.

• maybe the mean is also only calculated from the "clipped" time series after discarding initial observations due to the lags? – Christoph Hanck Jul 7 '20 at 7:59
• That is a great observation. I tried that as well and it did not work – amilkar0417 Jul 7 '20 at 19:35
• maybe you can post some code of what you did - please see my answer below, which suggests that it does work – Christoph Hanck Jul 8 '20 at 5:45

In principle, the procedure for computing $$R^2$$ should indeed be as you describe also for VAR models, as these are also simply fitted by OLS.

The discrepancy indeed seems to be in how VAR computes the numerator in the formula for $$R^2$$.

Consider the code below:

library(vars)

# generate some data
n <- 10
y1 <- rnorm(n)
y2 <- rnorm(n)
y <- cbind(y1,y2)

# R^2 from vars package
var.model <- VAR(y, p = 1)
R2.vars.1 <- summary(var.model$$varresult$$y1)\$r.squared