R:glmnet:强制系数为某个符号

R: glmnet: forcing the coefficient to a certain sign

我有一个非常大的矩阵,所以我使用 glmnet 进行回归。 我有一个条件,带p的名字必须有正系数,带n的名字必须有负系数。

如何在 glmnet 中强制执行此条件? 下面是一个小例子作为说明:

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) 

感谢您的帮助。

来自?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’

要限制您的参数,您必须调用:

fit1=glmnet(x, y, lower.limits=c(0,   0,   0,   -Inf, -Inf), 
                  upper.limits=c(Inf, Inf, Inf, 0,    0))