Python中具有多个约束的约束回归
我目前正在使用Python设置一个约束回归Python中具有多个约束的约束回归,python,constraints,regression,Python,Constraints,Regression,我目前正在使用Python设置一个约束回归 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
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.'''
来自StatsModel的函数,但在尝试使用多个约束设置它时遇到了一些问题。我有七个系数,包括一个常数。我想设置它,使虚拟1和虚拟2的加权和等于零,虚拟3和虚拟4的加权和等于零。要使用单个约束示例
results = model.fit_constrained(([0, 0, 0, a, b, 0, 0], 0))
其中a和b是假人3和假人4上的权重,是我预定义的变量
如果我没有a和b变量,并且假人的权重相等,我可以使用语法
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))
我得到了错误
ValueError: shapes (2,) and (7,6) not aligned: 2 (dim 0) != 7 (dim 0)
有人有什么想法吗?非常感谢 我仍然不确定您运行的是哪种型号(发布a肯定会有帮助),但以下内容应该适用于GLMs。从,我们有, 约束(公式表达式或元组)–如果是元组,则约束需要由两个数组(约束矩阵、约束值)给出,即(R,q)。否则,约束可以作为字符串或字符串列表给出。有关详细信息,请参见t_测试 这意味着函数调用应遵循以下原则:
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
,根据文档,它必须保持不变。这不在标准库中,所以您使用哪个软件包来适合您的模型?我想是statsmodels,但更明确一些会有帮助。抱歉,你是对的,这是statsmodel,我现在更新了问题以反映这一点。