简体   繁体   中英

Statsmodel multivariate OLS error “matrices are not aligned”

I am trying to solve multivariate regression. Here is the code attached for the regression. The model builds fine, but when I try to retrieve the summary, it gives following error

ValueError: matrices are not aligned

Here is the traceback:

Traceback (most recent call last):
File "/Users/mikhilraj/Desktop/try2.py", line 23, in <module>
    print mod.summary()
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/regression/linear_model.py", line 1967, in summary
    top_right = [('R-squared:', ["%#8.3f" % self.rsquared]),
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/tools/decorators.py", line 97, in __get__
    _cachedval = self.fget(obj)
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/regression/linear_model.py", line 1181, in rsquared
    return 1 - self.ssr/self.centered_tss
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/tools/decorators.py", line 97, in __get__
    _cachedval = self.fget(obj)
File "/Library/Python/2.7/site-packages/statsmodels-0.7.0-py2.7-macosx-10.10-intel.egg/statsmodels/regression/linear_model.py", line 1153, in ssr
    return np.dot(wresid, wresid)
ValueError: matrices are not aligned

Code:

import numpy as np
import statsmodels.api as sm


np.random.seed(12345)

N = 30

X = np.random.uniform(-20, 20, size=(N,10))
beta = np.random.randn(11)
X = sm.add_constant(X)


weights = np.random.uniform(1, 20, size=(N,))
weights = weights/weights.sum()

y = np.dot(X, beta) + weights*np.random.uniform(-100, 100, size=(N,))

Y = np.c_[y,y,y]

mod = sm.OLS(Y, X).fit()

print mod.summary()

The endog parameter should be a 1D vector of the dependent variable. Changing the parameter Y in your model to y (for example) allows the code to run without error.

http://statsmodels.sourceforge.net/devel/generated/statsmodels.regression.linear_model.OLS.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM