bayesplot (version 1.7.2)

PPC-discrete: PPCs for discrete outcomes

Description

Many of the PPC functions in bayesplot can be used with discrete data. The small subset of these functions that can only be used if y and yrep are discrete are documented on this page. Currently these include rootograms for count outcomes and bar plots for ordinal, categorical, and multinomial outcomes. See the Plot Descriptions section below.

Usage

ppc_bars(
  y,
  yrep,
  ...,
  prob = 0.9,
  width = 0.9,
  size = 1,
  fatten = 3,
  freq = TRUE
)

ppc_bars_grouped( y, yrep, group, ..., facet_args = list(), prob = 0.9, width = 0.9, size = 1, fatten = 3, freq = TRUE )

ppc_rootogram( y, yrep, style = c("standing", "hanging", "suspended"), ..., prob = 0.9, size = 1 )

Arguments

y

A vector of observations. See Details.

yrep

An \(S\) by \(N\) matrix of draws from the posterior predictive distribution, where \(S\) is the size of the posterior sample (or subset of the posterior sample used to generate yrep) and \(N\) is the number of observations (the length of y). The columns of yrep should be in the same order as the data points in y for the plots to make sense. See Details for additional instructions.

...

Currently unused.

prob

A value between 0 and 1 indicating the desired probability mass to include in the yrep intervals. Set prob=0 to remove the intervals. For ppc_rootogram() these are intervals of the square roots of the expected counts.

width

For ppc_bars() and ppc_bars_grouped(), passed to ggplot2::geom_bar() to control the bar width.

size, fatten

For ppc_bars() and ppc_bars_grouped(), size and fatten are passed to ggplot2::geom_pointrange() to control the appearance of the yrep points and intervals. For ppc_rootogram() size is passed to ggplot2::geom_line().

freq

For ppc_bars() and ppc_bars_grouped(), if TRUE (the default) the y-axis will display counts. Setting freq=FALSE will put proportions on the y-axis.

group

A grouping variable (a vector or factor) the same length as y. Each value in group is interpreted as the group level pertaining to the corresponding value of y.

facet_args

An optional list of arguments (other than facets) passed to ggplot2::facet_wrap() to control faceting.

style

For ppc_rootogram, a string specifying the rootogram style. The options are "standing", "hanging", and "suspended". See the Plot Descriptions section, below, for details on the different styles.

Value

A ggplot object that can be further customized using the ggplot2 package.

Plot Descriptions

ppc_bars()

Bar plot of y with yrep medians and uncertainty intervals superimposed on the bars.

ppc_bars_grouped()

Same as ppc_bars() but a separate plot (facet) is generated for each level of a grouping variable.

ppc_rootogram()

Rootograms allow for diagnosing problems in count data models such as overdispersion or excess zeros. They consist of a histogram of y with the expected counts based on yrep overlaid as a line along with uncertainty intervals. The y-axis represents the square roots of the counts to approximately adjust for scale differences and thus ease comparison between observed and expected counts. Using the style argument, the histogram style can be adjusted to focus on different aspects of the data:

  • Standing: basic histogram of observed counts with curve showing expected counts.

  • Hanging: observed counts counts hanging from the curve representing expected counts.

  • Suspended: histogram of the differences between expected and observed counts.

All of these are plotted on the square root scale. See Kleiber and Zeileis (2016) for advice on interpreting rootograms and selecting among the different styles.

Details

For all of these plots y and yrep must be integers, although they need not be integers in the strict sense of R's integer type. For rootogram plots y and yrep must also be non-negative.

References

Kleiber, C. and Zeileis, A. (2016). Visualizing count data regressions using rootograms. The American Statistician. 70(3): 296--303. https://arxiv.org/abs/1605.01311.

See Also

Other PPCs: PPC-distributions, PPC-errors, PPC-intervals, PPC-loo, PPC-overview, PPC-scatterplots, PPC-test-statistics

Examples

Run this code
# NOT RUN {
set.seed(9222017)

# bar plots
f <- function(N) {
  sample(1:4, size = N, replace = TRUE, prob = c(0.25, 0.4, 0.1, 0.25))
}
y <- f(100)
yrep <- t(replicate(500, f(100)))
dim(yrep)
group <- gl(2, 50, length = 100, labels = c("GroupA", "GroupB"))

color_scheme_set("mix-pink-blue")
ppc_bars(y, yrep)

# split by group, change interval width, and display proportion
# instead of count on y-axis
color_scheme_set("mix-blue-pink")
ppc_bars_grouped(y, yrep, group, prob = 0.5, freq = FALSE)

# rootograms for counts
y <- rpois(100, 20)
yrep <- matrix(rpois(10000, 20), ncol = 100)

color_scheme_set("brightblue")
ppc_rootogram(y, yrep)
ppc_rootogram(y, yrep, prob = 0)

ppc_rootogram(y, yrep, style = "hanging", prob = 0.8)
ppc_rootogram(y, yrep, style = "suspended")

# }

Run the code above in your browser using DataCamp Workspace