I'm attempting to run a constrained regression in Python, using the sm.GLM model and then the model.fit_constrained code.
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. 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.
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 .
constraint_str = f"SIZE*{int(SectorWgt.iloc[0])}+VQMadj*{int(SectorWgt.iloc[1])}"
results = model.fit_constrained(constraint_str)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.