[R] recursive function
Rolf Turner
rolf.turner at xtra.co.nz
Fri May 20 00:38:16 CEST 2011
(1) What has this to do with recursion?
(2) You probably need to use ifelse(). I believe that this is
(in effect) an FAQ.
cheers,
Rolf Turner
On 20/05/11 07:42, Tremblay, Pierre-Olivier wrote:
> 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
More information about the R-help
mailing list