Thank you Andrew for the nice suggestion. I found out the mistake I made in the code. Here is the complete function with the problem of optimization.

trun.mean<- function(x)     
{

mu=x[1];
sigma=x[2];
thre=x[3];

f <- function(x) (1/(sigma*sqrt(2*pi)))*exp(-(x-mu)^2/(2*sigma^2));

pdf.fun <- function(x) x*f(x);

sd.fun <- function(x) x^2*f(x);  

a=integrate(pdf.fun,thre,upper=Inf)$value;

a1=integrate(f,thre,upper=Inf)$value;

st=integrate(sd.fun,thre,upper=Inf)$value;

mut <- a/a1;

sdt <- sqrt(st/a1-(a/a1)^2)

(mut-mu)^2+(sdt-sd)^2

}

I need to minimize trun.mean ( ), so I tried

> trun.mean(c(100,20,13))

[1] 399200454


> optim(c(200,100,112),trun.mean);

Error in integrate(f, thre, upper = Inf) : 
        roundoff error is detected in the extrapolation table


Thanks!

Jenny

-----Original Message-----
From:Andrew Robinson ,   A.Robinson@ms.unimelb.edu.au
Sent: 2006-11-09,  15:14:58
To: Jenny Stadt
CC:r-help@stat.math.ethz.ch
Subject: Re: [R] optimize function with integral form ?
Hi Jenny,

whenever a function that calls a function complains about the function
it is calling, it helps to try to run that function yourself.  So, I
declared your function trun.mean as below and tried to call it:

> trun.mean(c(200,100,113))
Error in integrate(f, thre, upper = Inf) : 
        object "thre" not found

This might help you identify the current, and possibly future, errors.

Cheers

Andrew


On Thu, Nov 09, 2006 at 02:32:18PM -0700, Jenny Stadt wrote:
> Hi all,
> 
> Does anybody have the experience of using optim to estimate variables with integral forms?
> 
> here the code:
> 
> 
> trun.mean <- function(x)      # t is the threshold
> {
> 
> mu=x[1];
> 
> sigma=x[2];
> 
> t=x[3];
> 
> f  <- function(x) (1/(sigma*sqrt(2*pi)))*exp(-(x-mu)^2/(2*sigma^2));
> 
> pdf.fun  <- function(x) x*f(x);
> 
> integrate(f,thre,upper=Inf)$value/integrate(pdf.fun,thre,upper=Inf)$value ;  
> }
> 
> when I run :  
> 
>  > optim(c(200,100,113),trun.mean)
> 
> Error in optim(c(2, 3, 3), trun.mean) : function cannot be evaluated at initial parameters
> 
> 
> 
> I need your help, thanks!
> 
> Jen.
> 
>  [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@stat.math.ethz.ch 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.

-- 
Andrew Robinson  
Department of Mathematics and Statistics            Tel: +61-3-8344-9763
University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
http://www.ms.unimelb.edu.au/~andrewpr
http://blogs.mbs.edu/fishing-in-the-bay/ 

	[[alternative HTML version deleted]]

