[R] Discontinuities in a simple graph (machine precision?)

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed May 5 11:59:21 CEST 2004


Simon Cullen wrote:

> Hi,
> 
> I've got an ugly but fairly simple function:
> 
> mdevstdev <- function(a){
>     l <- dnorm(a)/(1-pnorm(a))
>     integrand  <- function(z)(abs(z-l)*dnorm(z))
>     inted <- integrate(integrand, a, Inf)

It's a matter of numerical accuracy, you might want to use, e.g.:

     inted <- integrate(integrand, a, Inf,
         rel.tol = .Machine$double.eps^0.5)

Uwe Ligges

>     inted[[1]]/((1- pnorm(a))*sqrt((1 + a*l - l^2)))
> }
> 
> I wanted to quickly produce a graph of this over the range [-3,3] so I  
> used:
> 
> plotit <-function(x=seq(-3,3,0.01),...){
>     y<-sapply(x,mdevstdev)
>     plot(x,y,...)
> }
> 
>> plotit()
> 
> 
> This produces the graph, but some discontinuities appear on it. I've  
> produced the same graph in Mathematica 5  
> (http://econserv2.bess.tcd.ie/cullens/R/DOverDelta.eps), and it was 
> smooth  over this range (it takes ages to run, though). Is this a 
> numerical  precision problem? Any suggestions on how to improve the 
> precision?
> 
> I'm running R 1.9 on WinXP, PIII. I haven't changed any R parameters 
> that  I know of.
>




More information about the R-help mailing list