To start, source R functions and load the needed packages:

 source("R functions.R")
 library(lme4)
 library(maxLik)
 library(MASS) 
 library(RColorBrewer)

Data Simulation and Model Fitting

Based on the specified model parameters, count data under both the null hypothesis and the alternative hypothesis are simulated by assuming a BNB model. LRT and Wald test under the BNB model and LRT under Poisson-LMM model are performed.

 ## set seed
 set.seed(1415)
 ## sample size (number of subjects)
 nvec <- c(3,4,5,6,7,8,9,10,12,15,20)
 ## 20%, 50%, and 80% expression of normal samples
 muvec.all <- c(14,681,3022)
 ## 20%, 50%, and 80% dispersion
 dispersionvec.all <- c(0.07,0.2,1)
 ## select 20% expression of normal samples
 muvec <- muvec.all[1] 
 ## select 80% dispersion of all samples
 thetavec <- 1/dispersionvec.all[3] 
 ## fold in primary samples vs. normals
 foldvec <- c(0.5, 1, 2) 
 ## expected false positive rate
 alphavec <- c(0.01, 0.005, 0.001)
 ## number of simulations
 T <- 20000
 
 ## arrays for test statistics
 array0.LRT.BNB <- array(0,dim=c(T,length(nvec),length(muvec),length(foldvec),length(thetavec)))
 array0.Wald.BNB <- array(0,dim=c(T,length(nvec),length(muvec),length(foldvec),length(thetavec)))
 array0.LRT.Poisson.Glmm <- array(0,dim=c(T,length(nvec),length(muvec),length(foldvec),length(thetavec)))

 f <- "TestResult.Rda"
 if (file.exists(f)) { load(f) } else 
 {
   for (l in 1:length(thetavec))
   {
     s1 <- thetavec[l]
     for (c in 1:length(foldvec))
     {
       for (k in 1:length(muvec))
       {  
         mu0 <- muvec[k]
         mu1 <- mu0*foldvec[c]
         for (j in 1:length(nvec))
         {  
           n <- nvec[j]
           for (i in 1:T)
           {
             data0 <- simubnb(n,mu0,mu1,s1)
             x <- data0$count[data0$group==0]
             y <- data0$count[data0$group==1]
             temp1 <- try(Test.BNB(x,y), silent=TRUE)
             if (!inherits(temp1,"try-error")) 
             {
               array0.LRT.BNB[i,j,k,c,l] <- temp1$lrt
               array0.Wald.BNB[i,j,k,c,l] <- temp1$stat2
             }
             temp2 <- try(Test.Poisson.Glmm(data0), silent=TRUE)
             if (!inherits(temp2,"try-error")) array0.LRT.Poisson.Glmm[i,j,k,c,l] <- temp2$lrt
           }
         }
       }
     }
   }
   save(array0.LRT.BNB,array0.Wald.BNB,array0.LRT.Poisson.Glmm,file=f)
 }

Summary Statistics

False positive rates are calculated when the asymptotic Chi-square distribution is used to determine the critical values of the LRT and Wald tests.

To have a better false positive rate control than the usage of asypotic Chi-square distirbution under the null hypothesis, critical values are calculated by using the empirical parametric method.

Power is calculated under the alternative hypothesis by using the above critical values.

Plotting

False positive rate is plotted against sample size.

Power is plotted against sample size.