Python 在statsmodels的摘要中保留变量名

Python 在statsmodels的摘要中保留变量名,python,statsmodels,Python,Statsmodels,我使用的是statsmodel的OLS,链接是 你可以看到X在摘要中显示为USD,这就是我想要的。 但是,在添加新变量之后 #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

我使用的是statsmodel的OLS,链接是

你可以看到X在摘要中显示为USD,这就是我想要的。 但是,在添加新变量之后

#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.

它显示的不是美元和日元,而是x1-x2。有办法解决吗?我试过谷歌,但什么也没找到

因为我的问题都是关于显示的,因此,如果我保留标题,那么问题就解决了,所以我会发布我的解决方案,以防有人有同样的问题

#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.

美元和日元到底是什么意思?当您将它们作为常量添加时,它们的值是多少?问题是,我发现当我使用np.column_stack时,它只会返回值,因此不包括标题日元和美元。然而,我还没有找到解决问题的方法,这不是我的问题;你只是在重复你原来的问题。当然,statsmodels很好地跟踪了常数的名称,并且尽可能地跟踪它们。当使用NumPy数组作为输入时,statsmodels不能再这样做了。我怀疑你是否可以手动检索这些名称,因为如果你可以的话,它很可能已经被编程到statsmodels中了。真正的问题是:这有什么关系?也许真正的解决方案是,
USD
JPY
需要保存为
pandas.Dataframe
s?是的,它们需要是pd.Dataframe。因此,我们可以使用join函数。您也可以使用公式,当您需要调整变量(如获取日志值)时,在代码和解决方案中都可能更容易阅读。
#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.