【问题标题】:Python equivalent for poly(x, 2) adds an orthogonal polynomial of degree 2poly(x, 2) 的 Python 等效项添加了一个 2 次正交多项式
【发布时间】:2018-07-10 00:48:48
【问题描述】:

这里是 R 代码

model1 <- glm(wt82_71 ~ qsmk + sex + race + poly(age, 2, raw = TRUE)   + education + poly(smokeintensity, 2, raw = TRUE) + poly(smokeyrs, 2, raw = TRUE) + exercise + active + poly(wt71, 2, raw = TRUE) + qsmk:smokeintensity,data = nhefs)

我在 Python 中写道:

mod3 = smf.glm(formula='qsmk ~ sex + race + education + exercise + active + poly(age,2) + poly(smokeintensity,2) + poly(smokeyrs,2) + poly(wt71,2)', family=sm.families.Binomial(), data=nhefs).fit()
mod3.summary()

poly() 在 python 中会是什么? 这是一些cmets 模型 1:协变量回归,允许进行一些效果修改 备注:

(1) poly(x, 2) 添加一个 2 次正交多项式,如果您希望它产生与 x + x^2 相同的系数,请添加参数 raw = TRUE

(2) x1*x2 输入 x1 和 x2 的主效应及其乘积项 x1:x2 仅输入乘积项(此处对于烟雾强度是必需的,因为我们希望在交互作用中对烟雾强度进行线性处理,但在主效应中进行二次处理,因此烟雾强度的线性项不可估计)

(3) 有缺失值的观测值会被自动删除

【问题讨论】:

标签: python r statsmodels glm


【解决方案1】:

AFAIK,poly 用于连续变量的多项式基函数,patsy 尚不支持。现有的poly 用于有序分类变量。

Numpy 有 vander 函数,可用于可直接在公式中使用的各种多项式基数。

对现有数据集进行正交化是否有用值得商榷。我不想这样做,因为当数据集更改时,基函数不会更改。

https://github.com/pydata/patsy/issues/20 https://github.com/pydata/patsy/pull/92/files

作为替代方案,可以直接指定幂项,请参阅python stats models - quadratic term in regression,但这不会正交化。

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多