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 <dwinsemius@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]]


