R: glmnet: forcing the coefficient to a certain sign

大城市里の小女人 提交于 2020-01-03 16:58:08

问题


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:


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))


来源:https://stackoverflow.com/questions/35927521/r-glmnet-forcing-the-coefficient-to-a-certain-sign

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!