Last updated: 2022-12-08

Checks: 7 0

Knit directory: gsmash/

This reproducible R Markdown analysis was created with workflowr (version 1.7.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(20220606) 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 results in this page were generated with repository version 6739f3c. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

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/
    Ignored:    data/poisson_mean_simulation/

Untracked files:
    Untracked:  Rplot.png
    Untracked:  data/real_data_singlecell/
    Untracked:  figure/
    Untracked:  output/poisson_MF_simulation/
    Untracked:  output/poisson_mean_simulation/
    Untracked:  output/poisson_smooth_simulation/

Unstaged changes:
    Modified:   analysis/index.Rmd
    Modified:   analysis/run_PMF_on_pbmc.Rmd
    Modified:   code/poisson_STM/real_PMF.R

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 repository in which changes were made to the R Markdown (analysis/library_size_distribution.Rmd) and HTML (docs/library_size_distribution.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 6739f3c DongyueXie 2022-12-08 wflow_publish("analysis/library_size_distribution.Rmd")
html 9259fc2 DongyueXie 2022-12-08 Build site.
Rmd b446697 DongyueXie 2022-12-08 wflow_publish("analysis/library_size_distribution.Rmd")

Introduction

I look at the distribution of single cell library size on two datasets. The main purpose is to learn a distribution of library size and generate data from it.

So to make it scale with the number of genes, the library size in the following is divided by the number of genes.

The pbmc dataset.

library(fastTopics)
library(Matrix)
data("pbmc_facs")
s = rowSums(pbmc_facs$counts)/ncol(pbmc_facs$counts)
hist(s,breaks = 100,main='pbmc data library size',probability = T)

Version Author Date
9259fc2 DongyueXie 2022-12-08

Fit a gamma distribution on \(s\), using method of moment.

rate = mean(s)/var(s)
shape = mean(s)*rate
rate
[1] 16.16446
shape
[1] 1.913245
hist(rgamma(1e5,shape=shape,rate=rate),breaks = 100,main='simulated library size',probability = T)

Version Author Date
9259fc2 DongyueXie 2022-12-08

Fit a inverse gamma distribution on \(s\), using method of moment.

library(invgamma)
shape = 2 + mean(s)^2/var(s)
rate = (shape-1)*mean(s)
shape
[1] 3.913245
rate
[1] 0.3448151
dd = rinvgamma(1e5,shape=shape,rate=rate)
hist(dd,breaks = 100,main='simulated library size',probability = T)

Version Author Date
9259fc2 DongyueXie 2022-12-08
hist(dd[dd<max(s)],breaks = 100,main='simulated library size',probability = T)

droplet data

load('data/real_data_singlecell/droplet.RData')
s = rowSums(counts)/ncol(counts)
hist(s,breaks = 100,main='droplet data library size',probability = T)

Version Author Date
9259fc2 DongyueXie 2022-12-08

Fit a gamma distribution on \(s\), using method of moment.

rate = mean(s)/var(s)
shape = mean(s)*rate
rate
[1] 6.503457
shape
[1] 2.258288
hist(rgamma(1e5,shape=shape,rate=rate),breaks = 100,main='simulated library size',probability = T)

Version Author Date
9259fc2 DongyueXie 2022-12-08

Fit a inverse gamma distribution on \(s\), using method of moment.

library(invgamma)
shape = 2 + mean(s)^2/var(s)
rate = (shape-1)*mean(s)
shape
[1] 4.258288
rate
[1] 1.131422
dd = rinvgamma(1e5,shape=shape,rate=rate)
hist(rinvgamma(1e5,shape=shape,rate=rate),breaks = 100,main='simulated library size',probability = T)

Version Author Date
9259fc2 DongyueXie 2022-12-08
hist(dd[dd<max(s)],breaks = 100,main='simulated library size',probability = T)

Version Author Date
9259fc2 DongyueXie 2022-12-08

Conclusion

Gamma distribution seems not able to capture the peak of library size distribution. Inverse-gamma has heavier tails. I’ll use inverse-Gamma, but set a maximum library size(truncate it).


sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

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

other attached packages:
[1] invgamma_1.1       Matrix_1.5-1       fastTopics_0.6-142 workflowr_1.7.0   

loaded via a namespace (and not attached):
 [1] mcmc_0.9-7         fs_1.5.2           progress_1.2.2     httr_1.4.4        
 [5] rprojroot_2.0.3    tools_4.2.1        bslib_0.4.0        utf8_1.2.2        
 [9] R6_2.5.1           irlba_2.3.5.1      uwot_0.1.14        DBI_1.1.3         
[13] lazyeval_0.2.2     colorspace_2.0-3   tidyselect_1.2.0   prettyunits_1.1.1 
[17] processx_3.7.0     compiler_4.2.1     git2r_0.30.1       cli_3.4.1         
[21] quantreg_5.94      SparseM_1.81       plotly_4.10.1      sass_0.4.2        
[25] scales_1.2.1       SQUAREM_2021.1     quadprog_1.5-8     callr_3.7.2       
[29] pbapply_1.6-0      mixsqp_0.3-48      stringr_1.4.1      digest_0.6.29     
[33] rmarkdown_2.17     MCMCpack_1.6-3     pkgconfig_2.0.3    htmltools_0.5.3   
[37] highr_0.9          fastmap_1.1.0      htmlwidgets_1.5.4  rlang_1.0.6       
[41] rstudioapi_0.14    jquerylib_0.1.4    generics_0.1.3     jsonlite_1.8.2    
[45] dplyr_1.0.10       magrittr_2.0.3     Rcpp_1.0.9         munsell_0.5.0     
[49] fansi_1.0.3        lifecycle_1.0.3    stringi_1.7.8      whisker_0.4       
[53] yaml_2.3.5         MASS_7.3-58        Rtsne_0.16         grid_4.2.1        
[57] parallel_4.2.1     promises_1.2.0.1   ggrepel_0.9.2      crayon_1.5.2      
[61] lattice_0.20-45    cowplot_1.1.1      splines_4.2.1      hms_1.1.2         
[65] knitr_1.40         ps_1.7.1           pillar_1.8.1       glue_1.6.2        
[69] evaluate_0.17      getPass_0.2-2      data.table_1.14.6  RcppParallel_5.1.5
[73] vctrs_0.4.2        httpuv_1.6.6       MatrixModels_0.5-1 gtable_0.3.1      
[77] purrr_0.3.5        tidyr_1.2.1        assertthat_0.2.1   ashr_2.2-54       
[81] cachem_1.0.6       ggplot2_3.3.6      xfun_0.33          coda_0.19-4       
[85] later_1.3.0        survival_3.4-0     viridisLite_0.4.1  truncnorm_1.0-8   
[89] tibble_3.1.8       ellipsis_0.3.2