Last updated: 2022-12-05
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 e9b7f51. 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: figure/
Untracked: output/poisson_mean_simulation/
Untracked: output/poisson_smooth_simulation/
Unstaged changes:
Modified: analysis/normal_mean_penalty_glm_simplified.Rmd
Modified: analysis/trendfiltering.ipynb
Modified: code/poisson_mean/simulation_summary.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/NB_PLN_mean_var.Rmd
) and
HTML (docs/NB_PLN_mean_var.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 |
---|---|---|---|---|
html | e9b7f51 | DongyueXie | 2022-12-05 | Build site. |
Rmd | 3e9e15d | DongyueXie | 2022-12-05 | wflow_publish("analysis/NB_PLN_mean_var.Rmd") |
html | 923ff9b | DongyueXie | 2022-12-04 | Build site. |
Rmd | 4ef5950 | DongyueXie | 2022-12-04 | wflow_publish("analysis/NB_PLN_mean_var.Rmd") |
The mean-var parameterization of Negative Binomial distribution is mean=\(m=rp/(1-p)\), and var=\(m + \frac{1}{r}m^2 = (1+\frac{1}{r}m)m\). So if we want to generate some count data with mean \(\lambda\) and variance being say \(\phi\) times the mean, we can set \(r = \lambda/(\phi-1)\).
The mean-var relationship in a Poisson log normal model is mean=\(m=e^{\mu+\sigma^2/2}\) and var=\(m+(e^{\sigma^2}-1)e^{2\mu+\sigma^2} = (1+(e^{\sigma^2}-1)m)m\). So given \(\mu\), if we want to generate some count data with mean \(\lambda\) and variance being say \(\phi\) times the mean, we can set \(\sigma^2 = \log(1+\frac{\phi-1}{\lambda})\). (\(\phi>1\)). However \(\lambda\) itself is a function of \(\sigma^2\), so we can solve \((e^{\sigma^2}-1)e^{0.5\sigma^2} = (\phi-1)e^{-\mu}\) for \(\sigma^2\). Further assume \(\sigma^2 = \log(1+\alpha),\alpha>0\), then we solve \(\alpha^3+\alpha^2 - d^2 = 0\) where \(d = (\phi-1)e^{-\mu}\). The wolframalpha gives the following solution:
cubsolver = function(a){
1/3*(((27*a)/2 + 3/2*sqrt(3)*sqrt(a*(27*a - 4)) - 1)^(1/3) + 1/((27*a)/2 + 3/2*sqrt(3)*sqrt(a*(27*a - 4)) - 1)^(1/3) - 1)
}
Lets verify it using polyroot
when \(d^2=1\), corresponding to \(\phi=2,\mu=0\).
cubsolver(1)
[1] 0.7548777
polyroot(c(-1,0,1,1))
[1] 0.7548777+0.0000000i -0.8774388+0.7448618i -0.8774388-0.7448618i
So in this case the \(sigma^2\) is
log(1+cubsolver(1))
[1] 0.5623991
However the solution from wolframalpha seems to only support \(27*d^2-4 > 0\)?
So we can simply use polyroot
then. The following
function takes input of \(\mu,\phi\)
and return the \(\sigma^2\):
calc_var_PLN = function(mu,phi){
a = ((phi-1)*exp(-mu))^2
return(log(1+Re(polyroot(c(-a,0,1,1))[1])))
}
calc_var_PLN(-5,2)
[1] 3.356975
calc_var_PLN(5,2)
[1] 0.006692988
So for both cases, fixing \(\phi\), if the mean is large, the extra variance from the over-dispersion parameter(\(r\) in NB, \(\sigma^2\) in PLN) is small. This not too surprising for example in PLN, if \(\mu\) is small, then a lot of observations are 0 so in order to make variance larger, \(\sigma^2\) has to be larger.
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] workflowr_1.7.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 compiler_4.2.1 pillar_1.8.1 bslib_0.4.0
[5] later_1.3.0 git2r_0.30.1 jquerylib_0.1.4 tools_4.2.1
[9] getPass_0.2-2 digest_0.6.29 jsonlite_1.8.2 evaluate_0.17
[13] tibble_3.1.8 lifecycle_1.0.3 pkgconfig_2.0.3 rlang_1.0.6
[17] cli_3.4.1 rstudioapi_0.14 yaml_2.3.5 xfun_0.33
[21] fastmap_1.1.0 httr_1.4.4 stringr_1.4.1 knitr_1.40
[25] fs_1.5.2 vctrs_0.4.2 sass_0.4.2 rprojroot_2.0.3
[29] glue_1.6.2 R6_2.5.1 processx_3.7.0 fansi_1.0.3
[33] rmarkdown_2.17 callr_3.7.2 magrittr_2.0.3 whisker_0.4
[37] ps_1.7.1 promises_1.2.0.1 htmltools_0.5.3 httpuv_1.6.6
[41] utf8_1.2.2 stringi_1.7.8 cachem_1.0.6