Last updated: 2020-03-31

Checks: 7 0

Knit directory: misc/

This reproducible R Markdown analysis was created with workflowr (version 1.6.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20191122) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Untracked files:
    Untracked:  analysis/contrainedclustering.Rmd
    Untracked:  analysis/gsea.Rmd
    Untracked:  analysis/ideas.Rmd
    Untracked:  analysis/methylation.Rmd
    Untracked:  analysis/susie.Rmd
    Untracked:  code/sccytokines.R
    Untracked:  code/scdeCalibration.R
    Untracked:  data/bart/
    Untracked:  data/cytokine/DE_controls_output_filter10.RData
    Untracked:  data/cytokine/DE_controls_output_filter10_addlimma.RData
    Untracked:  data/cytokine/README
    Untracked:  data/cytokine/test.RData
    Untracked:  data/cytokine_normalized.RData
    Untracked:  data/deconv/
    Untracked:  data/scde/

Unstaged changes:
    Modified:   analysis/binomthinultimate.Rmd
    Modified:   analysis/deconvolution.Rmd
    Modified:   analysis/index.Rmd
    Modified:   analysis/limma.Rmd
    Deleted:    data/mout_high.RData
    Deleted:    data/scCDT.RData
    Deleted:    data/sva_sva_high.RData

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd 4f066db DongyueXie 2020-03-31 wflow_publish(“analysis/scdeCheckAlpha.Rmd”)
html c0fdf5c DongyueXie 2020-03-19 Build site.
Rmd a7a0098 DongyueXie 2020-03-19 wflow_publish(“analysis/scdeCheckAlpha.Rmd”)

Compare the loglikelihood from sva-limma-ash, setting \(\alpha=0\) and \(\alpha=1\), on real data.

Dataset 1: PBMC

Summary

Even if we do not take log, \(\alpha=1\) still give larger loglikelihood.

library(vicar)
library(sva)
library(cate)
library(seqgendiff)
library(edgeR)
library(MASS)
load('data/scde/scCD4.RData')
load('data/scde/scCD8.RData')
load('data/scde/scCD14.RData')
load('data/scde/scMB.RData')

mglmfit = function(Y,X){
  G = nrow(Y)
  coefs = c()
  ses = c()
  for(i in 1:G){
    fit = glm(y~.,data.frame(y=Y[i,],x=X[,-1]),family = 'poisson')
    coefs = rbind(coefs,fit$coefficients)
    ses = rbind(ses,summary(fit)$coefficients[,2])
  }
  return(list(coefficients=coefs,se = ses))
}

CD4, CD8, CD14 and B cells. Look at number of genes and cells.

dim(CD4)
[1] 13713   709
dim(CD8)
[1] 13713   313
dim(CD14)
[1] 13713   432
dim(MB)
[1] 13713   342

CD4 vs MB cells.

Y = as.matrix(cbind(CD4,MB))

group_idx = c(rep(1,dim(CD4)[2]),rep(0,dim(MB)[2]))

# remove genes appearing in less than 10 cells

Y = Y[-which(rowSums(Y!=0)<10),]

X = model.matrix(~group_idx)

real_study = function(Y,X,run_sva_eb=TRUE,n.sv=NULL){
  
  if(run_sva_eb){
    sva_sva = sva((Y),mod=X,mod0=X[,1],n.sv = n.sv)
    X.sva <- cbind(X, sva_sva$sv)
    lmout <- limma::lmFit(object = (Y), design = X.sva)
    eout  <- limma::eBayes(lmout)
    svaout           <- list()
    svaout$betahat   <- lmout$coefficients[, 2]
    svaout$sebetahat <- lmout$stdev.unscaled[, 2] * sqrt(eout$s2.post)
    svaout$pvalues   <- eout$p.value[, 2]
    sva_limma_ash0 = ashr::ash(svaout$betahat,svaout$sebetahat,alpha=0)
    sva_limma_ash1 = ashr::ash(svaout$betahat,svaout$sebetahat,alpha=1)
    print(paste('sva-limma-ash0 loglik: ',round(sva_limma_ash0$loglik,2),';',
            'sva-limma-ash1 loglik: ',round(sva_limma_ash1$loglik)))
  }else{
    lmout <- limma::lmFit(object = (Y), design = X)
    out = list()
    out$betahat   <- lmout$coefficients[, 2]
    out$sebetahat <- lmout$stdev.unscaled[, 2] * lmout$sigma
    lm_ash0 = ashr::ash(out$betahat,out$sebetahat,alpha=0)
    lm_ash1 = ashr::ash(out$betahat,out$sebetahat,alpha=1)
    print(paste('lm-ash0 loglik: ',round(lm_ash0$loglik,2),';',
            'lm-ash1 loglik: ',round(lm_ash1$loglik)))
  }
}

real_study(Y,X,3)
Number of significant surrogate variables is:  3 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  16134.95 ; sva-limma-ash1 loglik:  17053"
real_study(Y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  16049.71 ; lm-ash1 loglik:  17018"

Take log1p of Y

real_study(log(Y+0.5),X,14)
Number of significant surrogate variables is:  14 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  16929.86 ; sva-limma-ash1 loglik:  17545"
real_study(log(Y+0.5),X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  16628.98 ; lm-ash1 loglik:  17339"

Voom-lm-ash

d = DGEList(Y)
d = calcNormFactors(d)
y = voom(d, design=X, plot = FALSE)
real_study(y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  2165.18 ; lm-ash1 loglik:  1471"

glm-ash

fit = mglmfit(Y,X)
glm_ash0 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=0)
glm_ash1 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=1)
print(paste('glm-ash0 loglik: ',round(glm_ash0$loglik,2),';',
            'glm-ash1 loglik: ',round(glm_ash1$loglik)))
[1] "glm-ash0 loglik:  -9342.16 ; glm-ash1 loglik:  -9895"

CD8 vs CD14 cells

Y = as.matrix(cbind(CD8,CD14))

group_idx = c(rep(1,dim(CD8)[2]),rep(0,dim(CD14)[2]))

# remove genes appearing in less than 10 cells

Y = Y[-which(rowSums(Y!=0)<10),]

X = model.matrix(~group_idx)
real_study(Y,X,3)
Number of significant surrogate variables is:  3 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  14146.63 ; sva-limma-ash1 loglik:  15014"
real_study(Y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  13372.89 ; lm-ash1 loglik:  14499"

Take log1p of Y

real_study(log(Y+0.5),X,27)
Number of significant surrogate variables is:  27 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  16140.94 ; sva-limma-ash1 loglik:  16406"
real_study(log(Y+0.5),X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  14028.98 ; lm-ash1 loglik:  14877"

Voom-lm-ash

d = DGEList(Y)
d = calcNormFactors(d)
y = voom(d, design=X, plot = FALSE)
real_study(y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  117.68 ; lm-ash1 loglik:  -1781"

glm-ash

fit = mglmfit(Y,X)
glm_ash0 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=0)
glm_ash1 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=1)
print(paste('glm-ash0 loglik: ',round(glm_ash0$loglik,2),';',
            'glm-ash1 loglik: ',round(glm_ash1$loglik)))
[1] "glm-ash0 loglik:  -7016.82 ; glm-ash1 loglik:  -8182"

Dataset 2: Jaitin et al. 2014

Taken from here

load('data/scde/MouseJaitinSpleen.rda')
suppressPackageStartupMessages(library(SummarizedExperiment))
suppressPackageStartupMessages(library(MultiAssayExperiment))
mat = exprs(MouseJaitinSpleen)
idx = which(MouseJaitinSpleen$ERCC_dilution=='2.50E-05')
cell_type = MouseJaitinSpleen$group_name[idx]
table(cell_type)
cell_type
                B cell                 CD11c+        CD11c+(2hr_LPS) 
                    48                   2112                   1536 
         CD8-CD4+ESAM+                CD8-pDC              CD8+CD86- 
                    96                     96                     96 
               CD8+pDC              GC B cell monocyte_or_neutrophil 
                    96                     48                     48 
               NK_cell             splenocyte 
                    48                     48 
mat = mat[,idx]

B cell and NK_cell

Y = as.matrix(cbind(mat[,which(cell_type=='B cell')],mat[,which(cell_type=='NK_cell')]))

group_idx = c(rep(1,dim(mat[,which(cell_type=='B cell')])[2]),
              rep(0,dim(mat[,which(cell_type=='NK_cell')])[2]))

# remove genes appearing in less than 10 cells

Y = Y[-which(rowSums(Y!=0)<10),]
rm.idx = which(colSums(Y)==0)
Y = Y[,-rm.idx]
group_idx = group_idx[-rm.idx]


X = model.matrix(~group_idx)
real_study(Y,X)
Number of significant surrogate variables is:  2 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  -372.28 ; sva-limma-ash1 loglik:  -106"
real_study(Y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  -388.83 ; lm-ash1 loglik:  -120"
# How about just run linear model and then ash?

X = model.matrix(~group_idx)

lmout <- limma::lmFit(object = (Y), design = X)

svaout           <- list()
svaout$betahat   <- lmout$coefficients[, 2]
svaout$sebetahat <- lmout$stdev.unscaled[, 2] * lmout$sigma
svaout$pvalues   <- eout$p.value[, 2]

# sva-limma-ash-alpha 0

sva_limma_ash0 = ashr::ash(svaout$betahat,svaout$sebetahat,alpha=0)
sva_limma_ash0$loglik

# sva-limma-ash-alpha 1

sva_limma_ash1 = ashr::ash(svaout$betahat,svaout$sebetahat,alpha=1)
sva_limma_ash1$loglik

Take log1p of y

real_study(log(Y+0.5),X)
Number of significant surrogate variables is:  15 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  304.17 ; sva-limma-ash1 loglik:  372"
real_study(log(Y+0.5),X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  -147.77 ; lm-ash1 loglik:  -24"

Voom-lm-ash

d = DGEList(Y)
d = calcNormFactors(d)
y = voom(d, design=X, plot = FALSE)
real_study(y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  -216.76 ; lm-ash1 loglik:  -173"

glm-ash

fit = mglmfit(Y,X)
glm_ash0 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=0)
glm_ash1 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=1)
print(paste('glm-ash0 loglik: ',round(glm_ash0$loglik,2),';',
            'glm-ash1 loglik: ',round(glm_ash1$loglik)))
[1] "glm-ash0 loglik:  -2043.3 ; glm-ash1 loglik:  -2080"

CD8-pDC and splenocyte

Y = as.matrix(cbind(mat[,which(cell_type=='CD8-pDC')],mat[,which(cell_type=='splenocyte')]))

group_idx = c(rep(1,dim(mat[,which(cell_type=='CD8-pDC')])[2]),
              rep(0,dim(mat[,which(cell_type=='splenocyte')])[2]))

# remove genes appearing in less than 10 cells

Y = Y[-which(rowSums(Y!=0)<10),]
rm.idx = which(colSums(Y)==0)
Y = Y[,-rm.idx]
group_idx = group_idx[-rm.idx]

X = model.matrix(~group_idx)
real_study(Y,X)
Number of significant surrogate variables is:  3 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  186.59 ; sva-limma-ash1 loglik:  539"
real_study(Y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  97.49 ; lm-ash1 loglik:  488"

Take log1p of y

real_study(log(Y+0.5),X)
Number of significant surrogate variables is:  24 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  927.11 ; sva-limma-ash1 loglik:  1011"
real_study(log(Y+0.5),X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  301.85 ; lm-ash1 loglik:  519"

Voom-lm-ash

d = DGEList(Y)
d = calcNormFactors(d)
y = voom(d, design=X, plot = FALSE)
real_study(y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  -122.92 ; lm-ash1 loglik:  -12"

glm-ash

fit = mglmfit(Y,X)
glm_ash0 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=0)
glm_ash1 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=1)
print(paste('glm-ash0 loglik: ',round(glm_ash0$loglik,2),';',
            'glm-ash1 loglik: ',round(glm_ash1$loglik)))
[1] "glm-ash0 loglik:  -3605.35 ; glm-ash1 loglik:  -3773"

Dataset3: Grun et al 2016

Taken here

datax=readRDS("data/scde/GSE81076-GPL18573.rds")
datax_gene = experiments(datax)[["gene"]]
mat = (assays(datax_gene)[["count"]])
cell63_idx = 1:96
TGFBR3_idx = 673:768
Y = cbind(mat[,cell63_idx],mat[,TGFBR3_idx])
group_idx = c(rep(1,length(cell63_idx)),
              rep(0,length(TGFBR3_idx)))
Y = Y[-which(rowSums(Y!=0)<10),]

X = model.matrix(~group_idx)
real_study(Y,X)
Number of significant surrogate variables is:  2 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  -5114.92 ; sva-limma-ash1 loglik:  -2264"
real_study(Y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  -5100.36 ; lm-ash1 loglik:  -2259"

Take log1p of y

real_study(log(Y+0.5),X)
Number of significant surrogate variables is:  30 
Iteration (out of 5 ):1  2  3  4  5  [1] "sva-limma-ash0 loglik:  2766.62 ; sva-limma-ash1 loglik:  3803"
real_study(log(Y+0.5),X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  160.62 ; lm-ash1 loglik:  1706"

Voom-lm-ash

d = DGEList(Y)
d = calcNormFactors(d)
y = voom(d, design=X, plot = FALSE)
real_study(y,X,run_sva_eb = FALSE)
[1] "lm-ash0 loglik:  -7375.07 ; lm-ash1 loglik:  -7302"

glm-ash

fit = mglmfit(Y,X)
glm_ash0 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=0)
glm_ash1 = ashr::ash(fit$coefficients[,2],fit$se[,2],alpha=1)
print(paste('glm-ash0 loglik: ',round(glm_ash0$loglik,2),';',
            'glm-ash1 loglik: ',round(glm_ash1$loglik)))
[1] "glm-ash0 loglik:  -15292.75 ; glm-ash1 loglik:  -16158"

Also compare log-limma-ash, voom-limma-ash and glm-ash on simulated and real data.


sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Scientific Linux 7.4 (Nitrogen)

Matrix products: default
BLAS/LAPACK: /software/openblas-0.2.19-el7-x86_64/lib/libopenblas_haswellp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] MultiAssayExperiment_1.8.3  SummarizedExperiment_1.12.0
 [3] DelayedArray_0.8.0          matrixStats_0.54.0         
 [5] Biobase_2.42.0              GenomicRanges_1.34.0       
 [7] GenomeInfoDb_1.18.1         IRanges_2.16.0             
 [9] S4Vectors_0.20.1            BiocGenerics_0.28.0        
[11] MASS_7.3-51.1               edgeR_3.24.0               
[13] limma_3.38.2                seqgendiff_1.2.1           
[15] cate_1.0.4                  sva_3.30.0                 
[17] BiocParallel_1.16.0         genefilter_1.64.0          
[19] mgcv_1.8-25                 nlme_3.1-137               
[21] vicar_0.1-10               

loaded via a namespace (and not attached):
 [1] svd_0.4.1              bitops_1.0-6           fs_1.3.1              
 [4] bit64_0.9-7            doParallel_1.0.14      rprojroot_1.3-2       
 [7] tools_3.5.1            backports_1.1.2        R6_2.3.0              
[10] DBI_1.0.0              lazyeval_0.2.1         colorspace_1.3-2      
[13] tidyselect_0.2.5       gridExtra_2.3          bit_1.1-14            
[16] compiler_3.5.1         git2r_0.26.1           scales_1.0.0          
[19] SQUAREM_2017.10-1      mixsqp_0.2-2           stringr_1.3.1         
[22] esaBcv_1.2.1           digest_0.6.18          rmarkdown_1.10        
[25] XVector_0.22.0         pscl_1.5.2             pkgconfig_2.0.2       
[28] htmltools_0.3.6        ruv_0.9.7              rlang_0.4.0           
[31] RSQLite_2.1.1          dplyr_0.8.0.1          leapp_1.2             
[34] RCurl_1.95-4.11        magrittr_1.5           GenomeInfoDbData_1.2.0
[37] Matrix_1.2-15          Rcpp_1.0.2             munsell_0.5.0         
[40] stringi_1.2.4          whisker_0.3-2          yaml_2.2.0            
[43] zlibbioc_1.28.0        plyr_1.8.4             grid_3.5.1            
[46] blob_1.1.1             promises_1.0.1         crayon_1.3.4          
[49] lattice_0.20-38        splines_3.5.1          annotate_1.60.0       
[52] locfit_1.5-9.1         knitr_1.20             pillar_1.3.1          
[55] corpcor_1.6.9          codetools_0.2-15       XML_3.98-1.16         
[58] glue_1.3.0             evaluate_0.12          httpuv_1.4.5          
[61] foreach_1.4.4          gtable_0.2.0           purrr_0.3.2           
[64] assertthat_0.2.0       ashr_2.2-39            ggplot2_3.1.1         
[67] xtable_1.8-3           later_0.7.5            survival_2.43-1       
[70] truncnorm_1.0-8        tibble_2.1.1           iterators_1.0.10      
[73] AnnotationDbi_1.44.0   memoise_1.1.0          workflowr_1.6.0