Tag Archives: ols

[Solved] ValueError: the indices for endog and exog are not aligned

When running the following code

x = data1        
y = data2
X = sm.add_constant(x)
result = (sm.OLS(y, X)).fit()
print(result.summary)

Error: valueerror: the indexes for endog and exog are not aligned

Solutions:

1. Check the data type: it has nothing to do with the service or dataframe type, and it has nothing to do with whether the data types of Y and X are consistent

2. Check data length: len (y) and Len (x) have the same length

3. It is found that although the length of len is the same, the index value of data is different. That’s the problem.

Because my x data is vertically combined by two dataframe data through concat, the index values of the data are different.

As shown in the figure below:

The complete data are as follows:

       date 
0    20180101
1    20180102
2    20180103
3    20180104
4    20180105
5    20180106

But the consolidated data is presented in this way, but:

       date 
0    20180101
1    20180102
2    20180103
3    20180104
0    20180105
1    20180106

Because the index values on the left side of the two groups of service data used for calculation are different, an error is reported. Solution:

Whether the original data is of dataframe or service type, it is converted to list type first. Take dataframe data as an example

datalist = data['close'].tolist()                    # change dataframe to list
datalist = data + templist                           # list MERGER
dataf = pd.DataFrame(datalist, columns=['close'])    # list TO dataframe

After a series of data processing in the early stage, the program is executed again, and the error disappears.

Normal display:

                            OLS Regression Results
==============================================================================
Dep. Variable:                  close   R-squared:                       0.326
Model:                            OLS   Adj. R-squared:                  0.326
Method:                 Least Squares   F-statistic:                     1932.
Date:                Tue, 22 Jan 2019   Prob (F-statistic):               0.00
Time:                        14:34:33   Log-Likelihood:                -11706.
No. Observations:                4000   AIC:                         2.342e+04
Df Residuals:                    3998   BIC:                         2.343e+04
Df Model:                           1
Covariance Type:            nonrobust
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const          6.8217      0.092     74.332      0.000       6.642       7.002
close          0.3305      0.008     43.951      0.000       0.316       0.345
==============================================================================
Omnibus:                      786.494   Durbin-Watson:                   0.009
Prob(Omnibus):                  0.000   Jarque-Bera (JB):             1384.316
Skew:                           1.260   Prob(JB):                    2.51e-301
Kurtosis:                       4.399   Cond. No.                         15.7
==============================================================================