I want to fit a linear model
y ~ a_1 * x_1 + ... + a_n * x_n
with parameter constraints
a_1,...,a_n >=0
and
a_1 + ... + a_n <= 1
in R.
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.
You can use constrOptim
with cost function least square and contraints defined such that ui %*% a >= ci
.
Suppose n=3
. You want constraints such as:
a1 >= 0
a2 >= 0
a3 >= 0
-a1 -a2 -a3 >= -1
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)
Set also explicitely grad=NULL
in constrOptim
if you do not use the gradient.
Hope it helps.