[R] integration function

Uwe Ligges ligges at statistik.tu-dortmund.de
Mon Mar 1 18:25:59 CET 2010

On 01.03.2010 17:09, li li wrote:
> Hi all,
>     I have some problem regarding the integration function. Can any one
> take a look at the following and give me some help? Thank you in advance!
>                Hannah
> ## f0 is a function of r,x, and y
> f0<- function(r, x, y){
>          pnorm((x-sqrt(r)*y)/sqrt(1-r), mean=0,sd=1, lower.tail=T)^2*dnorm(y)
> }
> ## f1 is the function of r and x after integrating out y
> f1<- function(r,x) {integrate(f1(r,x,y), -Inf, Inf)}
> I got the follow error message :
>> f1(0,0)
> Error: evaluation nested too deeply: infinite recursion /
> options(expressions=)?

Well, I get

Error in f1(r, x, y) : unused argument(s) (y)

hence I guess you made some copy&paste to mail message error as well.
Additionally, where your error comes from: you want t integarte f0 
rather than f1 *recursively*?

If this is homework, you should have read your error messages better and 
reported you exact code rather than something else, at least.

Note that integrate() expects as first argument "f" a function rather 
than a call (or evaluated function).

Hence you probably want (note order of arguments!):

f0 <- function(y, r, x){
     pnorm((x - sqrt(r)*y) / sqrt(1-r), mean=0, sd=1, lower.tail=TRUE)^2 
* dnorm(y)
f1 <- function(r,x) integrate(f0, -Inf, Inf, x=x, r=r)

Uwe Ligges

> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org 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.

More information about the R-help mailing list