3

pandas.ols一度に1 つの独立変数に対して複数の応答変数のデータ フレームにモデルを適用できるかどうか疑問に思っています。

だから私は次のものを持っていると想像してください:

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)

基本的に、4 つのモデル出力の結果、または少なくとも 4 つの係数にアクセスします。可能であれば、応答変数をループすることは避けたいと思います。

4

2 に答える 2

1

これでいいと思います。

#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-04-12T18:38:21.383 に答える
0

これを何度も繰り返しましたが、ループに代わる方法は見つかりませんでした。次のコードは、4 つの回帰の結果を dict に格納します。係数の一部のみに関心がある場合は、回帰をループするときにそれらをキャプチャできます。

model = {}
for i in y:
    model[i] = pd.ols(y=y[i], x=x)
于 2013-06-14T10:44:54.363 に答える