带约束的R线性模型 [英] R linear model with constraints

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

问题描述

我想拟合线性模型

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屋!

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