[R] Constrained Optimization, a full example

Ravi Varadhan RVaradhan at jhmi.edu
Wed Jun 17 15:34:47 CEST 2009


Look at this:

http://finzi.psych.upenn.edu/Rhelp08/2009-May/198540.html 


Ravi.
----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml



----------------------------------------------------------------------------
--------


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Hesen Peng
Sent: Tuesday, June 16, 2009 11:51 PM
To: Stu @ AGS; R Help
Subject: Re: [R] Constrained Optimization, a full example

Hmmmm, I'm assuming that you are trying to find the solution of \beta to the
question:

argmin |y-X\beta|_2^2, which is a quadratic programming problem.

On Tue, Jun 16, 2009 at 6:29 PM, Hesen Peng<hesen.peng at gmail.com> wrote:
> 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.060810810
>> 8108108 ,0.0128205128205128,0.0136078431372549);
>>
>> x1 <-
>> c(0,0.978723404255319,0.087378640776699,0.549295774647887,0.059602649
>> 0066225
>> ,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/
>



--
??? Hesen Peng
http://hesen.peng.googlepages.com/

______________________________________________
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.




More information about the R-help mailing list