# Chapter 13: Bayesian inference and decision making with bayestestR

#### Kjell Nygren

    library(glmbayes)
    library(bayestestR)

## 1. Purpose

bayestestR implements a wide range indices for posterior description and decision-oriented summaries (probability of direction, ropes, equivalence tests, etc.). Here we treat the `coefficients` component of a `glmb` fit as stored i.i.d. draws and summarize them using bayestestR.

This complements Chapter 12 (optional bayesplot graphics, commented out in the vignette) with tabular summaries you can cite in applied reports.

## 2. Example: logistic regression posterior for coefficients

    data(menarche, package = "MASS")

    Age2 <- menarche$Age - 13
    Menarche_Model_Data <- data.frame(
      Menarche  = menarche$Menarche,
      Total     = menarche$Total,
      Age2      = Age2
    )

    ps <- Prior_Setup(
      cbind(Menarche, Total - Menarche) ~ Age2,
      family = binomial(link = "logit"),
      data   = Menarche_Model_Data
    )

    fit_logit <- glmb(
      cbind(Menarche, Total - Menarche) ~ Age2,
      family  = binomial(link = "logit"),
      pfamily = dNormal(mu = ps$mu, Sigma = ps$Sigma),
      data    = Menarche_Model_Data,
      n             = 800,
      use_parallel  = FALSE
    )

    coef_draws <- as.data.frame(fit_logit$coefficients)

## 3. Point and interval summaries

    bayestestR::describe_posterior(coef_draws)
    #> Summary of Posterior Distribution
    #>
    #> Parameter   |    Median |        95% CI |     pd |          ROPE | % in ROPE
    #> ----------------------------------------------------------------------------
    #> (Intercept) | -6.58e-03 | [-0.13, 0.11] | 53.50% | [-0.10, 0.10] |    93.29%
    #> Age2        |      1.62 | [ 1.52, 1.73] |   100% | [-0.10, 0.10] |        0%
    bayestestR::hdi(coef_draws, ci = 0.89)
    #> Highest Density Interval
    #>
    #> Parameter   |       89% HDI
    #> ---------------------------
    #> (Intercept) | [-0.10, 0.09]
    #> Age2        | [ 1.54, 1.71]
    bayestestR::rope(coef_draws, range = c(-0.02, 0.02))
    #> # Proportion of samples inside the ROPE [-0.02, 0.02]:
    #>
    #> Parameter   | Inside ROPE
    #> -------------------------
    #> (Intercept) |     23.95 %
    #> Age2        |      0.00 %
    bayestestR::p_direction(coef_draws)
    #> Probability of Direction
    #>
    #> Parameter   |     pd
    #> --------------------
    #> (Intercept) | 53.50%
    #> Age2        |   100%

Tune `rope(..., range = ...)` to match substantive “practical equivalence” hypotheses on the logit scale (see also the binomial likelihood discussion in Chapter 09). For richer reporting pipelines (parameters, performance, …), see the easystats ecosystem linking out from `?bayestestR`.

## See also

- Chapter 12 — optional `bayesplot` workflows and `legacy_code/pp_check.glmb.R`.
- Chapter 08 — prior-to-posterior workflow and `summary.glmb()` defaults.