tags:

views:

35

answers:

1

What happens when I call rchisq(100,1:100,1:100):

  • does R generate 100 numbers with df=1,ncp=1?
  • or does it generate 100 numbers each with df=k,ncp=k for k=1...100?

What I want to know is whether df and ncp can be vectors or not. It is not clear in the documentation (when compared with rnorm). I suspect that they can also be vectors and recycling happens if the lengths differ(?)

+2  A: 

Just test it by using set.seed():

R> set.seed(42); rchisq(2, 1:2, 1:2)              # base case
[1] 8.676 1.653
R> set.seed(42); rchisq(1, 1, 1); rchisq(1, 2, 2) # matches
[1] 8.676
[1] 1.653
R> set.seed(42); rchisq(1, 1, 1); rchisq(1, 1, 1) # does not match
[1] 8.676
[1] 0.5874

so it looks like you get N draws using degrees of freedom and non-centrality from the corresponding value in the supplied vector.

Viewed another way, recycling as actually the rules as scalar values for df and ncp get recycled to vector length which makes some sense.

Dirk Eddelbuettel
I tried 'rchisq(10,1,rep(c(1,100),each=5))', confirms the answer. THX. General comment: I think that the R documentation is not really up to date when compared with MATLAB for example...
teucer
Easy. Just donate the few thousand dollars you save each time and the R Foundation can probably be persuaded to hire some help for editing. Kidding aside, the documentation is actually rather amazingly complete and if you make a convincing case on r-devel concerning a particular issue then _you_ can actually help to make it better. Deal?
Dirk Eddelbuettel
Deal!!!But how about a wiki type documentation where everybody can contribute?
teucer
That's not how R source code works, but wiki.R-Project.org is eagerly awaiting other contributions :)
Dirk Eddelbuettel