[R] regression constraints (again)
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Aug 24 16:03:59 CEST 2003
It's easier to use crossprod, and you appear to have transposed A.
Here's a small example
library(quadprog) # which you never mentioned!
X <- matrix(rnorm(100),, 4)
Y <- 0.25* X %*% rep(1, 4) + 0.1*rnorm(25)
D <- crossprod(X)
d <- crossprod(X, Y)
A <- cbind(1, diag(ncol(X)))
b <- c(1, rep(0,ncol(X)))
solve.QP(D, d, A, b, meq=1)
which works for me.
On Sun, 24 Aug 2003, Brett Robinson wrote:
> Im trying to do regressions with constraints that the weights
> are all >=0 and sum(weights) = 1. I've read the archive and have
weights? I assume you mean coefficients?
> set the problem up with solve.QP and just the non-negativity constraints
>
> along the lines of:
>
> y as the data vector
> X as the design matrix
>
> D <- t(X) %*% X
> d <- t(t(y) %*% X)
> A <- diag(ncol(X))
> b <- rep(0,ncol(X))
> fit <- solve.QP(D=D,d=d,A=t(A),b=b,meq=0)
>
> (as per Gardar Johannesson '01)
>
> When I try to add the extra constraint that sum(weights)=1 I get errors
> owing to incompatibility of matrices. I add the constraint by putting an
>
> extra column of all ones to A and setting meq=1.
>
> I can work round it I think, by using an intercept and using the extra
> column on the matrix for the sum(weights) constraint but I think that it
> should be possible without doing this.
> Grateful for any pointers as to where I am going wrong.
Please only send a message once, and do give the full details of what you
did so the helpers can spot your errors. Also, don't appreviate argument
names: at best you confuse your readers, and you are indanger of confusing
yourself.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list