How to set up the matrix in data generation in R? I am trying to generate two continuous predictors and one continuous outcome from mvrnorm. Now, I am trying to set up both standardized slopes at 0.3.
This is my R code.
n.tr=10
mu = c(3, 3, 3)
Sigma = cbind(c(0.91,0,0.3),
              c(0 0.91,0.3),
              c(0.3,0.3,0.91)
dat =  as.data.frame(mvrnorm(n=n.tr, mu=mu, Sigma=Sigma, 
                     empirical=TRUE))
colnames(dat) = c('z', 'w','y')

I want both of the standardized slopes to be 0.3, and 0 correlation between z and w. I also want the variance of y to be 1, so the residual variance of y is 0.91. If i want the diagonal of the matrix to be residual variance, how can I set up the matrix?
 A: If X, W are independent, then the covariance between two predictors is related to the regression slope by:
$\beta_{Y|X} = \text{Cov}(X,Y)/\sqrt{\text{Var}(X)}$
So your Sigma matrix should be modified. If $\text{Var}(X)$ is set to 1 instead of 0.91 you will have the correct regression coefficient from the covariance matrix.
You have correctly constrained the conditional variance of $Y$ to be 0.91. If you want the total variance of $Y$ additionally to be 1 you can apply the law of total variance:
$$ \text{Var}(Y) = E(\text{Var}(Y|X)) + \text{Var}(E(Y|X))$$
The first term is recognizable as the conditional variance, i.e. 0.91, the second term reduces to $$\text{Var}(E(Y|X)) = \beta_1^2 \text{Var}(X) + \beta_2^2 \text{Var}(W)$$
So if we control this parameter first, assuming $\beta_1 = \beta_2 = 0.3$ then one choice of the regressor variance is 0.5 for both $X$ and $W$. Then the constrained solution for the covariance of X,Y and W,Y would be set to $0.3/2=0.15 $ to meet the requirements you laid out.
> x <- rnorm(1e6, 0, sqrt(0.5))
> w <- rnorm(1e6, 0, sqrt(0.5))
> y <- rnorm(1e6, 0, sqrt(0.91))
> y <- y + 0.3*x + 0.3*w
> var(y)
[1] 0.9998229
> lm(y ~ x+w)

Call:
lm(formula = y ~ x + w)

Coefficients:
(Intercept)            x            w  
  -0.001749     0.299974     0.301086  

> var(lm(y ~ x+w)$residual)
[1] 0.9094854

> round(var(cbind(y,x,w)), 2)
     y    x    w
y 1.00 0.15 0.15
x 0.15 0.50 0.00
w 0.15 0.00 0.50

A: Here are some tips:

*

*Always set the random seed to make it fully reproducible.  Later, you'll be able to figure out what you did.

*I would create the matrix using rbind(), instead of cbind(), so that it's easier to see the matrix you are creating.

*Likewise, I add decimal places and spaces so that the values are aligned.  That makes for a better gestalt, and it's easier to see what you have at a glance.

*(You seem to be missing a comma and a closing parenthesis.)

*It's easier to make everything standard normal (i.e., variances equal to 1, and off-diagonal elements correlations), and rescale afterwards to whatever means and SDs you want.

*It's easiest to use mvrnorm() to generate the matrix of X variables, and then generate the Y data using a regression type specification.  Again, it's just easier to see that what you have written matches what you have in mind.

With those suggestions, here is how I might adapt your code:
set.seed(1)
n.tr  = 10
mu    = c(0, 0)
Sigma = rbind(c(1,0),
              c(0,1) )
X     = mvrnorm(n=n.tr, mu=mu, Sigma=Sigma, empirical=TRUE)
z     = X[,1]
w     = X[,2]
y     = 3 + .3*z + .3*w + rnorm(n.tr, mean=0, sd=sqrt(.91))
dat   = data.frame(z=z, w=w, y=y)

