Hi:
I want to integrate the following function with
respect to "z", "u" is another argument in the
function. My program is a loop and in each loop the
argument "u" will be given a specified numeric values.
But how can I use the "integrate" function with "u" in
R?
The function is:
zfz<-function(z,u)
{
return(z*dnorm(z,u,1) )
}
And in each loop, the integration interval is [0,u+3].
I can not just use integrate(zfz,0,u+3) because "u"
can not be specified.
Thank you!
liu
__________________________________________________
Download the latest ringtones, games, and more!
Hi Liu,
you should take advantage of lexical scoping, i.e.,
zfz <- function(z) z*dnorm(x=z, mean=u)
us <- seq(-1, 1, length=10)
out <- numeric(length(us))
for(i in seq(along=us)){
u <- us[i]
out[i] <- integrate(zfz, 0, u+3)$value
}
out
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "yyan liu" <zhliur at yahoo.com>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, October 28, 2004 12:56 AM
Subject: [R] integrate a function in R
> Hi:
> I want to integrate the following function with
> respect to "z", "u" is another argument in the
> function. My program is a loop and in each loop the
> argument "u" will be given a specified numeric values.
> But how can I use the "integrate" function with "u" in
> R?
> The function is:
> zfz<-function(z,u)
> {
> return(z*dnorm(z,u,1) )
> }
>
> And in each loop, the integration interval is [0,u+3].
> I can not just use integrate(zfz,0,u+3) because "u"
> can not be specified.
> Thank you!
>
> liu
>
> __________________________________________________
>
> Download the latest ringtones, games, and more!
>
> ______________________________________________
> 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
>
On 27-Oct-04 yyan liu wrote:> Hi: > I want to integrate the following function with > respect to "z", "u" is another argument in the > function. My program is a loop and in each loop the > argument "u" will be given a specified numeric values. > But how can I use the "integrate" function with "u" in > R? > The function is: > zfz<-function(z,u) > { > return(z*dnorm(z,u,1) ) > } > > And in each loop, the integration interval is [0,u+3]. > I can not just use integrate(zfz,0,u+3) because "u" > can not be specified. > Thank you!If you are only interested in this specific function, then there is an analytical solution and you do not need numerical integration. Namely (if I have understood your statement correctly) let (0,Z) be the limits of integration (in your case Z = u+3); then the integral is dnorm(u,0,1) - dnorm(Z-u,0,1) + u*pnorm(Z-u,0,1) - pnorm(-u,0,1) which, in your case, is dnorm(u) - dnorm(3) + u*pnorm(3) - pnorm(-u) (using the default values mu=0 and s=1 for dnorm and pnorm). Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 28-Oct-04 Time: 08:45:09 ------------------------------ XFMail ------------------------------