From 5aead64d9629953274c9a915a7817ddc5f282e74 Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Sat, 12 Aug 2023 15:27:38 -0500 Subject: [PATCH] fix missing @examples --- R/add_draws.R | 1 + R/density_bins.R | 1 + R/gather_emmeans_draws.R | 1 + R/predicted_draws.R | 1 + R/predicted_rvars.R | 1 + R/recover_types.R | 4 +- R/sample_draws.R | 1 + man/add_draws.Rd | 111 ++++++----------------- man/add_predicted_draws.Rd | 141 ++++++++--------------------- man/add_predicted_rvars.Rd | 174 +++++++++++------------------------- man/density_bins.Rd | 86 ++++++------------ man/gather_emmeans_draws.Rd | 147 +++++++++--------------------- man/recover_types.Rd | 4 +- man/sample_draws.Rd | 118 ++++++------------------ 14 files changed, 226 insertions(+), 565 deletions(-) diff --git a/R/add_draws.R b/R/add_draws.R index c13ca324..ad904b33 100644 --- a/R/add_draws.R +++ b/R/add_draws.R @@ -34,6 +34,7 @@ globalVariables(".draw") #' @author Matthew Kay #' @seealso [add_predicted_draws()], [add_draws()] #' @keywords manip +#' @examples #' \dontrun{ #' #' library(ggplot2) diff --git a/R/density_bins.R b/R/density_bins.R index 3da24930..8c4d22f4 100644 --- a/R/density_bins.R +++ b/R/density_bins.R @@ -36,6 +36,7 @@ #' @seealso See [add_predicted_draws()] and [stat_lineribbon()] for a better approach. These #' functions may be deprecated in the future. #' @keywords manip +#' @examples #' \dontrun{ #' #' library(ggplot2) diff --git a/R/gather_emmeans_draws.R b/R/gather_emmeans_draws.R index d7424518..510da300 100755 --- a/R/gather_emmeans_draws.R +++ b/R/gather_emmeans_draws.R @@ -38,6 +38,7 @@ #' @author Matthew Kay #' @seealso [emmeans::emmeans()] #' @keywords manip +#' @examples #' \dontrun{ #' #' library(dplyr) diff --git a/R/predicted_draws.R b/R/predicted_draws.R index 03f951d1..f51df1bc 100755 --- a/R/predicted_draws.R +++ b/R/predicted_draws.R @@ -55,6 +55,7 @@ #' @seealso [add_draws()] for the variant of these functions for use with packages that do not have #' explicit support for these functions yet. See [spread_draws()] for manipulating posteriors directly. #' @keywords manip +#' @examples #' \dontrun{ #' #' library(ggplot2) diff --git a/R/predicted_rvars.R b/R/predicted_rvars.R index ae84d31c..5b6d2e95 100755 --- a/R/predicted_rvars.R +++ b/R/predicted_rvars.R @@ -37,6 +37,7 @@ #' @seealso [add_predicted_draws()] for the analogous functions that use a long-data-frame-of-draws #' format instead of a data-frame-of-`rvar`s format. See [spread_rvars()] for manipulating posteriors directly. #' @keywords manip +#' @examples #' \dontrun{ #' #' library(ggplot2) diff --git a/R/recover_types.R b/R/recover_types.R index 196d9b20..7c74b9b5 100755 --- a/R/recover_types.R +++ b/R/recover_types.R @@ -69,8 +69,8 @@ apply_prototypes = function(...) { #' @aliases apply_prototypes #' @seealso [spread_draws()], [gather_draws()], [compose_data()]. #' @keywords manip -#' @examplesIf requireNamespace("rstan", quietly = TRUE) -#' \donttest{ +#' @examples +#' \dontrun{ #' #' library(dplyr) #' library(magrittr) diff --git a/R/sample_draws.R b/R/sample_draws.R index 187e382f..93310705 100644 --- a/R/sample_draws.R +++ b/R/sample_draws.R @@ -23,6 +23,7 @@ #' @param draw The name of the column indexing the draws; default `".draw"`. #' @author Matthew Kay #' @keywords manip +#' @examples #' \dontrun{ #' #' library(ggplot2) diff --git a/man/add_draws.Rd b/man/add_draws.Rd index 501db205..491b1c38 100644 --- a/man/add_draws.Rd +++ b/man/add_draws.Rd @@ -35,92 +35,39 @@ model types (like handling ordinal responses and distributional parameters in br \code{add_predicted_draws(df, m)} is roughly equivalent to \code{add_draws(df, posterior_predict(m, newdata = df))}, except that \code{add_predicted_draws} standardizes argument names and values across packages. } +\examples{ +\dontrun{ + +library(ggplot2) +library(dplyr) +library(brms) +library(modelr) + +theme_set(theme_light()) + +m_mpg = brm(mpg ~ hp * cyl, data = mtcars, + # 1 chain / few iterations just so example runs quickly + # do not use in practice + chains = 1, iter = 500) + +# plot posterior predictive intervals +mtcars \%>\% + group_by(cyl) \%>\% + data_grid(hp = seq_range(hp, n = 101)) \%>\% + # the line below is roughly equivalent to add_epred_draws(m_mpg), except + # that it does not standardize arguments across model types. + add_draws(posterior_epred(m_mpg, newdata = .)) \%>\% + ggplot(aes(x = hp, y = mpg, color = ordered(cyl))) + + stat_lineribbon(aes(y = .value), alpha = 0.25) + + geom_point(data = mtcars) + + scale_fill_brewer(palette = "Greys") + +} +} \seealso{ \code{\link[=add_predicted_draws]{add_predicted_draws()}}, \code{\link[=add_draws]{add_draws()}} } \author{ Matthew Kay } -\keyword{"Greys")} -\keyword{#} -\keyword{%>%} -\keyword{*} -\keyword{+} -\keyword{.))} -\keyword{.value),} -\keyword{/} -\keyword{0.25)} -\keyword{1} -\keyword{1,} -\keyword{101))} -\keyword{500)} -\keyword{=} -\keyword{\dontrun{} -\keyword{across} -\keyword{add_draws(posterior_epred(m_mpg,} -\keyword{add_epred_draws(m_mpg),} -\keyword{alpha} -\keyword{arguments} -\keyword{below} -\keyword{brm(mpg} -\keyword{chain} -\keyword{chains} -\keyword{color} -\keyword{cyl,} -\keyword{data} -\keyword{data_grid(hp} -\keyword{do} -\keyword{does} -\keyword{equivalent} -\keyword{example} -\keyword{except} -\keyword{few} -\keyword{geom_point(data} -\keyword{ggplot(aes(x} -\keyword{group_by(cyl)} -\keyword{hp} -\keyword{hp,} -\keyword{in} -\keyword{intervals} -\keyword{is} -\keyword{it} -\keyword{iter} -\keyword{iterations} -\keyword{just} -\keyword{library(brms)} -\keyword{library(dplyr)} -\keyword{library(ggplot2)} -\keyword{library(modelr)} -\keyword{line} -\keyword{m_mpg} \keyword{manip} -\keyword{model} -\keyword{mpg,} -\keyword{mtcars} -\keyword{mtcars)} -\keyword{mtcars,} -\keyword{n} -\keyword{newdata} -\keyword{not} -\keyword{ordered(cyl)))} -\keyword{plot} -\keyword{posterior} -\keyword{practice} -\keyword{predictive} -\keyword{quickly} -\keyword{roughly} -\keyword{runs} -\keyword{scale_fill_brewer(palette} -\keyword{seq_range(hp,} -\keyword{so} -\keyword{standardize} -\keyword{stat_lineribbon(aes(y} -\keyword{that} -\keyword{the} -\keyword{theme_set(theme_light())} -\keyword{to} -\keyword{types.} -\keyword{use} -\keyword{y} -\keyword{}} -\keyword{~} diff --git a/man/add_predicted_draws.Rd b/man/add_predicted_draws.Rd index 72954f81..25f36419 100755 --- a/man/add_predicted_draws.Rd +++ b/man/add_predicted_draws.Rd @@ -387,6 +387,44 @@ processing pipelines that start either with a data frame or a model. Given equal choice between the two, the spellings prefixed with \code{add_} are preferred. } +\examples{ +\dontrun{ + +library(ggplot2) +library(dplyr) +library(brms) +library(modelr) + +theme_set(theme_light()) + +m_mpg = brm(mpg ~ hp * cyl, data = mtcars, + # 1 chain / few iterations just so example runs quickly + # do not use in practice + chains = 1, iter = 500) + +# draw 100 lines from the posterior means and overplot them +mtcars \%>\% + group_by(cyl) \%>\% + data_grid(hp = seq_range(hp, n = 101)) \%>\% + # NOTE: only use ndraws here when making spaghetti plots; for + # plotting intervals it is always best to use all draws (omit ndraws) + add_epred_draws(m_mpg, ndraws = 100) \%>\% + ggplot(aes(x = hp, y = mpg, color = ordered(cyl))) + + geom_line(aes(y = .epred, group = paste(cyl, .draw)), alpha = 0.25) + + geom_point(data = mtcars) + +# plot posterior predictive intervals +mtcars \%>\% + group_by(cyl) \%>\% + data_grid(hp = seq_range(hp, n = 101)) \%>\% + add_predicted_draws(m_mpg) \%>\% + ggplot(aes(x = hp, y = mpg, color = ordered(cyl))) + + stat_lineribbon(aes(y = .prediction), .width = c(.99, .95, .8, .5), alpha = 0.25) + + geom_point(data = mtcars) + + scale_fill_brewer(palette = "Greys") + +} +} \seealso{ \code{\link[=add_draws]{add_draws()}} for the variant of these functions for use with packages that do not have explicit support for these functions yet. See \code{\link[=spread_draws]{spread_draws()}} for manipulating posteriors directly. @@ -394,107 +432,4 @@ explicit support for these functions yet. See \code{\link[=spread_draws]{spread_ \author{ Matthew Kay } -\keyword{"Greys")} -\keyword{#} -\keyword{%>%} -\keyword{(omit} -\keyword{*} -\keyword{+} -\keyword{.5),} -\keyword{.8,} -\keyword{.95,} -\keyword{.draw)),} -\keyword{.epred,} -\keyword{.prediction),} -\keyword{.width} -\keyword{/} -\keyword{0.25)} -\keyword{1} -\keyword{1,} -\keyword{100} -\keyword{100)} -\keyword{101))} -\keyword{500)} -\keyword{=} -\keyword{NOTE:} -\keyword{\dontrun{} -\keyword{add_epred_draws(m_mpg,} -\keyword{add_predicted_draws(m_mpg)} -\keyword{all} -\keyword{alpha} -\keyword{always} -\keyword{and} -\keyword{best} -\keyword{brm(mpg} -\keyword{c(.99,} -\keyword{chain} -\keyword{chains} -\keyword{color} -\keyword{cyl,} -\keyword{data} -\keyword{data_grid(hp} -\keyword{do} -\keyword{draw} -\keyword{draws} -\keyword{example} -\keyword{few} -\keyword{for} -\keyword{from} -\keyword{geom_line(aes(y} -\keyword{geom_point(data} -\keyword{ggplot(aes(x} -\keyword{group} -\keyword{group_by(cyl)} -\keyword{here} -\keyword{hp} -\keyword{hp,} -\keyword{in} -\keyword{intervals} -\keyword{is} -\keyword{it} -\keyword{iter} -\keyword{iterations} -\keyword{just} -\keyword{library(brms)} -\keyword{library(dplyr)} -\keyword{library(ggplot2)} -\keyword{library(modelr)} -\keyword{lines} -\keyword{m_mpg} -\keyword{making} \keyword{manip} -\keyword{means} -\keyword{mpg,} -\keyword{mtcars} -\keyword{mtcars)} -\keyword{mtcars,} -\keyword{n} -\keyword{ndraws} -\keyword{ndraws)} -\keyword{not} -\keyword{only} -\keyword{ordered(cyl)))} -\keyword{overplot} -\keyword{paste(cyl,} -\keyword{plot} -\keyword{plots;} -\keyword{plotting} -\keyword{posterior} -\keyword{practice} -\keyword{predictive} -\keyword{quickly} -\keyword{runs} -\keyword{scale_fill_brewer(palette} -\keyword{seq_range(hp,} -\keyword{so} -\keyword{spaghetti} -\keyword{stat_lineribbon(aes(y} -\keyword{the} -\keyword{them} -\keyword{theme_set(theme_light())} -\keyword{to} -\keyword{use} -\keyword{when} -\keyword{y} -\keyword{}} -\keyword{~} diff --git a/man/add_predicted_rvars.Rd b/man/add_predicted_rvars.Rd index 59a5a55d..d231bb18 100755 --- a/man/add_predicted_rvars.Rd +++ b/man/add_predicted_rvars.Rd @@ -316,6 +316,56 @@ processing pipelines that start either with a data frame or a model. Given equal choice between the two, the spellings prefixed with \code{add_} are preferred. } +\examples{ +\dontrun{ + +library(ggplot2) +library(dplyr) +library(posterior) +library(brms) +library(modelr) + +theme_set(theme_light()) + +m_mpg = brm(mpg ~ hp * cyl, data = mtcars, family = lognormal(), + # 1 chain / few iterations just so example runs quickly + # do not use in practice + chains = 1, iter = 500) + +# Look at mean predictions for some cars (epred) and compare to +# the exponeniated mu parameter of the lognormal distribution (linpred). +# Notice how they are NOT the same. This is because exp(mu) for a +# lognormal distribution is equal to its median, not its mean. +mtcars \%>\% + select(hp, cyl, mpg) \%>\% + add_epred_rvars(m_mpg) \%>\% + add_linpred_rvars(m_mpg, value = "mu") \%>\% + mutate(expmu = exp(mu), .epred - expmu) + +# plot intervals around conditional means (epred_rvars) +mtcars \%>\% + group_by(cyl) \%>\% + data_grid(hp = seq_range(hp, n = 101)) \%>\% + add_epred_rvars(m_mpg) \%>\% + ggplot(aes(x = hp, color = ordered(cyl), fill = ordered(cyl))) + + stat_lineribbon(aes(dist = .epred), .width = c(.95, .8, .5), alpha = 1/3) + + geom_point(aes(y = mpg), data = mtcars) + + scale_color_brewer(palette = "Dark2") + + scale_fill_brewer(palette = "Set2") + +# plot posterior predictive intervals (predicted_rvars) +mtcars \%>\% + group_by(cyl) \%>\% + data_grid(hp = seq_range(hp, n = 101)) \%>\% + add_predicted_rvars(m_mpg) \%>\% + ggplot(aes(x = hp, color = ordered(cyl), fill = ordered(cyl))) + + stat_lineribbon(aes(dist = .prediction), .width = c(.95, .8, .5), alpha = 1/3) + + geom_point(aes(y = mpg), data = mtcars) + + scale_color_brewer(palette = "Dark2") + + scale_fill_brewer(palette = "Set2") + +} +} \seealso{ \code{\link[=add_predicted_draws]{add_predicted_draws()}} for the analogous functions that use a long-data-frame-of-draws format instead of a data-frame-of-\code{rvar}s format. See \code{\link[=spread_rvars]{spread_rvars()}} for manipulating posteriors directly. @@ -323,128 +373,4 @@ format instead of a data-frame-of-\code{rvar}s format. See \code{\link[=spread_r \author{ Matthew Kay } -\keyword{"Dark2")} -\keyword{"Set2")} -\keyword{"mu")} -\keyword{#} -\keyword{%>%} -\keyword{(epred)} -\keyword{(epred_rvars)} -\keyword{(linpred).} -\keyword{(predicted_rvars)} -\keyword{*} -\keyword{+} -\keyword{-} -\keyword{.5),} -\keyword{.8,} -\keyword{.epred} -\keyword{.epred),} -\keyword{.prediction),} -\keyword{.width} -\keyword{/} -\keyword{1} -\keyword{1,} -\keyword{1/3)} -\keyword{101))} -\keyword{500)} -\keyword{=} -\keyword{Look} -\keyword{NOT} -\keyword{Notice} -\keyword{This} -\keyword{\dontrun{} -\keyword{a} -\keyword{add_epred_rvars(m_mpg)} -\keyword{add_linpred_rvars(m_mpg,} -\keyword{add_predicted_rvars(m_mpg)} -\keyword{alpha} -\keyword{and} -\keyword{are} -\keyword{around} -\keyword{at} -\keyword{because} -\keyword{brm(mpg} -\keyword{c(.95,} -\keyword{cars} -\keyword{chain} -\keyword{chains} -\keyword{color} -\keyword{compare} -\keyword{conditional} -\keyword{cyl,} -\keyword{data} -\keyword{data_grid(hp} -\keyword{distribution} -\keyword{do} -\keyword{equal} -\keyword{example} -\keyword{exp(mu)} -\keyword{exp(mu),} -\keyword{expmu)} -\keyword{exponeniated} -\keyword{family} -\keyword{few} -\keyword{fill} -\keyword{for} -\keyword{geom_point(aes(y} -\keyword{ggplot(aes(x} -\keyword{group_by(cyl)} -\keyword{how} -\keyword{hp} -\keyword{hp,} -\keyword{in} -\keyword{intervals} -\keyword{is} -\keyword{iter} -\keyword{iterations} -\keyword{its} -\keyword{just} -\keyword{library(brms)} -\keyword{library(dplyr)} -\keyword{library(ggplot2)} -\keyword{library(modelr)} -\keyword{library(posterior)} -\keyword{lognormal} -\keyword{lognormal(),} -\keyword{m_mpg} \keyword{manip} -\keyword{mean} -\keyword{mean.} -\keyword{means} -\keyword{median,} -\keyword{mpg)} -\keyword{mpg),} -\keyword{mtcars} -\keyword{mtcars)} -\keyword{mtcars,} -\keyword{mu} -\keyword{mutate(expmu} -\keyword{n} -\keyword{not} -\keyword{of} -\keyword{ordered(cyl)))} -\keyword{ordered(cyl),} -\keyword{parameter} -\keyword{plot} -\keyword{posterior} -\keyword{practice} -\keyword{predictions} -\keyword{predictive} -\keyword{quickly} -\keyword{runs} -\keyword{same.} -\keyword{scale_color_brewer(palette} -\keyword{scale_fill_brewer(palette} -\keyword{select(hp,} -\keyword{seq_range(hp,} -\keyword{so} -\keyword{some} -\keyword{stat_lineribbon(aes(dist} -\keyword{the} -\keyword{theme_set(theme_light())} -\keyword{they} -\keyword{to} -\keyword{use} -\keyword{value} -\keyword{}} -\keyword{~} diff --git a/man/density_bins.Rd b/man/density_bins.Rd index d4c96633..5017cf26 100755 --- a/man/density_bins.Rd +++ b/man/density_bins.Rd @@ -44,6 +44,35 @@ the now-deprecated \code{\link[=predict_curve]{predict_curve()}}. \code{histogram_bins} computes a density histogram using \code{\link[=hist]{hist()}}. } +\examples{ +\dontrun{ + +library(ggplot2) +library(dplyr) +library(brms) +library(modelr) + +theme_set(theme_light()) + +m_mpg = brm(mpg ~ hp * cyl, data = mtcars) + +step = 1 +mtcars \%>\% + group_by(cyl) \%>\% + data_grid(hp = seq_range(hp, by = step)) \%>\% + add_predicted_draws(m_mpg) \%>\% + summarise(density_bins(.prediction), .groups = "drop") \%>\% + ggplot() + + geom_rect(aes( + xmin = hp - step/2, ymin = lower, ymax = upper, xmax = hp + step/2, + fill = ordered(cyl), alpha = density + )) + + geom_point(aes(x = hp, y = mpg, fill = ordered(cyl)), shape = 21, data = mtcars) + + scale_alpha_continuous(range = c(0, 1)) + + scale_fill_brewer(palette = "Set2") + +} +} \seealso{ See \code{\link[=add_predicted_draws]{add_predicted_draws()}} and \code{\link[=stat_lineribbon]{stat_lineribbon()}} for a better approach. These functions may be deprecated in the future. @@ -51,61 +80,4 @@ functions may be deprecated in the future. \author{ Matthew Kay } -\keyword{"Set2")} -\keyword{"drop")} -\keyword{%>%} -\keyword{))} -\keyword{*} -\keyword{+} -\keyword{-} -\keyword{.groups} -\keyword{1} -\keyword{1))} -\keyword{21,} -\keyword{=} -\keyword{\dontrun{} -\keyword{add_predicted_draws(m_mpg)} -\keyword{alpha} -\keyword{brm(mpg} -\keyword{by} -\keyword{c(0,} -\keyword{cyl,} -\keyword{data} -\keyword{data_grid(hp} -\keyword{density} -\keyword{fill} -\keyword{geom_point(aes(x} -\keyword{geom_rect(aes(} -\keyword{ggplot()} -\keyword{group_by(cyl)} -\keyword{hp} -\keyword{hp,} -\keyword{library(brms)} -\keyword{library(dplyr)} -\keyword{library(ggplot2)} -\keyword{library(modelr)} -\keyword{lower,} -\keyword{m_mpg} \keyword{manip} -\keyword{mpg,} -\keyword{mtcars} -\keyword{mtcars)} -\keyword{ordered(cyl)),} -\keyword{ordered(cyl),} -\keyword{scale_alpha_continuous(range} -\keyword{scale_fill_brewer(palette} -\keyword{seq_range(hp,} -\keyword{shape} -\keyword{step} -\keyword{step))} -\keyword{step/2,} -\keyword{summarise(density_bins(.prediction),} -\keyword{theme_set(theme_light())} -\keyword{upper,} -\keyword{xmax} -\keyword{xmin} -\keyword{y} -\keyword{ymax} -\keyword{ymin} -\keyword{}} -\keyword{~} diff --git a/man/gather_emmeans_draws.Rd b/man/gather_emmeans_draws.Rd index 444ff7d5..ca87d49c 100755 --- a/man/gather_emmeans_draws.Rd +++ b/man/gather_emmeans_draws.Rd @@ -46,115 +46,52 @@ and can be applied to various Bayesian models, like \link[rstanarm:stanreg-objec \code{gather_emmeans_draws} returns a tidy format data frame of draws from the marginal posterior distributions generated by \code{\link[emmeans:emmeans]{emmeans::emmeans()}}. } +\examples{ +\dontrun{ + +library(dplyr) +library(magrittr) +library(brms) +library(emmeans) + +# Here's an example dataset with a categorical predictor (`condition`) with several levels: +set.seed(5) +n = 10 +n_condition = 5 +ABC = tibble( + condition = rep(c("A","B","C","D","E"), n), + response = rnorm(n * 5, c(0,1,2,1,-1), 0.5) +) + +m = brm(response ~ condition, data = ABC, + # 1 chain / few iterations just so example runs quickly + # do not use in practice + chains = 1, iter = 500) + +# Once we've fit the model, we can use emmeans() (and functions +# from that package) to get whatever marginal distributions we want. +# For example, we can get marginal means by condition: +m \%>\% + emmeans(~ condition) \%>\% + gather_emmeans_draws() \%>\% + median_qi() + +# or we could get pairwise differences: +m \%>\% + emmeans(~ condition) \%>\% + contrast(method = "pairwise") \%>\% + gather_emmeans_draws() \%>\% + median_qi() + +# see the documentation of emmeans() for more examples of types of +# contrasts supported by that packge. + +} +} \seealso{ \code{\link[emmeans:emmeans]{emmeans::emmeans()}} } \author{ Matthew Kay } -\keyword{"pairwise")} -\keyword{#} -\keyword{%>%} -\keyword{(`condition`)} -\keyword{(and} -\keyword{)} -\keyword{*} -\keyword{/} -\keyword{0.5)} -\keyword{1} -\keyword{1,} -\keyword{10} -\keyword{5} -\keyword{5,} -\keyword{500)} -\keyword{=} -\keyword{ABC} -\keyword{ABC,} -\keyword{For} -\keyword{Here's} -\keyword{Once} -\keyword{\dontrun{} -\keyword{a} -\keyword{an} -\keyword{brm(response} -\keyword{by} -\keyword{c(0,1,2,1,-1),} -\keyword{can} -\keyword{categorical} -\keyword{chain} -\keyword{chains} -\keyword{condition} -\keyword{condition)} -\keyword{condition,} -\keyword{condition:} -\keyword{contrast(method} -\keyword{contrasts} -\keyword{could} -\keyword{data} -\keyword{dataset} -\keyword{differences:} -\keyword{distributions} -\keyword{do} -\keyword{documentation} -\keyword{emmeans()} -\keyword{emmeans(~} -\keyword{example} -\keyword{example,} -\keyword{examples} -\keyword{few} -\keyword{fit} -\keyword{for} -\keyword{from} -\keyword{functions} -\keyword{gather_emmeans_draws()} -\keyword{get} -\keyword{in} -\keyword{iter} -\keyword{iterations} -\keyword{just} -\keyword{levels:} -\keyword{library(brms)} -\keyword{library(dplyr)} -\keyword{library(emmeans)} -\keyword{library(magrittr)} -\keyword{m} \keyword{manip} -\keyword{marginal} -\keyword{means} -\keyword{median_qi()} -\keyword{model,} -\keyword{more} -\keyword{n} -\keyword{n),} -\keyword{n_condition} -\keyword{not} -\keyword{of} -\keyword{or} -\keyword{package)} -\keyword{packge.} -\keyword{pairwise} -\keyword{practice} -\keyword{predictor} -\keyword{quickly} -\keyword{rep(c("A","B","C","D","E"),} -\keyword{response} -\keyword{rnorm(n} -\keyword{runs} -\keyword{see} -\keyword{set.seed(5)} -\keyword{several} -\keyword{so} -\keyword{supported} -\keyword{that} -\keyword{the} -\keyword{tibble(} -\keyword{to} -\keyword{types} -\keyword{use} -\keyword{want.} -\keyword{we} -\keyword{we've} -\keyword{whatever} -\keyword{with} -\keyword{}} -\keyword{~} diff --git a/man/recover_types.Rd b/man/recover_types.Rd index 55e67f5b..048cfc15 100755 --- a/man/recover_types.Rd +++ b/man/recover_types.Rd @@ -52,8 +52,7 @@ Additional data types can be supported by providing a custom implementation of the generic function \code{as_constructor}. } \examples{ -\dontshow{if (requireNamespace("rstan", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -\donttest{ +\dontrun{ library(dplyr) library(magrittr) @@ -122,7 +121,6 @@ m \%>\% median_qi() } -\dontshow{\}) # examplesIf} } \seealso{ \code{\link[=spread_draws]{spread_draws()}}, \code{\link[=gather_draws]{gather_draws()}}, \code{\link[=compose_data]{compose_data()}}. diff --git a/man/sample_draws.Rd b/man/sample_draws.Rd index 526e6ab6..e73be59f 100644 --- a/man/sample_draws.Rd +++ b/man/sample_draws.Rd @@ -26,96 +26,36 @@ the naive approach of using \code{filter} with the \code{.draw} column will give select a different sample within each group. \code{sample_draws()} ensures the same sample is selected within each group. } +\examples{ +\dontrun{ + +library(ggplot2) +library(dplyr) +library(brms) +library(modelr) + +theme_set(theme_light()) + +m_mpg = brm(mpg ~ hp * cyl, data = mtcars, + # 1 chain / few iterations just so example runs quickly + # do not use in practice + chains = 1, iter = 500) + +# draw 100 fit lines from the posterior and overplot them +mtcars \%>\% + group_by(cyl) \%>\% + data_grid(hp = seq_range(hp, n = 101)) \%>\% + add_epred_draws(m_mpg) \%>\% + # NOTE: only use sample_draws here when making spaghetti plots; for + # plotting intervals it is always best to use all draws + sample_draws(100) \%>\% + ggplot(aes(x = hp, y = mpg, color = ordered(cyl))) + + geom_line(aes(y = .epred, group = paste(cyl, .draw)), alpha = 0.25) + + geom_point(data = mtcars) + +} +} \author{ Matthew Kay } -\keyword{#} -\keyword{%>%} -\keyword{*} -\keyword{+} -\keyword{.draw)),} -\keyword{.epred,} -\keyword{/} -\keyword{0.25)} -\keyword{1} -\keyword{1,} -\keyword{100} -\keyword{101))} -\keyword{500)} -\keyword{=} -\keyword{NOTE:} -\keyword{\dontrun{} -\keyword{add_epred_draws(m_mpg)} -\keyword{all} -\keyword{alpha} -\keyword{always} -\keyword{and} -\keyword{best} -\keyword{brm(mpg} -\keyword{chain} -\keyword{chains} -\keyword{color} -\keyword{cyl,} -\keyword{data} -\keyword{data_grid(hp} -\keyword{do} -\keyword{draw} -\keyword{draws} -\keyword{example} -\keyword{few} -\keyword{fit} -\keyword{for} -\keyword{from} -\keyword{geom_line(aes(y} -\keyword{geom_point(data} -\keyword{ggplot(aes(x} -\keyword{group} -\keyword{group_by(cyl)} -\keyword{here} -\keyword{hp} -\keyword{hp,} -\keyword{in} -\keyword{intervals} -\keyword{is} -\keyword{it} -\keyword{iter} -\keyword{iterations} -\keyword{just} -\keyword{library(brms)} -\keyword{library(dplyr)} -\keyword{library(ggplot2)} -\keyword{library(modelr)} -\keyword{lines} -\keyword{m_mpg} -\keyword{making} \keyword{manip} -\keyword{mpg,} -\keyword{mtcars} -\keyword{mtcars)} -\keyword{mtcars,} -\keyword{n} -\keyword{not} -\keyword{only} -\keyword{ordered(cyl)))} -\keyword{overplot} -\keyword{paste(cyl,} -\keyword{plots;} -\keyword{plotting} -\keyword{posterior} -\keyword{practice} -\keyword{quickly} -\keyword{runs} -\keyword{sample_draws} -\keyword{sample_draws(100)} -\keyword{seq_range(hp,} -\keyword{so} -\keyword{spaghetti} -\keyword{the} -\keyword{them} -\keyword{theme_set(theme_light())} -\keyword{to} -\keyword{use} -\keyword{when} -\keyword{y} -\keyword{}} -\keyword{~}