Reputation: 31
Is there an easy way to extract R2 from Statsmodels' VAR package?
Following the example in statsmodels' documentation: http://www.statsmodels.org/dev/vector_ar.html
from statsmodels.tsa.api import VAR
model = VAR(data)
results = model.fit(2)
results.summary()
Summary of Regression Results
==================================
Model: VAR
Method: OLS
Date: Tue, 28, Feb, 2017
Time: 21:38:11
--------------------------------------------------------------------
No. of Equations: 3.00000 BIC: -27.5830
Nobs: 200.000 HQIC: -27.7892
Log likelihood: 1962.57 FPE: 7.42129e-13
AIC: -27.9293 Det(Omega_mle): 6.69358e-13
--------------------------------------------------------------------
.
.
It then goes on to show the coefficients for each equation and, at the end, a correlation matrix of the residuals. However, it does not show R-square for each of the equations.
Does anyone know if there is an easy way to extract the R-square from statsmodels VAR without having to work it out from scratch?
Upvotes: 3
Views: 1777
Reputation: 51
Using sklearn.metrics.r2_score for each equation would work (unfortunately going outside of statsmodels). The example code assumes that there is a column named 'foobar'
in the dataframe data
, and this will be the equation R2 we extract; obviously the VAR()
and fit()
methods should be adapted for your particular case
import statsmodels.api as sm
import sklearn.metrics as skm
estVAR = sm.tsa.VAR(data).fit(1)
skm.r2_score(estVAR.fittedvalues['foobar']+estVAR.resid['foobar'],
estVAR.fittedvalues['foobar'])
The reason for adding the fitted values to the residuals is to get back the actual data for which the VAR could construct fitted values (instead of the whole sample, some observations of which did not have fitted values constructed due to the need for lagged observations on the right hand side). Incidentally, we can confirm that this is the R2 we want, say by doing this
y = estVAR.fittedvalues['foobar']+estVAR.resid['foobar']
R2 = 1 - np.sum(estVAR.resid['foobar'].values**2)/np.sum((y.values-y.mean())**2)
Upvotes: 3