# Simulate variables from multivariable gaussian distribution in R

So I am asked to simulate data for 50 patients. Each patient has 15 features. I am told that the first 10 variables come from a multivariable Gaussian distribution with mean = 0 and a covariance matrix that equals to an identity matrix. The variance must be equal to 1. So in R, I did the following.

library(mvtnorm)

#create a 10x10 matrix with 0 values everywhere.Then fill with 1 the diagonal.
mat <- matrix(0, 10, 10)
diag(mat) <- 1

#This is how I sampled for the 10 first features.
rmvnorm(50, mean = c(0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ), sigma = mat)


For the rest 5 features, I am given the following formula to sample from.

$$\mathrm{X}_{\mathrm{ij}} \sim \mathrm{N}\left(0.2 \mathrm{X}_{\mathrm{i} 1}+0.4 \mathrm{X}_{\mathrm{i} 2}+0.6 \mathrm{X}_{\mathrm{i} 3}+0.8 \mathrm{X}_{\mathrm{i} 4}+1.1 \mathrm{X}_{\mathrm{i} 5}, 1\right), \mathrm{j}=11, \ldots, 15 \text{ and } \mathrm{i}=1, \ldots, 50$$

I am not sure how to express the covariance matrix.

The same applies to the y variable (target) which is given by the following formula.

$$Y_{i} \sim N\left(4+2 X_{i 1}-X_{i 5}+2.5 X_{i 7}+1.5 X_{i 11}+0.5 X_{i 13}, 1.5^{2}\right), i=1, \ldots, 50$$

Can this somehow be done with r norm package? It seems highly unlikely.

I think you are doing it right...

"I am not sure how to express the covariance matrix": but you specify variance to be 1, right? This is what you want? You just need to draw each time conditionally on drawing $$X's$$ in the right order: first draw first 10 $$X's$$, then the other five conditional on first five $$X's$$, and lastly draw $$Y's$$ condtional on 1, 5, 7, 11, and 13 $$X$$ variable.

You can do all with rnorm. The first 10 variables are uncorrelated (covariance - identity) so you can draw rnorm(50*10, mean=0,sd=1) and reshape.

Then, for $$j\in\{11,...,15\}$$ compute the mean, $$\mu = 0.2X_1+0.4X_2+0.6X_3+0.8X_4+1.1X_5$$, and draw 5 times from rnorm(50, mean=$$\mu$$,sd=1).

The same holds for your response, i.e. compute the mean and draw $$Y$$ once setting sd=1.5. Hope this helps.

• that's it, thank you very much for your time. May 29, 2020 at 17:51
• no problem, enjoy! May 29, 2020 at 19:49
• So I should expect x11 to x15 to be correlated with each other (multicollinearity problems may arise) and that Y will be best predicted by x1, x5, x7, x11, and x13. Is my intuition correct? May 30, 2020 at 13:30
• I should expect x11 to x15 to be correlated with each other (multicollinearity problems may arise) <- no. You draw them separately and they are uncorrelated, at least from the way you define your DGP. Y will be best predicted by x1, x5, x7, x11, and x13. <- this not sure, because x11 to x15 depend on x1 to x5, you need to think about this... Jun 2, 2020 at 13:43