繁体   English   中英

以变​​量为约束条件的约束回归Python

[英]Constrained regression python with variables as constraints

我正在尝试使用sm.GLM模型然后使用model.fit_constrained代码在Python中运行受限回归。

我要同时输入两个变量和两个虚拟变量,这些虚拟变量是我要限制的。 我希望两个虚拟变量系数乘以一个权重等于零。

当我将系数乘以整数权重时,效果很好,如下所示

results = model.fit_constrained('BOATS * 1 + CARS * 0.5')

但是,我希望这些整数是变量,并且取决于我的数据比例(每个虚拟变量为1)。 我已经计算了SectorWgt系列中的比例,但是无法弄清楚如何将其输入到model.fit_constrained代码中。

这是我最好的尝试

results = model.fit_constrained('SIZE*int(SectorWgt.iloc[0])+VQMadj*int(SectorWgt.iloc[1])')

但是然后我得到了错误

patsy.PatsyError: unrecognized token in constraint

因为

int(SectorWgt.iloc[0])

代码的一部分。

有人有想法吗? 谢谢!

使用字符串格式:

x = int(SectorWgt.iloc[0])
y = int(SectorWgt.iloc[1])

results = model.fit_constrained('SIZE*{}+VQMadj*{}'.format(x, y))

如果使用的是Python 3.6或更高版本,则可以利用Python的f-strings使用更简洁的字符串插值语法。

constraint_str = f"SIZE*{int(SectorWgt.iloc[0])}+VQMadj*{int(SectorWgt.iloc[1])}"
results = model.fit_constrained(constraint_str)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM