Peter, Thank you very much!

2011/6/26 Peter Ehlers <ehlers@ucalgary.ca>

> On 2011-06-26 06:34, li li wrote:
>
>> Thank you all for the answering.
>> Sorry I did not state the problem clearly. I want to take the
>> integration with respect to mu, not x.
>> For example, f1 should have been a function of x after integrating mu
>> out of f(x, mu) which is the following:
>> f(x, mu)=dnorm(x, mean=mu, sd=1)*dnorm(mu, mean=2, sd=1).
>>
>
> So you probably want something like:
>
>  h <- function(mu, x) dnorm(x, mu, 1) * dnorm(mu, 2, 1)
>  f1 <- function(x)
>    integrate(function(mu, x) h(mu, x), -Inf, Inf, x)$value
>
> In your original formulation (below), all you have
> to do is change
>
>   {integrand <- function (x, mu){
>
> to
>
>   {integrand <- function (mu, x){
>
>
> Peter Ehlers
>
>
>> Thank you!
>>       Hannah
>>
>>
>> f1<- function(x)
>>
>>     {integrand <- function (x, mu){
>>
>> dnorm(x, mean=mu, sd=1)*dnorm(mu, mean=2, sd=1)
>>
>>                                    }
>>
>> integrate(integrand, -Inf, Inf,x)$val
>>
>> }
>>
>>
>> f2<- function(x)
>>
>>     {integrand <- function (x, mu){
>>
>> dnorm(x, mean=mu, sd=1)*mu^2*dnorm(mu, mean=2, sd=1)
>>
>>                                    }
>>
>> integrate(integrand, -Inf, Inf,x)$val
>>
>> }
>>
>>
>>
>> 2011/6/25 Peter Ehlers <ehlers@ucalgary.ca <mailto:ehlers@ucalgary.ca>>
>>
>>
>>    On 2011-06-25 08:48, li li wrote:
>>
>>        Hi all,
>>            Can anyone please take a look at the following two functions.
>>        The answer does not seem to be right.
>>            Thank you very much!
>>
>>        f1<- function(x)
>>
>>             {integrand<- function (x, mu){
>>
>>             dnorm(x, mean=mu, sd=1)*dnorm(mu, mean=2, sd=1)
>>
>>                                            }
>>
>>             integrate(integrand, -Inf, Inf,x)$val
>>
>>             }
>>
>>
>>        f2<- function(x)
>>
>>             {integrand<- function (x, mu){
>>
>>             dnorm(x, mean=mu, sd=1)*mu^2*dnorm(mu, mean=2, sd=1)
>>
>>                                            }
>>
>>             integrate(integrand, -Inf, Inf,x)$val
>>
>>             }
>>
>>
>>    Your x and mu will get mightily confused.
>>    The argument x in f1 is in fact used as the argument mu
>>    in integrand() because, as the help page clearly indicates,
>>    additional arguments follow the lower/upper limits in integrate().
>>
>>    A cleaner version of what you're doing is the following:
>>
>>      f1 <- function(mu){
>>
>>       integrand <- function (x, mu){
>>         dnorm(x, mean=mu, sd=1) * dnorm(mu, mean=2, sd=1)
>>       }
>>       integrate(integrand, -Inf, Inf, mu)[["value"]]
>>      }
>>
>>    But then again, you could just evaluate dnorm(mu, 2, 1).
>>    So I suspect that you want something different.
>>
>>    Ditto for f2.
>>
>>    Peter Ehlers
>>
>>
>>
>>                [[alternative HTML version deleted]]
>>
>>        ______________________________**__________________
>>        R-help@r-project.org <mailto:R-help@r-project.org> mailing list
>>
>>        https://stat.ethz.ch/mailman/_**_listinfo/r-help<https://stat.ethz.ch/mailman/__listinfo/r-help>
>>        <https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> >
>>        PLEASE do read the posting guide
>>        http://www.R-project.org/__**posting-guide.html<http://www.R-project.org/__posting-guide.html>
>>        <http://www.R-project.org/**posting-guide.html<http://www.R-project.org/posting-guide.html>
>> >
>>        and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>>
>

	[[alternative HTML version deleted]]

