All Projects → easystats → Bayestestr

easystats / Bayestestr

Licence: gpl-3.0
👻 Utilities for analyzing Bayesian models and posterior distributions

Programming Languages

r
7636 projects

Projects that are alternatives of or similar to Bayestestr

Votemapswitzerland
A Swiss version of the famous visualization «Land doesn't vote, people do.»
Stars: ✭ 150 (-56.65%)
Mutual labels:  rstats, map
Report
📜 🎉 Automated reporting of objects in R
Stars: ✭ 348 (+0.58%)
Mutual labels:  bayesian, rstats
BayesHMM
Full Bayesian Inference for Hidden Markov Models
Stars: ✭ 35 (-89.88%)
Mutual labels:  rstats, bayesian
Ggextra
📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements
Stars: ✭ 299 (-13.58%)
Mutual labels:  rstats
Ggpage
Creates Page Layout Visualizations in R 📄📄📄
Stars: ✭ 306 (-11.56%)
Mutual labels:  rstats
Home Assistant Custom Components Xiaomi Cloud Map Extractor
This custom integration provides a way to present a live view of a map for a Xiaomi (and Roborock) vacuums.
Stars: ✭ 318 (-8.09%)
Mutual labels:  map
Fmap
宇行地图:高仿高德地图Android客户端(持续更新中...)
Stars: ✭ 347 (+0.29%)
Mutual labels:  map
Wgs2mars.js
地球坐标系 (WGS-84)转火星坐标系 (GCJ-02)地图纠偏算法Javascript版
Stars: ✭ 297 (-14.16%)
Mutual labels:  map
Xaringanthemer
😎 Give your xaringan slides some style
Stars: ✭ 337 (-2.6%)
Mutual labels:  rstats
Hotels
酒店预订App
Stars: ✭ 323 (-6.65%)
Mutual labels:  map
Deeplearning
Python for《Deep Learning》,该书为《深度学习》(花书) 数学推导、原理剖析与源码级别代码实现
Stars: ✭ 4,020 (+1061.85%)
Mutual labels:  bayesian
Lbsmap
利用百度地图快速实现支付宝的“到位”功能。基于LBS精确获取地理位置的周边数据,地图动态显示网络图标 支持多点聚合与分离,点击图标动态放大选中效果,移动、放大、缩小地图后重新更新数据,聚合图标点击动画展开,根据实际距离调整地图层级为屏幕大小 ,分页刷新,简书:
Stars: ✭ 313 (-9.54%)
Mutual labels:  map
Ggsignif
Easily add significance brackets to your ggplots
Stars: ✭ 322 (-6.94%)
Mutual labels:  rstats
Maptalks.js
A light and plugable JavaScript library for integrated 2D/3D maps.
Stars: ✭ 3,377 (+876.01%)
Mutual labels:  map
Bayadera
High-performance Bayesian Data Analysis on the GPU in Clojure
Stars: ✭ 342 (-1.16%)
Mutual labels:  bayesian
Leaflet.markercluster
Marker Clustering plugin for Leaflet
Stars: ✭ 3,305 (+855.2%)
Mutual labels:  map
Mbxmapkit
DEPRECATED - Lightweight Mapbox integration with MapKit on iOS
Stars: ✭ 332 (-4.05%)
Mutual labels:  map
Ggdag
↙️ ↘️ An R package for working with causal directed acyclic graphs (DAGs)
Stars: ✭ 317 (-8.38%)
Mutual labels:  rstats
Roguesharp
A .NET Standard class library providing map generation, path-finding, and field-of-view utilities frequently used in roguelikes or 2D tile based games. Inspired by libtcod
Stars: ✭ 316 (-8.67%)
Mutual labels:  map
Trustroots
Travellers' community for sharing, hosting and getting people together.
Stars: ✭ 321 (-7.23%)
Mutual labels:  map

bayestestR

DOI downloads total

Become a Bayesian master you will

Existing R packages allow users to easily fit a large variety of models and extract and visualize the posterior draws. However, most of these packages only return a limited set of indices (e.g., point-estimates and CIs). bayestestR provides a comprehensive and consistent set of functions to analyze and describe posterior distributions generated by a variety of models objects, including popular modeling packages such as rstanarm, brms or BayesFactor.

You can reference the package and its documentation as follows:

  • Makowski, D., Ben-Shachar, M. S., & Lüdecke, D. (2019). bayestestR: Describing Effects and their Uncertainty, Existence and Significance within the Bayesian Framework. Journal of Open Source Software, 4(40), 1541. 10.21105/joss.01541
  • Makowski, D., Ben-Shachar, M. S., Chen, S. H. A., & Lüdecke, D. (2019). Indices of Effect Existence and Significance in the Bayesian Framework. Frontiers in Psychology 2019;10:2767. 10.3389/fpsyg.2019.02767

Installation

CRAN Tests

Run the following to install the stable release of bayestestR from CRAN:

install.packages("bayestestR")

Or this one to install the latest development version:

install.packages("remotes")
remotes::install_github("easystats/bayestestR")

Documentation

Documentation Blog Features

Click on the buttons above to access the package documentation and the easystats blog, and check-out these vignettes:

Tutorials

Articles

Features

In the Bayesian framework, parameters are estimated in a probabilistic fashion as distributions. These distributions can be summarised and described by reporting 4 types of indices:

describe_posterior() is the master function with which you can compute all of the indices cited below at once.

describe_posterior(
  rnorm(10000),
  centrality = "median",
  test = c("p_direction", "p_significance")
)
## Summary of Posterior Distribution
## 
## Parameter |   Median |        89% CI |     pd |   ps
## ----------------------------------------------------
## Posterior | 9.87e-03 | [-1.64, 1.54] | 50.39% | 0.46

describe_posterior() works for many objects, including more complex brmsfit-models. For better readability, the output is separated by model components:

zinb <- read.csv("http://stats.idre.ucla.edu/stat/data/fish.csv")
set.seed(123)
model <- brm(
  bf(
    count ~ child + camper + (1 | persons), 
    zi ~ child + camper + (1 | persons)
  ),
  data = zinb,
  family = zero_inflated_poisson(),
  chains = 1,
  iter = 500
)

describe_posterior(
  model,
  effects = "all",
  component = "all",
  test = c("p_direction", "p_significance"),
  centrality = "all"
)
## Summary of Posterior Distribution
## 
## Parameter   | Median |  Mean |   MAP |         89% CI |     pd |   ps |  Rhat |    ESS
## --------------------------------------------------------------------------------------
## (Intercept) |   0.96 |  0.96 |  0.96 | [-0.34,  2.29] | 90.00% | 0.88 | 1.011 | 109.68
## child       |  -1.16 | -1.16 | -1.16 | [-1.32, -1.00] |   100% | 1.00 | 0.996 | 278.16
## camper      |   0.73 |  0.72 |  0.73 | [ 0.57,  0.87] |   100% | 1.00 | 0.996 | 271.17
## 
## # Fixed effects (zero-inflated)
## 
## Parameter   | Median |  Mean |   MAP |         89% CI |     pd |   ps |  Rhat |    ESS
## --------------------------------------------------------------------------------------
## (Intercept) |  -0.48 | -0.51 | -0.22 | [-1.69,  0.43] | 78.00% | 0.73 | 0.997 | 137.70
## child       |   1.85 |  1.86 |  1.81 | [ 1.37,  2.47] |   100% | 1.00 | 0.996 | 302.74
## camper      |  -0.88 | -0.86 | -0.99 | [-1.47, -0.22] | 98.40% | 0.96 | 0.996 | 292.50
## 
## # Random effects (conditional) Intercept: persons
## 
## Parameter |    Median |  Mean |   MAP |         89% CI |     pd |   ps |  Rhat |    ESS
## ---------------------------------------------------------------------------------------
## persons.1 |     -0.99 | -1.01 | -0.84 | [-2.25,  0.38] | 92.00% | 0.90 | 1.007 | 105.68
## persons.2 | -4.65e-03 | -0.04 |  0.03 | [-1.64,  0.95] | 50.00% | 0.45 | 1.013 | 108.94
## persons.3 |      0.69 |  0.66 |  0.69 | [-0.58,  2.08] | 79.60% | 0.78 | 1.010 | 113.79
## persons.4 |      1.57 |  1.56 |  1.56 | [ 0.09,  2.72] | 96.80% | 0.96 | 1.009 | 113.68
## 
## # Random effects (zero-inflated) Intercept: persons
## 
## Parameter | Median |  Mean |   MAP |         89% CI |     pd |   ps |  Rhat |    ESS
## ------------------------------------------------------------------------------------
## persons.1 |   1.10 |  1.11 |  1.08 | [-0.06,  2.07] | 94.80% | 0.93 | 0.997 | 166.26
## persons.2 |   0.18 |  0.18 |  0.22 | [-0.83,  1.12] | 63.20% | 0.54 | 0.996 | 153.96
## persons.3 |  -0.30 | -0.31 | -0.54 | [-1.44,  0.57] | 64.00% | 0.59 | 0.997 | 153.79
## persons.4 |  -1.45 | -1.46 | -1.44 | [-2.44, -0.09] | 98.00% | 0.97 | 1.000 | 189.30
## 
## # Random effects (conditional) SD/Cor: persons
## 
## Parameter   | Median | Mean |  MAP |         89% CI |   pd |   ps |  Rhat |    ESS
## ----------------------------------------------------------------------------------
## (Intercept) |   1.42 | 1.58 | 1.07 | [ 0.66,  2.49] | 100% | 1.00 | 1.010 | 126.41
## 
## # Random effects (zero-inflated) SD/Cor: persons
## 
## Parameter   | Median | Mean |  MAP |         89% CI |   pd |   ps |  Rhat |    ESS
## ----------------------------------------------------------------------------------
## (Intercept) |   1.30 | 1.49 | 0.99 | [ 0.61,  2.67] | 100% | 1.00 | 0.996 | 129.12

bayestestR also includes many other features useful for your Bayesian analsyes. Here are some more examples:

Point-estimates

library(bayestestR)

posterior <- distribution_gamma(10000, 1.5)  # Generate a skewed distribution
centrality <- point_estimate(posterior)  # Get indices of centrality
centrality
## Point Estimate
## 
## Median | Mean |  MAP
## --------------------
## 1.18   | 1.50 | 0.51

As for other easystats packages, plot() methods are available from the see package for many functions:

While the median and the mean are available through base R functions, map_estimate() in bayestestR can be used to directly find the Highest Maximum A Posteriori (MAP) estimate of a posterior, i.e., the value associated with the highest probability density (the “peak” of the posterior distribution). In other words, it is an estimation of the mode for continuous parameters.

Uncertainty (CI)

hdi() computes the Highest Density Interval (HDI) of a posterior distribution, i.e., the interval which contains all points within the interval have a higher probability density than points outside the interval. The HDI can be used in the context of Bayesian posterior characterization as Credible Interval (CI).

Unlike equal-tailed intervals (see eti()) that typically exclude 2.5% from each tail of the distribution, the HDI is not equal-tailed and therefore always includes the mode(s) of posterior distributions.

By default, hdi() returns the 89% intervals (ci = 0.89), deemed to be more stable than, for instance, 95% intervals. An effective sample size of at least 10.000 is recommended if 95% intervals should be computed (Kruschke, 2015). Moreover, 89 indicates the arbitrariness of interval limits - its only remarkable property is being the highest prime number that does not exceed the already unstable 95% threshold (McElreath, 2018).

posterior <- distribution_chisquared(10000, 4)

hdi(posterior, ci = .89)
## 89% HDI: [0.18, 7.63]

eti(posterior, ci = .89)
## 89% ETI: [0.75, 9.25]

Existence and Significance Testing

Probability of Direction (pd)

p_direction() computes the Probability of Direction (pd, also known as the Maximum Probability of Effect - MPE). It varies between 50% and 100% (i.e., 0.5 and 1) and can be interpreted as the probability (expressed in percentage) that a parameter (described by its posterior distribution) is strictly positive or negative (whichever is the most probable). It is mathematically defined as the proportion of the posterior distribution that is of the median’s sign. Although differently expressed, this index is fairly similar (i.e., is strongly correlated) to the frequentist p-value.

Relationship with the p-value: In most cases, it seems that the pd corresponds to the frequentist one-sided p-value through the formula p-value = (1-pd/100) and to the two-sided p-value (the most commonly reported) through the formula p-value = 2*(1-pd/100). Thus, a pd of 95%, 97.5% 99.5% and 99.95% corresponds approximately to a two-sided p-value of respectively .1, .05, .01 and .001. See the reporting guidelines.

posterior <- distribution_normal(10000, 0.4, 0.2)
p_direction(posterior)
## Probability of Direction (pd): 0.98

ROPE

rope() computes the proportion (in percentage) of the HDI (default to the 89% HDI) of a posterior distribution that lies within a region of practical equivalence.

Statistically, the probability of a posterior distribution of being different from 0 does not make much sense (the probability of it being different from a single point being infinite). Therefore, the idea underlining ROPE is to let the user define an area around the null value enclosing values that are equivalent to the null value for practical purposes (Kruschke & Liddell, 2018, p. @kruschke2018rejecting).

Kruschke suggests that such null value could be set, by default, to the -0.1 to 0.1 range of a standardized parameter (negligible effect size according to Cohen, 1988). This could be generalized: For instance, for linear models, the ROPE could be set as 0 +/- .1 * sd(y). This ROPE range can be automatically computed for models using the rope_range function.

Kruschke suggests using the proportion of the 95% (or 90%, considered more stable) HDI that falls within the ROPE as an index for “null-hypothesis” testing (as understood under the Bayesian framework, see equivalence_test).

posterior <- distribution_normal(10000, 0.4, 0.2)
rope(posterior, range = c(-0.1, 0.1))
## # Proportion of samples inside the ROPE [-0.10, 0.10]:
## 
## inside ROPE
## -----------
## 1.33 %

Bayes Factor

bayesfactor_parameters() computes Bayes factors against the null (either a point or an interval), bases on prior and posterior samples of a single parameter. This Bayes factor indicates the degree by which the mass of the posterior distribution has shifted further away from or closer to the null value(s) (relative to the prior distribution), thus indicating if the null value has become less or more likely given the observed data.

When the null is an interval, the Bayes factor is computed by comparing the prior and posterior odds of the parameter falling within or outside the null; When the null is a point, a Savage-Dickey density ratio is computed, which is also an approximation of a Bayes factor comparing the marginal likelihoods of the model against a model in which the tested parameter has been restricted to the point null (Wagenmakers, Lodewyckx, Kuriyal, & Grasman, 2010).

prior <- distribution_normal(10000, mean = 0, sd = 1)
posterior <- distribution_normal(10000, mean = 1, sd = 0.7)

bayesfactor_parameters(posterior, prior, direction = "two-sided", null = 0)
## # Bayes Factor (Savage-Dickey density ratio)
## 
## BF   
## -----
## 1.946
## 
## * Evidence Against The Null: [0]

The lollipops represent the density of a point-null on the prior distribution (the blue lollipop on the dotted distribution) and on the posterior distribution (the red lollipop on the yellow distribution). The ratio between the two - the Savage-Dickey ratio - indicates the degree by which the mass of the parameter distribution has shifted away from or closer to the null.

For more info, see the Bayes factors vignette.

Utilities

Find ROPE’s appropriate range

rope_range(): This function attempts at automatically finding suitable “default” values for the Region Of Practical Equivalence (ROPE). Kruschke (2018) suggests that such null value could be set, by default, to a range from -0.1 to 0.1 of a standardized parameter (negligible effect size according to Cohen, 1988), which can be generalised for linear models to -0.1 * sd(y), 0.1 * sd(y). For logistic models, the parameters expressed in log odds ratio can be converted to standardized difference through the formula sqrt(3)/pi, resulting in a range of -0.05 to 0.05.

rope_range(model)

Density Estimation

estimate_density(): This function is a wrapper over different methods of density estimation. By default, it uses the base R density with by default uses a different smoothing bandwidth ("SJ") from the legacy default implemented the base R density function ("nrd0"). However, Deng & Wickham suggest that method = "KernSmooth" is the fastest and the most accurate.

Perfect Distributions

distribution(): Generate a sample of size n with near-perfect distributions.

distribution(n = 10)
##  [1] -1.28 -0.88 -0.59 -0.34 -0.11  0.11  0.34  0.59  0.88  1.28

Probability of a Value

density_at(): Compute the density of a given point of a distribution.

density_at(rnorm(1000, 1, 1), 1)
## [1] 0.38

References

Kruschke, J. K. (2015). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan (2. ed). Amsterdam: Elsevier, Academic Press.

Kruschke, J. K. (2018). Rejecting or accepting parameter values in Bayesian estimation. Advances in Methods and Practices in Psychological Science, 1(2), 270–280. https://doi.org/10.1177/2515245918771304

Kruschke, J. K., & Liddell, T. M. (2018). The Bayesian new statistics: Hypothesis testing, estimation, meta-analysis, and power analysis from a Bayesian perspective. Psychonomic Bulletin & Review, 25(1), 178–206. https://doi.org/10.3758/s13423-016-1221-4

McElreath, R. (2018). Statistical rethinking. https://doi.org/10.1201/9781315372495

Wagenmakers, E.-J., Lodewyckx, T., Kuriyal, H., & Grasman, R. (2010). Bayesian hypothesis testing for psychologists: A tutorial on the SavageDickey method. Cognitive Psychology, 60(3), 158–189. https://doi.org/10.1016/j.cogpsych.2009.12.001

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].