[R] optimize function with integral form ?

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Thu Nov 9 23:13:58 CET 2006


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 at 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/



More information about the R-help mailing list