Skip to content
Prev 244061 / 398503 Next

Optimize multiple variable sets

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) 

c<-dim(data)[2]

results<-vector(length=c)

for (i in 1:c){

f<-function(x){

x^2+x*sum(data[,i])

}

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

} #

results

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

Hope this helps,
Ravi. 

-------------------------------------------------------
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:
the
is
as
______________________________________________
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.