我目前正在使用
在Python中设置约束回归。for i in range(2 ** 32):
byte3 = i & 0xff
byte2 = i & 0xff00
byte1 = i & 0xff0000
byte0 = i & 0xff000000
print('%d.%d.%d.%d' % (byte0 >> 24, byte1 >> 16, byte2 >> 8, byte3))
StatsModel的函数,但是在尝试使用多个约束进行设置时遇到了一些问题。我有七个系数,包括一个常数。我要进行设置,以使虚拟1和虚拟2的加权和等于零,并且虚拟3和虚拟4的加权和等于零。要使用单个约束示例,
import statsmodels.api as sm
model = sm.GLM(Y,X)
model.fit_constrained
'''Setting the restrictions on parameters in the form of (R, q), where R
and q are constraints' matrix and constraints' values, respectively. As
for the restriction in the aforementioned regression model, i.e.,
c = b - 1 or b - c = 1, R = [0, 1, -1] and q = 1.'''
其中a和b是虚拟3和虚拟4的权重,并且是我预先定义的变量。
如果我没有a和b变量,并且假人的权重相等,那么我可以使用语法
results = model.fit_constrained(([0, 0, 0, a, b, 0, 0], 0))
但是当我尝试使用类似的语法时
fit_constrained('Dummy1 + Dummy2, Dummy3 + Dummy4')
我得到了错误
results = model.fit_constrained(([0, 0, 0, a, b, 0, 0], 0),([0, c, d, 0, 0, 0, 0], 0))
有人有什么想法吗?非常感谢!
答案 0 :(得分:0)
我仍然不确定您正在运行哪种模型(发布Minimal, Complete, and Verifiable example肯定会有所帮助),但是以下方法应该适用于GLM。在docs中,我们有
约束(公式表达式或元组)–如果是元组,则约束必须由两个数组(constraint_matrix,constraint_value)给定,即(R ,q)。否则,约束可以给出为字符串或字符串列表。有关详细信息,请参见t_test。
这意味着函数调用应遵循以下几行
R = [[0, 0, 0, a, b, 0, 0],
[0, c, d, 0, 0, 0, 0]]
q = [0, 0]
results = model.fit_constrained((R, q))
这应该可以,但是由于我们没有您的模型,因此我不确定R * params = q
是否必须根据文档进行保存。