| Title: | Beta Kernel Process Modeling with Negative Binomial Response |
|---|---|
| Description: | An extension of the Beta Kernel Process model designed to handle negative binomial responses for count data modeling, building upon Zhao, Qing and Xu (2025) <doi:10.48550/arXiv.2508.10447>. |
| Authors: | Xueqin Li [aut], Xingyue Fa [aut], Shanqing Yin [aut, cre], Yi Zhuo [aut] |
| Maintainer: | Shanqing Yin <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-06-17 20:13:22 UTC |
| Source: | https://github.com/cran/NBKP |
Fits a Negative Binomial Kernel Process (NBKP) model to count response data using local kernel smoothing. The method constructs a flexible latent mean surface by updating Gamma priors with kernel-weighted observations.
fit_NBKP( X, y, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 0.1, mu0 = mean(y), kernel = c("gaussian", "matern52", "matern32"), loss = c("mse", "nll"), n_multi_start = NULL, theta = NULL )fit_NBKP( X, y, Xbounds = NULL, prior = c("noninformative", "fixed", "adaptive"), r0 = 0.1, mu0 = mean(y), kernel = c("gaussian", "matern52", "matern32"), loss = c("mse", "nll"), n_multi_start = NULL, theta = NULL )
X |
A numeric input matrix of size |
y |
A numeric vector of observed counts (length |
Xbounds |
Optional |
prior |
Global prior type: |
r0 |
Global prior precision (used when |
mu0 |
Global prior mean (used when |
kernel |
Kernel function for local weighting: |
loss |
Loss function for kernel hyperparameter tuning: |
n_multi_start |
Number of random initializations for multi-start
optimization. Default is |
theta |
Optional. A positive scalar or numeric vector of length |
A list of class "NBKP" containing the fitted NBKP model,
including:
theta_opt |
Optimized kernel hyperparameters (lengthscales). |
kernel |
Kernel function used, as a string. |
loss |
Loss function used for hyperparameter tuning. |
loss_min |
Minimum loss achieved during optimization, or
|
X |
Original input matrix ( |
Xnorm |
Normalized input matrix scaled to |
Xbounds |
Normalization bounds for each input dimension ( |
y |
Observed counts. |
phi |
Estimated negative binomial dispersion parameter. |
prior |
Type of prior used. |
r0 |
Prior precision parameter. |
mu0 |
Prior mean (for fixed priors). |
alpha0 |
Prior Gamma shape parameter |
beta0 |
Prior Gamma rate parameter |
alpha_n |
Posterior shape parameter |
beta_n |
Posterior rate parameter |
Xueqin Li
Zhao J, Qing K, Xu J (2025). BKP: An R Package for Beta Kernel Process Modeling.
predict.NBKP, plot.NBKP, summary.NBKP
# -------------------------- 1D Example -------------------------- set.seed(123) true_mu_fun <- function(x) { exp(sin(x) + 0.5) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow=1) X <- matrix(seq(-2, 2, length.out = n)) true_mu <- true_mu_fun(X) y <- rnbinom(n, size = 1, mu = true_mu) model1 <- fit_NBKP(X, y, Xbounds=Xbounds) print(model1)# -------------------------- 1D Example -------------------------- set.seed(123) true_mu_fun <- function(x) { exp(sin(x) + 0.5) } n <- 30 Xbounds <- matrix(c(-2, 2), nrow=1) X <- matrix(seq(-2, 2, length.out = n)) true_mu <- true_mu_fun(X) y <- rnbinom(n, size = 1, mu = true_mu) model1 <- fit_NBKP(X, y, Xbounds=Xbounds) print(model1)
Compute the posterior fitted values from a fitted NBKP object.
For a NBKP object, this returns the posterior mean count from the Gamma‑Negative Binomial conjugate posterior.
## S3 method for class 'NBKP' fitted(object, ...)## S3 method for class 'NBKP' fitted(object, ...)
object |
An object of class |
... |
Additional arguments (currently unused). |
For a NBKP model, the fitted values correspond to the posterior mean count,
computed from the Gamma Kernel Process posterior distribution.
A numeric vector containing posterior mean count estimates at the training inputs.
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.
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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Extract fitted values fitted(model)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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Extract fitted values fitted(model)
Computes prior parameters for Beta Kernel Process (BKP, binary) or Dirichlet Kernel Process (DKP, multi-class) models. Supports three prior strategies: noninformative, fixed, adaptive.
get_prior( prior = c("noninformative", "fixed", "adaptive"), model = c("BKP", "DKP"), r0 = 2, p0 = NULL, y = NULL, m = NULL, Y = NULL, K = NULL )get_prior( prior = c("noninformative", "fixed", "adaptive"), model = c("BKP", "DKP"), r0 = 2, p0 = NULL, y = NULL, m = NULL, Y = NULL, K = NULL )
prior |
Character string; prior type. One of: '"noninformative"', '"fixed"', '"adaptive"'. |
model |
Character string; model type. One of: '"BKP"' (binary), '"DKP"' (multi-class). |
r0 |
Numeric; prior precision (positive scalar, default = 2). |
p0 |
Numeric; global prior mean. BKP: scalar in (0,1); DKP: vector summing to 1. |
y |
Numeric vector; observed success counts (BKP only). |
m |
Numeric vector; number of trials (BKP only, same length as 'y'). |
Y |
Numeric matrix; observed class counts (DKP only, n × q). |
K |
Numeric matrix; precomputed kernel matrix. |
Prior strategies: * 'noninformative': flat prior (Beta(1,1) or Dirichlet(1,...,1)). * 'fixed': global constant prior. * 'adaptive': kernel-smoothed local prior, estimated from observed data.
For BKP: a list with 'alpha0' and 'beta0'. For DKP: a matrix 'alpha0' of prior Dirichlet parameters.
# BKP example set.seed(123) n <- 10 X <- matrix(runif(n*2), ncol = 2) y <- rbinom(n, size = 5, prob = 0.6) m <- rep(5, n) K <- matrix(1, n, n) prior_bkp <- get_prior( model = "BKP", prior = "adaptive", r0 = 2, y = y, m = m, K = K )# BKP example set.seed(123) n <- 10 X <- matrix(runif(n*2), ncol = 2) y <- rbinom(n, size = 5, prob = 0.6) m <- rep(5, n) K <- matrix(1, n, n) prior_bkp <- get_prior( model = "BKP", prior = "adaptive", r0 = 2, y = y, m = m, K = K )
Computes the kernel matrix between two sets of input locations using a specified kernel function. Supports both isotropic and anisotropic lengthscales. Available kernels include the Gaussian, Matérn 5/2, and Matérn 3/2.
kernel_matrix( X, Xprime = NULL, theta = 0.1, kernel = c("gaussian", "matern52", "matern32"), anisotropic = TRUE )kernel_matrix( X, Xprime = NULL, theta = 0.1, kernel = c("gaussian", "matern52", "matern32"), anisotropic = TRUE )
X |
A numeric matrix (or vector) of input locations with shape |
Xprime |
An optional numeric matrix of input locations with shape |
theta |
A positive numeric value or vector specifying the kernel
lengthscale(s). If a scalar, the same lengthscale is applied to all input
dimensions. If a vector, it must be of length |
kernel |
A character string specifying the kernel function. Must be one
of |
anisotropic |
Logical. If |
Let and denote two input points.
The scaled distance is defined as
The available kernels are defined as:
Gaussian:
Matérn 5/2:
Matérn 3/2:
The function performs consistency checks on input dimensions and
automatically broadcasts theta when it is a scalar.
A numeric matrix of size , where each element
gives the kernel similarity between input and
.
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.
Rasmussen, C. E., & Williams, C. K. I. (2006). Gaussian Processes for Machine Learning. MIT Press.
# Basic usage with default Xprime = X X <- matrix(runif(20), ncol = 2) K1 <- kernel_matrix(X, theta = 0.2, kernel = "gaussian") # Anisotropic lengthscales with Matérn 5/2 K2 <- kernel_matrix(X, theta = c(0.1, 0.3), kernel = "matern52") # Isotropic Matérn 3/2 K3 <- kernel_matrix(X, theta = 1, kernel = "matern32", anisotropic = FALSE) # Use Xprime different from X Xprime <- matrix(runif(10), ncol = 2) K4 <- kernel_matrix(X, Xprime, theta = 0.2, kernel = "gaussian")# Basic usage with default Xprime = X X <- matrix(runif(20), ncol = 2) K1 <- kernel_matrix(X, theta = 0.2, kernel = "gaussian") # Anisotropic lengthscales with Matérn 5/2 K2 <- kernel_matrix(X, theta = c(0.1, 0.3), kernel = "matern52") # Isotropic Matérn 3/2 K3 <- kernel_matrix(X, theta = 1, kernel = "matern32", anisotropic = FALSE) # Use Xprime different from X Xprime <- matrix(runif(10), ncol = 2) K4 <- kernel_matrix(X, Xprime, theta = 0.2, kernel = "gaussian")
Computes prediction loss for kernel process models (BKP binary / DKP multi-class) during kernel hyperparameter tuning. Supports Brier score (MSE) and log-loss (cross-entropy) with leave-one-out cross-validation (LOOCV).
loss_fun( gamma, Xnorm, y = NULL, m = NULL, Y = NULL, model = c("BKP", "DKP"), prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = NULL, loss = c("brier", "log_loss"), kernel = c("gaussian", "matern52", "matern32") )loss_fun( gamma, Xnorm, y = NULL, m = NULL, Y = NULL, model = c("BKP", "DKP"), prior = c("noninformative", "fixed", "adaptive"), r0 = 2, p0 = NULL, loss = c("brier", "log_loss"), kernel = c("gaussian", "matern52", "matern32") )
gamma |
Numeric vector; log10-transformed kernel lengthscale parameters. |
Xnorm |
Numeric matrix; normalized input matrix (values in [0,1]). |
y |
Numeric vector; observed success counts (BKP model only). |
m |
Numeric vector; number of trials (BKP model only). |
Y |
Numeric matrix; observed class counts (DKP model only). |
model |
Character string; model type: |
prior |
Character string; prior type: |
r0 |
Numeric scalar; prior precision (default = 2). |
p0 |
Numeric; global prior mean (for fixed prior). |
loss |
Character string; loss type: |
kernel |
Character string; kernel function: |
This function is used internally to optimize kernel lengthscales.
It converts log10 hyperparameters gamma to lengthscales theta,
computes the kernel matrix, applies LOOCV (diag(K)=0), and calculates loss
between estimated and empirical probabilities.
Numeric scalar; loss value to minimize during optimization.
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
# -------------------------- BKP Example -------------------------- set.seed(123) n <- 10 Xnorm <- matrix(runif(2 * n), ncol = 2) m <- rep(10, n) y <- rbinom(n, size = m, prob = runif(n)) loss_fun(gamma = rep(0, 2), Xnorm = Xnorm, y = y, m = m, model = "BKP") # -------------------------- DKP Example -------------------------- set.seed(123) n <- 10 q <- 3 Xnorm <- matrix(runif(2 * n), ncol = 2) Y <- matrix(rmultinom(n, size = 10, prob = rep(1/q, q)), nrow = n, byrow = TRUE) loss_fun(gamma = rep(0, 2), Xnorm = Xnorm, Y = Y, model = "DKP")# -------------------------- BKP Example -------------------------- set.seed(123) n <- 10 Xnorm <- matrix(runif(2 * n), ncol = 2) m <- rep(10, n) y <- rbinom(n, size = m, prob = runif(n)) loss_fun(gamma = rep(0, 2), Xnorm = Xnorm, y = y, m = m, model = "BKP") # -------------------------- DKP Example -------------------------- set.seed(123) n <- 10 q <- 3 Xnorm <- matrix(runif(2 * n), ncol = 2) Y <- matrix(rmultinom(n, size = 10, prob = rep(1/q, q)), nrow = n, byrow = TRUE) loss_fun(gamma = rep(0, 2), Xnorm = Xnorm, Y = Y, model = "DKP")
Retrieve the key model parameters from a fitted NBKP object.
This includes the optimized kernel hyperparameters, posterior Gamma parameters,
and the negative‑binomial dispersion parameter.
parameter(object, ...) ## S3 method for class 'NBKP' parameter(object, ...)parameter(object, ...) ## S3 method for class 'NBKP' parameter(object, ...)
object |
An object of class |
... |
Additional arguments (currently unused). |
A named list containing:
theta: Estimated kernel hyperparameters.
phi: Negative‑binomial dispersion parameter.
alpha_n: Posterior Gamma parameters.
beta_n: Posterior Gamma parameters.
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.
fit_NBKP for fitting NBKP models.
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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Extract posterior and kernel parameters parameter(model)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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Extract posterior and kernel parameters parameter(model)
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.
## S3 method for class 'NBKP' plot(x, only_mean = FALSE, n_grid = 80, dims = NULL, ...)## S3 method for class 'NBKP' plot(x, only_mean = FALSE, n_grid = 80, dims = NULL, ...)
x |
An object of class |
only_mean |
Logical. If |
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 |
dims |
Integer vector indicating which input dimensions to plot. Must
have length 1 (for 1D) or 2 (for 2D). If |
... |
Additional arguments passed to internal plotting routines (currently unused). |
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):
Predictive mean
97.5th percentile (upper bound of 95
Predictive variance
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).
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.
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.
fit_NBKP for fitting NBKP models;
predict.NBKP for generating predictions from fitted NBKP models.
# -------------------------- 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)# -------------------------- 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)
Generate predictions from a fitted NBKP model.
## S3 method for class 'NBKP' predict(object, newdata = NULL, ci_level = 0.95, ...)## S3 method for class 'NBKP' predict(object, newdata = NULL, ci_level = 0.95, ...)
object |
Fitted NBKP model object. |
newdata |
Matrix of new input points. If NULL, uses training data. |
ci_level |
Credible interval level. |
... |
Additional arguments. |
A list containing predictions, credible intervals, and variance.
Provides formatted console output for fitted NBKP model objects and their predictions. The following specialized methods are supported:
print.NBKP – display fitted NBKP model objects.
print.predict_NBKP – display posterior predictive results.
## S3 method for class 'NBKP' print(x, ...) ## S3 method for class 'predict_NBKP' print(x, ...)## S3 method for class 'NBKP' print(x, ...) ## S3 method for class 'predict_NBKP' print(x, ...)
x |
An object of class |
... |
Additional arguments passed to the generic |
Invisibly returns the input object. Called for the side effect of printing human-readable summaries to the console.
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.
fit_NBKP for model fitting;
predict.NBKP for posterior prediction.
# -------------------------- 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) print(model1) # fitted object pred1 <- predict(model1) print(pred1) # predictions # -------------------------- 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) print(model2) pred2 <- predict(model2) print(pred2)# -------------------------- 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) print(model1) # fitted object pred1 <- predict(model1) print(pred1) # predictions # -------------------------- 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) print(model2) pred2 <- predict(model2) print(pred2)
Compute posterior quantiles from a fitted NBKP model.
This returns posterior quantiles of the latent mean count from the Gamma posterior distribution.
## S3 method for class 'NBKP' quantile(x, probs = c(0.025, 0.5, 0.975), ...)## S3 method for class 'NBKP' quantile(x, probs = c(0.025, 0.5, 0.975), ...)
x |
An object of class |
probs |
Numeric vector of probabilities specifying which posterior quantiles to return.
Defaults to |
... |
Additional arguments (currently unused). |
For a NBKP model, posterior quantiles are computed from the Gamma Kernel Process posterior distribution.
A numeric vector (if length(probs) = 1) or numeric matrix (if length(probs) > 1)
of posterior quantiles. Rows correspond to observations, and columns correspond to the requested probabilities.
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.
fit_NBKP for model fitting.
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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Extract posterior quantiles quantile(model)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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Extract posterior quantiles quantile(model)
Generates random draws from the posterior predictive distribution
of a fitted NBKP model at specified input locations.
For NBKP models, posterior samples are generated from Gamma distributions characterizing latent mean count values for negative‑binomial observations.
## S3 method for class 'NBKP' simulate(object, nsim = 1, seed = NULL, Xnew = NULL, ...)## S3 method for class 'NBKP' simulate(object, nsim = 1, seed = NULL, Xnew = NULL, ...)
object |
An object of class |
nsim |
Number of posterior samples to generate (default |
seed |
Optional integer seed for reproducibility. |
Xnew |
A numeric matrix or vector of new input locations at which simulations are generated. |
... |
Additional arguments (currently unused). |
A list with the following components:
samplesA numeric matrix of size nrow(Xnew) × nsim,
where each column corresponds to one posterior draw of latent mean counts.
meanA numeric vector of posterior mean count values
at each Xnew.
XThe training input matrix used to fit the NBKP model.
XnewThe new input locations at which simulations are generated.
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.
fit_NBKP for model fitting;
predict.NBKP for posterior prediction.
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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Simulate 5 posterior draws of latent mean counts Xnew <- matrix(seq(-2, 2, length.out = 5), ncol = 1) simulate(model, Xnew = Xnew, nsim = 5)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 model <- fit_NBKP(X, y, Xbounds = Xbounds) # Simulate 5 posterior draws of latent mean counts Xnew <- matrix(seq(-2, 2, length.out = 5), ncol = 1) simulate(model, Xnew = Xnew, nsim = 5)
Provides a structured summary of a fitted Negative Binomial Kernel Process (NBKP) model. This function reports the model configuration, prior specification, kernel settings, and key posterior quantities, giving users a concise overview of the fitting results.
## S3 method for class 'NBKP' summary(object, ...)## S3 method for class 'NBKP' summary(object, ...)
object |
An object of class |
... |
Additional arguments passed to the generic |
A list containing key summaries of the fitted model:
n_obsNumber of training observations.
input_dimInput dimensionality (number of columns in X).
kernelKernel type used in the model.
theta_optEstimated kernel hyperparameters.
lossLoss function type used in the model.
loss_minMinimum value of the loss function achieved.
priorPrior type used (e.g., "noninformative", "fixed", "adaptive").
r0Prior precision parameter.
mu0Prior mean parameter (for fixed prior).
phiNegative‑binomial dispersion parameter.
post_meanPosterior mean count estimates at training points.
post_varPosterior variance estimates of latent mean counts.
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.
fit_NBKP for model fitting.
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) summary(model1) # 2D Example set.seed(123) 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) model2 <- fit_NBKP(X, y, Xbounds = Xbounds) summary(model2)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) summary(model1) # 2D Example set.seed(123) 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) model2 <- fit_NBKP(X, y, Xbounds = Xbounds) summary(model2)