之前曾在CSDN chongminglun
这个账号上发过一篇python statsmodel 回归结果提取的文章,现在在知乎重发一篇完整版,含代码和示例结果展示,并回答一些疑问
- statsmodel是python中一个很强大的做回归统计的包,类似R语言中的lm函数,通过summary可以快速查看训练的回归模型多种具体参数,但是很多同学不太清楚如何将特定的指标数值提取出来,本文以OLS回归结果为例展示相关提取。
- 相关函数官网链接: https://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.OLSResults.html
数据说明
波士顿房价数据集: sklearn
包中的示例数据集 boston
from sklearn import datasets
import pandas as pd
import statsmodels.api as sm
### 获取数据集
boston = datasets.load_boston()
### 提取自变量
X = pd.DataFrame(boston.data, columns = boston.feature_names)
### 提取因变量
y = pd.DataFrame(boston.target)
### 展示自变量
X.head()
CRIM | ZN | INDUS | CHAS | NOX | RM | AGE | DIS | RAD | TAX | PTRATIO | B | LSTAT |
---|
模型训练及结果总体展示
### 自变量增加截距项
X = sm.add_constant(X)
# 模型训练
model = sm.OLS(y, X).fit()
# 查看模型结果
print(model.summary())
OLS Regression Results
==============================================================================
Dep. Variable: 0 R-squared: 0.741
Model: OLS Adj. R-squared: 0.734
Method: Least Squares F-statistic: 108.1
Date: Mon, 12 Jul 2021 Prob (F-statistic): 6.72e-135
Time: 15:48:48 Log-Likelihood: -1498.8
No. Observations: 506 AIC: 3026.
Df Residuals: 492 BIC: 3085.
Df Model: 13
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 36.4595 5.103 7.144 0.000 26.432 46.487
CRIM -0.1080 0.033 -3.287 0.001 -0.173 -0.043
ZN 0.0464 0.014 3.382 0.001 0.019 0.073
INDUS 0.0206 0.061 0.334 0.738 -0.100 0.141
CHAS 2.6867 0.862 3.118 0.002 0.994 4.380
NOX -17.7666 3.820 -4.651 0.000 -25.272 -10.262
RM 3.8099 0.418 9.116 0.000 2.989 4.631
AGE 0.0007 0.013 0.052 0.958 -0.025 0.027
DIS -1.4756 0.199 -7.398 0.000 -1.867 -1.084
RAD 0.3060 0.066 4.613 0.000 0.176 0.436
TAX -0.0123 0.004 -3.280 0.001 -0.020 -0.005
PTRATIO -0.9527 0.131 -7.283 0.000 -1.210 -0.696
B 0.0093 0.003 3.467 0.001 0.004 0.015
LSTAT -0.5248 0.051 -10.347 0.000 -0.624 -0.425
==============================================================================
Omnibus: 178.041 Durbin-Watson: 1.078
Prob(Omnibus): 0.000 Jarque-Bera (JB): 783.126
Skew: 1.521 Prob(JB): 8.84e-171
Kurtosis: 8.281 Cond. No. 1.51e+04
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.51e+04. This might indicate that there are
strong multicollinearity or other numerical problems.
上述结果中coef列表示回归系数,const表示截距,所以回归公式应该为:
我们可以使用其中某个显著的自变量话模型残差相关绘图
import matplotlib.pyplot as plt #绘图
import seaborn as sns #绘图
sns.set()
import warnings # 忽略warning
warnings.filterwarnings("ignore")
# 回归模型检验 针对CRIM变量
fig = plt.figure(figsize=(15,8))
fig = sm.graphics.plot_regress_exog(model,"CRIM", fig =fig)
接下来是具体指标提取
提取元素-回归系数类
# 提取回归系数
model.params
const 36.459488
CRIM -0.108011
ZN 0.046420
INDUS 0.020559
CHAS 2.686734
NOX -17.766611
RM 3.809865
AGE 0.000692
DIS -1.475567
RAD 0.306049
TAX -0.012335
PTRATIO -0.952747
B 0.009312
LSTAT -0.524758
dtype: float64
# 提取回归系数标准差
model.bse
const 5.103459
CRIM 0.032865
ZN 0.013727
INDUS 0.061496
CHAS 0.861580
NOX 3.819744
RM 0.417925
AGE 0.013210
DIS 0.199455
RAD 0.066346
TAX 0.003761
PTRATIO 0.130827
B 0.002686
LSTAT 0.050715
dtype: float64
# 提取回归系数p值
model.pvalues
const 3.283438e-12
CRIM 1.086810e-03
ZN 7.781097e-04
INDUS 7.382881e-01
CHAS 1.925030e-03
NOX 4.245644e-06
RM 1.979441e-18
AGE 9.582293e-01
DIS 6.013491e-13
RAD 5.070529e-06
TAX 1.111637e-03
PTRATIO 1.308835e-12
B 5.728592e-04
LSTAT 7.776912e-23
dtype: float64
# 提取回归系数t值
model.tvalues
const 7.144074
CRIM -3.286517
ZN 3.381576
INDUS 0.334310
CHAS 3.118381
NOX -4.651257
RM 9.116140
AGE 0.052402
DIS -7.398004
RAD 4.612900
TAX -3.280009
PTRATIO -7.282511
B 3.466793
LSTAT -10.347146
dtype: float64
# 提取回归系数置信区间 默认5%,括号中可填具体数字 比如0.05, 0.1
model.conf_int()
# 回归系数表汇总提取
coef_df = pd.DataFrame({"params": model.params, # 回归系数
"std err": model.bse, # 回归系数标准差
"t": round(model.tvalues,3), # 回归系数T值
"p-values": round(model.pvalues,3) # 回归系数P值
})
coef_df[['coef_0.025','coef_0.975']] = model.conf_int() # 回归系数置信区间 默认5%,括号中可填具体数字 比如0.05, 0.1
coef_df
模型总体数据类
# 提取模型预测值
model.fittedvalues
0 30.003843
1 25.025562
2 30.567597
3 28.607036
4 27.943524
...
501 23.533341
502 22.375719
503 27.627426
504 26.127967
505 22.344212
Length: 506, dtype: float64
# 提取残差
model.resid
0 -6.003843
1 -3.425562
2 4.132403
3 4.792964
4 8.256476
...
501 -1.133341
502 -1.775719
503 -3.727426
504 -4.127967
505 -10.444212
Length: 506, dtype: float64
# pearson残差
model.resid_pearson
array([-1.26521941e+00, -7.21885590e-01, 8.70841646e-01, 1.01004475e+00,
1.73992770e+00, 7.25711095e-01, -2.14545566e-02, 1.59400132e+00,
1.04869346e+00, -4.26993337e-03, -8.42833550e-01, -5.66201654e-01,
1.67213617e-01, 1.78512953e-01, -2.28327496e-01, 1.26971324e-01,
5.42113501e-01, 1.24038286e-01, 8.47573491e-01, -4.34400591e-02,
2.26780791e-01, 4.06499914e-01, -1.33370184e-01, 1.46189897e-01,
-1.65086181e-02, 1.08173264e-01, 2.39399804e-01, 1.92876645e-02,
-2.41791518e-01, 2.60408921e-02, 2.62340187e-01, -7.50054646e-01,
9.24903445e-01, -2.49248434e-01, -4.35713215e-02, -1.03568524e+00,
-4.93527906e-01, -4.44421266e-01, 3.76156316e-01, -1.17511201e-01,
1.44331867e-01, -2.99362460e-01, 2.02587310e-02, 1.90098222e-02,
-3.66993114e-01, -5.89361947e-01, -8.91830833e-02, -3.02731426e-01,
1.11551393e+00, 4.62336064e-01, -3.33282608e-01, -7.31718580e-01,
-5.59680488e-01, -1.36770771e-01, 7.45612217e-01, 8.95065046e-01,
-3.30579398e-02, -3.18040722e-01, 3.21290690e-01, -3.12927764e-01,
1.74366198e-01, -5.29176879e-01, -3.76673603e-01, 5.15438908e-01,
2.02872679e+00, -1.44595415e+00, -1.29192411e+00, 1.86840600e-01,
-4.53877833e-03, 2.42690067e-02, -2.11048612e-01, -8.98946766e-03,
-3.70355983e-01, -1.35493513e-01, -2.96081955e-01, -5.40941813e-01,
-6.20709999e-01, -5.38848790e-01, -1.30619110e-02, -4.48480506e-01,
-8.55098387e-02, -6.52195235e-01, -2.60418402e-01, -4.54920795e-01,
-1.86409095e-01, -2.50878218e-01, 6.98514099e-02, -7.78194333e-01,
-1.49086907e+00, -4.49098486e-01, -9.52315160e-01, -1.14063798e+00,
-1.27309758e+00, -8.60020868e-01, -1.35708514e+00, -4.73309530e-02,
-7.01209840e-01, 6.15009786e-01, 1.83032842e+00, 1.99981529e-01,
6.15299545e-01, 1.90897436e-01, -2.50803383e-01, -2.13194461e-01,
-2.81294422e-01, 2.02318143e-01, 4.87311860e-01, -7.38605344e-02,
-6.00234387e-01, -7.84011207e-02, 2.21343595e-01, -7.85170351e-01,
-4.15829796e-01, -4.24732666e-01, -1.40604206e+00, -4.48919294e-01,
-4.58821801e-01, -9.46290925e-01, 1.35326285e-02, -3.14376184e-01,
1.76341478e-02, -4.57521885e-01, -1.20931407e-02, 1.96695801e-01,
-3.71103801e-01, -2.27969755e-01, 2.28211933e-01, 2.15209481e-01,
-1.97813897e-01, 5.14756164e-02, -1.76021392e-01, 4.00113906e-02,
6.19156948e-01, 5.56745470e-01, 4.93867304e-01, 1.76432042e-01,
3.20650275e-01, -4.76606412e-01, -1.08494545e-01, 2.84741907e-01,
9.03169126e-02, 2.19399172e+00, -2.51818692e-01, 7.27285213e-01,
6.47330528e-01, 3.71767275e-01, -4.65346057e-02, 1.28232401e+00,
1.70306655e+00, 1.25489746e-01, 1.39383964e-01, 2.73742188e-01,
-1.01633773e+00, 4.45489616e-01, -1.13080402e+00, -9.49099309e-01,
-1.09847450e-01, 1.69434517e+00, -9.96812529e-01, -4.77847251e-01,
-1.20293319e+00, 2.78703874e+00, 1.98983103e+00, 1.71806239e+00,
-4.40156490e-01, -7.98458523e-02, 2.69667468e+00, 1.50151265e-01,
-5.48488093e-01, -9.17502119e-01, -1.08636937e+00, -1.09388871e+00,
2.60105488e-02, -1.15312945e+00, -8.26490401e-01, -2.78610638e-01,
-5.09281604e-01, -9.56190659e-01, -3.23629753e-01, 9.01457433e-01,
1.06960515e+00, 1.77743918e+00, 8.45495190e-01, 3.17708094e-01,
7.75883786e-01, 1.01859182e+00, 2.97452914e+00, -3.00248196e-01,
-5.50437795e-01, 8.11119748e-02, 1.31475513e+00, 4.43931203e-02,
7.33528795e-01, -2.13391799e-01, -5.18136047e-01, 1.92916071e+00,
-5.95895281e-01, -4.99274863e-01, -2.20620834e-02, 1.01290756e+00,
4.75430805e-01, -1.09312309e+00, 1.10183088e+00, 1.36304791e+00,
1.43521046e+00, -1.90394703e-02, 1.51129151e-01, 9.78974911e-01,
1.90864735e-01, 6.30966213e-01, -1.45936238e-01, 4.71956108e-01,
-7.14242187e-02, 6.07037609e-01, 2.65122067e+00, 1.03156536e-01,
-6.96130701e-01, 7.26641108e-02, -7.19713389e-01, -1.40908483e+00,
-1.36644679e+00, -4.37183604e-01, -9.77919490e-01, 7.46381135e-02,
1.35480959e+00, 2.14641545e+00, 2.93816106e-03, -1.68489412e-01,
2.36936588e+00, 5.60312277e-02, -3.88789623e-02, -3.34725628e-01,
7.69581817e-01, 2.34689439e+00, -5.71899830e-01, -2.67012876e-01,
-1.05369721e+00, -2.57069527e-01, -9.96179878e-01, -1.08094509e+00,
-1.11557992e+00, -7.67628217e-01, -4.04627662e-01, -7.80158379e-01,
2.67933510e-01, 1.07497721e+00, 9.02706655e-01, 1.34481691e-01,
6.76814560e-01, 4.46780881e-01, 4.08118779e-02, -5.10311900e-02,
9.86315243e-01, 2.70886376e+00, -4.36702413e-01, -1.67704712e-01,
1.36743938e+00, 1.41142017e+00, -1.01914398e-01, -1.03046969e+00,
-2.13287984e-01, 1.25043495e+00, 1.64599034e+00, -7.26264349e-01,
1.40354622e-01, -1.14760818e+00, -1.11001650e-01, 1.93042506e+00,
8.81309437e-01, -1.05539818e+00, -2.53504687e-01, -4.22188373e-01,
-8.66477630e-01, -5.83242552e-02, -7.81066121e-01, -3.78623780e-01,
-5.08052073e-01, -3.66664802e-01, -2.63824634e-01, -4.42136619e-02,
1.39136258e+00, 2.25226761e-01, 1.19283742e+00, 1.12256731e+00,
1.27096153e-01, -1.12882101e+00, -3.67003493e-04, -8.09657600e-01,
-1.03547672e+00, -4.45484838e-01, -1.04011021e+00, 6.10394449e-01,
-8.28904328e-01, -4.04154238e-01, -5.75269115e-01, 2.99995124e-02,
-5.53537365e-02, 1.60308666e-01, -1.39361073e+00, -6.13353681e-01,
-1.25842344e+00, -1.46308165e+00, -5.23025135e-01, 6.34581158e-02,
6.09223136e-01, -4.99087272e-01, -4.55665479e-01, -9.50214521e-01,
-1.23120658e+00, -6.93245850e-01, -5.14755633e-01, -1.00708075e+00,
-8.17933803e-01, -8.31518339e-01, -3.54287656e-01, -9.14399653e-01,
3.88330226e-02, 3.00125109e-01, -2.50922645e-01, -6.85500424e-02,
-2.29031433e-01, -3.72868627e-01, -5.20415026e-01, -2.00459037e-01,
-2.48317401e-02, -1.43023957e-02, -1.43460273e-01, 6.02512999e-01,
-3.94955382e-01, -3.48237491e-01, -3.77765289e-01, -6.07857703e-01,
-8.30059534e-01, 1.25161945e-02, -1.80199275e-01, 1.01420588e-01,
-1.39289827e-01, -1.65406649e-01, -3.30163278e-01, -4.74081429e-01,
-5.75178408e-01, 4.99886825e-01, -1.16902027e+00, -8.02158048e-01,
5.58881387e-01, 2.01249479e-01, 5.09220601e-01, -4.58095726e-01,
-6.41066390e-01, 9.38112676e-01, 5.14311000e-01, 7.48940454e-01,
3.62329546e-01, 9.89919866e-01, 8.16625960e-01, 8.44017642e-01,
-3.87129922e-01, -2.14542066e-01, 1.04906186e-01, 7.15343756e-01,
4.91821348e-01, 2.04006952e-01, 5.41911564e-01, -7.23138564e-01,
-3.28630010e+00, 2.78551361e+00, 1.33967082e+00, 2.58537531e+00,
5.52110109e+00, 3.65749964e+00, 3.24387615e+00, 5.28031869e+00,
5.05767814e+00, 1.61711124e+00, 2.74423259e+00, -2.17207228e+00,
-8.09350751e-01, -1.46097123e+00, -5.76004479e-01, -1.39825267e+00,
-8.36606701e-01, -1.59670226e+00, -4.48377051e-01, -1.60532613e-01,
1.16342928e+00, -1.81278747e-01, 9.21244118e-01, 3.75393038e-01,
7.89837468e-01, -5.70595829e-01, -4.45125206e-01, 1.24357356e+00,
-4.02854415e-02, -1.35317986e+00, -1.10463274e+00, -1.51823079e+00,
-1.43206823e+00, -1.65138786e+00, -3.26981178e-01, -9.67308608e-01,
-1.32372265e+00, -2.23626517e+00, -1.29839383e+00, -9.86131456e-01,
2.36410095e-01, -6.76703231e-01, 8.07902459e-01, 1.66839929e+00,
7.35978262e-01, 1.61155636e+00, -4.70113717e-02, 5.04246901e-02,
3.41000319e+00, 9.47088245e-01, 2.37736603e+00, -5.02343009e-01,
-1.23629915e+00, 7.38494451e-01, 5.58911448e-01, -1.30795109e+00,
-6.11136875e-01, -8.26981700e-01, 4.80102323e-01, 4.73550632e-02,
-6.16647496e-01, -3.42180765e-01, -1.29515707e+00, -6.69124285e-01,
-6.86482135e-01, -7.46496372e-01, -7.71196412e-01, -9.68441469e-01,
-1.14371801e+00, -5.75605263e-01, -8.97921309e-01, 8.13097332e-03,
-1.03698306e+00, -2.52376246e-02, 7.44418758e-01, -5.60371802e-02,
-4.64901658e-01, -4.12156591e-02, -7.17520772e-02, -5.60346303e-01,
-1.50622112e-01, -3.66686307e-02, -5.86569290e-01, -1.16472183e+00,
-7.20364953e-01, -8.90866055e-01, -6.58065351e-01, -8.87807423e-01,
-5.23076162e-01, -9.88230397e-01, -8.00795473e-02, -3.62652311e-01,
2.68564511e-03, 1.31427378e-01, -4.81884430e-01, 3.13473221e-01,
-5.58153615e-01, -5.20955518e-01, -5.77567913e-02, -4.69814033e-01,
2.27779460e-01, 4.24385864e-01, 9.74694233e-01, 4.53550317e-01,
4.45902933e-01, 3.18550273e-01, -5.63071909e-02, -7.11689801e-01,
1.56827392e-01, 8.89670906e-01, -5.46114536e-01, -5.92473370e-01,
-8.08129616e-01, 9.63633055e-02, -9.70426487e-01, -9.75205116e-02,
-9.87901382e-02, -7.16261239e-01, -7.52311631e-01, 1.50888762e-01,
2.41257346e-01, -2.15425691e-01, -1.17151670e-01, -1.52858897e-01,
7.04731864e-01, -2.57911478e-01, 9.34820766e-01, -3.35248442e-02,
8.78514772e-01, 2.47272018e-01, 8.19233210e-01, 1.30962905e+00,
1.19840565e+00, -1.70387910e-01, -2.06629258e-02, -2.01249399e-01,
-7.73124961e-01, -2.38834422e-01, -3.74205972e-01, -7.85498814e-01,
-8.69906726e-01, -2.20096017e+00])
# 模型系数特征根
model.eigenvals
array([1.58387279e+08, 1.18747467e+07, 4.17002361e+05, 1.61646715e+05,
2.52698040e+04, 1.47636812e+04, 8.18419000e+03, 6.07824057e+03,
4.23776822e+03, 6.06464350e+02, 3.29009355e+02, 3.04181386e+01,
2.71710279e+00, 6.93408540e-01])
# 模型自由度(系数自由度)
model.df_model
13.0
# 残差自由度(样本自由度)
model.df_resid
492.0
# 模型样本数量
model.nobs
506.0
模型评价类
# R方
model.rsquared
0.7406426641094094
# 调整R方
model.rsquared_adj
0.7337897263724629
# AIC
model.aic
3025.608594075548
# 提取BIC
model.bic
3084.7801074455724
# 提取F-statistic
model.fvalue
108.0766661743262
# 提取F-statistic 的pvalue
model.f_pvalue
6.722174750114747e-135
# 模型mse
model.mse_model
2433.654679774215
# 残差mse
model.mse_resid
22.517854833241827
# 总体mse
model.mse_total
84.58672359409856
# 模型SSR
model.ssr
11078.784577954979
# 最大似然值
model.llf
-1498.804297037774
计量经济学指标
主要包括white稳健标准误和异方差等
# 协方差矩阵比例因子
model.scale
22.517854833241827
# White异方差稳健标准误
model.HC0_se
const 7.889557
CRIM 0.028541
ZN 0.013574
INDUS 0.049678
CHAS 1.275810
NOX 3.733026
RM 0.833130
AGE 0.016235
DIS 0.211718
RAD 0.060580
TAX 0.002653
PTRATIO 0.115804
B 0.002641
LSTAT 0.098262
dtype: float64
# MacKinnon和White(1985)的异方差稳健标准误
model.HC1_se
const 8.001020
CRIM 0.028944
ZN 0.013765
INDUS 0.050380
CHAS 1.293834
NOX 3.785766
RM 0.844900
AGE 0.016464
DIS 0.214709
RAD 0.061436
TAX 0.002691
PTRATIO 0.117440
B 0.002679
LSTAT 0.099650
dtype: float64
# White异方差矩阵
model.cov_HC0
array([[ 6.22451170e+01, -4.05837192e-02, 4.66213977e-02,
-2.02608278e-02, -5.16908156e-01, -1.84963497e+01,
-5.92308790e+00, 6.49468377e-02, -9.04271785e-01,
2.48754354e-01, -3.17889895e-03, -4.33848652e-01,
-8.06514578e-03, -4.55830059e-01],
[-4.05837192e-02, 8.14575028e-04, -7.30467495e-05,
7.68196041e-05, -1.48005320e-04, 1.08132391e-02,
4.53386408e-03, -3.19647054e-05, 1.35870378e-03,
-6.37552526e-04, 2.86982066e-06, -4.57267662e-05,
3.83565001e-06, 3.45785790e-04],
[ 4.66213977e-02, -7.30467495e-05, 1.84244584e-04,
2.61234126e-06, 6.03717812e-04, -1.21706740e-02,
-5.73718237e-03, 5.87974087e-05, -1.62801330e-03,
3.18341130e-04, -8.35890471e-06, 1.73821267e-04,
-1.50295327e-06, -4.28123907e-04],
[-2.02608278e-02, 7.68196041e-05, 2.61234126e-06,
2.46794590e-03, -2.98284972e-03, -4.43042501e-02,
5.40243883e-03, 1.28039193e-04, 2.81174900e-03,
1.16918247e-04, -2.04184173e-05, -1.23180596e-03,
5.49048862e-06, -6.26102272e-04],
[-5.16908156e-01, -1.48005320e-04, 6.03717812e-04,
-2.98284972e-03, 1.62769029e+00, -2.50863874e-01,
-2.60459059e-02, 4.90596013e-04, -1.41767569e-02,
-1.10240540e-03, 9.93743845e-04, 3.67078932e-02,
-2.60236135e-05, -1.98981785e-02],
[-1.84963497e+01, 1.08132391e-02, -1.21706740e-02,
-4.43042501e-02, -2.50863874e-01, 1.39354837e+01,
1.19367217e+00, -2.85013691e-02, 3.37144580e-01,
-8.29792499e-02, -2.22770278e-04, 1.70057224e-01,
2.54063758e-03, 9.39816822e-02],
[-5.92308790e+00, 4.53386408e-03, -5.73718237e-03,
5.40243883e-03, -2.60459059e-02, 1.19367217e+00,
6.94104919e-01, -7.54495425e-03, 7.25867496e-02,
-2.53401948e-02, 2.23533865e-04, 1.92295864e-02,
4.91467605e-04, 5.78136563e-02],
[ 6.49468377e-02, -3.19647054e-05, 5.87974087e-05,
1.28039193e-04, 4.90596013e-04, -2.85013691e-02,
-7.54495425e-03, 2.63572686e-04, 6.57956534e-05,
3.96860769e-04, 5.05955056e-07, -5.01624568e-04,
-3.26677463e-06, -1.17600486e-03],
[-9.04271785e-01, 1.35870378e-03, -1.62801330e-03,
2.81174900e-03, -1.41767569e-02, 3.37144580e-01,
7.25867496e-02, 6.57956534e-05, 4.48245167e-02,
-4.22432340e-03, 5.62545500e-05, 2.71758246e-04,
4.38917701e-05, 4.30040209e-03],
[ 2.48754354e-01, -6.37552526e-04, 3.18341130e-04,
1.16918247e-04, -1.10240540e-03, -8.29792499e-02,
-2.53401948e-02, 3.96860769e-04, -4.22432340e-03,
3.66989640e-03, -9.63653112e-05, -9.19943656e-04,
2.61522969e-06, -2.61760546e-03],
[-3.17889895e-03, 2.86982066e-06, -8.35890471e-06,
-2.04184173e-05, 9.93743845e-04, -2.22770278e-04,
2.23533865e-04, 5.05955056e-07, 5.62545500e-05,
-9.63653112e-05, 7.04077297e-06, -2.24690016e-06,
5.85050554e-07, -1.51941175e-05],
[-4.33848652e-01, -4.57267662e-05, 1.73821267e-04,
-1.23180596e-03, 3.67078932e-02, 1.70057224e-01,
1.92295864e-02, -5.01624568e-04, 2.71758246e-04,
-9.19943656e-04, -2.24690016e-06, 1.34106205e-02,
3.16335314e-05, 6.98263961e-04],
[-8.06514578e-03, 3.83565001e-06, -1.50295327e-06,
5.49048862e-06, -2.60236135e-05, 2.54063758e-03,
4.91467605e-04, -3.26677463e-06, 4.38917701e-05,
2.61522969e-06, 5.85050554e-07, 3.16335314e-05,
6.97639154e-06, 2.11772355e-05],
[-4.55830059e-01, 3.45785790e-04, -4.28123907e-04,
-6.26102272e-04, -1.98981785e-02, 9.39816822e-02,
5.78136563e-02, -1.17600486e-03, 4.30040209e-03,
-2.61760546e-03, -1.51941175e-05, 6.98263961e-04,
2.11772355e-05, 9.65534634e-03]])
# MacKinnon和White(1985)的异方差矩阵
model.cov_HC1
array([[ 6.40163195e+01, -4.17385405e-02, 4.79480228e-02,
-2.08373554e-02, -5.31616925e-01, -1.90226686e+01,
-6.09163105e+00, 6.67949185e-02, -9.30003096e-01,
2.55832730e-01, -3.26935542e-03, -4.46193939e-01,
-8.29464179e-03, -4.68800834e-01],
[-4.17385405e-02, 8.37753993e-04, -7.51253155e-05,
7.90055278e-05, -1.52216853e-04, 1.11209329e-02,
4.66287648e-03, -3.28742701e-05, 1.39736609e-03,
-6.55694264e-04, 2.95148223e-06, -4.70279343e-05,
3.94479452e-06, 3.55625223e-04],
[ 4.79480228e-02, -7.51253155e-05, 1.89487316e-04,
2.68667618e-06, 6.20896774e-04, -1.25169939e-02,
-5.90043553e-03, 6.04705057e-05, -1.67433888e-03,
3.27399617e-04, -8.59675973e-06, 1.78767400e-04,
-1.54572023e-06, -4.40306295e-04],
[-2.08373554e-02, 7.90055278e-05, 2.68667618e-06,
2.53817200e-03, -3.06772755e-03, -4.55649401e-02,
5.55616676e-03, 1.31682585e-04, 2.89175812e-03,
1.20245189e-04, -2.09994292e-05, -1.26685735e-03,
5.64672204e-06, -6.43918191e-04],
[-5.31616925e-01, -1.52216853e-04, 6.20896774e-04,
-3.06772755e-03, 1.67400668e+00, -2.58002277e-01,
-2.67870495e-02, 5.04556062e-04, -1.45801606e-02,
-1.13377466e-03, 1.02202111e-03, 3.77524267e-02,
-2.67641228e-05, -2.04643869e-02],
[-1.90226686e+01, 1.11209329e-02, -1.25169939e-02,
-4.55649401e-02, -2.58002277e-01, 1.43320219e+01,
1.22763845e+00, -2.93123837e-02, 3.46738125e-01,
-8.53404481e-02, -2.29109270e-04, 1.74896251e-01,
2.61293215e-03, 9.66559577e-02],
[-6.09163105e+00, 4.66287648e-03, -5.90043553e-03,
5.55616676e-03, -2.67870495e-02, 1.22763845e+00,
7.13855872e-01, -7.75964807e-03, 7.46522262e-02,
-2.60612573e-02, 2.29894585e-04, 1.97767698e-02,
5.05452455e-04, 5.94587604e-02],
[ 6.67949185e-02, -3.28742701e-05, 6.04705057e-05,
1.31682585e-04, 5.04556062e-04, -2.93123837e-02,
-7.75964807e-03, 2.71072722e-04, 6.76678874e-05,
4.08153555e-04, 5.20352151e-07, -5.15898438e-04,
-3.35973163e-06, -1.20946841e-03],
[-9.30003096e-01, 1.39736609e-03, -1.67433888e-03,
2.89175812e-03, -1.45801606e-02, 3.46738125e-01,
7.46522262e-02, 6.76678874e-05, 4.61000111e-02,
-4.34452773e-03, 5.78552892e-05, 2.79491204e-04,
4.51407229e-05, 4.42277125e-03],
[ 2.55832730e-01, -6.55694264e-04, 3.27399617e-04,
1.20245189e-04, -1.13377466e-03, -8.53404481e-02,
-2.60612573e-02, 4.08153555e-04, -4.34452773e-03,
3.77432434e-03, -9.91074135e-05, -9.46120914e-04,
2.68964679e-06, -2.69209016e-03],
[-3.26935542e-03, 2.95148223e-06, -8.59675973e-06,
-2.09994292e-05, 1.02202111e-03, -2.29109270e-04,
2.29894585e-04, 5.20352151e-07, 5.78552892e-05,
-9.91074135e-05, 7.24112017e-06, -2.31083635e-06,
6.01698334e-07, -1.56264705e-05],
[-4.46193939e-01, -4.70279343e-05, 1.78767400e-04,
-1.26685735e-03, 3.77524267e-02, 1.74896251e-01,
1.97767698e-02, -5.15898438e-04, 2.79491204e-04,
-9.46120914e-04, -2.31083635e-06, 1.37922235e-02,
3.25336726e-05, 7.18133261e-04],
[-8.29464179e-03, 3.94479452e-06, -1.54572023e-06,
5.64672204e-06, -2.67641228e-05, 2.61293215e-03,
5.05452455e-04, -3.35973163e-06, 4.51407229e-05,
2.68964679e-06, 6.01698334e-07, 3.25336726e-05,
7.17490674e-06, 2.17798397e-05],
[-4.68800834e-01, 3.55625223e-04, -4.40306295e-04,
-6.43918191e-04, -2.04643869e-02, 9.66559577e-02,
5.94587604e-02, -1.20946841e-03, 4.42277125e-03,
-2.69209016e-03, -1.56264705e-05, 7.18133261e-04,
2.17798397e-05, 9.93009197e-03]])
延申
如果看其他类型回归结果如何提取,则看以下网址:
- 逻辑回归 https://www.statsmodels.org/stable/generated/statsmodels.discrete.discrete_model.LogitResults.htm
- GAM回归 https://www.statsmodels.org/stable/generated/statsmodels.gam.generalized_additive_model.GLMGamResults.html
- 混合线性回归 https://www.statsmodels.org/stable/generated/statsmodels.regression.mixed_linear_model.MixedLMResults.html
- 稳健线性回归 https://www.statsmodels.org/stable/generated/statsmodels.robust.robust_linear_model.RLMResults.html#statsmodels.robust.robust_linear_model.RLMResults
- 贝叶斯回归 https://www.statsmodels.org/stable/generated/statsmodels.genmod.bayes_mixed_glm.BayesMixedGLMResults.html
计量经济学类
- 工具变量回归 https://www.statsmodels.org/stable/generated/statsmodels.sandbox.regression.gmm.IVRegressionResults.html
- GMM https://www.statsmodels.org/stable/generated/statsmodels.sandbox.regression.gmm.GMMResults.html
- IVGMM https://www.statsmodels.org/stable/generated/statsmodels.sandbox.regression.gmm.IVGMMResults.html