'How to generate multivariate normal data in R?
I'm completing an assignment, in which I have to generate a sample X = (X1, X2) from a bivariate normal in which each marginal is N(0,1) and the correlation between X1 and X2 is 0.5.
I think the way to approach this is to use the mvrnorm function, but I'm not quite sure how to proceed after that. Any advice? Thanks in advance!
Solution 1:[1]
Indeed, the mvrnorm function from the MASS package is probably your best bet. This function can generate pseudo-random data from multivariate normal distributions.
Examining the help page for this function (??mvrnorm) shows that there are three key arguments that you would need to simulate your data based your given parameters, ie:
n- the number of samples required (an integer);mu- a vector giving the means of the variables - here, your distributions are standard normal so it will be a vector of zeros; andSigma- a positive-definite symmetric matrix specifying the covariance matrix of the variables - ie, in your case, a matrix with variance on the diagonal of ones and covariance on the off-diagonals of 0.5.
Have a look at the examples in this help page, which should help you put these ideas together!
Solution 2:[2]
Here are some options:
mvtnorm::rmvnormandMASS::mvrnormwork the same way, although themvtnorm::rmvnormfunction does not require that you specify the means (i.e., the default is 0). Giving names to themuvector will specify the names of the simulated variables.
n <- 100
R <- matrix(c(1, 0.5,
0.5, 1),
nrow = 2, ncol = 2, byrow = TRUE)
mu <- c(X = 0, Y = 0)
mvtnorm::rmvnorm(n, mean = mu, sigma = R)
MASS::mvrnorm(n, mu = mu, Sigma = R)
simstandard::sim_standardizedwill make standardized data only, but will do so with less typing:
simstandard::sim_standardized("X ~~ 0.5 * Y", n = 100)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
