Learn R Programming

JointAI (version 0.6.1)

predict.JointAI: Predict values from an object of class JointAI

Description

Obtains predictions and corresponding credible intervals from an object of class 'JointAI'.

Usage

# S3 method for JointAI
predict(
  object,
  newdata,
  quantiles = c(0.025, 0.975),
  type = c("link", "response", "prob", "class", "lp", "risk"),
  start = NULL,
  end = NULL,
  thin = NULL,
  exclude_chains = NULL,
  mess = TRUE,
  ...
)

Arguments

object

object inheriting from class 'JointAI'

newdata

optional new dataset for prediction. If left empty, the original data is used.

quantiles

quantiles of the predicted distribution of the outcome

type

the type of prediction. The default is on the scale of the linear predictor ("link" or "lp"). For generalized linear (mixed) models type = "response" transforms the predicted values to the scale of the response. For ordinal (mixed) models type may be "prob" (to obtain probabilities per class) or "class" to obtain the class with the highest posterior probability.

start

the first iteration of interest (see window.mcmc)

end

the last iteration of interest (see window.mcmc)

thin

thinning interval (see window.mcmc)

exclude_chains

optional vector of the index numbers of chains that should be excluded

mess

logical; should messages be given? Default is TRUE. (Note: this applies only to messages given directly by JointAI.)

...

currently not used

Value

A list with entries dat, fit and quantiles, where fit contains the predicted values (mean over the values calculated from the iterations of the MCMC sample), quantiles contain the specified quantiles (by default 2.5% and 97.5%), and dat is newdata, extended with fit and quantiles (unless prediction for an ordinal outcome is done with type = "prob", in which case the quantiles are an array with three dimensions and are therefore not included in dat).

Details

A model.matrix \(X\) is created from the model formula (currently fixed effects only) and newdata. \(X\beta\) is then calculated for each iteration of the MCMC sample in object, i.e., \(X\beta\) has n.iter rows and nrow(newdata) columns. A subset of the MCMC sample can be selected using start, end and thin.

See Also

predDF.JointAI, *_imp

Examples

Run this code
# NOT RUN {
# fit model
mod <- lm_imp(y ~ C1 + C2 + I(C2^2), data = wideDF, n.iter = 100)

# calculate the fitted values
fit <- predict(mod)

# create dataset for prediction
newDF <- predDF(mod, var = "C2")

# obtain predicted values
pred <- predict(mod, newdata = newDF)

# plot predicted values and 95% confidence band
plot(newDF$C2, pred$fit, type = "l", ylim = range(pred$quantiles),
     xlab = "C2", ylab = "predicted values")
matplot(newDF$C2, pred$quantiles, lty = 2, add = TRUE, type = "l", col = 1)

# }

Run the code above in your browser using DataLab