Statsmodels:如何为回归选择不同的置信区间

时间:2019-06-21 20:14:28

标签: regression statsmodels confidence-interval

我希望使用statsmodels以99%的置信区间进行回归,而不是使用默认的95%进行回归。

我看了看文档,看是否在fit()方法中有一个参数,但是我没有注意到。我也尝试了conf_int方法,但对输出感到困惑。

import pandas as pd
import math
import statsmodels.formula.api as sm
df = pd.read_excel(r'C:\TestData.xlsx')
df['LogBalance'] = df['Balance'].map(lambda x: math.log(x))
est = sm.ols(formula= 'LogBalance ~ N + Rate',       
    data=df).fit(cov_type='HAC',cov_kwds={'maxlags':1})
print(est.summary())
print(est.conf_int(alpha=0.01, cols=None))

由于我是Python新手,如果可以的话,您能告诉我是否以及如何在初始回归输出中以可调的置信区间对statsmodels进行回归吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您可以在.summary() directly中指定置信区间,请考虑以下示例:

import statsmodels.formula.api as smf
import seaborn as sns

# load a sample dataset
df = sns.load_dataset('tips')

# run model
formula = 'tip ~ size + total_bill'
results = smf.ols(formula=formula, data=df).fit()
# use 95 % CI (default setting)
print(results.summary())
OLS Regression Results                            
==============================================================================
Dep. Variable:                    tip   R-squared:                       0.468
Model:                            OLS   Adj. R-squared:                  0.463
Method:                 Least Squares   F-statistic:                     105.9
Date:                Fri, 21 Jun 2019   Prob (F-statistic):           9.67e-34
Time:                        21:42:09   Log-Likelihood:                -347.99
No. Observations:                 244   AIC:                             702.0
Df Residuals:                     241   BIC:                             712.5
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept      0.6689      0.194      3.455      0.001       0.288       1.050
size           0.1926      0.085      2.258      0.025       0.025       0.361
total_bill     0.0927      0.009     10.172      0.000       0.075       0.111
==============================================================================
Omnibus:                       24.753   Durbin-Watson:                   2.100
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               46.169
Skew:                           0.545   Prob(JB):                     9.43e-11
Kurtosis:                       4.831   Cond. No.                         67.6
==============================================================================
# use 99 % CI
print(results.summary(alpha=0.01))
OLS Regression Results                            
==============================================================================
Dep. Variable:                    tip   R-squared:                       0.468
Model:                            OLS   Adj. R-squared:                  0.463
Method:                 Least Squares   F-statistic:                     105.9
Date:                Fri, 21 Jun 2019   Prob (F-statistic):           9.67e-34
Time:                        21:45:57   Log-Likelihood:                -347.99
No. Observations:                 244   AIC:                             702.0
Df Residuals:                     241   BIC:                             712.5
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.005      0.995]
------------------------------------------------------------------------------
Intercept      0.6689      0.194      3.455      0.001       0.166       1.172
size           0.1926      0.085      2.258      0.025      -0.029       0.414
total_bill     0.0927      0.009     10.172      0.000       0.069       0.116
==============================================================================
Omnibus:                       24.753   Durbin-Watson:                   2.100
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               46.169
Skew:                           0.545   Prob(JB):                     9.43e-11
Kurtosis:                       4.831   Cond. No.                         67.6
==============================================================================