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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With