[R] Error in integrate(int.fn, lower = 0, upper = Inf) : evaluation of function gave a result of wrong length
Berend Hasselman
bhh at xs4all.nl
Mon Apr 9 15:34:39 CEST 2012
On 09-04-2012, at 14:54, Guaramy wrote:
> I read it believe me . The reason that a i post this is because i am making a
> thesis and a i am having this problem for over 2 weeks.
> I can´t solve it and its causing me real problems.
You didn't give us a reproducible example.
I'm assuming your original function is
GNL.pdf.fn <- function(x,mu,sigma,alpha,beta,rho) # missing in your original post
{
y <- x-rho*mu
cf.fn <- function(s){
cplex = complex(1,0,1)
temp1 = alpha*beta*exp(-sigma*s^2/2)
temp2 = (alpha-cplex*s)*(beta+cplex*s)
out = (temp1/temp2)^rho
out
}
temp.fn <- function(s){ Mod(cf.fn(s))*cos(Arg(cf.fn(s))-s*y) }
int.fn <- function(t){sapply(t,FUN=temp.fn)}
te <- integrate(int.fn,lower=0,upper=Inf,rel.tol=1e-10,subdivisions=1000000)
temp3 <- ifelse(te$message == "OK",te$value/pi,NA)
temp3
}
Question: why are you using ifelse when te$message is a scalar?
if( te$message == "OK" ) te$value/pi else NA
would do what you want.
David meant that you should have a look at Vectorize. And then do some experimenting.
So I tried this
GNL.pdf.fn(1, .2, .3, 1, 1, .6)
GNL.vec <- Vectorize(GNL.pdf.fn, "x")
GNL.vec(c(.9,1,1.1), .2, .3, 1, 1, .6)
And now it's your turn for seq(-4,4,0.1).
Berend
More information about the R-help
mailing list