[R] Using R to illustrate the Central Limit Theorem

Kevin E. Thorpe kevin.thorpe at utoronto.ca
Fri May 13 14:45:28 CEST 2005


Thanks. I'd forgotton that detail about ifelse. Also, I was so focused 
on the U[0,1] it totally did not occur to me to go direct from U[-1,1]. 
Your suggestion should obviate the need for a local function at all.

Peter Dalgaard wrote:

>"Kevin E. Thorpe" <kevin.thorpe at utoronto.ca> writes:
>
>  
>
>>rparab <- function(nn) {
>>u <- 2*runif(nn) - 1
>>ifelse(u<0,-(abs(u)^(1/3)),u^(1/3))
>>}
>>
>>It seems that in my version of R (2.0.1) on Linux, that calculating the cube
>>root of a negative number using ^(1/3) returns NaN. I looked at the help in
>>the arithmetic operators and did help.search("cube root"),
>>help.search("root")
>>and help.search("cube") and recognised no alternatives. So I used an
>>ifelse() to
>>deal with the negatives. Have I missed something really elementary?
>>    
>>
>
>Not really. You might have used u <- runif(nn,-1,1) and
>sign(u)*abs(u)^(1/3) instead of the ifelse construct (remember that
>ifelse generally evaluates both the 'yes' and 'no' parts and on some
>architectures the NaN results may be slow to compute).
>
>
>  
>


-- 
Kevin E. Thorpe
Biostatistician/Trialist, Knowledge Translation Program
Assistant Professor, Department of Public Health Sciences
Faculty of Medicine, University of Toronto
email: kevin.thorpe at utoronto.ca  Tel: 416.946.8081  Fax: 416.971.2462




More information about the R-help mailing list