[R] Area() artefacts??

Sergey Goriatchev sergeyg at gmail.com
Sat May 12 16:15:21 CEST 2007


Hello, everybody

I run the following program, and depending on the size of eps I get
different results.
With eps=1e-05, the program calculates wrong values for x=65:67 and
others. The program runs fine with eps=1e-07. Why is this so?
Also, I am using area() instead of integrate() because I cannot make
integrate to work, especially with imaginary numbers. Maybe someone
can show me how to use integrate in this particular code? THanks in
advance!

#Computes the p.m.f. via (10.53) of the number of i.i.d. Ber(p) trials
#required until m consecutive successes occur.
#Requires MASS package

#==========================================================#
consecpmf <- function(xvec, m, p, eps=1e-05){
library(MASS)
f<-numeric()
for(j in seq(xvec)){
	x <- xvec[j]
	f[j] <- area(fun, -pi, pi, limit=1000, eps=eps, x, m, p)
}
f<-Re(f)
round(f,4)
}

fun <- function(t,x,m,p){
I <- exp(-1i*t*x)*cf(t,m,p)/(2*pi)
I
}

cf <- function(t,m,p){
q <- 1-p
if(m==1) {g <- p*exp(1i*t)/(1-q*exp(1i*t))} else
{kk <- exp(1i*t)*cf(t, m-1, p); g <- (p*kk)/(1-q*kk)}
g
}

#===================TESTING================================#
consecpmf(seq(0,200),3,0.5)



More information about the R-help mailing list