You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Statsmodels的VAR包中便捷提取R2值?

How to Extract R² Values from VAR Results in Statsmodels

Hey, great question! You absolutely don’t have to compute R² values manually for your VAR model in statsmodels—there’s a straightforward way to pull them right from the results object.

Accessing R² Values Directly

After fitting your VAR model (like you did with results = model.fit(2)), you can use two built-in attributes to get the R² metrics:

  1. Unadjusted R²: Use the rsquared attribute. This returns a pandas Series where each entry corresponds to the R² of the regression equation for one of your endogenous variables.
    # Get unadjusted R² for all equations
    r_squared = results.rsquared
    print(r_squared)
    
  2. Adjusted R²: For R² corrected for the number of predictors, use rsquared_adj:
    # Get adjusted R² for all equations
    adjusted_r_squared = results.rsquared_adj
    print(adjusted_r_squared)
    

Example Usage

If your VAR model uses variables like 'GDP' and 'Inflation', the output might look like this:

GDP         0.852
Inflation   0.724
dtype: float64

You can also grab the R² for a specific variable directly by indexing the Series:

# Get R² only for the GDP equation
gdp_r2 = results.rsquared['GDP']
print(gdp_r2)

That’s all—no manual calculations required! 😊

内容的提问来源于stack exchange,提问作者GroJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:45:42