s-news
[Top] [All Lists]

Re: Integration in a function

To: "Zhan, Ping N." <Ping.Zhan@celera.com>
Subject: Re: Integration in a function
From: Ping Zhang <pingzhang@avaya.com>
Date: 28 Apr 2004 15:06:39 -0400
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <B97FA25EDA418049A146320ADFE65506022CCC90@celmrkv2.rkv.ad.celera.com>
Organization:
References: <B97FA25EDA418049A146320ADFE65506022CCC90@celmrkv2.rkv.ad.celera.com>
The problem appears to be that function calls failed to pass
objects. I am not sure if there is a better solution, but here
is a quick one. Basically if you make all variables global with where=0,
then you can access them anywhere in the program.




fx<-function(a,b,A,B)
{
assign("a",a,where=0)
assign("b",b,where=0)
assign("A",A,where=0)
assign("B",B,where=0)
gx<-function(x,...)
{
x^3*(1-x)^3*x^a*(1-x)^b/((1+(A/B-1)*x)^(a+b))
}
L<-(a/A)/(a/A+b/B)
numer<-integrate(gx,L,1)$integral
denom<-integrate(gx,0,1)$integral
return(c(numer/denom))
}



On Wed, 2004-04-28 at 14:41, Zhan, Ping N. wrote:
> Hi All,
> 
> I have the following function defined with parameters a,b,A,B
> 
> fx<-function(a,b,A,B)
> {
>       gx<-function(x,...)
>       {
>               x^3*(1-x)^3*x^a*(1-x)^b/((1+(A/B-1)*x)^(a+b))
>       }
>       L<-(a/A)/(a/A+b/B)
>       numer<-integrate(gx,L,1)$integral
>       denom<-integrate(gx,0,1)$integral
>       return(c(numer/denom))
> }
> 
> However, when I tried to run the line below, I got an error message.
> 
> fx(a=120,b=120,A=10000,B=10000)
> 
> Problem in f(c(0.75, 0.502136157219797, 0.997863842780203,..: Object "a"
> not found 
> Use traceback() to see the call stack
> 
> It looks like that values of a,b,A and B didn't pass on. Can anyone tell
> me how to fix it? Many thanks. I am using windows version splus 6.1.
> 
> Thanks,
> Ping
> 
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
-- 
Ping Zhang | Research Scientist | Data Analysis Research Department | Avaya 
Labs | 233 Mt. Airy Rd., Basking Ridge, NJ 07920 | Room 2D14 | (908)696-5138


<Prev in Thread] Current Thread [Next in Thread>