plot.mcpfit: Plot full fits

View source: R/plot.R

plot.mcpfitR Documentation

Plot full fits

Description

Plot prior or posterior model draws on top of data. Use plot_pars to plot individual parameter estimates.

Usage

## S3 method for class 'mcpfit'
plot(
  x,
  facet_by = NULL,
  lines = 25,
  geom_data = "point",
  cp_dens = TRUE,
  q_fit = FALSE,
  q_predict = FALSE,
  rate = TRUE,
  prior = FALSE,
  which_y = "ct",
  arma = TRUE,
  nsamples = 2000,
  scale = "response",
  ...
)

Arguments

x

An mcpfit object

facet_by

String. Name of a varying group.

lines

Positive integer or FALSE. Number of lines (posterior draws). FALSE or lines = 0 plots no lines. Note that lines always plot fitted values - not predicted. For prediction intervals, see the q_predict argument.

geom_data

String. One of "point", "line" (good for time-series), or FALSE (don not plot).

cp_dens

TRUE/FALSE. Plot posterior densities of the change point(s)? Currently does not respect facet_by. This will be added in the future.

q_fit

Whether to plot quantiles of the posterior (fitted value).

  • TRUE Add 2.5% and 97.5% quantiles. Corresponds to q_fit = c(0.025, 0.975).

  • FALSE No quantiles

  • A vector of quantiles. For example, quantiles = 0.5 plots the median and quantiles = c(0.2, 0.8) plots the 20% and 80% quantiles.

q_predict

Same as q_fit, but for the prediction interval.

rate

Boolean. For binomial models, plot on raw data (rate = FALSE) or response divided by number of trials (rate = TRUE). If FALSE, linear interpolation on trial number is used to infer trials at a particular x.

prior

TRUE/FALSE. Plot using prior samples? Useful for mcp(..., sample = "both")

which_y

What to plot on the y-axis. One of

  • "ct": The central tendency which is often the mean after applying the link function.

  • "sigma": The variance

  • "ar1", "ar2", etc. depending on which order of the autoregressive effects you want to plot.

arma

Whether to include autoregressive effects.

  • TRUE Compute autoregressive residuals. Requires the response variable in newdata.

  • FALSE Disregard the autoregressive effects. For family = gaussian(), predict() just use sigma for residuals.

nsamples

Integer or NULL. Number of samples to return/summarise. If there are varying effects, this is the number of samples from each varying group. NULL means "all". Ignored if both are FALSE. More samples trade speed for accuracy.

scale

One of

  • "response": return on the observed scale, i.e., after applying the inverse link function.

  • "linear": return on the parameter scale (where the linear trends are modelled).

...

Currently ignored.

Details

plot() uses fit$simulate() on posterior samples. These represent the (joint) posterior distribution.

Value

A ggplot2 object.

Author(s)

Jonas Kristoffer Lindeløv jonas@lindeloev.dk

Examples

# Typical usage. demo_fit is an mcpfit object.
plot(demo_fit)

plot(demo_fit, prior = TRUE)  # The prior

plot(demo_fit, lines = 0, q_fit = TRUE)  # 95% HDI without lines
plot(demo_fit, q_predict = c(0.1, 0.9))  # 80% prediction interval
plot(demo_fit, which_y = "sigma", lines = 100)  # The variance parameter on y

# Show a panel for each varying effect
# plot(fit, facet_by = "my_column")

# Customize plots using regular ggplot2
library(ggplot2)
plot(demo_fit) + theme_bw(15) + ggtitle("Great plot!")



mcp documentation built on May 29, 2024, 6:15 a.m.