Learn R Programming

bayesQRsurvey (version 0.1.4)

plot.bqr.svy: Plot Method for Bayesian Weighted Quantile Regression

Description

Plot method for objects of class bqr.svy produced by bqr.svy(). It can display fitted quantile curves, coefficient–quantile profiles, MCMC trace plots, and posterior densities.

Usage

# S3 method for bqr.svy
plot(
  x,
  y = NULL,
  type = c("fit", "quantile", "trace", "density"),
  predictor = NULL,
  tau = NULL,
  which = NULL,
  add_points = TRUE,
  combine = TRUE,
  show_ci = FALSE,
  ci_probs = c(0.1, 0.9),
  at = NULL,
  grid_length = 200,
  points_alpha = 0.4,
  point_size = 1.5,
  line_size = 1.2,
  main = NULL,
  use_ggplot = TRUE,
  theme_style = c("minimal", "classic", "bw", "light"),
  color_palette = c("viridis", "plasma", "set2", "dark2"),
  add_h0 = FALSE,
  add_ols = FALSE,
  ols_fit = NULL,
  ols_weights = NULL,
  ...
)

# S3 method for bwqr_fit plot(x, ...)

# S3 method for bwqr_fit_multi plot(x, ...)

Value

invisible(NULL) for base R graphics, or a ggplot object if use_ggplot = TRUE.

Arguments

x

Object of class bqr.svy.

y

Ignored (S3 signature).

type

One of "fit", "quantile", "trace", "density".

predictor

(fit) Name of a numeric predictor; if NULL, the first numeric predictor (excluding the response) is used.

tau

Quantile(s) to plot; must appear in x$quantile. If NULL, all available are used.

which

(quantile/trace/density) Coefficient name or index to display. The default is the first coefficient associated with the first variable in the model.

add_points

(fit) Logical; overlay observed data points.

combine

(fit) Logical; if multiple tau: TRUE overlays curves in one panel; FALSE uses one panel per quantile.

show_ci

(fit) Logical; draw credible bands.

ci_probs

(fit) Length-2 numeric vector with lower/upper probabilities for credible bands.

at

(fit) Named list of fixed values for non-predictor covariates (see Details).

grid_length

(fit) Integer; number of points in the predictor grid.

points_alpha

(fit) Point transparency in [0,1].

point_size

(fit) Point size.

line_size

(fit/quantile) Line width for fitted/summary lines.

main

Optional main title.

use_ggplot

Logical; if TRUE, return a ggplot object.

theme_style

(ggplot) One of "minimal", "classic", "bw", "light".

color_palette

(ggplot) One of "viridis", "plasma", "set2", "dark2".

add_h0

(quantile) Logical; add a horizontal reference at \(y = 0\).

add_ols

(quantile) Logical; add the OLS estimate (dotted line) for the selected coefficient.

ols_fit

(quantile) Optional precomputed lm object; if NULL, an lm() is fitted internally using x$model and x$terms.

ols_weights

(quantile) Optional numeric vector of weights when fitting OLS internally (length must match nrow(x$model)).

...

Accepted for compatibility; ignored by internal plotting code.

Details

Supported plot types:

  • type = "fit": Fitted quantile curves versus a single numeric predictor. Optionally overlay observed points and credible bands. Other covariates can be held fixed via at.

  • type = "quantile": A single coefficient as a function of the quantile \(\tau\). Optionally add a reference line at 0 and the corresponding OLS estimate.

  • type = "trace": MCMC trace for one selected coefficient at a chosen \(\tau\).

  • type = "density": Posterior density for one selected coefficient at a chosen \(\tau\).

Notes:

  • tau must be included in x$quantile. If NULL, all available quantiles in the object are used.

  • For type = "fit", predictor must be a numeric column in the original model. If NULL, the first numeric predictor (different from the response) is chosen automatically.

  • For type = "fit", at is a named list (list(var = value, ...)) used to fix other covariates while plotting versus predictor. Provide valid levels for factors.

  • When use_ggplot = TRUE, a ggplot object is returned and the appearance is controlled by theme_style and color_palette. Otherwise, base graphics are used and the function returns invisible(NULL).

Examples

Run this code
# \donttest{
data(mtcars)
fit <- bqr.svy(mpg ~ wt + hp + cyl, data = mtcars,
               quantile = c(0.5, 0.75), method = "ald",
               niter = 20000, burnin = 10000, thin = 5)

plot(fit, type = "fit", predictor = "wt", show_ci = TRUE)
plot(fit, type = "quantile", which = "wt", add_h0 = TRUE, add_ols = TRUE)
plot(fit, type = "trace", which = "wt", tau = 0.5)
plot(fit, type = "density", which = "wt", tau = 0.5)
# }

Run the code above in your browser using DataLab