0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ reopened as this requires statistical expertise to answer $\endgroup$
    – Glen_b
    Commented Sep 9, 2018 at 14:00

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Thanks!! if you don't mind, could you let me know how i can apply your formula to R code? $\endgroup$
    – 김만인
    Commented Sep 9, 2018 at 7:54
  • 1
    $\begingroup$ do you face any difficulty in using mvrnorm? $\endgroup$ Commented Sep 9, 2018 at 7:58
  • $\begingroup$ @김만인 this would not generally be an appropriate site for "how do I code this in R" questions. $\endgroup$
    – Glen_b
    Commented Sep 9, 2018 at 14:02
  • $\begingroup$ 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. $\endgroup$
    – 김만인
    Commented Sep 10, 2018 at 1:23
  • $\begingroup$ 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. $\endgroup$ Commented Sep 10, 2018 at 2:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.