0

I am using R to do some multivariate analysis. For this work I need to integrate the trivariate PDF. While doing this I got the error:

Error in integrate(Vectorize(function(x) { : 
  evaluation of function gave a result of wrong length

As you could see, vectorize is not the solution so something wrong is going on.

If somebody can give me a hint about what is going on, I'll really appreciate it.

Here is my code:

library(mvtnorm)
f1<-function(x, y, z) {dmvnorm(x=as.matrix(cbind(x,y,z)), mean=mu.t, sigma)} #PDF with all three
#integrate out x
integrate(Vectorize(function(x) {f1(x=c1, y=c2, z=c3)}), 0.1,0.5, rel.tol=1.5e-20)$value
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
user2282564
  • 5
  • 1
  • 5

1 Answers1

0

Can't tell for sure w/out more info on your inputs and outputs, but what's the length of the output of f1? If it's not the same length as your inputs, I believe that'll cause the error you're getting, as on integrate 's help page:

f   an R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error.
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • I think I figures what was wrong. f1 output is the lenght of the nrow of x (n) and I want a vector of that same length. I did this: for(i in 1:n) { f1=function(x, y, z) {dmvnorm(x=as.matrix(cbind(x,y,z)), mean=mu.t, sigma=sigma)} #Integrate out x integrate(Vectorize(function(x) {f1(x=c1[i], y=c2[i], z=c3[i])}), lower = -Inf, upper = -xb1[i], rel.tol=1.e-06)$value} } – user2282564 Apr 16 '13 at 22:12