# I like to generate correlated variables with a existing variable in R [duplicate]

In R, I like to create a set of variables that are correlated with a given variable.I scanned related questions but failed to find the same issue i have.

Here is instance.

x<-rnorm(100, mean=3,sd=1.2)


Assuming $x$ is an exiting variable, I like to generate $z, y, t$ correlated variables with $4$ by $4$ correlation matrix.

So, variables $y, t, z$ would correlate $x$ according to predefined correlation matrix I will use as an input. It would be grateful if you leave a comment on me with a specific code.

• reopened as this requires statistical expertise to answer – Glen_b -Reinstate Monica Sep 9 '18 at 14:00

Suppose

$$\begin{bmatrix} t\\ y \\ z \\ x \end{bmatrix} \sim N\left( \begin{bmatrix} \mu_p \\ \mu_x\end{bmatrix}, \begin{bmatrix} \Sigma_{pp} & \Sigma_{px} \\ \Sigma_{xp} & \Sigma_{xx}\end{bmatrix}\right)$$

where $\mu_p \in \mathbb{R}^3, \Sigma_{pp} \in \mathbb{R}^{3 \times 3}, \Sigma_{px} \in \mathbb{R}^{3 \times 1}, \Sigma_{xp} \in \mathbb{R}^{1 \times 3}, \Sigma_{xx} \in \mathbb{R}, \mu_x \in \mathbb{R}$.

Then, we have

$$\begin{bmatrix} t\\ y \\ z \end{bmatrix} \mid x=a \sim N\left( \mu_p+\Sigma_{px}\Sigma_{xx}^{-1}(a-\mu_x),\Sigma_{pp}- \Sigma_{px}\Sigma_{xx}^{-1}\Sigma_{xp}\right)$$

Hence we can generate $t,y,z$ given $x=a$ with the above distribution.

• Thanks!! if you don't mind, could you let me know how i can apply your formula to R code? – 김만인 Sep 9 '18 at 7:54
• do you face any difficulty in using mvrnorm? – Siong Thye Goh Sep 9 '18 at 7:58
• @김만인 this would not generally be an appropriate site for "how do I code this in R" questions. – Glen_b -Reinstate Monica Sep 9 '18 at 14:02
• i did't know that. usually i think i can get help from others here about R code. anyway, I am not good at translating the formula to R code. i hope somebody help me out with this. – 김만인 Sep 10 '18 at 1:23
• if possible, always try things out first before you seek help. updating of what you have tried and where you are stuck again. anyway, there are some codes at the linked post. – Siong Thye Goh Sep 10 '18 at 2:00