[R] integrate a function in R
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Oct 28 10:06:29 CEST 2004
Where is the lexical scoping in this solution? Your one function is
defined in the global environment, and `u' just happens to be too (unlike
C and another implementation of the S language).
Even if it were relevant, it seems to obscure what is going on. Compare
zfz <- function(z, mu) z*dnorm(x=z, mean=mu)
for(i in seq(along=us)) {
u <- us[i]
out[i] <- integrate(zfz, 0, u+3, mu=u)$value
}
which is easier to follow and is less error-prone (even if you need to
ensure that `u' does not match `upper'). When I use lexical scoping to
capture variables I normally add a note in the code to remind readers.
Otherwise someone will streamline the code to
for(i in seq(along=us)) out[i] <- integrate(zfz, 0, us[i]+3)$value
On Thu, 28 Oct 2004, Dimitris Rizopoulos wrote:
> 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
> >
>
> ______________________________________________
> 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
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list