R: glmnet: forcing the coefficient to a certain sign

Viewed 2837

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.

1 Answers
Related