Re: [R] Using R to illustrate the Central Limit Theorem

From: Kevin E. Thorpe <kevin.thorpe_at_utoronto.ca>
Date: Fri 13 May 2005 - 22:45:28 EST

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@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@utoronto.ca  Tel: 416.946.8081  Fax: 416.971.2462

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri May 13 22:49:32 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:45 EST