plot.NBKP: Plot Fitted NBKP Models

View source: R/plot_NBKP.R

plot.NBKPR Documentation

Plot Fitted NBKP Models

Description

Visualizes fitted NBKP (count) models according to the input dimensionality. For 1D inputs, it shows predicted mean counts with credible intervals and observed count data. For 2D inputs, it generates contour plots of posterior summaries. For higher-dimensional inputs, users must specify which dimensions to plot.

Usage

## S3 method for class 'NBKP'
plot(x, only_mean = FALSE, n_grid = 80, dims = NULL, ...)

Arguments

x

An object of class "NBKP", typically returned by fit_NBKP.

only_mean

Logical. If TRUE, only the predicted mean surface is plotted for 2D inputs. Default is FALSE.

n_grid

Positive integer specifying the number of grid points per dimension for constructing the prediction grid. Larger values produce smoother and more detailed surfaces, but increase computation time. Default is 80.

dims

Integer vector indicating which input dimensions to plot. Must have length 1 (for 1D) or 2 (for 2D). If NULL (default), all dimensions are used when their number is <= 2.

...

Additional arguments passed to internal plotting routines (currently unused).

Details

The plotting behavior depends on the dimensionality of the input covariates:

  • 1D inputs:

    • The function plots the posterior mean curve with a shaded 95 interval, overlaid with the observed counts.

  • 2D inputs:

    • The function generates contour plots over a 2D prediction grid.

    • Users can choose to plot only the predictive mean surface (only_mean = TRUE) or a set of four summary plots (only_mean = FALSE):

      1. Predictive mean

      2. 97.5th percentile (upper bound of 95

      3. Predictive variance

      4. 2.5th percentile (lower bound of 95

  • Input dimensions greater than 2:

    • The function does not automatically support visualization and will terminate with an error.

    • Users must specify which dimensions to visualize via the dims argument (length 1 or 2).

Value

This function is called for its side effects and does not return a value. It produces plots visualizing the predicted counts, credible intervals, and posterior summaries.

References

Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling. arXiv. https://doi.org/10.48550/arxiv.2508.10447.

See Also

fit_NBKP for fitting NBKP models; predict.NBKP for generating predictions from fitted NBKP models.

Examples


# -------------------------- 1D Example --------------------------
set.seed(123)

# Define true mean function
true_mu_fun <- function(x) {
  exp(sin(x) + 0.5)
}

n <- 30
Xbounds <- matrix(c(-2, 2), nrow=1)
X <- tgp::lhs(n = n, rect = Xbounds)
true_mu <- true_mu_fun(X)
y <- rnbinom(n, size = 1, mu = true_mu)

# Fit NBKP model
model1 <- fit_NBKP(X, y, Xbounds=Xbounds)

# Plot results
plot(model1)

# -------------------------- 2D Example --------------------------
set.seed(123)

# Define 2D latent function and mean transformation
true_mu_fun <- function(X) {
  if(is.null(nrow(X))) X <- matrix(X, nrow=1)
  x1 <- 4*X[,1] - 2
  x2 <- 4*X[,2] - 2
  f <- sin(2*pi*x1) * cos(2*pi*x2)
  return(exp(f))
}

n <- 100
Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2)
X <- tgp::lhs(n = n, rect = Xbounds)
true_mu <- true_mu_fun(X)
y <- rnbinom(n, size = 0.5, mu = true_mu)

# Fit NBKP model
model2 <- fit_NBKP(X, y, Xbounds=Xbounds)

# Plot results
plot(model2, n_grid = 50)



NBKP documentation built on June 18, 2026, 1:06 a.m.