Python 中的约束回归 [英] Constrained regression in Python

查看:41
本文介绍了Python 中的约束回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的回归模型:

I have this simple regression model:

 y = a + b * x + c * z + error

对参数有限制:

c = b - 1

SO 上发布了类似的问题(例如 Constrained LinearPython 中的回归).但是,约束的类型是 lb <= parameter =<;ub.

There are similar questions posted on SO (like Constrained Linear Regression in Python). However, the constraints' type is lb <= parameter =< ub.

处理这个特定约束线性回归问题的可用选项有哪些?

What are the available options to handle this specific constrained linear regression problem?

推荐答案

这是如何使用 GLM 完成的:

This is how it can be done using GLM:

import statsmodels
import statsmodels.api as sm
import numpy as np

# Set the link function to identity
statsmodels.genmod.families.links.identity()

OLS_from_GLM = sm.GLM(y, sm.add_constant(np.column_stack(x, z)))

 '''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.'''

res_OLS_from_GLM = OLS_from_GLM.fit_constrained(([0, 1.0, -1.0], 1))

print(res_OLS_from_GLM.summary())

这篇关于Python 中的约束回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆