[R] Minimization problem

Ravi Varadhan rvaradhan at jhmi.edu
Thu May 20 15:34:13 CEST 2010

That would not work in "optim".  The objective function must take in a
vector of parameters and return a scalar value.  Your objective function
does not return a scalar.  

This would work:

test <- function(x, ...){
m.error <- mean(distribution(x)) - observed.mean
sd.error <- std(distribution(x)) - observed.std
res <- c(m.error, sd.error)
return(sum(res^2))  # returns a scalar


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Bernardo Rangel Tura
Sent: Thursday, May 20, 2010 6:02 AM
To: r-help at r-project.org
Subject: Re: [R] Minimization problem

On Thu, 2010-05-20 at 01:35 -0700, Fred wrote:
> Dear R users,
> I am trying to minimize two function simultaneously in R,
> function(x)
> minimize x[1],x[2],x[3]
> mean(distribution(x1,x2,x3) ) - observed mean
> std(distribution(x1,x2,x3)) - observed std
> What I want to achieve is that simulated mean and standard deviation
> of distribution related to x1 x2 x3  would be close to observed mean
> and observed standard deviation.
> is there any function in R can reach this?
> Thank you for the help first .
> F.

Do you need use optim, something like this

test <- function(parameters){
m.error <- mean(distribution(x1,x2,x3) ) - observed mean
m.sd <- std(distribution(x1,x2,x3)) - observed std
res <- cbind(m.error,sd.error)
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology

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