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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
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.