[R] Optimize multiple variable sets

Ravi Varadhan rvaradhan at jhmi.edu
Mon Dec 6 18:42:44 CET 2010

Let us look at the objective function:

f(x) = x^2 + Si * x, where Si is the sum of the i-th column.  This function
has a stationary point at x = -Si/2, and the second derivative is 2, so it
is a minimum.

Now, the column sums of your data matrix are all positive.  So, your minimum
has to be negative, but your interval does not contain that.  So, your
results are wrong.  

Here is the correct approach:

data<-matrix(c(1,1,1, 2,2,2, 3,3,3, 4,4,4), nrow=3, ncol=4) 



for (i in 1:c){




results[i]<-optimize(f,int=c(-10,10), tol=1.e-07)$min 

} #


all.equal(results, -colSums(data)/2)

Hope this helps,

Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology School of Medicine Johns
Hopkins University

Ph. (410) 502-2619
email: rvaradhan at jhmi.edu

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Jonathan P Daily
Sent: Monday, December 06, 2010 9:57 AM
To: peter dalgaard
Cc: r-help at r-project.org; r-help-bounces at r-project.org; sandra lag
Subject: Re: [R] Optimize multiple variable sets

I suppose I should have been more clear. I saw that her interval did not 
include the actual minimum, but I was asking if (and if, why) she was 
expecting the minimum x value to be different for each run. If the y value 
were returned the same on each run that would be puzzling.

As for the returned x issue, you are correct that it is a 'tol' issue: 
reducing tol to something reasonably low approximates the min fairly well.
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

peter dalgaard <pdalgd at gmail.com> wrote on 12/06/2010 09:39:43 AM:

> [image removed] 
> Re: [R] Optimize multiple variable sets
> peter dalgaard 
> to:
> Jonathan P Daily
> 12/06/2010 09:39 AM
> Cc:
> sandra lag, r-help, r-help-bounces
> On Dec 6, 2010, at 15:15 , Jonathan P Daily wrote:
> > Correct me if I'm wrong, but isn't the minimal x value in your example 
> > same regardless of what positive coefficient you apply to x? If that 
> > the case, you would expect the same min(x) for each iteration.
> > 
> > i.e. in the interval [0,1] the minimum x value of x^2 + x is the same 
> > x^2 + 100000000*x, at x = 0.
> You're wrong --- slightly. The returned $minimum is the x, the y is 
> $objective. But the interval given doesn't bracket the minimum, as 
> you'll clearly see if you put int=c(-10,10). The only puzzling bit 
> is that optimize() doesn't actually return the left endpoint, but 
> rather the first evaluation point inside the interval. The rather 
> wide tolerance of .Machine$double.eps^0.25  == 0.0001220703 probably
> plays a role in this.
> -- 
> Peter Dalgaard
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

R-help at r-project.org mailing list
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