Hi all. I may be wrong, (and often am), but in trying to determine how to calculate the erf function, the documentation for 'pnorm' states: ## if you want the so-called 'error function' erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1 ## and the so-called 'complementary error function' erfc <- function(x) 2 * pnorm(x * sqrt(2), lower=FALSE) Should, instead, it read: ## if you want the so-called 'error function' erf <- function(x) 2 * pnorm(x / sqrt(2)) - 1 ## and the so-called 'complementary error function' erfc <- function(x) 2 * pnorm(x / sqrt(2), lower=FALSE) I've looked at a couple references and they all show that 'x' should be divided by, not multiplied by, 'sqrt(2)'. Again, I may be incorrect. If so, kindly let me know. But, if I am correct, perhaps the documentation could be corrected in a subsequent R release. Thanks, Charles
On Wed, 16 Jun 2004, Charles Maner wrote:> > Hi all. I may be wrong, (and often am), but in trying > to determine how to calculate the erf function, the > documentation for 'pnorm' states: > > ## if you want the so-called 'error function' > erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1 > ## and the so-called 'complementary error function' > erfc <- function(x) 2 * pnorm(x * sqrt(2), > lower=FALSE) > > Should, instead, it read: > ## if you want the so-called 'error function' > erf <- function(x) 2 * pnorm(x / sqrt(2)) - 1 > ## and the so-called 'complementary error function' > erfc <- function(x) 2 * pnorm(x / sqrt(2), > lower=FALSE) > > I've looked at a couple references and they all showWhich are? `Both', surely.> that 'x' should be divided by, not multiplied by, > 'sqrt(2)'. > > Again, I may be incorrect. If so, kindly let me know. > But, if I am correct, perhaps the documentation could > be corrected in a subsequent R release.According to Abramowitz and Stegun section 7.1, erf z = \frac{2}{\sqrt{pi} \int^z_0 e^_{-t^2} dt Now pnorm(x) = \frac{1}{sqrt{2\pi}} \int^x_{-\infty} e^_{-u^2/2} du so pnorm(x) - 1/2 = \frac{1}{sqrt{2\pi}} \int^x_0 e^_{-u^2/2} du Now substitute t = u/sqrt{2} pnorm(x) - 1/2 = \frac{1}{sqrt{2\pi}} \int^{x\sqrt{2}}_0 e^_{-t^2} dt\sqrt{2} so pnorm(x) - 1/2 = 1/2 erf x\sqrt{2} or erf z = 2 pnorm(x/\sqrt{2} - 1 as you suggest. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Wed, 16 Jun 2004 14:14:16 -0700 (PDT), Charles Maner <ckjmaner at yahoo.com> wrote:>Hi all. I may be wrong, (and often am), but in trying >to determine how to calculate the erf function, the >documentation for 'pnorm' states: > >## if you want the so-called 'error function' >erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1 >## and the so-called 'complementary error function' >erfc <- function(x) 2 * pnorm(x * sqrt(2), >lower=FALSE) > >Should, instead, it read: >## if you want the so-called 'error function' >erf <- function(x) 2 * pnorm(x / sqrt(2)) - 1 >## and the so-called 'complementary error function' >erfc <- function(x) 2 * pnorm(x / sqrt(2), >lower=FALSE) > >I've looked at a couple references and they all show >that 'x' should be divided by, not multiplied by, >'sqrt(2)'.The "Mathworld" web page at http://mathworld.wolfram.com/NormalDistributionFunction.html shows pnorm(x) - 1/2 = (1/2)*erf(x/sqrt(2)) (note that their Phi(x) = pnorm(x) - 1/2). This agrees with the pnorm documentation once you rearrange things. Duncan Murdoch