Perhaps this is useful:
x=c(-2,0,2)
sign(x)*abs(x)
[1] -2 0 2
------------------------------------------
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A. T. Still University of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965
------------------------------------------
Robert W. Baer, Ph.D.
Professor of Physiology
Kirksville College of Osteopathic Medicine
A. T. Still University of Health Sciences
800 W. Jefferson St.
Kirksville, MO 63501
660-626-2322
FAX 660-626-2965
--------------------------------------------------
From: "Tremblay, Pierre-Olivier" <potremb...@pharmanet.com>
Sent: Thursday, May 19, 2011 2:42 PM
To: <r-help@r-project.org>
Subject: [R] recursive function
Hi,
I created a function for obtaining the normal cumulative distribution (I
know all this already exists in R, I just wanted to verify my
understanding of it). below is the code I came up with.
cdf<-function(x) {
erf<-function(x) {
# approximation to the error function (erf) of the
# normal cumulative distribution function
# from Winitzki (2008)
a <- 0.147
partc <- 1+a*x^2
partb <- 4/pi+a*x^2
parta <- -x^2*(partb/partc)
erf <- sqrt(1-exp(parta))
erf
}
# cumulative density function
cdf<- 1/2*(1+erf(x/sqrt(2)))
cdf
}
The erf(x) produces positive values whatever the sign of x. Instead of
obtaining the expected sigmoid shape of the erf() I end up with a
positive values V shape. erf(x) should be negative for negative values
of x. I figure I need some form of conditional statement in the above
code but all my attempts at using "if" and "for" failed miserably.
Clearly, I don't understand how to use them properly in R. If anyone
could point me in the right direction as on how to code conditional
events in R that would be much appreciated.
many thanks in advance
Pierre-Olivier
[[alternative HTML version deleted]]
______________________________________________
R-help@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.
______________________________________________
R-help@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.