Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: glmnet: forcing the coefficient to a certain sign

Tags:

r

glmnet

I have a very large matrix, so I am using glmnet for a regression. I have a condition that names with p must have a positive coefficient and names with n a negative coefficient.

How can I force this condition in glmnet? Below is a small example as an illustration:

library(glmnet)

y <- cumsum(sample(c(-1, 1),100, TRUE))
p1 <- cumsum(sample(c(-1, 1),100, TRUE))
p2 <- cumsum(sample(c(-1, 1),100, TRUE))
p3 <- cumsum(sample(c(-1, 1),100, TRUE))
n1 <- cumsum(sample(c(-1, 1),100, TRUE))
n2 <- cumsum(sample(c(-1, 1),100, TRUE))

df1  <- data.frame(y,p1,p2,p3,n1,n2)
df1




y <-  as.matrix(df1[,1])
x <-  as.matrix(df1[,-1])

fit1=glmnet(x,y)

coefall <- coef(fit1,s=0.005) 

Thank you for your help.

like image 856
adam.888 Avatar asked Mar 12 '23 23:03

adam.888


1 Answers

From ?glmnet:

Arguments:

...

lower.limits: Vector of lower limits for each coefficient; default ‘-Inf’. Each of these must be non-positive. Can be presented as a single value (which will then be replicated), else a vector of length ‘nvars’

upper.limits: Vector of upper limits for each coefficient; default ‘Inf’. See ‘lower.limits’

To constrain your parameters, you have to call:

fit1=glmnet(x, y, lower.limits=c(0,   0,   0,   -Inf, -Inf), 
                  upper.limits=c(Inf, Inf, Inf, 0,    0))
like image 75
sieste Avatar answered Mar 16 '23 03:03

sieste