0
$\begingroup$

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_{t} (y_t-\hat y_t)^2 \over \sum_{t}(y_t - \overline y)^2},$$

with $y_t$ = actual value, $ \hat y_t$ = 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.

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

1 Answer 1

1
$\begingroup$

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

# handmade
u1 <- resid(var.model)[,1]
# gives different results when computing numerator from full series:
#R2.1 <- 1-sum(u1^2)/sum((y1-mean(y1))^2) 

# numerator of R^2 when only using the portion that can actually serve as a dependent variable
R2.1 <- 1-sum(u1^2)/sum((y1[2:n]-mean(y1[2:n]))^2) 

all.equal(R2.1,R2.vars.1) # TRUE (identical and == say FALSE because of small numerical differences)
$\endgroup$
1
  • 2
    $\begingroup$ vars::VAR just constructs the matrix of lagged values and passes it off to stats::lm to do OLS, which is where the $R^2$ value comes from. lm never even sees the first $p$ points of the dependent variable and does not know anything about the lag relationship with the regressors, so obviously can't use these to compute the mean. $\endgroup$
    – Chris Haug
    Commented Mar 13, 2022 at 14:48

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.