2017-02-02 1 views
0

저는 python statsmodels 패키지를 처음 사용합니다. 나는 log (x)에 선형 적으로 관련된 몇 가지 데이터를 시뮬레이션하고 statsmodels 공식 인터페이스를 사용하여 간단한 선형 회귀 분석을 실행하려고한다. 여기 코드는 다음과 같습니다 statsmodel : 데이터를 시뮬레이트하고 간단한 선형 회귀 분석을 실행하십시오.

ValueError        Traceback (most recent call last) 
<ipython-input-34-c0ab32ca2acf> in <module>() 
     7 y=B0 + B1*np.log(X)+epsilon 
     8 df1 = pd.DataFrame({'Y':y, 'X':X}) 
----> 9 smf.OLS ('Y~np.log(X)', data=df1) 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/regression/linear_model.py in __init__(self, endog, exog, missing, hasconst, **kwargs) 
    689     **kwargs): 
    690   super(OLS, self).__init__(endog, exog, missing=missing, 
--> 691         hasconst=hasconst, **kwargs) 
    692   if "weights" in self._init_keys: 
    693    self._init_keys.remove("weights") 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/regression/linear_model.py in __init__(self, endog, exog, weights, missing, hasconst, **kwargs) 
    584    weights = weights.squeeze() 
    585   super(WLS, self).__init__(endog, exog, missing=missing, 
--> 586         weights=weights, hasconst=hasconst, **kwargs) 
    587   nobs = self.exog.shape[0] 
    588   weights = self.weights 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/regression/linear_model.py in __init__(self, endog, exog, **kwargs) 
    89  """ 
    90  def __init__(self, endog, exog, **kwargs): 
---> 91   super(RegressionModel, self).__init__(endog, exog, **kwargs) 
    92   self._data_attr.extend(['pinv_wexog', 'wendog', 'wexog', 'weights']) 
    93 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/base/model.py in __init__(self, endog, exog, **kwargs) 
    184 
    185  def __init__(self, endog, exog=None, **kwargs): 
--> 186   super(LikelihoodModel, self).__init__(endog, exog, **kwargs) 
    187   self.initialize() 
    188 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/base/model.py in __init__(self, endog, exog, **kwargs) 
    58   hasconst = kwargs.pop('hasconst', None) 
    59   self.data = self._handle_data(endog, exog, missing, hasconst, 
---> 60          **kwargs) 
    61   self.k_constant = self.data.k_constant 
    62   self.exog = self.data.exog 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/base/model.py in _handle_data(self, endog, exog, missing, hasconst, **kwargs) 
    82 
    83  def _handle_data(self, endog, exog, missing, hasconst, **kwargs): 
---> 84   data = handle_data(endog, exog, missing, hasconst, **kwargs) 
    85   # kwargs arrays could have changed, easier to just attach here 
    86   for key in kwargs: 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/base/data.py in handle_data(endog, exog, missing, hasconst, **kwargs) 
    562   exog = np.asarray(exog) 
    563 
--> 564  klass = handle_data_class_factory(endog, exog) 
    565  return klass(endog, exog=exog, missing=missing, hasconst=hasconst, 
    566     **kwargs) 

/Users/tiger/anaconda/lib/python3.5/site-packages/statsmodels/base/data.py in handle_data_class_factory(endog, exog) 
    551  else: 
    552   raise ValueError('unrecognized data structures: %s/%s' % 
--> 553       (type(endog), type(exog))) 
    554  return klass 
    555 

ValueError: unrecognized data structures: <class 'str'>/<class 'NoneType'> 

내가 문서화를 확인하고 모든 것이 잘 될 것 같다 :

import pandas as pd 
import numpy as np 
import statsmodels.formula.api as smf 

B0 = 3 
B1 = 0.5 
x = np.linspace(10, 1e4, num = 1000) 
epsilon = np.random.normal(0,3, size=1000) 

y=B0 + B1*np.log(x)+epsilon 
df1 = pd.DataFrame({'Y':y, 'X':x}) 

model = smf.OLS ('Y~np.log(X)', data=df1).fit() 

나는 아래에 오류가 발생했습니다. 오랜 시간 동안 왜 이런 오류가 있었는지 이해하려고 노력했으나 알아 내지 못했습니다. 도움말은 대단히 감사하겠습니다.

답변

3

statsmodels.formula.api에서 ols 메소드는 소문자입니다. statsmodels.api에서 OLS는 모두 대문자입니다. 귀하의 경우에 ...

model = smf.ols('Y~np.log(X)', data=df1).fit() 
+0

정말 고마워요! 차이를 단지 실현하십시오 ... @ rtk22 – zesla