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!
Sampling from a multivariate normal It is then possible to sample from N ( μ Y , Σ Y ) by sampling and applying the affine transform on the samples. This transform is Y = L X + u where we know from the previous section that the covariance of will be Σ Y = L Σ X L ⊤ .
Description. Calculates the probability density function of the multivariate normal distribution.
The multivariate normal distribution is a generalization of the univariate normal distribution to two or more variables. It is a distribution for random vectors of correlated variables, where each vector element has a univariate normal distribution.
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!
Here are some options:
1) mvtnorm::rmvnorm
and MASS::mvrnorm
work the same way, although the mvtnorm::rmvnorm
function does not require that you specify the means (i.e., the default is 0). Giving names to the mu
vector will specify the names of the simulated variables.
n <- 100
R <- matrix(c(1, 0.5,
0.5, 1),
nrow = 2, ncol = 2)
mu <- c(X = 0, Y = 0)
mvtnorm::rmvnorm(n, mean = mu, sigma = R)
MASS::mvrnorm(n, mu = mu, Sigma = R)
2) simstandard::sim_standardized
will make standardized data only, but will do so with less typing:
simstandard::sim_standardized("X ~~ 0.5 * Y", n = 100)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With