Learn R Programming

brms (version 0.5.0)

fitted.brmsfit: Extract Model Fitted Values of brmsfit Objects

Description

Extract Model Fitted Values of brmsfit Objects

Usage

## S3 method for class 'brmsfit':
fitted(object, newdata = NULL, re_formula = NULL,
  scale = c("response", "linear"), 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.
scale
Either "response" or "linear". If scale = "response" results are returned on the scale of the response variable. If scale = "linear" fitted values are returned on the scale of the linear predictor.
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

  • Fitted values extracted from object. The output depends on the family: If summary = TRUE it is a N x E x C array for categorical and ordinal models and a N x E matrix else. If summary = FALSE it is a S x N x C array for categorical and ordinal models and a S x N matrix else. N is the number of observations, S is the number of samples, C is the number of categories, and E is equal to length(probs) + 2.

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(rating ~ treat + period + carry + (1|subject), data = inhaler,
           n.cluster = 2)

## extract fitted values
fitted_values <- fitted(fit)
head(fitted_values)

## plot fitted means against actual response
dat <- as.data.frame(cbind(Y = fit$data$Y, fitted_values))
ggplot(dat) + geom_point(aes(x = Estimate, y = Y))

Run the code above in your browser using DataLab