简体   繁体   中英

R: How to generate several random variables at once

I want to generate 1000 random variables coming from different normal distributions. I use the function "rmvnorm" for that and in a small setting, it is easily done but I have no idea how to automate it, especially for the sigma matrix (I want no correlation between the Xs). I don't really care about their means or their standard deviation. I was thinking of using a loop (eg increase by A the mean and by B the variance) but I want something more random and have no idea how I can do that. Again, writing down a matrix of 1000 dimension is not smart (with the condition that the off-diag elements are 0).

I have searched online but I am probably not using the rights words so I apologize if it was already asked and answered.

Thanks!

You can pass equal-length vectors for the parameters of rnorm . The first value returned will be a random draw from a normal distribution with a mean equal to the first value in the mean vector and sd equal to the first value in the sd vector:

rnorm(1e3, 1:1e3, 1:1e3)

Not sure what is meant by "I want something more random", but you can use random values for the mean and sd vectors:

rnorm(1e3, runif(1e3)*1e3, 1/rgamma(1e3, 10, 20))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM