Learn R Programming

brms (version 0.5.0)

predict.brmsfit: Model Predictions of brmsfit Objects

Description

Make predictions based on the fitted model parameters. Can be performed for the data used to fit the model (posterior predictive checks) or for new data.

Usage

## S3 method for class 'brmsfit':
predict(object, newdata = NULL, re_formula = NULL,
  transform = NULL, allow_new_levels = FALSE, summary = TRUE,
  probs = c(0.025, 0.975), ...)

Arguments

object
An object of class brmsfit
newdata
An optional data.frame containing new data to make predictions for. If NULL (default), the data used to fit the model is applied.
re_formula
formula containing random effects to be considered in the prediction. If NULL (default), include all random effects; if NA, include no random effects. Other options will be implemented in the future.
transform
A function or a character string naming a function to be applied on the predicted responses before summary statistics are computed.
allow_new_levels
Currenly, FALSE (no new levels allowed) is the only option. This will change in future versions of the package.
summary
logical. Should summary statistics (i.e. means, sds, and 95% intervals) be returned instead of the raw values. Default is TRUE
probs
The percentiles to be computed by the quantile function. Only used if summary is TRUE.
...
Currently ignored

Value

  • Predicted values of the response variable. If summary = TRUE the output depends on the family: For catagorical and ordinal families, it is a N x C matrix where N is the number of observations and C is the number of categories. For all other families, it is a N x E matrix where E is equal to length(probs) + 2. If summary = FALSE, the output is as a S x N matrix, where S is the number of samples.

Details

Be careful when using newdata with factors in fixed or random effects: The predicted results are only valid if all factor levels present in the initial data are also defined and ordered correctly for the factors in newdata. Grouping factors may contain fewer levels than in the inital data without causing problems.

Examples

Run this code
## fit a model
fit <- brm(time | cens(censored) ~ age + sex, data = kidney,
           family = "exponential", silent = TRUE)

## posterior predictive checks
pp <- predict(fit)
head(pp)

## predict response for new data (be careful with factors)
newdata <- data.frame(sex = factor(c("male", "female"),
                                   levels = c("male", "female")),
                      age = c(20,50))
predict(fit, newdata = newdata)

Run the code above in your browser using DataLab