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).

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 <ehl...@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 mailing list
>> 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>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to