Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tmalsburg/binomialcris

R package with functions for calculating and plotting binomial credible intervals
https://github.com/tmalsburg/binomialcris

bayesian r statistics

Last synced: 27 days ago
JSON representation

R package with functions for calculating and plotting binomial credible intervals

Awesome Lists containing this project

README

        

#+PROPERTY: header-args:R :session *R*

An R package for calculating and plotting binomial credible intervals. It was written for educational purposes and not necessarily for serious use.

* Installation
#+BEGIN_SRC R :exports both :results value output
devtools::install_github("tmalsburg/binomialCRIs")
#+END_SRC

* Usage examples
Note: When plotting to PDF, use the function ~cairo_pdf()~ instead of ~pdf()~ to get Greek characters in the results.

50% credible intervals after seeing 6 successes out of 9 trials, with flat prior:

#+BEGIN_SRC R :exports both :results value output
library(binomialCRIs)

binomial_hpdi(n_successes=6, n_trials=9, prob=0.5, prior_shape1=1, prior_shape2=1)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.5643603 0.7593849

#+BEGIN_SRC R :exports both :results output graphics :file plots/hpdi1.png
plot_binomial_hpdi(6, 9, 0.5)
#+END_SRC

#+RESULTS:
[[file:plots/hpdi1.png]]

#+BEGIN_SRC R :exports both :results value output
binomial_pi(6, 9, 0.5)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.5423038 0.7391494

#+BEGIN_SRC R :exports both :results output graphics :file plots/pi1.png
plot_binomial_pi(6, 9, 0.5)
#+END_SRC

#+RESULTS:
[[file:plots/pi1.png]]

50% credible intervals after seeing 6 successes out of 9 trials, with prior assuming one earlier success and one earlier failure:

#+BEGIN_SRC R :exports both :results value output
binomial_hpdi(6, 9, 0.5, 2, 2)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.5419229 0.7242251

#+BEGIN_SRC R :exports both :results output graphics :file plots/hpdi2.png
plot_binomial_hpdi(6, 9, 0.5, 2, 2)
#+END_SRC

#+RESULTS:
[[file:plots/hpdi2.png]]

#+BEGIN_SRC R :exports both :results value output
binomial_pi(6, 9, 0.5, 2, 2)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.5269063 0.7101529

#+BEGIN_SRC R :exports both :results output graphics :file plots/pi2.png
plot_binomial_pi(6, 9, 0.5, 2, 2)
#+END_SRC

#+RESULTS:
[[file:plots/pi2.png]]

50% credible intervals after seeing 7 successes our of 11 trials (equivalent to 6/9 with prior_shape1=2, beta=2):

#+BEGIN_SRC R :exports both :results value output
binomial_hpdi(7, 11, 0.5, 1, 1)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.5419229 0.7242251

#+BEGIN_SRC R :exports both :results output graphics :file plots/hpdi3.png
plot_binomial_hpdi(7, 11, 0.5)
#+END_SRC

#+RESULTS:
[[file:plots/hpdi3.png]]

#+BEGIN_SRC R :exports both :results value output
binomial_pi(7, 11, 0.5)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.5269063 0.7101529

#+BEGIN_SRC R :exports both :results output graphics :file plots/pi3.png
plot_binomial_pi(7, 11, 0.5)
#+END_SRC

#+RESULTS:
[[file:plots/pi3.png]]

50% credible intervals after seeing 1 successes out of 2 trials, with flat prior:

#+BEGIN_SRC R :exports both :results value output
binomial_hpdi(1, 1, 0.5)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.7070744 0.9999771

#+BEGIN_SRC R :exports both :results output graphics :file plots/hpdi4.png
plot_binomial_hpdi(1, 1, 0.5)
#+END_SRC

#+RESULTS:
[[file:plots/hpdi4.png]]

#+BEGIN_SRC R :exports both :results value output
binomial_pi(1, 1, 0.5)
#+END_SRC

#+RESULTS:
: |0.5 0.5|
: 0.5000000 0.8660254

#+BEGIN_SRC R :exports both :results output graphics :file plots/pi4.png
plot_binomial_pi(1, 1, 0.5)
#+END_SRC

#+RESULTS:
[[file:plots/pi4.png]]

Probability of parameter being larger than 0.5 after seeing 6 successes out of 9 trials with flat prior:

#+BEGIN_SRC R :exports both :results value output
binomial_prob(6, 9, 0.5)
#+END_SRC

#+RESULTS:
: [1] 0.828125

Probability of parameter being larger than 0.5 after seeing 6 successes out of 9 trials with prior assuming one earlier success and one earlier failure:

#+BEGIN_SRC R :exports both :results value output
binomial_prob(6, 9, 0.5, prior_shape1=2, prior_shape2=2)
#+END_SRC

#+RESULTS:
: [1] 0.8061523

Probability of parameter being smaller than 0.5 after seeing 6 successes out of 9 trials with prior assuming one earlier success and one earlier failure:

#+BEGIN_SRC R :exports both :results value output
binomial_prob(6, 9, prob_upper=0.5, prior_shape1=2, prior_shape2=2)
#+END_SRC

#+RESULTS:
: [1] 0.1938477

Probability of parameter being larger than 0.5 and smaller than 0.75 after seeing 6 successes our of 9 trials:

#+BEGIN_SRC R :exports both :results value output
binomial_prob(6, 9, 0.5, 0.75)
#+END_SRC

#+RESULTS:
: [1] 0.6040001

Plot the same interval:

#+BEGIN_SRC R :exports both :results output graphics :file plots/some_interval.png
plot_binomial_cri(6, 9, 0.5, 0.75)
#+END_SRC

#+RESULTS:
[[file:plots/some_interval.png]]

Probability of parameter being smaller than 0.5 after seeing 6 successes our of 9 trials:

#+BEGIN_SRC R :exports both :results value output
binomial_prob(6, 9, prob_upper=0.5)
#+END_SRC

#+RESULTS:
: [1] 0.171875

Plot:

#+BEGIN_SRC R :exports both :results output graphics :file plots/some_interval2.png
plot_binomial_cri(6, 9, prob_upper=0.5)
#+END_SRC

#+RESULTS:
[[file:plots/some_interval2.png]]