2
以下代碼應運行MNLogit模型並返回置信區間。它成功地返回摘要,你可以看到那裏的置信區間,但是當試圖通過conf_int()返回置信區間時,我得到一個ValueError:必須傳遞2-d輸入。ValueError:嘗試返回statsmodels時必須通過2-d輸入MNLogit置信區間
import pandas as pd
import statsmodels.api as sm
tmp = pd.read_csv('http://surveyanalysis.org/images/8/82/TrickedLogitMaxDiffExample.csv')
model = sm.MNLogit.from_formula('Choice ~ 1+B+C+D+E+F', tmp, missing='drop')
res = model.fit(method='ncg')
print(res.summary())
res.conf_int()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-255-a332500964e4> in <module>()
6 res = model.fit(method='ncg')
7 print(res.summary())
----> 8 res.conf_int()
~/anaconda/lib/python3.6/site-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs)
93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:])
94 elif how:
---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how)
96 return obj
97
~/anaconda/lib/python3.6/site-packages/statsmodels/base/data.py in wrap_output(self, obj, how, names)
405 def wrap_output(self, obj, how='columns', names=None):
406 if how == 'columns':
--> 407 return self.attach_columns(obj)
408 elif how == 'rows':
409 return self.attach_rows(obj)
~/anaconda/lib/python3.6/site-packages/statsmodels/base/data.py in attach_columns(self, result)
522 return Series(result, index=self.param_names)
523 else: # for e.g., confidence intervals
--> 524 return DataFrame(result, index=self.param_names)
525
526 def attach_columns_eq(self, result):
~/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
304 else:
305 mgr = self._init_ndarray(data, index, columns, dtype=dtype,
--> 306 copy=copy)
307 elif isinstance(data, (list, types.GeneratorType)):
308 if isinstance(data, types.GeneratorType):
~/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in _init_ndarray(self, values, index, columns, dtype, copy)
461 # by definition an array here
462 # the dtypes will be coerced to a single dtype
--> 463 values = _prep_ndarray(values, copy=copy)
464
465 if dtype is not None:
~/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in _prep_ndarray(values, copy)
5686 return arrays, arr_columns
5687
-> 5688
5689 def _list_to_arrays(data, columns, coerce_float=False, dtype=None):
5690 if len(data) > 0 and isinstance(data[0], tuple):
ValueError: Must pass 2-d input
請編輯您的帖子,包括你的錯誤的完整追蹤。 –
我認爲這是一個錯誤...我注意到,如果我使用正常(非公式)語法運行模型,它可以正常工作,但是使用公式語法它不會。 – BirdLaw
你可以顯示你的'tmp'變量嗎?它是否有公式中指定的列? –