Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created May 8, 2021 10:54
Show Gist options
  • Save richarddmorey/c9585316266f432bebe2ea30314dba8b to your computer and use it in GitHub Desktop.
Save richarddmorey/c9585316266f432bebe2ea30314dba8b to your computer and use it in GitHub Desktop.
simulating correlations
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