Learn R Programming

brms (version 0.6.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), ntrys = 5, ...)

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.
ntrys
Parameter used in rejection sampling for truncated discrete models only (defaults to 5). See Details for more information.
...
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

For truncated discrete models only: In the absence of any general algorithm to sample from truncated discrete distributions, rejection sampling is applied in this special case. This means that values are sampled until a value lies within the defined truncation boundaries. In practice, this procedure may be rather slow (especially in R). Thus, we try to do approximate rejection sampling by sampling each value ntrys times and then select a valid value. If all values are invalid, the closest boundary is used, instead. If there are more than a few of these pathological cases, a warning will occure suggesting to increase argument ntrys. For models fitted with brms <= 0.5.0="" only:="" 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. When using higher versions of brms, all factors are automatically checked for correctness and amended if necessary.

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
newdata <- data.frame(sex = factor(c("male", "female")),
                      age = c(20,50))
predict(fit, newdata = newdata)

Run the code above in your browser using DataLab