Created
May 8, 2021 10:54
-
-
Save richarddmorey/c9585316266f432bebe2ea30314dba8b to your computer and use it in GitHub Desktop.
simulating correlations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N = 20 | |
library(mvtnorm) | |
# No shared variance between a and (b,c) | |
Sigma = matrix(c(1,0,0, | |
0,1,.5, | |
0,.5,1), 3, 3) | |
# Simulation | |
res = replicate(10000, { | |
# Sample data | |
X = mvtnorm::rmvnorm(n = N, sigma = Sigma) | |
# Correlation test between a and the shared variance | |
ct = cor.test(X[,1], fitted(lm(X[,2] ~ X[,3]))) | |
# return values | |
c(ct$estimate, ct$conf.int) | |
}) | |
# Histogram of estimates | |
hist(res[1,]) | |
# Coverage of CIs | |
mean(res[2,]<0 & res[3,]>0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment