[R] integrate function
David Winsemius
dwinsemius at comcast.net
Sat Dec 5 23:27:08 CET 2009
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
More information about the R-help
mailing list