[R] Constrained Optimization, a full example

Hesen Peng hesen.peng at gmail.com
Wed Jun 17 00:29:35 CEST 2009


Hello,

Since the optimization goal is quadratic and constraint is linear,
quadratic programming functions will help. There are many such
packages available, e.g. quadprog.

On Tue, Jun 16, 2009 at 5:54 PM, Stu @ AGS<stu at agstechnet.com> wrote:
> After a few days of work, I think I nearly have it.
>
> Unfortunately, theta is unchanged after I run this (as a script from a
> file).  I thought that theta would contain the fitted parameters.
>
>
>
> The goal here is to find the least squares fit according to the function
> defined as "rss" subject to the constraints defined as ui and ci.
>
> I defined ui and ci to (hopefully) force par2 and par3 into the range  0.0
> <= par2 <= 1.0 and 0.0 <= par3 <= 1.0.
>
> I am not at all sure that ui and ci are defined correctly.
>
>
>
> The call to constrOptim returns normally, but without a solution
> (apparently).
>
>
>
> Any suggestions appreciated.
>
> Thanks
>
> Stu
>
>
>
> # data
>
> y <-
> c(0.111111111111111,0.0921052631578947,0.0564516129032258,0.0608108108108108
> ,0.0128205128205128,0.0136078431372549);
>
> x1 <-
> c(0,0.978723404255319,0.087378640776699,0.549295774647887,0.0596026490066225
> ,0.61578947368421);
>
> x2 <- c(1,3,4,5,6,7);
>
> x3 <- c(3600,169200,185400,255600,271800,342000);
>
>
>
> observs <- data.frame(y, x1, x2, x3);
>
>
>
> # function definition
>
> rss <- function(par, y, x1, x2, x3) {
>
>  par1 <- par[1]
>
>  par2 <- par[2]
>
>  par3 <- par[3]
>
>  ressq <- (y - par1 * (x1 + 1) * x2^(-par2) * x3^par3)^2
>
>  sum(ressq)
>
>  }
>
>
>
> #call to optimizer
>
> opti <- constrOptim(c(0.5, 0.5, 0.1), rss, NULL, ui = rbind(c(0, 0), c(1,
> 0), c(0, 1)), ci = c(0, 1, 1));
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
彭河森 Hesen Peng
http://hesen.peng.googlepages.com/




More information about the R-help mailing list