Here is how you can do it:
f1 <- function (p,x) { sapply(p, function(p) prod( p*x+(1-p)*x^2) ) # the function should be vectorized for `integrate' } f2 <- function(x) { integrate(f1, 0, 1, x)$val # you need to only return the `value' of integration } f2(x=1:3) Hope this helps, Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu ----- Original Message ----- From: li li <hannah....@gmail.com> Date: Saturday, December 5, 2009 7:13 pm Subject: Re: [R] integrate function To: David Winsemius <dwinsem...@comcast.net> Cc: r-help@r-project.org > Thank you very much for your reply! > It is not an assignment. > What I want to do is what we often do. If we have a bivariate > function of x > and p, > I first fix x and integrate out p. Then the resultant function is > only in > terms of x. > > > Here f1 is a bivariate function of x and p. > > f1 <- function (p,x) > + { > + prod(p*x+(1-p)*x^{2}) > } > > f2 is a function of x only after integrating out p from f1. Here f1 > is the > integrand, x is the additional argument to be > passed to f1. The integration is in terms of p. > > > f2 <- function(x) > + { > + integrate(f1, lower=0,upper=1, x) > + } > There is error output when I evaluate f2 at specific x values. See below: > > > f2(c(2,3)) > Error in integrate(f1, lower = 0, upper = 1, x) : > evaluation of function gave a result of wrong length > In addition: Warning messages: > 1: In p * x : > longer object length is not a multiple of shorter object length > 2: In (1 - p) * x^{ : > longer object length is not a multiple of shorter object length > > > Can any one give me some suggestions! Thank you! > 2009/12/5 David Winsemius <dwinsem...@comcast.net> > > > > > On Dec 5, 2009, at 4:59 PM, li li wrote: > > > > Hello, > >> I have some trouble in terms of using integrate function in R. > >> > >> f1 is a function of p and x where x is supposed to be a vector. > (See the > >> code). > >> Then I want to write function f2 which is a function of the vector > >> x after I integrate out p. > >> Can some one give me some help? Many thanks! > >> Hannah > >> > >> f1 <- function (p,x) > >>> > >> + { > >> + y <- p*x+(1-p)*x^{2} > >> + prod(y) > >> + } > >> > >>> > >>> f2 <- function(x) > >>> > >> + { > >> + integrate(f1, 0,1, x) > >> > > > > Just looking at the help page for integrate makes me wonder what > you are > > doing. There are 3 named arguments before the dots, so 0 and 1 will > become > > "lower" and "upper", while x is not among the named arguemnts, so I > suppose > > x will go to f1 as "p". But f1 takes two arguments, so where will > it get the > > second argument? Maybe if you would tell us a bit more about why > you made > > those functions the way you did, it would help. I am guessing it's > classwork > > but perhaps not. > > > > > > + } > >> > >> f2(x=c(1,2)) > >>> > >> Error in integrate(f1, 0, 1, x) : > >> evaluation of function gave a result of wrong length > >> In addition: Warning messages: > >> 1: In p * x : > >> longer object length is not a multiple of shorter object length > >> 2: In (1 - p) * x^{ : > >> longer object length is not a multiple of shorter object length > >> > >> [[alternative HTML version deleted]] > >> > > > > -- > > > > David Winsemius, MD > > Heritage Laboratories > > West Hartford, CT > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@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.