我正在运行以下源代码:
import statsmodels.formula.api as sm
# Add one column of ones for the intercept term
X = np.append(arr= np.ones((50, 1)).astype(int), values=X, axis=1)
regressor_OLS = sm.OLS(endog=y, exog=X).fit()
print(regressor_OLS.summary())
Run Code Online (Sandbox Code Playgroud)
在哪里
X 是一个 50x5(在添加截距项之前)的 numpy 数组,如下所示:
[[0 1 165349.20 136897.80 471784.10]
[0 0 162597.70 151377.59 443898.53]...]
Run Code Online (Sandbox Code Playgroud)
并且y是一个 50x1 numpy 数组,具有因变量的浮点值。
前两列用于具有三个不同值的虚拟变量。其余的列是三个不同的独立变量。
虽然,据说statsmodels.formula.api.OLS自动添加了一个拦截项(参见@stellacia 的回答:OLS using statsmodel.formula.api vs statsmodel.api)它summary没有显示拦截项的统计值,在我的情况下如下所示:
OLS Regression Results
==============================================================================
Dep. Variable: Profit R-squared: 0.988
Model: OLS Adj. R-squared: 0.986
Method: Least Squares F-statistic: …Run Code Online (Sandbox Code Playgroud)