Setup and Model Fitting

set.seed(123)
# model with fixed effects only
model <- rstanarm::stan_glm(Sepal.Length ~ Petal.Width * Species, data = iris)

# model with fixed and random effects as well as zero-inflation component
model2 <- insight::download_model("brms_zi_3")

Density Estimation

(related function documentation)

Plotting density estimations of Bayesian regression models will produce plots of the posterior distributions from model parameters, i.e. posterior interval estimates from MCMC draws.

By default, all distributions are “stacked”, i.e. overlaying each other.

result <- estimate_density(model)

plot(result)

To get ridge lines separated by parameters, use stack = FALSE.

plot(result, stack = FALSE)

Adding Prior Samples

For many plots, when the model has defined priors, you can add a layer from the prior distribution for the parameters with priors = TRUE.

plot(result, stack = FALSE, priors = TRUE)

Probability of Direction (pd)

(related function documentation)

The probability of direction (also known as the maximum probability of effect - MPE) 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. Altough differently expressed, this index is fairly similar (i.e., is strongly correlated) to the frequentist p-value

result <- p_direction(model)

result
#> # Probability of Direction (pd)
#> 
#> Parameter                     |      pd
#> ---------------------------------------
#> (Intercept)                   | 100.00%
#> Petal.Width                   |  92.30%
#> Speciesversicolor             |  94.20%
#> Speciesvirginica              |  80.47%
#> Petal.Width:Speciesversicolor |  79.20%
#> Petal.Width:Speciesvirginica  |  62.20%

plot(result)

For more complex models with different components, each component is displayed in a separate facet. Use n_columns to define the layout, i.e. how many columns are used to display the facets.

result <- p_direction(model2, effects = "all", component = "all")

result
#> # Probability of Direction (pd)
#> 
#> # Fixed Effects (Conditional Model)
#> 
#> Parameter |      pd
#> -------------------
#> Intercept |  90.00%
#> child     | 100.00%
#> camper    | 100.00%
#> 
#> # Fixed Effects (Zero-Inflated Model)
#> 
#> Parameter |      pd
#> -------------------
#> Intercept |  78.00%
#> child     | 100.00%
#> camper    |  98.40%
#> 
#> # Random Effects (Conditional Model)
#> 
#> Parameter              |      pd
#> --------------------------------
#> persons 1              |  92.00%
#> persons 2              |  50.00%
#> persons 3              |  79.60%
#> persons 4              |  96.80%
#> SD persons (Intercept) | 100.00%
#> 
#> # Random Effects (Zero-Inflated Model)
#> 
#> Parameter              |      pd
#> --------------------------------
#> persons 1              |  94.80%
#> persons 2              |  63.20%
#> persons 3              |  64.00%
#> persons 4              |  98.00%
#> SD persons (Intercept) | 100.00%

plot(result)

plot(result, n_columns = NULL)

Adding Prior Samples

result <- p_direction(model)
plot(result, priors = TRUE)

Practical Significance

(related function documentation)

The probability of practical significance is conceptualized as a unidirectional equivalence test. It returns the probability that an effect is above a given threshold corresponding to a negligible effect in the median’s direction.

Mathematically, it is defined as the proportion of the posterior distribution of the median sign above the threshold.

result <- p_significance(model)

result
#> # Probability of Significance (ps [0.08])
#> 
#> Parameter                     |      ps
#> ---------------------------------------
#> (Intercept)                   | 100.00%
#> Petal.Width                   |  90.03%
#> Speciesversicolor             |  91.72%
#> Speciesvirginica              |  75.92%
#> Petal.Width:Speciesversicolor |  75.40%
#> Petal.Width:Speciesvirginica  |  57.55%

plot(result)

result <- p_significance(model2, effects = "all", component = "all")

result
#> # Probability of Significance (ps [0.10])
#> 
#> # Fixed Effects (Conditional Model)
#> 
#> Parameter |      ps
#> -------------------
#> Intercept |  88.40%
#> child     | 100.00%
#> camper    | 100.00%
#> 
#> # Fixed Effects (Zero-Inflated Model)
#> 
#> Parameter |      ps
#> -------------------
#> Intercept |  73.20%
#> child     | 100.00%
#> camper    |  96.40%
#> 
#> # Random Effects (Conditional Model)
#> 
#> Parameter              |      ps
#> --------------------------------
#> persons 1              |  90.40%
#> persons 2              |  45.20%
#> persons 3              |  77.60%
#> persons 4              |  96.00%
#> SD persons (Intercept) | 100.00%
#> 
#> # Random Effects (Zero-Inflated Model)
#> 
#> Parameter              |      ps
#> --------------------------------
#> persons 1              |  93.20%
#> persons 2              |  54.40%
#> persons 3              |  58.80%
#> persons 4              |  97.20%
#> SD persons (Intercept) | 100.00%

plot(result)

plot(result, n_columns = NULL)

Adding Prior Samples

result <- p_significance(model)
plot(result, priors = TRUE)

Point Estimates

(related function documentation)

point_estimate() computes various point-estimates, such as the mean, the median or the MAP, to describe posterior distributions. The plot()-method shows the posterior distribution of related parameters and emphasizes the point estimate within the plot.

result <- point_estimate(model)

result
#> # Point Estimates
#> 
#> Parameter                     | Median |  Mean |   MAP
#> ------------------------------------------------------
#> (Intercept)                   |   4.80 |  4.80 |  4.79
#> Petal.Width                   |   0.86 |  0.86 |  0.85
#> Speciesversicolor             |  -0.76 | -0.75 | -0.79
#> Speciesvirginica              |   0.46 |  0.46 |  0.53
#> Petal.Width:Speciesversicolor |   0.56 |  0.56 |  0.60
#> Petal.Width:Speciesvirginica  |  -0.21 | -0.20 | -0.24

plot(result)

result <- point_estimate(model, centrality = c("map", "mean"))

result
#> # Point Estimates
#> 
#> Parameter                     |  Mean |   MAP
#> ---------------------------------------------
#> (Intercept)                   |  4.80 |  4.79
#> Petal.Width                   |  0.86 |  0.85
#> Speciesversicolor             | -0.75 | -0.79
#> Speciesvirginica              |  0.46 |  0.53
#> Petal.Width:Speciesversicolor |  0.56 |  0.60
#> Petal.Width:Speciesvirginica  | -0.20 | -0.24

plot(result, panel = FALSE)[[2]]

Addin Prior Samples

result <- point_estimate(model)
plot(result, priors = TRUE)

Highest Density Interval (HDI)

(related function documentation)

hdi() computes the Highest Density Interval (HDI) of posterior distributions. All points within this interval have a higher probability density than points outside the interval.

The HDI can be used in the context of uncertainty characterisation of posterior distributions as Credible Interval (CI).

result <- hdi(model, ci = c(0.5, 0.75, 0.89, 0.95))

result
#> # Highest Density Intervals
#> 
#> Parameter                     |        50% HDI
#> ----------------------------------------------
#> (Intercept)                   | [ 4.69,  4.90]
#> Petal.Width                   | [ 0.41,  1.20]
#> Speciesversicolor             | [-1.09, -0.44]
#> Speciesvirginica              | [ 0.07,  0.77]
#> Petal.Width:Speciesversicolor | [ 0.02,  0.94]
#> Petal.Width:Speciesvirginica  | [-0.64,  0.22]
#> 
#> 
#> Parameter                     |        75% HDI
#> ----------------------------------------------
#> (Intercept)                   | [ 4.59,  4.96]
#> Petal.Width                   | [ 0.19,  1.56]
#> Speciesversicolor             | [-1.22, -0.13]
#> Speciesvirginica              | [-0.11,  1.11]
#> Petal.Width:Speciesversicolor | [-0.19,  1.36]
#> Petal.Width:Speciesvirginica  | [-0.92,  0.52]
#> 
#> 
#> Parameter                     |        89% HDI
#> ----------------------------------------------
#> (Intercept)                   | [ 4.52,  5.04]
#> Petal.Width                   | [-0.07,  1.83]
#> Speciesversicolor             | [-1.46,  0.06]
#> Speciesvirginica              | [-0.36,  1.29]
#> Petal.Width:Speciesversicolor | [-0.60,  1.53]
#> Petal.Width:Speciesvirginica  | [-1.21,  0.84]
#> 
#> 
#> Parameter                     |        95% HDI
#> ----------------------------------------------
#> (Intercept)                   | [ 4.47,  5.10]
#> Petal.Width                   | [-0.34,  1.96]
#> Speciesversicolor             | [-1.65,  0.23]
#> Speciesvirginica              | [-0.55,  1.49]
#> Petal.Width:Speciesversicolor | [-0.67,  1.90]
#> Petal.Width:Speciesvirginica  | [-1.45,  1.03]

plot(result) + scale_fill_flat()

result <- hdi(model2, ci = c(0.5, 0.75, 0.89), effects = "all", component = "all")

plot(result, n_columns = 2) + scale_fill_metro()

plot(result, n_columns = NULL) + scale_fill_material()

Support Interval

(related function documentation)

Plotting the result of a call to si() results in a plot presenting the prior and posterior distributions for each parameter (note that by default show_intercept = FALSE). The support interval will be denoted by a shaded border.

result <- si(model)
result
#> # Support Interval
#> 
#> Parameter                     |     BF = 1 SI
#> ---------------------------------------------
#> (Intercept)                   | [ 4.38, 5.21]
#> Petal.Width                   | [-0.17, 1.92]
#> Speciesversicolor             | [-1.71, 0.20]
#> Speciesvirginica              | [-0.56, 1.48]
#> Petal.Width:Speciesversicolor | [-0.53, 1.73]
#> Petal.Width:Speciesvirginica  | [-1.21, 0.77]

plot(result) + 
  scale_color_metro(palette = "ice") +
  scale_fill_metro(palette = "ice")


plot(result, support_only = TRUE) + 
  scale_color_metro(palette = "ice") +
  scale_fill_metro(palette = "ice")

Region of Practical Equivalence (ROPE)

(related function documentation)

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

The related plot()-method plots posterior distributions, coloring different HDI levels, and adds a “rope” region to the plot that indicates which portion of the posterior distributions lies inside (and outside) the ROPE.

result <- rope(model, ci = c(0.9, 0.95))

result
#> # Proportions of samples inside the ROPE [-0.08, 0.08]:
#> 
#> ROPE for the 90% HDI:
#> 
#> Parameter                     | inside ROPE
#> -------------------------------------------
#> (Intercept)                   |      0.00 %
#> Petal.Width                   |      4.67 %
#> Speciesversicolor             |      4.42 %
#> Speciesvirginica              |      9.66 %
#> Petal.Width:Speciesversicolor |      8.28 %
#> Petal.Width:Speciesvirginica  |      9.91 %
#> 
#> 
#> ROPE for the 95% HDI:
#> 
#> Parameter                     | inside ROPE
#> -------------------------------------------
#> (Intercept)                   |      0.00 %
#> Petal.Width                   |      4.42 %
#> Speciesversicolor             |      4.55 %
#> Speciesvirginica              |      9.16 %
#> Petal.Width:Speciesversicolor |      7.84 %
#> Petal.Width:Speciesvirginica  |      9.39 %

plot(result, rope_color = "red") +
  scale_fill_brewer(palette = "Greens", direction = -1)

result <- rope(model2, ci = c(0.9, 0.95), effects = "all", component = "all")

result 
#> # Proportions of samples inside the ROPE [-0.10, 0.10]:
#> 
#> ROPE for the 90% HDI:
#> 
#> # Fixed Effects (Conditional Model)
#> 
#> Parameter | inside ROPE
#> -----------------------
#> Intercept |      2.65 %
#> child     |      0.00 %
#> camper    |      0.00 %
#> 
#> # Fixed Effects (Zero-Inflated Model)
#> 
#> Parameter | inside ROPE
#> -----------------------
#> Intercept |     11.06 %
#> child     |      0.00 %
#> camper    |      0.00 %
#> 
#> # Random Effects (Conditional Model)
#> 
#> Parameter              | inside ROPE
#> ------------------------------------
#> persons 1              |      1.77 %
#> persons 2              |     14.60 %
#> persons 3              |      5.31 %
#> persons 4              |      0.88 %
#> SD persons (Intercept) |      0.00 %
#> 
#> # Random Effects (Zero-Inflated Model)
#> 
#> Parameter              | inside ROPE
#> ------------------------------------
#> persons 1              |      3.54 %
#> persons 2              |     15.49 %
#> persons 3              |     12.83 %
#> persons 4              |      0.00 %
#> SD persons (Intercept) |      0.00 %
#> 
#> 
#> ROPE for the 95% HDI:
#> 
#> # Fixed Effects (Conditional Model)
#> 
#> Parameter | inside ROPE
#> -----------------------
#> Intercept |      2.51 %
#> child     |      0.00 %
#> camper    |      0.00 %
#> 
#> # Fixed Effects (Zero-Inflated Model)
#> 
#> Parameter | inside ROPE
#> -----------------------
#> Intercept |     10.46 %
#> child     |      0.00 %
#> camper    |      1.67 %
#> 
#> # Random Effects (Conditional Model)
#> 
#> Parameter              | inside ROPE
#> ------------------------------------
#> persons 1              |      2.09 %
#> persons 2              |     13.81 %
#> persons 3              |      5.02 %
#> persons 4              |      2.09 %
#> SD persons (Intercept) |      0.00 %
#> 
#> # Random Effects (Zero-Inflated Model)
#> 
#> Parameter              | inside ROPE
#> ------------------------------------
#> persons 1              |      3.35 %
#> persons 2              |     14.64 %
#> persons 3              |     12.13 %
#> persons 4              |      0.00 %
#> SD persons (Intercept) |      0.00 %

plot(result, rope_color = "grey70") +
  scale_fill_social()

Test for Practical Equivalence

(related function documentation)

The test for practical equivalence is based on the “HDI+ROPE decision rule” to check whether parameter values should be accepted or rejected against an explicitly formulated “null hypothesis” (i.e., a ROPE). In other words, it checks the percentage of the 89% HDI that is the null region (the ROPE). If this percentage is sufficiently low, the null hypothesis is rejected. If this percentage is sufficiently high, the null hypothesis is accepted.

result <- equivalence_test(model)

result
#> # Test for Practical Equivalence
#> 
#>   ROPE: [-0.08 0.08]
#> 
#> Parameter                     |        H0 | inside ROPE |      89% HDI
#> ----------------------------------------------------------------------
#> (Intercept)                   |  Rejected |      0.00 % | [ 4.52 5.04]
#> Petal.Width                   | Undecided |      4.41 % | [-0.07 1.83]
#> Speciesversicolor             | Undecided |      4.47 % | [-1.46 0.06]
#> Speciesvirginica              | Undecided |      9.77 % | [-0.36 1.29]
#> Petal.Width:Speciesversicolor | Undecided |      8.37 % | [-0.60 1.53]
#> Petal.Width:Speciesvirginica  | Undecided |     10.03 % | [-1.21 0.84]

plot(result) +
  theme_blackboard() +
  scale_fill_material()

result <- equivalence_test(model, ci = c(.89, .95))

result
#> # Test for Practical Equivalence
#> 
#>   ROPE: [-0.08 0.08]
#> 
#> Parameter                     |        H0 | inside ROPE |      89% HDI
#> ----------------------------------------------------------------------
#> (Intercept)                   |  Rejected |      0.00 % | [ 4.52 5.04]
#> Petal.Width                   | Undecided |      4.41 % | [-0.07 1.83]
#> Speciesversicolor             | Undecided |      4.47 % | [-1.46 0.06]
#> Speciesvirginica              | Undecided |      9.77 % | [-0.36 1.29]
#> Petal.Width:Speciesversicolor | Undecided |      8.37 % | [-0.60 1.53]
#> Petal.Width:Speciesvirginica  | Undecided |     10.03 % | [-1.21 0.84]
#> 
#> 
#> Parameter                     |        H0 | inside ROPE |      95% HDI
#> ----------------------------------------------------------------------
#> (Intercept)                   |  Rejected |      0.00 % | [ 4.47 5.10]
#> Petal.Width                   | Undecided |      4.42 % | [-0.34 1.96]
#> Speciesversicolor             | Undecided |      4.55 % | [-1.65 0.23]
#> Speciesvirginica              | Undecided |      9.16 % | [-0.55 1.49]
#> Petal.Width:Speciesversicolor | Undecided |      7.84 % | [-0.67 1.90]
#> Petal.Width:Speciesvirginica  | Undecided |      9.39 % | [-1.45 1.03]

plot(result) +
  theme_abyss() +
  scale_fill_flat()

result <- equivalence_test(model2, ci = c(.89, .95), effects = "all", component = "all")

result
#> # Test for Practical Equivalence
#> 
#>   ROPE: [-0.10 0.10]
#> 
#> # Fixed Effects (Conditional Model)
#> 
#> Parameter |        H0 | inside ROPE |       89% HDI
#> ---------------------------------------------------
#> Intercept | Undecided |      2.68 % | [-0.34  2.29]
#> child     |  Rejected |      0.00 % | [-1.32 -1.00]
#> camper    |  Rejected |      0.00 % | [ 0.57  0.87]
#> 
#> # Fixed Effects (Zero-Inflated Model)
#> 
#> Parameter |        H0 | inside ROPE |       89% HDI
#> ---------------------------------------------------
#> Intercept | Undecided |     11.16 % | [-1.69  0.43]
#> child     |  Rejected |      0.00 % | [ 1.37  2.47]
#> camper    |  Rejected |      0.00 % | [-1.47 -0.22]
#> 
#> # Random Effects (Conditional Model)
#> 
#> Parameter           |        H0 | inside ROPE |       89% HDI
#> -------------------------------------------------------------
#> persons 1           | Undecided |      2.23 % | [-2.25  0.38]
#> persons 2           | Undecided |     14.73 % | [-1.64  0.95]
#> persons 3           | Undecided |      5.36 % | [-0.58  2.08]
#> persons 4           | Undecided |      0.89 % | [ 0.09  2.72]
#> SD/Cor: (Intercept) |  Rejected |      0.00 % | [ 0.66  2.49]
#> 
#> # Random Effects (Zero-Inflated Model)
#> 
#> Parameter           |        H0 | inside ROPE |       89% HDI
#> -------------------------------------------------------------
#> persons 1           | Undecided |      3.57 % | [-0.06  2.07]
#> persons 2           | Undecided |     15.62 % | [-0.83  1.12]
#> persons 3           | Undecided |     12.95 % | [-1.44  0.57]
#> persons 4           | Undecided |      0.45 % | [-2.44 -0.09]
#> SD/Cor: (Intercept) |  Rejected |      0.00 % | [ 0.61  2.67]
#> 
#> 
#> # Fixed Effects (Conditional Model)
#> 
#> Parameter |        H0 | inside ROPE |       95% HDI
#> ---------------------------------------------------
#> Intercept | Undecided |      2.51 % | [-0.64  2.74]
#> child     |  Rejected |      0.00 % | [-1.39 -0.97]
#> camper    |  Rejected |      0.00 % | [ 0.51  0.89]
#> 
#> # Fixed Effects (Zero-Inflated Model)
#> 
#> Parameter |        H0 | inside ROPE |       95% HDI
#> ---------------------------------------------------
#> Intercept | Undecided |     10.46 % | [-1.75  1.16]
#> child     |  Rejected |      0.00 % | [ 1.28  2.61]
#> camper    | Undecided |      1.67 % | [-1.61 -0.04]
#> 
#> # Random Effects (Conditional Model)
#> 
#> Parameter              |        H0 | inside ROPE |       95% HDI
#> ----------------------------------------------------------------
#> persons 1              | Undecided |      2.09 % | [-2.71  0.78]
#> persons 2              | Undecided |     13.81 % | [-1.73  1.54]
#> persons 3              | Undecided |      5.02 % | [-1.06  2.28]
#> persons 4              | Undecided |      2.09 % | [-0.22  3.12]
#> SD persons (Intercept) |  Rejected |      0.00 % | [ 0.60  3.07]
#> 
#> # Random Effects (Zero-Inflated Model)
#> 
#> Parameter              |        H0 | inside ROPE |       95% HDI
#> ----------------------------------------------------------------
#> persons 1              | Undecided |      3.35 % | [-0.29  2.73]
#> persons 2              | Undecided |     14.64 % | [-0.94  1.60]
#> persons 3              | Undecided |     12.13 % | [-1.54  1.17]
#> persons 4              |  Rejected |      0.00 % | [-3.03 -0.13]
#> SD persons (Intercept) |  Rejected |      0.00 % | [ 0.47  2.90]

plot(result, n_columns = 3) + theme_modern()

Bayes Factors (BFs)

Bayes Factors for Model Parameters

(related function documentation)

Plotting the result of a call to bayesfactor_parameters() results in a plot presenting the prior and posterior distributions for each parameter (note that by default show_intercept = FALSE). When a point null was tested, two dots represent the density of the null at the value - the ratio of their heights is the value of the Savage-Dickey Bayes factor:

result <- bayesfactor_parameters(model)

result
#> # Bayes Factor (Savage-Dickey density ratio)
#> 
#> Parameter                     |        BF
#> -----------------------------------------
#> (Intercept)                   | 1.428e+33
#> Petal.Width                   |     0.594
#> Speciesversicolor             |     0.360
#> Speciesvirginica              |     0.174
#> Petal.Width:Speciesversicolor |     0.284
#> Petal.Width:Speciesvirginica  |     0.321
#> 
#> * Evidence Against The Null: [0]

plot(result) +
  scale_color_material() +
  scale_fill_material()

When an interval null was tested, two dashed lines mark the edges of the null interval at the value - the Bayes factor represents the degree by which the distribution mass of the posterior has shifted outside or inside the null interval relative to the prior distribution:

result <- bayesfactor_parameters(model, null = rope_range(model))

result
#> # Bayes Factor (Null-Interval)
#> 
#> Parameter                     |        BF
#> -----------------------------------------
#> (Intercept)                   | 1.001e+33
#> Petal.Width                   |     0.598
#> Speciesversicolor             |     0.337
#> Speciesvirginica              |     0.167
#> Petal.Width:Speciesversicolor |     0.271
#> Petal.Width:Speciesvirginica  |     0.295
#> 
#> * Evidence Against The Null: [-0.083, 0.083]

plot(result) +
  scale_color_material() +
  scale_fill_material()

Bayes Factors for Model Comparison

(related function documentation)

lm0 <- lm(qsec ~ 1, data = mtcars)
lm1 <- lm(qsec ~ drat, data = mtcars)
lm2 <- lm(qsec ~ wt, data = mtcars)
lm3 <- lm(qsec ~ drat + wt, data = mtcars)

result <- bayesfactor_models(lm1, lm2, lm3, denominator = lm0)

result
#> # Bayes Factors for Model Comparison
#> 
#>   Model            BF
#>   [1] drat      0.202
#>   [2] wt        0.290
#>   [3] drat + wt 0.053
#> 
#> * Against Denominator: [4] (Intercept only)
#> *   Bayes Factor Type: BIC approximation

Pizza plots are a visual way of representing the posterior probabilities of several models, with ratio of the areas of any two models corresponding to their posterior odds.1 It is possible to plot all compared models on one (pizza) pie:

plot(result, n_pies = "one", value = "probability") +
  scale_fill_pizza(reverse = TRUE) 

But it is also possible to plot one pizza for each model and the denominator model (and who doesn’t like more pizza?):

plot(result, n_pies = "many", value = "BF") +
  scale_fill_flat(palette = "rainbow", reverse = TRUE)


  1. When all models are given equal prior probabilities, then all prior odds are 1, and the posterior odds are equal to the Bayes factor.↩︎