tags:

views:

65

answers:

2

When I call rnorm passing a single value as mean, it's obvious what happens: a value is generated from Normal(10,1).

y <- rnorm(20, mean=10, sd=1)

But, I see examples of a whole vector being passed to rnorm (or rcauchy, etc..); in this case, I am not sure what the R machinery really does. For example:

a = c(10,22,33,44,5,10,30,22,100,45,97)
y <- rnorm(a, mean=a, sd=1)

Any ideas?

A: 

Perhaps it's for getting random values from a multivariate normal distribution with the given means.

matrix(rnorm(10*11, mean=a, sd=1),ncol=11,byrow=T)
Brani
That's what `MASS::mvrnorm` is for.
Joshua Ulrich
+3  A: 

The number of random numbers rnorm generates equals the length of a. From ?rnorm:

n: number of observations. If ‘length(n) > 1’, the length is taken to be the number required.

To see what is happening when a is passed to the mean argument, it's easier if we change the example:

a = c(0, 10, 100)
y = rnorm(a, mean=a, sd=1)
[1] -0.4853138  9.3630421 99.7536461

So we generate length(a) random numbers with mean a[i].

csgillespie