【问题标题】:Getting Ptython Error with OLS Anova table - statsmodels使用 OLS Anova 表获取 Ptython 错误 - statsmodels
【发布时间】:2019-07-16 01:32:28
【问题描述】:

我正在尝试使用statsmodels 创建一个方差分析并收到错误

[table image]

variable    value

0 x-y-20 -0.070000 1 x-y-20 0.090000 2 x-y-20 0.080000 3 x-y-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

任何建议将不胜感激!

谢谢

【问题讨论】:

    标签: python-3.x statsmodels


    【解决方案1】:

    我不确定您是否真的需要 Q() 转换,但如果您确实需要/想要使用它,您似乎在 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' 而不使用Q() 得到同一张表。)

    【讨论】:

    • 谢谢@brendan!你是对的。当我删除“Q”时它会起作用。谢谢!
    • @RTP 很高兴这对你有用。如果您觉得这有帮助,请考虑接受/点赞。
    猜你喜欢
    • 1970-01-01
    • 2022-06-08
    • 2020-11-23
    • 2023-02-15
    • 2020-07-03
    • 2020-12-19
    • 2019-09-07
    • 2018-10-17
    • 2021-02-28
    相关资源
    最近更新 更多