2013-08-30 3 views
1

팬더와 통계 모델을 통해 물류 회귀를 시도합니다. 왜 내가 오류가 발생했는지 또는 어떻게 수정해야하는지 모르겠다.파이썬의 회귀

import pandas as pd 
import statsmodels.api as sm 
x = [1, 3, 5, 6, 8] 
y = [0, 1, 0, 1, 1] 
d = { "x": pd.Series(x), "y": pd.Series(y)} 
df = pd.DataFrame(d) 

model = "y ~ x" 
glm = sm.Logit(model, df=df).fit() 

ERROR : 당신은 Logit에 수식을 전달할 수 없습니다

Traceback (most recent call last): 
    File "regress.py", line 45, in <module> 
    glm = sm.Logit(model, df=df).fit() 
TypeError: __init__() takes exactly 3 arguments (2 given) 

답변

8

. 해야할 일 :

In [82]: import patsy 

In [83]: f = 'y ~ x' 

In [84]: y, X = patsy.dmatrices(f, df, return_type='dataframe') 

In [85]: sm.Logit(y, X).fit().summary() 
Optimization terminated successfully. 
     Current function value: 0.511631 
     Iterations 6 
Out[85]: 
<class 'statsmodels.iolib.summary.Summary'> 
""" 
          Logit Regression Results 
============================================================================== 
Dep. Variable:      y No. Observations:     5 
Model:       Logit Df Residuals:      3 
Method:       MLE Df Model:       1 
Date:    Fri, 30 Aug 2013 Pseudo R-squ.:     0.2398 
Time:      16:56:38 Log-Likelihood:    -2.5582 
converged:      True LL-Null:      -3.3651 
             LLR p-value:     0.2040 
============================================================================== 
       coef std err   z  P>|z|  [95.0% Conf. Int.] 
------------------------------------------------------------------------------ 
Intercept  -2.0544  2.452  -0.838  0.402  -6.861  2.752 
x    0.5672  0.528  1.073  0.283  -0.468  1.603 
============================================================================== 
""" 

이것은 바로 the docs on how to do exactly what you're asking에서 곧바로입니다.

편집 : user333700 @ 의해 제안 또한, 공식 API를 사용할 수 있습니다 smf`로`수입 statsmodels.api이

In [22]: print sm.formula.logit(model, data=df).fit().summary() 
Optimization terminated successfully. 
     Current function value: 0.511631 
     Iterations 6 
          Logit Regression Results 
============================================================================== 
Dep. Variable:      y No. Observations:     5 
Model:       Logit Df Residuals:      3 
Method:       MLE Df Model:       1 
Date:    Fri, 30 Aug 2013 Pseudo R-squ.:     0.2398 
Time:      18:14:26 Log-Likelihood:    -2.5582 
converged:      True LL-Null:      -3.3651 
             LLR p-value:     0.2040 
============================================================================== 
       coef std err   z  P>|z|  [95.0% Conf. Int.] 
------------------------------------------------------------------------------ 
Intercept  -2.0544  2.452  -0.838  0.402  -6.861  2.752 
x    0.5672  0.528  1.073  0.283  -0.468  1.603 
============================================================================== 
+0

또는 사용 수식 함수를 다음 smf.logit (공식 .. .) – user333700

+0

편집 됨. 나는 그것에 대해 몰랐다, 고마워! –

+0

수정 된 답변을 제공해 주셔서 감사합니다. 내 의견은 금식했다. 나는'import statsmodels.formula.api as smf'를 작성하여 수식 인터페이스의 바로 가기, 소문자 함수에 대한 액세스를 제공하기를 원했습니다. 그것들은 단지 모델의 'from_formula' 메소드를 둘러싼 편의성 래퍼 일뿐입니다. 예를 들어'sm.Logit.from_formula' – user333700