英文:
Declare manually the coefficients before doing an ols regression in python
问题
我想在这种类型的方程上运行OLS回归:y = b0 + b1*x1 + (1 - b1) * x2 + b2 * x3。
所以我需要手动声明系数,但我在Python中使用statsmodel包找不到如何做到这一点的方法。请问有人知道如何在方程中声明系数吗?
英文:
I want to run an ols regression on this type of equations: y = b0 + b1*x1 + (1 - b1) * x2 + b2 * x3.
So I need to declare manually the coefficients but I didn't find how to do it in python using the statsmodel package. Please anyone have an idea how to declare the coefficients in the equation?
答案1
得分: 0
如果您想要强加线性约束并估计剩余参数,有三种选项。
重新编写OLS公式
将公式:
y = b0 + b1*x1 + (1 - b1) * x2 + b2 * x3
重写为:
y - x2 = b0 + b1*(x1 - x2) + b2 * x3
y_new = b0 + b1 * x_diff + b2 *x3
然后使用后面的方程使用OLS来估计系数。缺点是:predict
返回了 y_new 的期望值。
带偏移的GLM
GLM: y ~ b0 + b1*(x1 - x2) + b2 * x3, offset=x2
这里使用了线性函数中的 offset
,系数为 1。
predict
用于 y
,但需要偏移项。
GLM 的默认 family 是具有线性链接的高斯分布,即与OLS相同。
带有fit_constrained的GLM
强加线性(实际上是仿射)约束,使x1和x2的系数相加等于1:
GLM.from_formula("y = x1 + x2 + x3").fit_constrained("x1 + x2 = 1")
(对于OLS不可用的 fit_constrained
)。
英文:
Three options if you want to impose the linear constraint and estimate the remaining parameters
Rewrite formula for OLS
y = b0 + b1*x1 + (1 - b1) * x2 + b2 * x3
as
y - x2 = b0 + b1*(x1 - x2) + b2 * x3
y_new = b0 + b1 * x_diff + b2 *x3
and estimate coefficients with OLS using the later equation.
disadvantage: predict
returns expectation for y_new.
GLM with offset
GLM: y ~ b0 + b1*(x1 - x2) + b2 * x3, offset=x2
This uses offset
with coefficient 1 in the linear function.
predict
is for y
, but needs offset.
Default family for GLM is Gaussian with linear link, i.e. the same as OLS.
GLM with fit_constrained
impose linear (actually affine) restriction that coefficients for x1 and x2 add to 1:
GLM.from_formula("y = x1 + x2 + x3").fit_constrained("x1 + x2 = 1")
(fit_constrained is not available for OLS)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论