-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathPPC-intervals.Rd
252 lines (220 loc) · 7.58 KB
/
PPC-intervals.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ppc-intervals.R
\name{PPC-intervals}
\alias{PPC-intervals}
\alias{ppc_intervals}
\alias{ppc_intervals_grouped}
\alias{ppc_ribbon}
\alias{ppc_ribbon_grouped}
\alias{ppc_intervals_data}
\alias{ppc_ribbon_data}
\title{PPC intervals}
\usage{
ppc_intervals(
y,
yrep,
x = NULL,
...,
prob = 0.5,
prob_outer = 0.9,
alpha = 0.33,
size = 1,
fatten = 2.5,
linewidth = 1
)
ppc_intervals_grouped(
y,
yrep,
x = NULL,
group,
...,
facet_args = list(),
prob = 0.5,
prob_outer = 0.9,
alpha = 0.33,
size = 1,
fatten = 2.5,
linewidth = 1
)
ppc_ribbon(
y,
yrep,
x = NULL,
...,
prob = 0.5,
prob_outer = 0.9,
alpha = 0.33,
size = 0.25,
y_draw = c("line", "points", "both")
)
ppc_ribbon_grouped(
y,
yrep,
x = NULL,
group,
...,
facet_args = list(),
prob = 0.5,
prob_outer = 0.9,
alpha = 0.33,
size = 0.25,
y_draw = c("line", "points", "both")
)
ppc_intervals_data(
y,
yrep,
x = NULL,
group = NULL,
...,
prob = 0.5,
prob_outer = 0.9
)
ppc_ribbon_data(
y,
yrep,
x = NULL,
group = NULL,
...,
prob = 0.5,
prob_outer = 0.9
)
}
\arguments{
\item{y}{A vector of observations. See \strong{Details}.}
\item{yrep}{An \code{S} by \code{N} matrix of draws from the posterior (or prior)
predictive distribution. The number of rows, \code{S}, is the size of the
posterior (or prior) sample used to generate \code{yrep}. The number of columns,
\code{N} is the number of predicted observations (\code{length(y)}). The columns of
\code{yrep} should be in the same order as the data points in \code{y} for the plots
to make sense. See the \strong{Details} and \strong{Plot Descriptions} sections for
additional advice specific to particular plots.}
\item{x}{A numeric vector to use as the x-axis
variable. For example, \code{x} could be a predictor variable from a
regression model, a time variable for time-series models, etc. If \code{x}
is missing or \code{NULL} then the observation index is used for the x-axis.}
\item{...}{Currently unused.}
\item{prob, prob_outer}{Values between \code{0} and \code{1} indicating the desired
probability mass to include in the inner and outer intervals. The defaults
are \code{prob=0.5} and \code{prob_outer=0.9}.}
\item{alpha, size, fatten, linewidth}{Arguments passed to geoms. For ribbon
plots \code{alpha} is passed to \code{\link[ggplot2:geom_ribbon]{ggplot2::geom_ribbon()}} to control the opacity
of the outer ribbon and \code{size} is passed to \code{\link[ggplot2:geom_path]{ggplot2::geom_line()}} to
control the size of the line representing the median prediction (\code{size=0}
will remove the line). For interval plots \code{alpha}, \code{size}, \code{fatten}, and
\code{linewidth} are passed to \code{\link[ggplot2:geom_linerange]{ggplot2::geom_pointrange()}} (\code{fatten=0} will
remove the point estimates).}
\item{group}{A grouping variable of the same length as \code{y}.
Will be coerced to \link[base:factor]{factor} if not already a factor.
Each value in \code{group} is interpreted as the group level pertaining
to the corresponding observation.}
\item{facet_args}{A named list of arguments (other than \code{facets}) passed
to \code{\link[ggplot2:facet_wrap]{ggplot2::facet_wrap()}} or \code{\link[ggplot2:facet_grid]{ggplot2::facet_grid()}}
to control faceting. Note: if \code{scales} is not included in \code{facet_args}
then \strong{bayesplot} may use \code{scales="free"} as the default (depending
on the plot) instead of the \strong{ggplot2} default of \code{scales="fixed"}.}
\item{y_draw}{For ribbon plots only, a string specifying how to draw \code{y}. Can
be \code{"line"} (the default), \code{"points"}, or \code{"both"}.}
}
\value{
The plotting functions return a ggplot object that can be further
customized using the \strong{ggplot2} package. The functions with suffix
\verb{_data()} return the data that would have been drawn by the plotting
function.
}
\description{
Medians and central interval estimates of \code{yrep} with \code{y} overlaid.
See the \strong{Plot Descriptions} section, below.
}
\section{Plot Descriptions}{
\describe{
\item{\verb{ppc_intervals(), ppc_ribbon()}}{
\code{100*prob}\% central intervals for \code{yrep} at each \code{x}
value. \code{ppc_intervals()} plots intervals as vertical bars with points
indicating \code{yrep} medians and darker points indicating observed
\code{y} values. \code{ppc_ribbon()} plots a ribbon of connected intervals
with a line through the median of \code{yrep} and a darker line connecting
observed \code{y} values. In both cases an optional \code{x} variable can
also be specified for the x-axis variable.
Depending on the number of observations and the variability in the
predictions at different values of \code{x}, one of these plots may be easier
to read than the other.
}
\item{\verb{ppc_intervals_grouped(), ppc_ribbon_grouped()}}{
Same as \code{ppc_intervals()} and \code{ppc_ribbon()}, respectively, but a
separate plot (facet) is generated for each level of a grouping variable.
}
}
}
\examples{
y <- rnorm(50)
yrep <- matrix(rnorm(5000, 0, 2), ncol = 50)
color_scheme_set("brightblue")
ppc_intervals(y, yrep)
ppc_ribbon(y, yrep)
ppc_ribbon(y, yrep, y_draw = "points")
\dontrun{
ppc_ribbon(y, yrep, y_draw = "both")
}
ppc_intervals(y, yrep, size = 1.5, fatten = 0) # remove the yrep point estimates
color_scheme_set("teal")
year <- 1950:1999
ppc_intervals(y, yrep, x = year, fatten = 1) + ggplot2::xlab("Year")
ppc_ribbon(y, yrep, x = year) + ggplot2::xlab("Year")
color_scheme_set("pink")
year <- rep(2000:2009, each = 5)
group <- gl(5, 1, length = 50, labels = LETTERS[1:5])
ppc_ribbon_grouped(y, yrep, x = year, group, y_draw = "both") +
ggplot2::scale_x_continuous(breaks = pretty)
ppc_ribbon_grouped(y, yrep, x = year, group,
facet_args = list(scales = "fixed")) +
xaxis_text(FALSE) +
xaxis_ticks(FALSE) +
panel_bg(fill = "gray20")
# get the data frames used to make the ggplots
ppc_dat <- ppc_intervals_data(y, yrep, x = year, prob = 0.5)
ppc_group_dat <- ppc_intervals_data(y, yrep, x = year, group = group, prob = 0.5)
\dontrun{
library("rstanarm")
fit <- stan_glmer(mpg ~ wt + (1|cyl), data = mtcars, refresh = 0)
yrep <- posterior_predict(fit)
color_scheme_set("purple")
ppc_intervals(y = mtcars$mpg, yrep = yrep, x = mtcars$wt, prob = 0.8) +
panel_bg(fill="gray90", color = NA) +
grid_lines(color = "white")
ppc_ribbon(y = mtcars$mpg, yrep = yrep, x = mtcars$wt,
prob = 0.6, prob_outer = 0.8)
ppc_ribbon_grouped(y = mtcars$mpg, yrep = yrep, x = mtcars$wt,
group = mtcars$cyl)
color_scheme_set("gray")
ppc_intervals(mtcars$mpg, yrep, prob = 0.5) +
ggplot2::scale_x_continuous(
labels = rownames(mtcars),
breaks = 1:nrow(mtcars)
) +
xaxis_text(angle = -70, vjust = 1, hjust = 0) +
xaxis_title(FALSE)
}
}
\references{
Gabry, J. , Simpson, D. , Vehtari, A. , Betancourt, M. and
Gelman, A. (2019), Visualization in Bayesian workflow.
\emph{J. R. Stat. Soc. A}, 182: 389-402. doi:10.1111/rssa.12378.
(\href{https://rss.onlinelibrary.wiley.com/doi/full/10.1111/rssa.12378}{journal version},
\href{https://arxiv.org/abs/1709.01449}{arXiv preprint},
\href{https://github.com/jgabry/bayes-vis-paper}{code on GitHub})
Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari,
A., and Rubin, D. B. (2013). \emph{Bayesian Data Analysis.} Chapman & Hall/CRC
Press, London, third edition. (Ch. 6)
}
\seealso{
Other PPCs:
\code{\link{PPC-censoring}},
\code{\link{PPC-discrete}},
\code{\link{PPC-distributions}},
\code{\link{PPC-errors}},
\code{\link{PPC-loo}},
\code{\link{PPC-overview}},
\code{\link{PPC-scatterplots}},
\code{\link{PPC-test-statistics}}
}
\concept{PPCs}