[R] Additional args to fun in integrate() not found?

Sergey Goriatchev sergeyg at gmail.com
Tue Feb 27 10:49:22 CET 2007


Hello, fellow Rdicts,

I have  the code for the program below. I need to integrate a function
of "x" and "p". I use integrate to integrate over "x" and pass "p" as
an additional argument. "p" is specified and given default value in
the argument list. Still, integrate() cannot read "p", unless I
explicitly insert a numeric value in the integrate() argument list.
And when I do that, I get the right result, but still some warnings.

Please, help me with these problems:
1) why is "p" not recognized?
2) what are these warning messages?

PROGRAM CODE:
---------------------------

#THIS LIBRARY IS NEEDED FOR THE INCOMPLETE GAMMA FUNCTION

library(zipfR)

#------------------------------------------------------------------------

gedCDF = function(yvec, p=2, mu=0, scale=1, numint=0)
{

#-------------------------------------------------------------------------
#Setting k to sqrt(2) and the GED with p=2 coincides with standard normal.
#Set k=1 and GED with p=1 coincides with Laplace.

	k<-sqrt(2)
	#k<-1
	scale<-scale*k
	
	zvec<-(yvec-mu)/scale
	cdf<-matrix(0, length(zvec),1)

	for(i in 1:length(zvec))
	{
		z<-zvec[i]

		if(numint==0)
		{
		if(z<=0)
		{
		t<-0.5*(1-1/gamma(1/p)*Igamma((1/p),(-z)^p,lower=TRUE))
		}
		else
		{
		t<-1-(0.5*(1-1/gamma(1/p)*Igamma((1/p),(z)^p,lower=TRUE )))
		}
		}
		else
		{
		t<-integrate(geddenstandard, -35, z, subdivisions=1000,
		rel.tol=100*.Machine$double.eps, abs.tol=rel.tol,
		stop.on.error=TRUE, keep.xy=FALSE, aux=NULL,p)
		}
	cdf[i]<-t
	}
cdf
}

#-------------------------------------------------------------------------
geddenstandard = function(z,p)
{
f<-p/(2*gamma(1/p))*exp(-abs(z)^p)
}
-----------------------------------------------------------------------------------------------------------------------

If I run with this definition I get the following error message and abort:
> gedCDF(c(1,2,3,4,5), numint=1)
Error in eval(expr, envir, enclos) : ..1 used in an incorrect context,
no ... to look in

If I replace "p" in integrate() with 2, I get correct answers, but
still some warning messages:

> gedCDF(c(1,2,3,4,5), numint=1)
[[1]]
[1] 0.8413447

[[2]]
[1] 0.9772499

[[3]]
[1] 0.9986501

[[4]]
[1] 0.9999683

[[5]]
[1] 0.9999997

Warning messages:
1: number of items to replace is not a multiple of replacement length
2: number of items to replace is not a multiple of replacement length
3: number of items to replace is not a multiple of replacement length
4: number of items to replace is not a multiple of replacement length
5: number of items to replace is not a multiple of replacement length

-------------------------------
Do I get these warnings because I define cdf as a matrix and the
output-cdf is a list?

Please, help me with these!
Email to my gmail account, please: sergeyg at gmail.com

THanks in advance
Sergey



More information about the R-help mailing list