Learn R Programming

rstanarm (version 2.9.0-3)

plot.stanreg: Plot method for stanreg objects

Description

For models fit using MCMC or one of the variational approximations, there are a variety of plots that can be generated. For models fit using optimization, the regression coefficients and standard errors are passed to coefplot (arm).

Usage

## S3 method for class 'stanreg':
plot(x, plotfun = NULL, pars = NULL, regex_pars = NULL,
  ...)

Arguments

x
A fitted model object returned by one of the rstanarm modeling functions. See stanreg-objects.
plotfun
A character string naming the plotting function to apply to the stanreg object. See plots for the names and descriptions. Also see the Examples section below. plotfun can be either the full nam
pars
An optional character vector of parameter names.
regex_pars
An optional character vector of regular expressions to use for parameter selection. regex_pars can be used in place of pars or in addition to pars. Currently, al
...
Additional arguments to pass to plotfun (see plots) or, for models fit using algorithm="optimizing", coefplot.

Value

  • In most cases, a ggplot object (or several) that can be further customized using the ggplot2 package. The exception is for models fit using "optimizing" as the estimation algorithm, in which case a plot is produced but nothing is returned.

See Also

plots for details on the individual plotting functions.

Examples

Run this code
# Use rstanarm example model
fit <- example_model

# Intervals and point estimates
plot(fit) + 
ggplot2::ggtitle("Posterior medians \n with 80% and 95% credible intervals")
plot(fit, pars = "size", regex_pars = "period", 
     ci_level = 0.95, outer_level = 1, show_density = TRUE)

# Traceplot
# note: rstanarm doesn't store the warmup draws by default 
(trace <- plot(fit, "trace", pars = "(Intercept)"))
trace + ggplot2::scale_color_discrete()
trace + ggplot2::scale_color_manual(values = c("maroon", "skyblue2"))

# Distributions 
plot_title <- ggplot2::ggtitle("Posterior Distributions")
plot(fit, "hist", fill = "skyblue", regex_pars = "period") + plot_title
plot(fit, "dens", pars = "(Intercept)", regex_pars = "period", 
     separate_chains = TRUE, alpha = 1/3) + plot_title

# Scatterplot
plot(fit, plotfun = "scat", pars = paste0("period", 2:3))
plot(fit, plotfun = "scat", pars = c("(Intercept)", "size"), 
     color = "black", size = 5, alpha = 0.2)

# Some diagnostics
plot(fit, "rhat")
plot(fit, "ess")

# Using regex_pars
plot(fit, regex_pars = "period")
plot(fit, regex_pars = "herd:1")
plot(fit, regex_pars = "herd:1\\]")
plot(fit, regex_pars = "herd:[279]")
plot(fit, regex_pars = "herd:[279]|period2")
plot(fit, regex_pars = c("herd:[279]", "period2"))

# For graphical posterior predictive checks see 
# help("pp_check", package = "rstanarm")

Run the code above in your browser using DataLab