Learn R Programming

BayesSIM (version 1.0.0)

predict.bsim: Prediction Method for BayesSIM

Description

Generate predictions from a fitted Bayesian single-index model.

Usage

# S3 method for bsim
predict(
  object,
  newdata = NULL,
  se.fit = FALSE,
  type = c("response", "latent", "index"),
  method = c("mean", "median"),
  interval = c("none", "credible"),
  level = 0.95,
  ...
)

Value

A list containing at least the following components:

fitted

Posterior mean or median predictions. If se.fit = TRUE or interval = "credible", standard error and lower, upper bounds of the credible interval is also included.

truey

True response value of test data. When true response value is not available, NULL is saved.

idxValue

Linear index value is saved where \(\theta\) is estimated by method.

level

Credible level.

Arguments

object

A fitted object of BayesSIM or individual model.

newdata

Optional data frame for which predictions should be made. If NULL, predictions are returned for the training data.

se.fit

A logical value indicating whether standard errors are required. Default is FALSE.

type

Character string specifying the type of prediction. "response" is the default.

"response"

Posterior predictive summaries of the response \(Y\). This corresponds to draws from the posterior predictive distribution \(Y^{(m)} \sim N(f(X'\theta^{(m)}), \sigma^{2(m)})\) and therefore incorporates both the uncertainty in the link function and the variability of the error term for each \(m^{th}\) MCMC sample.

"latent"

Posterior summaries of the latent mean structure \(E(Y \mid X) = f^{(m)}(t^{(m)})\), where \(t^{(m)} = X'\theta^{(m)}\). Unlike "response", it excludes the noise term and calculated by \(f^{(m)}(X'\theta^{(m)})\) for each \(m^{th}\) MCMC sample of \(\theta\).

"index"

Posterior summaries of the single index \(t^{(m)} = X'\theta^{(m)}\).

method

Character string determining the posterior summary used for point predictions. Options are "mean" or "median". Default is "mean".

interval

Character string indicating whether a credible interval should be returned. Default is "none".

"none"

Return only point predictions.

"credible"

Return a \(100 \times \text{level}\%\) credible interval.

level

Numeric value between 0 and 1 specifying the credible level. level = 0.95 yields a 95% credible interval. Default is 0.95.

...

Additional arguments.

Details

This method extracts MCMC posterior samples stored in a BayesSIM object and computes posterior summaries of:

  • the posterior predictive response \(Y \mid X\) (type "response"),

  • the latent link function evaluation \(E(Y \mid X) = f(X'\theta)\) (type "latent"), or

  • the single index \(X'\theta\) (type "index").

The key distinction is that "response" incorporates the posterior variability of the error term \(\epsilon\), whereas "latent" represents the noiseless conditional expectation \(E(Y \mid X)\) computed directly from the link function and the posterior draws of \(\theta\).

When interval = "credible", the returned object includes lower and upper credible bounds computed via posterior quantiles for the chosen prediction scale.

If newdata is supplied, predictions are evaluated at the new covariate values by computing the corresponding posterior index \(t = X'\theta\) and applying the link function.

Examples

Run this code
# \donttest{
simdata2 <- data.frame(DATA1$X, y = DATA1$y)

# 1. One tool version
fit_one <- BayesSIM(y ~ ., data = simdata2,
                    niter = 5000, nburnin = 1000, nchain = 1)

# Check median index vector estimates with standard errors
coef(fit_one, method = "median", se = TRUE)

# Fitted index values of median prediction
fitted(fit_one, type = "linpred", method = "median")

# Residuals of median prediction
residuals(fit_one, method = "median")

# Summary of the model
summary(fit_one)

# Convergence diagnostics
nimTraceplot(fit_one)

# Goodness of fit
GOF(fit_one)

# Fitted plot
plot(fit_one)

# Prediction with 95% credible interval at new data
newx <- data.frame(X1 = rnorm(10), X2 = rnorm(10), X3 = rnorm(10), X4 = rnorm(10))
pred <- predict(fit_one, newdata = newx, interval = "credible", level = 0.95)
plot(pred)


# 2. Split version
models <- BayesSIM_setup(y ~ ., data = simdata2)
Ccompile <- compileModelAndMCMC(models)
nimSampler <- get_sampler(Ccompile)
initList <- getInit(models)
mcmc.out <- runMCMC(nimSampler, niter = 5000, nburnin = 1000, thin = 1,
                    nchains = 1, setSeed = TRUE, inits = initList,
                    summary = TRUE, samplesAsCodaMCMC = TRUE)

# "fit_split" becomes exactly the same as the class of "fit_one" object and apply generic functions.
fit_split <- as_bsim(models, mcmc.out)

# }

Run the code above in your browser using DataLab