[R] Evaluation error

li li
Sun, 06 Dec 2009 19:13:46 -0000

Hello,
  I want to write a function calculating posterior probability.
  Can anyone help have a look at the following program? Here function f is
what I eventually
need. It is related to function f1, f2 and f3. The argument x is a vector.
I get evaluation errors here.
  Thank you very much!


> f1 <- function(p, x, i)
+   {
+     b <- p*dnorm(x[i], 2,1)
+     a <- (1-p)*dnorm(x[i], 0,1)
+     r <- a/(a +b)
+     return(r)
+    }
>
> f2 <- function (p,x)
+   {
+    a <- p*dnorm(x, 2,1)
+    b <- (1-p)*dnorm(x,0,1)
+    prod(a+b)
+   }
>
> f3 <- function(x,alpha, beta)
+     {
+       integrand <- function(p,x,alpha, beta)
+           {
+            f2(p,x)*dbeta(p, alpha, beta)
+          }
+       integrate(Vectorize(integrand, Vectorize.args =c("p")),  0,1,
x,
alpha, beta)$val
+      }
>
> f <- function(x, i, alpha, beta)
+    {
+    integrand <-function(p,x,i, alpha, beta)
+      {
+        f1(p, x, i)*f2(p,x)*dbeta(p, alpha, beta)
+       }
+
+    integrate(Vectorize(integrand,vectorize.args =c("p")),  0, 1,x,i,
alpha, beta)$val/f3(x, alpha, beta)
+      }
>
> w<- seq(-2,2,by=0.1)
> prob <- numeric(length(w))
> for (i in 1:length(w)) { prob[i] <- f(w,i,2,3)}
Error in eval(expr, envir, enclos) :
  ..1 used in an incorrect context, no ... to look in

	[[alternative HTML version deleted]]