|
library(pwr) |
|
#library(mvtnorm) |
|
library(parallel) |
|
library(MASS) |
|
n_samples = 300 |
|
|
|
set.seed(42) |
|
stouffer_liptak = function(pvalues, adjust=FALSE, lower.tail=TRUE){ |
|
qvalues = qnorm(pvalues, mean=0, sd=1, lower.tail=lower.tail) |
|
sigma = adjust |
|
C = chol(sigma) |
|
Cm1 = solve(C) # C^-1 |
|
qvalues = Cm1 %*% qvalues # Qstar = C^-1 * Q |
|
Cp = sum(qvalues) / sqrt(length(qvalues)) |
|
pnorm(Cp, mean=0, sd=1, lower.tail=lower.tail) |
|
} |
|
|
|
cor.val = 0.2 |
|
|
|
gen.correlated = function(rho, nrow=100, ncol=2, mean=0, sd=1){ |
|
X = matrix(rnorm(nrow * ncol, mean=mean, sd=sd), nrow=nrow) |
|
sigma = diag(ncol) |
|
sigma <- rho ^ abs(row(sigma)-col(sigma)) |
|
X %*% chol(sigma) |
|
} |
|
|
|
expr_means = c(0.035145, 0.038902, 0.044048, 0.050961) |
|
#expr_means = c( 0.038902, 0.044048, 0.050961) |
|
expr_sd50 = 0.16183939707577316 |
|
|
|
meth_means = c(0.004042, 0.005181, 0.007036, 0.010723) |
|
#meth_means = c( 0.005181, 0.007036, 0.010723) |
|
meth_sd50 = 0.014325652249100367 |
|
|
|
|
|
cc = c(rep(1, n_samples / 2), rep(0, n_samples / 2)) |
|
|
|
sigma.base = matrix(c(1, 0.01, |
|
0.01, 1), nrow=2) |
|
sigma.meth = matrix(c(1, cor.val, |
|
cor.val, 1), nrow=2) |
|
|
|
n_sims = 10000 |
|
ei = 1 |
|
for (expr_mean in expr_means){ |
|
mi = 1 |
|
row = c() |
|
for (meth_mean in meth_means){ |
|
|
|
sigma = sigma.base |
|
|
|
detected = unlist(mclapply(1:n_sims, function(i){ |
|
|
|
# we have 4 time-points, so we simulate 4 p-values |
|
time.ps = unlist(lapply(1:4, function(i){ |
|
simmed.asthma.meth = gen.correlated(cor.val, nrow=n_samples/2, ncol=4, mean=0, sd=meth_sd50) |
|
simmed.control.meth = gen.correlated(cor.val, nrow=n_samples/2, ncol=4, mean=meth_mean, sd=meth_sd50) |
|
|
|
simmed.asthma.expr = rnorm(n_samples/2, expr_mean, sd=expr_sd50) |
|
simmed.control.expr = rnorm(n_samples/2, 0, sd=expr_sd50) |
|
|
|
simmed.asthma = cbind(simmed.asthma.meth, simmed.asthma.expr) |
|
simmed.control = cbind(simmed.control.meth, simmed.control.expr) |
|
|
|
simmed = rbind(simmed.asthma, simmed.control) |
|
|
|
|
|
#colnames(simmed) = c("methylation", "expression") |
|
mp1 = t.test(simmed[,1] ~ cc)$p.value |
|
mp2 = t.test(simmed[,2] ~ cc)$p.value |
|
mp3 = t.test(simmed[,3] ~ cc)$p.value |
|
mp4 = t.test(simmed[,4] ~ cc)$p.value |
|
|
|
sigma.m = cor(simmed[,1:4]) |
|
|
|
ep = t.test(simmed[,ncol(simmed)] ~ cc)$p.value |
|
|
|
mp = stouffer_liptak(c(mp1, mp2, mp3, mp4), sigma.m) |
|
#mp = stouffer_liptak(c(mp, mp4), sigma.meth) |
|
combined.p = stouffer_liptak(c(ep, mp), sigma.base) |
|
#write(sprintf("%.4g\t%.4g\t%.4g", mp, ep, combined.p), stdout()) |
|
combined.p |
|
})) |
|
# detected means at least 1 probe with p < 2.5e-6 and another with at least 1e-4 |
|
return(sum((time.ps < 2.5e-6) > 0) & (sum(time.ps < 1e-5) > 1)) |
|
}, mc.cores=12)) |
|
|
|
|
|
row = c(row, (sum(detected) / n_sims)) |
|
mi = mi + 1 |
|
} |
|
write(paste(row, collapse="\t"), stdout()) |
|
ei = ei + 1 |
|
} |
|
|
|
if(FALSE){ |
|
pvals = unlist(lapply(1:1000, function(i){ |
|
asthma = rnorm(n_samples / 2, mean=0, sd=0.0326) |
|
ctrl = rnorm(n_samples / 2, mean=0.0107, sd=0.0326) |
|
t.test(asthma, ctrl)$p.value |
|
})) |
|
print(sum(pvals < 0.05) / length(pvals)) |
|
} |
brentp, can you please edit your answer. Seems that something went wrong.
updated. thanks for letting me know.