Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve variable names in summary from statsmodels

I am using OLS from statsmodel, the link is https://www.statsmodels.org/stable/examples/notebooks/generated/ols.html

#USD
X = sm.add_constant(USD)
model = sm.OLS(y, X)
results = model.fit()
print(results.summary())
                                 OLS Regression Results                                 
========================================================================================
Dep. Variable:     All Ordinaries closing price   R-squared:                       0.265
Model:                                      OLS   Adj. R-squared:                  0.265
Method:                           Least Squares   F-statistic:                     352.4
Date:                          Tue, 23 Oct 2018   Prob (F-statistic):           2.35e-67
Time:                                  17:30:24   Log-Likelihood:                -8018.8
No. Observations:                           977   AIC:                         1.604e+04
Df Residuals:                               975   BIC:                         1.605e+04
Df Model:                                     1                                         
Covariance Type:                      nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const       1843.1414    149.675     12.314      0.000    1549.418    2136.864
USD         3512.5040    187.111     18.772      0.000    3145.318    3879.690
==============================================================================
Omnibus:                      276.458   Durbin-Watson:                   0.009
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               74.633
Skew:                           0.438   Prob(JB):                     6.22e-17
Kurtosis:                       1.967   Cond. No.                         10.7
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified

You can see the X is showing as USD in the summary which is what I want. However, after adding a new variable

#JPY + USD
X = sm.add_constant(JPY)
X = np.column_stack((X, USD))
model = sm.OLS(y, X)
results = model.fit()
print(results.summary())


 OLS Regression Results                                 
========================================================================================
Dep. Variable:     All Ordinaries closing price   R-squared:                       0.641
Model:                                      OLS   Adj. R-squared:                  0.640
Method:                           Least Squares   F-statistic:                     868.8
Date:                          Tue, 23 Oct 2018   Prob (F-statistic):          2.80e-217
Time:                                  17:39:19   Log-Likelihood:                -7669.4
No. Observations:                           977   AIC:                         1.534e+04
Df Residuals:                               974   BIC:                         1.536e+04
Df Model:                                     2                                         
Covariance Type:                      nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const      -1559.5880    149.478    -10.434      0.000   -1852.923   -1266.253
x1            78.6589      2.466     31.902      0.000      73.820      83.497
x2          -366.5850    178.672     -2.052      0.040    -717.211     -15.958
==============================================================================
Omnibus:                       24.957   Durbin-Watson:                   0.031
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               27.278
Skew:                           0.353   Prob(JB):                     1.19e-06
Kurtosis:                       3.415   Cond. No.                         743.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

It is not showing USD and JPY, but x1 x2. Is there a way to fix it? I tried google but found nothing.

like image 904
Michael Avatar asked Oct 23 '18 06:10

Michael


1 Answers

As my question is all care about the showing, thus, if I keep the header, then the problem solved, so I post my solution in case someone may have the same problem.

#JPY + USD
X = JPY.join(USD)
X = sm.add_constant(X)
#X = np.column_stack((X, USD))
model = sm.OLS(y, X)
results = model.fit()
print(results.summary())


     OLS Regression Results                                 
========================================================================================
Dep. Variable:     All Ordinaries closing price   R-squared:                       0.641
Model:                                      OLS   Adj. R-squared:                  0.640
Method:                           Least Squares   F-statistic:                     868.8
Date:                          Tue, 23 Oct 2018   Prob (F-statistic):          2.80e-217
Time:                                  22:51:43   Log-Likelihood:                -7669.4
No. Observations:                           977   AIC:                         1.534e+04
Df Residuals:                               974   BIC:                         1.536e+04
Df Model:                                     2                                         
Covariance Type:                      nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const      -1559.5880    149.478    -10.434      0.000   -1852.923   -1266.253
JPY           78.6589      2.466     31.902      0.000      73.820      83.497
USD         -366.5850    178.672     -2.052      0.040    -717.211     -15.958
==============================================================================
Omnibus:                       24.957   Durbin-Watson:                   0.031
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               27.278
Skew:                           0.353   Prob(JB):                     1.19e-06
Kurtosis:                       3.415   Cond. No.                         743.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
like image 52
Michael Avatar answered Sep 21 '22 13:09

Michael