[英]quadratic programming in R with more parameters (constraints) than observations
I want to run a constrained linear regression such that the coefficients are nonnegative and sum to one.我想运行一个约束线性回归,使得系数为非负且总和为 1。 Usually this can be done with quadratic programming, However, I have more parameters (constraints) than observations (p > n).
通常这可以通过二次规划来完成,但是,我有比观察更多的参数(约束)(p > n)。 How can I do this?
我怎样才能做到这一点?
This quadprog-solution only works for problems with n > p:这个 quadprog-solution 仅适用于 n > p 的问题:
library(quadprog);
N <- 20
P <- 40
X <- matrix(runif(N*P), ncol=P)
btrue <- c(1,1,rep(0,(P-2)))
Y <- X %*% btrue + rnorm(N, sd=0.2)
C <- cbind(rep(1,P), diag(P))
b <- c(1,rep(0,P))
solve.QP(Dmat = t(X)%*%X, dvec = t(Y)%*%X , Amat = C, bvec = b, meq = 1)
You can do this using the following model:您可以使用以下模型执行此操作:
min r'r
r = X'b - y
b >= 0
sum(b) = 1
r free
where b are the parameters to estimate and r are the residuals.其中 b 是要估计的参数,r 是残差。 Both b and r are decision variables.
b 和 r 都是决策变量。 This problem is always convex (ie Q=I is always positive definite) even if n < p.
即使 n < p,这个问题也总是凸的(即 Q=I 总是正定的)。
However, Quadprog may still not like it (I think it wants a strictly pos def D matrix, unlike most QP solvers).然而,Quadprog 可能仍然不喜欢它(我认为它需要一个严格的 pos def D 矩阵,与大多数 QP 求解器不同)。 Fix this by changing D to:
通过将 D 更改为:
D = [ 0.0001*I 0 ]
[ 0 I ]
The R code can look like: R 代码可能如下所示:
#
#
# b = [b] (P)
# [r] (N)
#
# D = [ 0 0 ]
# [ 0 I ]
#
# d = [0]
# [0]
#
# A' = [ 1' 0' ]
# [ I 0 ]
#
# b0 = [1]
# [0]
#
# fudge left upper sub matrix
D = rbind( cbind( 0.0001*diag(P), matrix(rep(0,P*N),nrow=P,ncol=N)),
cbind( matrix(rep(0,N*P),nrow=N,ncol=P), diag(N) )
)
d = rep(0, P+N)
A = rbind( cbind( matrix(rep(1,P),nrow=1), matrix(rep(0,N),nrow=1)),
cbind(diag(P),matrix(rep(0,P*N),nrow=P,ncol=N)))
b0 = rbind( matrix(c(1),nrow=1,ncol=1), matrix(rep(0,P),nrow=P,ncol=1))
solve.QP(Dmat = D, dvec = d , Amat = t(A), bvec = b0, meq = 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.