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

View source: R/plot_quantile.R

plot.bqr.svyR Documentation

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 class 'bqr.svy'
plot(
  x,
  y = NULL,
  type = c("fit", "quantile", "trace", "density"),
  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 = 2.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 class 'bwqr_fit'
plot(x, ...)

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

Arguments

x

Object of class bqr.svy.

y

Ignored (S3 signature).

type

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

tau

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

which

Variable name(s) or coefficient index(es) to display. For type = "fit", the name of a numeric predictor to plot on the x-axis (if NULL, the first numeric predictor is used). For type = "quantile", a character vector of coefficient names or integer vector of indices; when more than one is given the plot uses facet_wrap to show all coefficients in a single figure. For type = "trace" and type = "density", a single coefficient name or index (default: first coefficient 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-plotted 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 (selected via which). 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", which must name 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 the selected 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).

Value

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

Examples


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", which = "wt", show_ci = TRUE)
plot(fit, type = "quantile", which = "wt", add_h0 = TRUE, add_ols = TRUE)
plot(fit, type = "quantile", which = c("(Intercept)", "wt", "hp", "cyl"),
     add_h0 = TRUE, add_ols = TRUE)
plot(fit, type = "trace", which = "wt", tau = 0.5)
plot(fit, type = "density", which = "wt", tau = 0.5)



bayesQRsurvey documentation built on April 7, 2026, 1:06 a.m.