简体   繁体   English

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

[英]Constrained regression python with variables as constraints

I'm attempting to run a constrained regression in Python, using the sm.GLM model and then the model.fit_constrained code. 我正在尝试使用sm.GLM模型然后使用model.fit_constrained代码在Python中运行受限回归。

I am feeding in two variables alongside two dummy variables, the dummies are what I am trying to constrain. 我要同时输入两个变量和两个虚拟变量,这些虚拟变量是我要限制的。 I want the two dummy variables coefficients multiplied by a weight to equal zero. 我希望两个虚拟变量系数乘以一个权重等于零。

This works fine when I am multiply the coefficients by integer weights, as below 当我将系数乘以整数权重时,效果很好,如下所示

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

However, I want these integers to be variable, and depend on the proportion of my data with a 1 for each dummy variable. 但是,我希望这些整数是变量,并且取决于我的数据比例(每个虚拟变量为1)。 I have calculated the proportions in the series SectorWgt, but cannot work out how to then feed it in to the model.fit_constrained code. 我已经计算了SectorWgt系列中的比例,但是无法弄清楚如何将其输入到model.fit_constrained代码中。

This has been my best attempt 这是我最好的尝试

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

But then I get the error 但是然后我得到了错误

patsy.PatsyError: unrecognized token in constraint

due to the 因为

int(SectorWgt.iloc[0])

part of code. 代码的一部分。

Does anyone have any thoughts? 有人有想法吗? Thanks! 谢谢!

Use string formatting: 使用字符串格式:

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

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

If using Python 3.6 or newer, you can take advantage of cleaner string interpolation syntax with Python's f-strings . 如果使用的是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