Learn R Programming

brms (version 0.9.1)

fitted.brmsfit: Extract Model Fitted Values of brmsfit Objects

Description

Predict fitted values (i.e. the 'regression line') of a fitted model. Can be performed for the data used to fit the model (posterior predictive checks) or for new data. By definition, these predictions have smaller variance than the response predictions performed by the predict method. This is because the measurement error is not incorporated. The estimated means of both methods should, however, be very similar.

Usage

## S3 method for class 'brmsfit':
fitted(object, newdata = NULL, re_formula = NULL,
  scale = c("response", "linear"), allow_new_levels = FALSE,
  subset = NULL, nsamples = NULL, summary = TRUE, probs = c(0.025,
  0.975), ...)

Arguments

object
An object of class brmsfit
newdata
An optional data.frame for which to evaluate predictions. If NULL (default), the orginal data of the model is used.
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.
scale
Either "response" or "linear". If "response" results are returned on the scale of the response variable. If "linear" fitted values are returned on the scale of the linear predictor.
allow_new_levels
A flag indicating if new levels of random effects are allowed (defaults to FALSE). Only relevant if newdata is provided.
subset
A numeric vector specifying the posterior samples to be used. If NULL (the default), all samples are used.
nsamples
Positive integer indicating how many posterior samples should be used. If NULL (the default) all samples are used. Ignored if subset is not NULL.
summary
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

NA values within factors in newdata, are interpreted as if all dummy variables of this factor are zero. This allows, for instance, to make predictions of the grand mean when using sum coding. 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(rating ~ treat + period + carry + (1|subject), 
           data = inhaler)

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

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

Run the code above in your browser using DataLab