Guaramy
2012-Apr-08 17:28 UTC
[R] Error in integrate(int.fn, lower = 0, upper = Inf) : evaluation of function gave a result of wrong length
Hi, i am writing a function to plot a pdf functions of a Generalized normal laplace distribution. The code is this { 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 } for example if i call the function like this : GNL.pdf.fn(x[100],mu,sigma,alpha,beta,rho) there is no problem and as expected a number is returned but if i try to call it with a sequence of number ex: x = seq(-4,4,0.1) this error keeps show in up Error in integrate(int.fn, lower = 0, upper = Inf) : evaluation of function gave a result of wrong length Anyone call help me please ? Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Error-in-integrate-int-fn-lower-0-upper-Inf-evaluation-of-function-gave-a-result-of-wrong-length-tp4541250p4541250.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Apr-09 02:33 UTC
[R] Error in integrate(int.fn, lower = 0, upper = Inf) : evaluation of function gave a result of wrong length
On Apr 8, 2012, at 1:28 PM, Guaramy wrote:> Hi, i am writing a function to plot a pdf functions of a Generalized > normal > laplace distribution. > The code is this > { > 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 > } > for example if i call the function like this : > GNL.pdf.fn(x[100],mu,sigma,alpha,beta,rho) > there is no problem and as expected a number is returned > > but if i try to call it with a sequence of number ex: x = > seq(-4,4,0.1)?Vectorize> > this error keeps show in up > > Error in integrate(int.fn, lower = 0, upper = Inf) : > evaluation of function gave a result of wrong length >David Winsemius, MD West Hartford, CT
peter dalgaard
2012-Apr-09 13:35 UTC
[R] Error in integrate(int.fn, lower = 0, upper = Inf) : evaluation of function gave a result of wrong length
On Apr 9, 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.Well, if you have a function that is not vectorized, i.e. it works for a single argument but not for a vector of arguments, and you want it to be vectorized, the tool is Vectorize(), the help page of which David pointed you to. (This essentially works around the problem by creating a new function which calls the function one vector element at the time.) The direct cause of your problem seems to be that if x is a vector, so is y, and your temp.fn returns a vector, so int.fn returns a matrix, and integrate() is unhappy with that. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Guaramy
2012-Apr-09 14:08 UTC
[R] Error in integrate(int.fn, lower = 0, upper = Inf) : evaluation of function gave a result of wrong length
Tanks a lot to all of you that take the time to help me. This is a really useful and helpful forum and i will try to help as much as i can Best Regards Jorge -- View this message in context: http://r.789695.n4.nabble.com/Error-in-integrate-int-fn-lower-0-upper-Inf-evaluation-of-function-gave-a-result-of-wrong-length-tp4541250p4542842.html Sent from the R help mailing list archive at Nabble.com.
Uwe Ligges
2012-Apr-09 14:28 UTC
[R] Error in integrate(int.fn, lower = 0, upper = Inf) : evaluation of function gave a result of wrong length
On 09.04.2012 16:08, Guaramy wrote:> Tanks a lot to all of you that take the time to help me. > This is a really useful and helpful forum and i will try to help as much as > i canThanks for offering help to the R-help *mailing list*. Please try to use the mailing list interface (rather than Nabble) and quote the context. Also, please send your message not only to the list, but also to those who need helped (or in this case, who helped you). Best, Uwe Ligges> Best Regards Jorge > > -- > View this message in context: http://r.789695.n4.nabble.com/Error-in-integrate-int-fn-lower-0-upper-Inf-evaluation-of-function-gave-a-result-of-wrong-length-tp4541250p4542842.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.