python statsmodel 中的 VIF 计算
VIF calculation in python statsmodel
statsmodel中计算VIF的代码如下:
k_vars = exog.shape[1]
x_i = exog[:, exog_idx]
mask = np.arange(k_vars) != exog_idx
x_noti = exog[:, mask]
r_squared_i = OLS(x_i, x_noti).fit().rsquared ## NO INTERCEPT
vif = 1. / (1. - r_squared_i)
拟合时不包括截距。根据 Wooldridge 的 "Introductory Econometrics (6ed)",似乎截距应该包括在内:“... R 平方来自所有其他自变量的回归 Xj( 并包括截距 )。”。 =12=]
statmodels 错了吗?我可以交叉检查另一个包裹吗?谢谢
使用统计模型时,请始终注意添加常量(在这种情况下这是必要的);引用自 docs:
An intercept is not included by default and should be added by the user. See statsmodels.tools.add_constant
.
来自 MATLAB 的参考资料:
https://www.mathworks.com/help/econ/examples/time-series-regression-ii-collinearity-and-estimator-variance.html
在线性回归的传统矩阵公式中,X-matrix 总是在第一个位置有一个 1 的列向量 - 如果没有这个,我们将通过原点得到回归,即没有截距项.我在 statsmodels 中寻找 VIF 时遇到了这个问题。
statsmodel中计算VIF的代码如下:
k_vars = exog.shape[1]
x_i = exog[:, exog_idx]
mask = np.arange(k_vars) != exog_idx
x_noti = exog[:, mask]
r_squared_i = OLS(x_i, x_noti).fit().rsquared ## NO INTERCEPT
vif = 1. / (1. - r_squared_i)
拟合时不包括截距。根据 Wooldridge 的 "Introductory Econometrics (6ed)",似乎截距应该包括在内:“... R 平方来自所有其他自变量的回归 Xj( 并包括截距 )。”。 =12=]
statmodels 错了吗?我可以交叉检查另一个包裹吗?谢谢
使用统计模型时,请始终注意添加常量(在这种情况下这是必要的);引用自 docs:
An intercept is not included by default and should be added by the user. See
statsmodels.tools.add_constant
.
来自 MATLAB 的参考资料: https://www.mathworks.com/help/econ/examples/time-series-regression-ii-collinearity-and-estimator-variance.html
在线性回归的传统矩阵公式中,X-matrix 总是在第一个位置有一个 1 的列向量 - 如果没有这个,我们将通过原点得到回归,即没有截距项.我在 statsmodels 中寻找 VIF 时遇到了这个问题。