【问题标题】:X=sm.add_constant(X, prepend=True) is not workingX=sm.add_constant(X, prepend=True) 不工作
【发布时间】:2014-11-20 08:50:03
【问题描述】:

我正在尝试从 python 中的线性回归 (OLS) 中获取 beta 和误差项。我被困在声明X=sm.add_constant(X, prepend=True),它返回一个

error:"AttributeError: 'module' 对象没有属性 'add_constant'"

我已经安装了 statsmodels 模块。

【问题讨论】:

    标签: python regression linear-regression


    【解决方案1】:

    尝试导入 statsmodel.api

    import statsmodels.api as sm

    【讨论】:

    • 我的错误是我使用了“import statsmodels as sm”——使用上面的行解决了我的问题。
    【解决方案2】:

    尝试导入add_constant,例如:

    from statsmodels.api import add_constant
    

    【讨论】:

      【解决方案3】:

      如果smstatsmodels中定义的对象,则需要通过statsmodels.sm调用,或者使用from statsmodel import sm,则可以直接调用sm

      【讨论】:

      • 谢谢,我有 "print sm.OLS(Y,X).fit().params" 打印 beta 和系数。如果我只想要 beta 或 coefficint 怎么办?我该怎么做?
      • 如果Y和X是numpy数组,那么需要使用切片来获取具体参数,params[0],params[1],...如果Y和X是pandas Series和DataFrames或者当使用公式,那么params 将是一个熊猫系列,访问是按名称。
      猜你喜欢
      • 1970-01-01
      • 2013-12-23
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多