[R] lsqlin in R package pracma

Wang, Xue, Ph.D. Wang.Xue at mayo.edu
Thu Aug 27 23:12:29 CEST 2015


Hi Richard,

It is good to know that solve.QP could solve quadratic programming problem. The difficulty here is that the objective function might not be in quadratic form. It is not in the form of t(X)QX, where Q is an n by n symmetric matrix.

Thanks,

Xue 

 

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Raubertas, Richard
Sent: Thursday, August 27, 2015 12:07 PM
To: Hans W Borchers; r-help at stat.math.ethz.ch
Subject: Re: [R] lsqlin in R package pracma

Is it really that complicated?  This looks like an ordinary quadratic programming problem, and 'solve.QP' from the 'quadprog' package seems to solve it without user-specified starting values:

library(quadprog)
Dmat <- t(C) %*% C
dvec <- (t(C) %*% d)
Amat <- -1 * t(A)
bvec <- -1 * b

rslt <- solve.QP(Dmat, dvec, Amat, bvec) sum((C %*% rslt$solution - d)^2)

[1] 0.01758538

Richard Raubertas
Merck & Co.

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Hans W Borchers
Sent: Wednesday, August 26, 2015 6:22 AM
To: r-help at stat.math.ethz.ch
Subject: Re: [R] lsqlin in R package pracma

On Mon Aug 24 Wang, Xue, Ph.D. Wang.Xue at mayo.edu wrote
> I am looking for a R version of Matlab function lsqlin. I came across 
> R pracma package which has a lsqlin function. Compared with Matlab 
> lsqlin, the R version does not allow inequality constraints.
> I am wondering if this functionality will be available in future. And 
> also like to get your opinion on which R package/function is the best 
> for
solving
> least square minimization problem with linear inequality constraints.
> Thanks very much for your time and attention!


Solving (linear) least-squares problems with linear inequality constraints is more difficult then one would expect. Inspecting the MATLAB code reveals that it employs advanced methods such as active-set (linear inequality
constraints) and interior-point (for bounds constraints).

Function nlsLM() in package *minpack.lm* supports bound constraints if that is sufficient for you. The same is true for *nlmrt*. Convex optimization might be a promising approach for linear inequality constraints, but there is no easy-to-handle convex solver in R at this moment.

So the most straightforward way would be to use constrOptim(), that is optim with linear constraints. It requires a reasonable starting point, and keeping your fingers crossed that you are able to find such a point in the interior of the feasible region.

I someone wants to try: Here is the example from the MATLAB "lsqlin" page:

    C <- matrix(c(
        0.9501,   0.7620,   0.6153,   0.4057,
        0.2311,   0.4564,   0.7919,   0.9354,
        0.6068,   0.0185,   0.9218,   0.9169,
        0.4859,   0.8214,   0.7382,   0.4102,
        0.8912,   0.4447,   0.1762,   0.8936), 5, 4, byrow=TRUE)
    d <- c(0.0578, 0.3528, 0.8131, 0.0098, 0.1388)
    A <- matrix(c(
        0.2027,   0.2721,   0.7467,   0.4659,
        0.1987,   0.1988,   0.4450,   0.4186,
        0.6037,   0.0152,   0.9318,   0.8462), 3, 4, byrow=TRUE)
    b <- c(0.5251, 0.2026, 0.6721)

The least-square function to be minimized is  ||C x - d||_2 , and the constraints are  A x <= b :

    f <- function(x) sum((C %*% x - d)^2)

The solution x0 returned by MATLAB has a minimum of  f(x0) = 0.01759204 .
This point does not lie in the interior and cannot be used for a start.

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Notice:  This e-mail message, together with any attachme...{{dropped:11}}

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list