predict {BKP}R Documentation

Predict Method for BKP or DKP Models

Description

Generates posterior predictive summaries from a fitted BKP or DKP model at new input locations.

Usage

## S3 method for class 'BKP'
predict(object, Xnew, CI_level = 0.05, threshold = 0.5, ...)

## S3 method for class 'DKP'
predict(object, Xnew, CI_level = 0.05, ...)

Arguments

object

An object of class "BKP" or "DKP", typically returned by fit.BKP or fit.DKP.

Xnew

A numeric matrix (or vector) of new input locations where predictions are desired.

CI_level

Credible level for prediction intervals (default is 0.05, corresponding to 95% CI).

threshold

Classification threshold for binary prediction based on posterior mean (used only for BKP; default is 0.5).

...

Additional arguments passed to generic predict methods (currently not used; included for S3 method consistency).

Value

A list with the following components:

Xnew

The new input locations.

mean

BKP: Posterior mean of the success probability at each location. DKP: A matrix of posterior mean class probabilities (rows = inputs, columns = classes).

variance

BKP: Posterior variance of the success probability. DKP: A matrix of posterior variances for each class.

lower

BKP: Lower bound of the prediction interval (e.g., 2.5th percentile for 95% CI). DKP: A matrix of lower bounds for each class (e.g., 2.5th percentile).

upper

BKP: Upper bound of the prediction interval (e.g., 97.5th percentile for 95% CI). DKP: A matrix of upper bounds for each class (e.g., 97.5th percentile).

class

BKP: Predicted binary label (0 or 1), based on posterior mean and threshold, if m = 1. DKP: Predicted class label (i.e., the class with the highest posterior mean probability).

See Also

fit.BKP for fitting Beta Kernel Process models. fit.DKP for fitting Dirichlet Kernel Process models. plot.BKP for visualizing fitted BKP models. plot.DKP for visualizing fitted DKP models.

Examples

# ============================================================== #
# ========================= BKP Examples ======================= #
# ============================================================== #

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

# Define true success probability function
true_pi_fun <- function(x) {
  (1 + exp(-x^2) * cos(10 * (1 - exp(-x)) / (1 + exp(-x)))) / 2
}

n <- 30
Xbounds <- matrix(c(-2,2), nrow=1)
X <- tgp::lhs(n = n, rect = Xbounds)
true_pi <- true_pi_fun(X)
m <- sample(100, n, replace = TRUE)
y <- rbinom(n, size = m, prob = true_pi)

# Fit BKP model
model1 <- fit.BKP(X, y, m, Xbounds=Xbounds)

Xnew = matrix(seq(-2, 2, length = 100), ncol=1) #new data points
head(predict(model1, Xnew))


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

# Define 2D latent function and probability transformation
true_pi_fun <- function(X) {
  if(is.null(nrow(X))) X <- matrix(X, nrow=1)
  m <- 8.6928
  s <- 2.4269
  x1 <- 4*X[,1]- 2
  x2 <- 4*X[,2]- 2
  a <- 1 + (x1 + x2 + 1)^2 *
    (19- 14*x1 + 3*x1^2- 14*x2 + 6*x1*x2 + 3*x2^2)
  b <- 30 + (2*x1- 3*x2)^2 *
    (18- 32*x1 + 12*x1^2 + 48*x2- 36*x1*x2 + 27*x2^2)
  f <- log(a*b)
  f <- (f- m)/s
  return(pnorm(f))  # Transform to probability
}

n <- 100
Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2)
X <- tgp::lhs(n = n, rect = Xbounds)
true_pi <- true_pi_fun(X)
m <- sample(100, n, replace = TRUE)
y <- rbinom(n, size = m, prob = true_pi)

# Fit BKP model
model2 <- fit.BKP(X, y, m, Xbounds=Xbounds)

x1 <- seq(Xbounds[1,1], Xbounds[1,2], length.out = 100)
x2 <- seq(Xbounds[2,1], Xbounds[2,2], length.out = 100)
Xnew <- expand.grid(x1 = x1, x2 = x2)
head(predict(model2, Xnew))

# ============================================================== #
# ========================= BKP Examples ======================= #
# ============================================================== #

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

# Define true class probability function (3-class)
true_pi_fun <- function(X) {
  p <- (1 + exp(-X^2) * cos(10 * (1 - exp(-X)) / (1 + exp(-X)))) / 2
  return(matrix(c(p/2, p/2, 1 - p), nrow = length(p)))
}

n <- 30
Xbounds <- matrix(c(-2, 2), nrow = 1)
X <- tgp::lhs(n = n, rect = Xbounds)
true_pi <- true_pi_fun(X)
m <- sample(100, n, replace = TRUE)

# Generate multinomial responses
Y <- t(sapply(1:n, function(i) rmultinom(1, size = m[i], prob = true_pi[i, ])))

# Fit DKP model
model1 <- fit.DKP(X, Y, Xbounds = Xbounds)
print(model1)


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

# Define latent function and transform to 3-class probabilities
true_pi_fun <- function(X) {
  if (is.null(nrow(X))) X <- matrix(X, nrow = 1)
  m <- 8.6928; s <- 2.4269
  x1 <- 4 * X[,1] - 2
  x2 <- 4 * X[,2] - 2
  a <- 1 + (x1 + x2 + 1)^2 *
    (19 - 14*x1 + 3*x1^2 - 14*x2 + 6*x1*x2 + 3*x2^2)
  b <- 30 + (2*x1 - 3*x2)^2 *
    (18 - 32*x1 + 12*x1^2 + 48*x2 - 36*x1*x2 + 27*x2^2)
  f <- (log(a * b) - m) / s
  p <- pnorm(f)
  return(matrix(c(p/2, p/2, 1 - p), nrow = length(p)))
}

n <- 100
Xbounds <- matrix(c(0, 0, 1, 1), nrow = 2)
X <- tgp::lhs(n = n, rect = Xbounds)
true_pi <- true_pi_fun(X)
m <- sample(100, n, replace = TRUE)

# Generate multinomial responses
Y <- t(sapply(1:n, function(i) rmultinom(1, size = m[i], prob = true_pi[i, ])))

# Fit DKP model
model2 <- fit.DKP(X, Y, Xbounds = Xbounds)
print(model2)


[Package BKP version 0.1.0 Index]