# Expected covariances in a causal network

This is a skeleton post

## Standardized variables (Wright’s rules)

```
n <- 10^5
# z is the common cause of g1 and g2
z <- rnorm(n)
# effects of z on g1 and g2
b1 <- 0.7
b2 <- 0.7
r12 <- b1*b2
g1 <- b1*z + sqrt(1-b1^2)*rnorm(n)
g2 <- b2*z + sqrt(1-b2^2)*rnorm(n)
var(g1) # E(VAR(g1)) = 1
```

`## [1] 0.997149`

`var(g2) # E(VAR(g2)) = 1`

`## [1] 0.9972956`

`cor(g1, g2) # E(COR(g1,g2)) = b1*b2`

`## [1] 0.4881183`

`b1*b2`

`## [1] 0.49`

```
# effects of g1 and g2 on y1 and y2
a11 <- 0.5 # effect of g1 on y1
a12 <- 0.5 # effect of g1 on y2
a21 <- 0.5 # effect of g2 on y1
a22 <- 0.5 # effect of g2 on y2
r2.1 <- a11^2 + a21^2 + 2*a11*a21*b1*b2 # systematic variance of y1
r2.2 <- a12^2 + a22^2 + 2*a12*a22*b1*b2 # systematic variance of y1
y1 <- a11*g1 + a21*g2 + sqrt(1-r2.1)*rnorm(n)
y2 <- a12*g1 + a22*g2 + sqrt(1-r2.2)*rnorm(n)
var(y1) # E() = 1
```

`## [1] 0.9963031`

`var(y2) # E() = 1`

`## [1] 0.994697`

`cor(y1, y2) # E() = a11*a12 + a21*a22 + a11*r12*a22 + a21*r12*a12`

`## [1] 0.7454303`

`a11*a12 + a21*a22 + a11*r12*a22 + a21*r12*a12 # sum of all paths`

`## [1] 0.745`

## Non-standardized variables (Wright’s rules)

```
n <- 10^6
# z is the common cause of g1 and g2
sigma_z <- 2
z <- rnorm(n, sd=sigma_z)
# effects of z on g1 and g2
b1 <- 1.2
b2 <- 1.2
s12 <- b1*b2*sigma_z^2 # cov(g1, g2)
sigma.sq_g1.sys <- (b1*sigma_z)^2 # systematic variance of g1
sigma.sq_g2.sys <- (b2*sigma_z)^2 # systematic variance of g1
r2.g1 <- 0.9 # R^2 for g1
r2.g2 <- 0.9 # R^2 for g2
sigma.sq_g1 <- sigma.sq_g1.sys/r2.g1
sigma.sq_g2 <- sigma.sq_g2.sys/r2.g2
sigma_g1 <- sqrt(sigma.sq_g1)
sigma_g2 <- sqrt(sigma.sq_g2)
g1 <- b1*z + sqrt(sigma.sq_g1 - sigma.sq_g1.sys)*rnorm(n)
g2 <- b2*z + sqrt(sigma.sq_g2 - sigma.sq_g2.sys)*rnorm(n)
var(g1) # E(VAR(g1)) = sigma.sq_g1
```

`## [1] 6.400034`

`var(g2) # E(VAR(g2)) = sigma.sq_g2`

`## [1] 6.403562`

`sigma.sq_g1`

`## [1] 6.4`

`sigma.sq_g2`

`## [1] 6.4`

`cov(g1,g2) # E() = b1*b2*sigma_z^2`

`## [1] 5.759874`

`s12 # b1*b2*sigma_z^2`

`## [1] 5.76`

```
# effects of g1 and g2 on y1 and y2
a11 <- 1.5 # effect of g1 on y1
a12 <- 1.5 # effect of g1 on y2
a21 <- 3 # effect of g2 on y1
a22 <- 1.5 # effect of g2 on y2
sigma.sq_y1.sys <- a11^2*sigma.sq_g1 + a21^2*sigma.sq_g2 + 2*a11*a21*s12 # systematic variance of y1
sigma.sq_y2.sys <- a12^2*sigma.sq_g1 + a22^2*sigma.sq_g2 + 2*a12*a22*s12 # systematic variance of y2
r2.1 <- 0.9 # R^2 for y1
r2.2 <- 0.9 # R^2 for y2
sigma.sq_y1 <- sigma.sq_y1.sys/r2.1
sigma.sq_y2 <- sigma.sq_y2.sys/r2.2
sigma_y1 <- sqrt(sigma.sq_y1)
sigma_y2 <- sqrt(sigma.sq_y2)
y1 <- a11*g1 + a21*g2 + sqrt(sigma.sq_y1 - sigma.sq_y1.sys)*rnorm(n)
y2 <- a12*g1 + a22*g2 + sqrt(sigma.sq_y2 - sigma.sq_y2.sys)*rnorm(n)
var(y1) # E() = sigma.sq_y1
```

`## [1] 137.5161`

`var(y2) # E() = sigma.sq_y1`

`## [1] 60.87395`

`sigma.sq_y1`

`## [1] 137.6`

`sigma.sq_y2`

`## [1] 60.8`

```
a11.s <- a11*sigma_g1/sigma_y1
a12.s <- a12*sigma_g1/sigma_y2
a21.s <- a21*sigma_g2/sigma_y1
a22.s <- a22*sigma_g2/sigma_y2
r12 <- s12/sigma_g1/sigma_g2
r12 # expected correlation between g1 and g2
```

`## [1] 0.9`

`cor(g1, g2) # actual correlation between g1 and g2`

`## [1] 0.8997275`

```
# covariance y1 and y2
cov(y1, y2) #
```

`## [1] 82.10576`

`(a11.s*a12.s + a21.s*a22.s + a11.s*r12*a22.s + a21.s*r12*a12.s)*sigma_y1*sigma_y2 # expected cov(y1, y2)`

`## [1] 82.08`

```
# more compact and not using the standardized parameters
# = sum of paths where path coef = stand coef * parent sd
a11*a12*sigma.sq_g1 + a21*a22*sigma.sq_g2 + a11*s12*a22 + a21*s12*a12
```

`## [1] 82.08`

```
# correlation y1 and y2
cor(y1, y2)
```

`## [1] 0.8973897`

`a11.s*a12.s + a21.s*a22.s + a11.s*r12*a22.s + a21.s*r12*a12.s`

`## [1] 0.8973799`

## Expected value of b with missing covariate

`coef(summary(lm(y1 ~ g1 + g2)))`

```
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.006165963 0.003710027 1.661972 0.09651863
## g1 1.503519031 0.003360074 447.466106 0.00000000
## g2 2.994464649 0.003359148 891.435776 0.00000000
```

`coef(summary(lm(y1 ~ g1)))`

```
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.004241297 0.004970132 0.8533571 0.3934615
## g1 4.198463665 0.001964609 2137.0475423 0.0000000
```

```
# expected = direct + cor*indirect using cor
a11 + cor(g1,g2)*a21
```

`## [1] 4.199183`

```
# expected = direct + cor*indirect, using cov as model param
a11 + (s12/sigma_g1/sigma_g2)*a21
```

`## [1] 4.2`

```
# need to correct if different variances in g1 and g2
# expected is a11 + rho*sigma_g2/sigma_g1*a21
rho <- s12/sigma_g1/sigma_g2
a11 + rho*sigma_g2/sigma_g1*a21
```

`## [1] 4.2`

```
# this is equal to
a11 + s12/sigma_g1^2*a21
```

`## [1] 4.2`

`# which is equal to a11 + b21*a21`