Re: [R] Random numbers from noncentral t-distribution

From: Long Qu <>
Date: Sat 01 Jul 2006 - 02:46:14 EST

Thank you very much for your kind reply. It solved the problem of rt( ). :D    

  But it seems that the qt( ) also have problems:   I modified the rt( ) function as you suggested,   rt <- function (n, df, ncp = 0)
if (ncp == 0)
.Internal(rt(n, df))
else rnorm(n, ncp)/sqrt(rchisq(n, df)/df) }

  Then I increase the number of random variables to 10000, and made a QQ-plot:   > qqplot(rt(10000,df=20,ncp=3),qt(runif(10000),df=20,ncp=3))

  I've got some spurious points at lower-left corner. It seems that qt( ) results were truncated.    

  I also tried this with another df and ncp:   > pt(-.75,df=2,ncp=1)
[1] 0.05726429
> sum(qt(1:10000/10001,df=2,ncp=1)< -.75)
[1] 0
where I'd expected the last number should be > 550 or so, not 0.         

  Thanks again, the modified rt( ) is now OK for my work.   Long       

Thomas Lumley <> wrote   On Fri, 30 Jun 2006, Long Qu wrote:

> Hi there:
> I'd thought these two versions of noncentral t-distribution are essentially the same:
> > qqplot(rt(1000,df=20,ncp=3),qt(runif(1000),df=20,ncp=3))
> But, the scales of the x-axis and the y-axis are quite different according to the QQ-plot.
> Did I make any mistakes somewhere?

No, I think we did.

We have
> rt

function (n, df, ncp = 0)
if (ncp == 0)
.Internal(rt(n, df))
else rnorm(n, ncp)/(rchisq(n, df)/sqrt(df)) }

and the rchisq() in the denominator should be inside the sqrt().



        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide! Received on Sat Jul 01 04:00:57 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 01 Jul 2006 - 04:15:58 EST.

Mailing list information is available at Please read the posting guide before posting to the list.