I would like to know how to constrain certain parameters in lm()
to have positive coefficients. There are a few packages or functions (e.g. display
) that can make all coefficients, and the intercept, positive.
For instance, in this example, I would like to force only x1
and x2
to have positive coefficients.
x1=c(NA,rnorm(99)*10)
x2=c(NA,NA,rnorm(98)*10)
x3=rnorm(100)*10
y=sin(x1)+cos(x2)-x3+rnorm(100)
lm(y~x1+x2+x3)
Call:
lm(formula = y ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
-0.06278 0.02261 -0.02233 -0.99626
I have tried function nnnpls()
in package nnls
, it can control the coefficient sign easily. Unfortunately I can't use it due to issues with NA
s in the data as this function doesn't allow NA
.
I saw function glmc()
can be used to apply constraints but I couldn't get it working.
Could someone let me know what should I do?