我想知道是否可以将pandas.ols模型应用于一次针对一个自变量的多个响应变量的数据框架。
因此,假设我有以下内容:
In [109]: y=pandas.DataFrame(np.random.randn(10,4))
In [110]: x=pandas.DataFrame(np.random.randn(10,1))我想做这样的事情:
In [111]: model=pandas.ols(y=y, x=x)基本上使用四个模型输出的结果,或者至少访问四个模型的系数。如果可能的话,我倾向于避免遍历响应变量。
发布于 2013-04-13 02:38:21
我想这个应该可以了。
#First generate the data
x=pd.DataFrame(np.random.randn(10,1))
y=pd.DataFrame(np.random.randn(10,4))
#Since we are doing things manually we'll need to add the constant term to the x matrix
x[1] = ones(10)
#This matrix precomputes (X'X)^-1X which we will premultiply the y matrix by to get results
tmpmat = np.dot(np.linalg.pinv(np.dot(x.T ,x)),x.T)
#Solve for the betas
betamatrix = np.dot(tmpmat,y)
#Compare with the pandas output one at a time.
model=pd.ols(y=y[0], x=x, intercept=False)
model=pd.ols(y=y[1], x=x, intercept=False)发布于 2013-06-14 18:44:54
我已经做过很多次了,还没有找到循环的替代方法。下面的代码将把四个回归的结果存储在一个字典中。如果您只对部分系数感兴趣,则可以在循环遍历回归时捕获它们。
model = {}
for i in y:
model[i] = pd.ols(y=y[i], x=x)https://stackoverflow.com/questions/14940948
复制相似问题