# NOT RUN {
set.seed(1)
l = 1000 ## number of testing units
neach = 20 ## number of subjects in each group
pi0 = 0.8 ## null proportion
signal1 = rep(0, l)
signal2 = signal1
signal2[1:round((1-pi0)*l)] = rnorm(round((1-pi0)*l), mean = 0, sd = 3)
## I will generate the sigma^2 parameter
sigma2 = rep(1,l)
sigma = sqrt(sigma2)
## Then I can generate data
data1 <- data2 <- matrix(NA, nrow = l, ncol = neach)
for (i in 1:l) {
data1[i,] = rnorm(neach, mean = rep(signal1[i], each = neach), sd = sigma[i])
data2[i,] = rnorm(neach, mean = rep(signal2[i], each = neach), sd = sigma[i])
}
thetaHat = rowMeans(data2) - rowMeans(data1)
sd1 = apply(data1, 1, sd)
sd2 = apply(data2, 1, sd)
s2 = sd1^2/neach + sd2^2/neach
fit.EM = mixtwice(thetaHat, s2, Btheta = 15, Bsigma2 = 10, df = 2*neach - 2,
method = "EM-pava", maxit = 100, prop = 1)
## you can try to visualize the result
plot(fit.EM$grid.theta, cumsum(fit.EM$mix.theta), type = "s",
xlab = "grid.theta", ylab = "ecdf of theta", lwd = 2)
lines(ecdf(signal2 - signal1),cex = 0.1, lwd = 0.5, lty = 2, col = "red")
legend("topleft", legend = c("fit.mix", "true.mix"), col = c("black", "red"),
lwd = 1, pch = 19)
## calculate false discovery rate and true positive under 0.05
oo = order(fit.EM$lfdr)
# number of discovery
x1 = sum(cumsum(fit.EM$lfdr[oo])/c(1:l) <= 0.05)
# number of true discovery
x2 = sum(cumsum(fit.EM$lfdr[oo])/c(1:l) <= 0.05 & (signal2 != 0)[oo])
# number of real positive
x3 = sum(signal2 != 0)
# number of false discovery
x4 = sum(cumsum(fit.EM$lfdr[oo])/c(1:l) <= 0.05 & (signal2 == 0)[oo])
x4/x1 ## false discovery rate FDR
x2/x3 ## true positive rate
## null proportion estimation
max(fit.EM$mix.theta)
## you can also try using another method (Augmented Lagrangian), the result would be similar
# fit.AugLag = mixtwice2(thetaHat, s2, Btheta = 15, Bsigma2 = 10, df = 2*neach - 2,
# method = "AugLag", prop = 1)
# }
Run the code above in your browser using DataLab