使用OLS Anova表获取Ptython错误-statsmodels

时间:2019-07-15 17:21:32

标签: python-3.x statsmodels

我正在尝试使用statsmodels创建ANOVA并出现错误

[table image]

variable    value

0 x-y-20 -0.070000 1 x-y-20 0.090000 2 x-y-20 0.080000 3 xyy-20 -0.030000

代码

melt_t['variable'].replace({'x-y-20':'set-a','x-y-40':'set-b','x-y-60':'set-c'}, inplace=True)
results = ols('value ~ Q(variable)', data=melt_t).fit()
results.summary()

错误


PatsyError: Error evaluating factor: TypeError: 'Series' objects are mutable, thus they cannot be hashed
    value ~ Q(variable)
            ^^^^^^^^^^^

我正在尝试获取下表 https://i0.wp.com/pythonfordatascience.org/wp-content/uploads/2018/04/anova-statsmodel-results-1.png?w=613&ssl=1

任何建议将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:0)

我不确定您实际上 Q()转换是什么,但是如果您确实需要/想要使用它,似乎您已经省略了{{1}周围的引号}:

variable

给予(摘录):

data = {'variable': {0: 'x-y-20', 1: 'x-y-40', 2: 'x-y-60', 3: 'x-y-20'},
        'value': {0: -0.07, 1: 0.09, 2: 0.08, 3: -0.03}}
df = pd.DataFrame(data)
df['variable'].replace({'x-y-20':'set-a','x-y-40':'set-b','x-y-60':'set-c'}, inplace=True)
results = smf.ols('value ~ Q("variable")', data=df).fit()
results.summary()

(我使用 coef std err t P>|t| [0.025 0.975] ------------------------------------------------------------------------------------------ Intercept -0.0500 0.020 -2.500 0.242 -0.304 0.204 Q("variable")[T.set-b] 0.1400 0.035 4.041 0.154 -0.300 0.580 Q("variable")[T.set-c] 0.1300 0.035 3.753 0.166 -0.310 0.570 而不使用'value ~ variable'来获得同一张表。)