带约束的R线性模型 [英] R linear model with constraints
问题描述
我想拟合线性模型
y ~ a_1 * x_1 + ... + a_n * x_n
具有参数约束
a_1,...,a_n >=0
和
a_1 + ... + a_n <= 1
在R.
是否有一种优雅而又快速的方法来执行此操作,而无需使用quadprog软件包的Solve.QP. 如果为建议的解决方案概述一个简短而详细的用例,那将是很棒的事情.
Is there an elegant and fast way to do that and without using solve.QP of the quadprog package. It would be wonderful if a short but detailed use case would be outlined for a proposed solution.
推荐答案
您可以将constrOptim
与成本函数最小二乘和约束定义为ui %*% a >= ci
一起使用.
You can use constrOptim
with cost function least square and contraints defined such that ui %*% a >= ci
.
假设n=3
.您需要以下约束:
Suppose n=3
. You want constraints such as:
a1 >= 0
a2 >= 0
a3 >= 0
-a1 -a2 -a3 >= -1
因此,您必须提供constrOptim
以下参数:
Thus you have to provide constrOptim
the following parameters:
ui = rbind(c(1,0,0),
c(0,1,0),
c(0,0,1),
c(-1,-1,-1))
ci = c(0,0,0,-1)
如果不使用渐变,也请在constrOptim
中明确设置grad=NULL
.
Set also explicitely grad=NULL
in constrOptim
if you do not use the gradient.
希望有帮助.
这篇关于带约束的R线性模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!