Learn R Programming

brms (version 1.10.2)

brm: Fit Bayesian Generalized (Non-)Linear Multilevel Models

Description

Fit Bayesian generalized (non-)linear multilevel models using Stan for full Bayesian inference. A wide range of distributions and link functions are supported, allowing users to fit -- among others -- linear, robust linear, count data, survival, response times, ordinal, zero-inflated, hurdle, and even self-defined mixture models all in a multilevel context. Further modeling options include non-linear and smooth terms, auto-correlation structures, censored data, meta-analytic standard errors, and quite a few more. In addition, all parameters of the response distribution can be predicted in order to perform distributional regression. Prior specifications are flexible and explicitly encourage users to apply prior distributions that actually reflect their beliefs. In addition, model fit can easily be assessed and compared with posterior predictive checks and leave-one-out cross-validation.

Usage

brm(formula, data, family = gaussian(), prior = NULL, autocor = NULL,
  nonlinear = NULL, threshold = c("flexible", "equidistant"),
  cov_ranef = NULL, sample_prior = c("no", "yes", "only"), sparse = FALSE,
  knots = NULL, stan_funs = NULL, fit = NA, save_ranef = TRUE,
  save_mevars = FALSE, save_all_pars = FALSE, inits = "random",
  chains = 4, iter = 2000, warmup = floor(iter/2), thin = 1,
  cores = getOption("mc.cores", 1L), control = NULL,
  algorithm = c("sampling", "meanfield", "fullrank"),
  future = getOption("future", FALSE), silent = TRUE, seed = 12345,
  save_model = NULL, save_dso = TRUE, ...)

Arguments

formula

An object of class formula or brmsformula (or one that can be coerced to that classes): A symbolic description of the model to be fitted. The details of model specification are explained in brmsformula.

data

An object of class data.frame (or one that can be coerced to that class) containing data of all variables used in the model.

family

A description of the response distribution and link function to be used in the model. This can be a family function, a call to a family function or a character string naming the family. Every family function has a link argument allowing to specify the link function to be applied on the response variable. If not specified, default links are used. For details of supported families see brmsfamily. By default, a linear gaussian model is applied.

prior

One or more brmsprior objects created by set_prior or related functions and combined using the c method. A single brmsprior object may be passed without c() surrounding it. See also get_prior for more help.

autocor

An optional cor_brms object describing the correlation structure within the response variable (i.e., the 'autocorrelation'). See the documentation of cor_brms for a description of the available correlation structures. Defaults to NULL, corresponding to no correlations.

nonlinear

(Deprecated) An optional list of formulas, specifying linear models for non-linear parameters. If NULL (the default) formula is treated as an ordinary formula. If not NULL, formula is treated as a non-linear model and nonlinear should contain a formula for each non-linear parameter, which has the parameter on the left hand side and its linear predictor on the right hand side. Alternatively, it can be a single formula with all non-linear parameters on the left hand side (separated by a +) and a common linear predictor on the right hand side. As of brms 1.4.0, we recommend specifying non-linear parameters directly within formula.

threshold

(Deprecated) A character string indicating the type of thresholds (i.e. intercepts) used in an ordinal model. "flexible" provides the standard unstructured thresholds and "equidistant" restricts the distance between consecutive thresholds to the same value. As of brms 1.8.0, we recommend specifying threshold directly within the ordinal family functions.

cov_ranef

A list of matrices that are proportional to the (within) covariance structure of the group-level effects. The names of the matrices should correspond to columns in data that are used as grouping factors. All levels of the grouping factor should appear as rownames of the corresponding matrix. This argument can be used, among others to model pedigrees and phylogenetic effects. See vignette("brms_phylogenetics") for more details.

sample_prior

Indicate if samples from all specified proper priors should be drawn additionally to the posterior samples (defaults to "no"). Among others, these samples can be used to calculate Bayes factors for point hypotheses. If set to "only", samples are drawn solely from the priors ignoring the likelihood. In this case, all parameters must have proper priors.

sparse

Logical; indicates whether the population-level design matrix should be treated as sparse (defaults to FALSE). For design matrices with many zeros, this can considerably reduce required memory. Sampling speed is currently not improved or even slightly decreased.

knots

Optional list containing user specified knot values to be used for basis construction of smoothing terms. See gamm for more details.

stan_funs

An optional character string containing self-defined Stan functions, which will be included in the functions block of the generated Stan code. Note that these functions must additionally be defined as vectorized R functions in the global environment for various post-processing methods to work on the returned model object.

fit

An instance of S3 class brmsfit derived from a previous fit; defaults to NA. If fit is of class brmsfit, the compiled model associated with the fitted result is re-used and all arguments modifying the model code or data are ignored. It is not recommended to use this argument directly, but to call the update method, instead.

save_ranef

A flag to indicate if group-level effects for each level of the grouping factor(s) should be saved (default is TRUE). Set to FALSE to save memory. The argument has no impact on the model fitting itself. A deprecated alias is ranef.

save_mevars

A flag to indicate if samples of noise-free variables obtained by using me terms should be saved (default is FALSE). Saving these samples allows to use methods such as predict with the noise-free variables but leads to very large R objects even for models of moderate size and complexity.

save_all_pars

A flag to indicate if samples from all variables defined in Stan's parameters block should be saved (default is FALSE). Saving these samples is required in order to apply the methods bridge_sampler, bayes_factor, and post_prob.

inits

Either "random" or "0". If inits is "random" (the default), Stan will randomly generate initial values for parameters. If it is "0", all parameters are initiliazed to zero. This option is recommended for exponential and weibull models, as it happens that default ("random") inits cause samples to be essentially constant. Generally, setting inits = "0" is worth a try, if chains do not behave well. Alternatively, inits can be a list of lists containing the initial values, or a function (or function name) generating initial values. The latter options are mainly implemented for internal testing.

chains

Number of Markov chains (defaults to 4).

iter

Number of total iterations per chain (including warmup; defaults to 2000).

warmup

A positive integer specifying number of warmup (aka burnin) iterations. This also specifies the number of iterations used for stepsize adaptation, so warmup samples should not be used for inference. The number of warmup should not be larger than iter and the default is iter/2.

thin

Thinning rate. Must be a positive integer. Set thin > 1 to save memory and computation time if iter is large.

cores

Number of cores to use when executing the chains in parallel, which defaults to 1 but we recommend setting the mc.cores option to be as many processors as the hardware and RAM allow (up to the number of chains). For non-Windows OS in non-interactive R sessions, forking is used instead of PSOCK clusters. A deprecated alias is cluster.

control

A named list of parameters to control the sampler's behavior. It defaults to NULL so all the default values are used. The most important control parameters are discussed in the 'Details' section below. For a comprehensive overview see stan.

algorithm

Character string indicating the estimation approach to use. Can be "sampling" for MCMC (the default), "meanfield" for variational inference with independent normal distributions, or "fullrank" for variational inference with a multivariate normal distribution.

future

Logical; If TRUE, the future package is used for parallel execution of the chains and argument cores will be ignored. Can be set globally for the current R session via the future option. The execution type is controlled via plan (see the examples section below).

silent

logical; If TRUE (the default), most of the informational messages of compiler and sampler are suppressed. The actual sampling progress is still printed. Set refresh = 0 to turn this off as well.

seed

Used by set.seed to make results reproducable. Be aware that brm resets the seed to the value specified in seed (default: 12345) every time it is run. If you want to use different seeds per run, use, for instance, seed = sample(1e+7, size = 1). Be aware that generally, the seed also affects subsequently called functions (such as predict), which make use of the random number generator of R.

save_model

Either NULL or a character string. In the latter case, the model code is saved in a file named after the string supplied in save_model, which may also contain the full path where to save the file. If only a name is given, the file is saved in the current working directory.

save_dso

Logical, defaulting to TRUE, indicating whether the dynamic shared object (DSO) compiled from the C++ code for the model will be saved or not. If TRUE, we can draw samples from the same model in another R session using the saved DSO (i.e., without compiling the C++ code again).

...

Further arguments to be passed to Stan.

Value

An object of class brmsfit, which contains the posterior samples along with many other useful information about the model. Use methods(class = "brmsfit") for an overview on available methods.

Details

Fit a generalized (non-)linear multilevel model via full Bayesian inference using Stan. A general overview is provided in the vignettes vignette("brms_overview") and vignette("brms_multilevel"). For a full list of available vignettes see vignette(package = "brms").

Formula syntax of brms models

Details of the formula syntax applied in brms can be found in brmsformula.

Families and link functions

Details of families supported by brms can be found in brmsfamily.

Prior distributions

Priors should be specified using the set_prior function. Its documentation contains detailed information on how to correctly specify priors. To find out on which parameters or parameter classes priors can be defined, use get_prior. Default priors are chosen to be non or very weakly informative so that their influence on the results will be negligable and you don't have to worry about them. However, after getting more familiar with Bayesian statistics, I recommend you to start thinking about reasonable informative priors for your model parameters: Nearly always, there is at least some prior information available that can be used to improve your inference.

Adjusting the sampling behavior of Stan

In addition to choosing the number of iterations, warmup samples, and chains, users can control the behavior of the NUTS sampler, by using the control argument. The most important reason to use control is to decrease (or eliminate at best) the number of divergent transitions that cause a bias in the obtained posterior samples. Whenever you see the warning "There were x divergent transitions after warmup." you should really think about increasing adapt_delta. To do this, write control = list(adapt_delta = <x>), where <x> should usually be value between 0.8 (current default) and 1. Increasing adapt_delta will slow down the sampler but will decrease the number of divergent transitions threatening the validity of your posterior samples.

Another problem arises when the depth of the tree being evaluated in each iteration is exceeded. This is less common than having divergent transitions, but may also bias the posterior samples. When it happens, Stan will throw out a warning suggesting to increase max_treedepth, which can be accomplished by writing control = list(max_treedepth = <x>) with a positive integer <x> that should usually be larger than the current default of 10. For more details on the control argument see stan.

References

Paul-Christian Buerkner (2017). brms: An R Package for Bayesian Multilevel Models Using Stan. Journal of Statistical Software, 80(1), 1-28. doi:10.18637/jss.v080.i01

See Also

brms, brmsformula, brmsfamily, brmsfit

Examples

Run this code
# NOT RUN {
## Poisson regression for the number of seizures in epileptic patients
## using student_t priors for population-level effects 
## and half cauchy priors for standard deviations of group-level effects 
fit1 <- brm(count ~ log_Age_c + log_Base4_c * Trt  
              + (1|patient) + (1|obs), 
            data = epilepsy, family = poisson(), 
            prior = c(prior(student_t(5,0,10), class = b),
                      prior(cauchy(0,2), class = sd)))
## generate a summary of the results
summary(fit1)
## plot the MCMC chains as well as the posterior distributions
plot(fit1, ask = FALSE)
## extract random effects standard devations and covariance matrices
VarCorr(fit1)
## extract group specific effects of each level
ranef(fit1)
## predict responses based on the fitted model
head(predict(fit1))  
## plot marginal effects of each predictor
plot(marginal_effects(fit1), ask = FALSE)
## investigate model fit
WAIC(fit1)
pp_check(fit1)
 
## Ordinal regression modeling patient's rating of inhaler instructions 
## category specific effects are estimated for variable 'treat'
fit2 <- brm(rating ~ period + carry + cs(treat), 
            data = inhaler, family = sratio("cloglog"), 
            prior = set_prior("normal(0,5)"), chains = 2)
summary(fit2)
plot(fit2, ask = FALSE) 
WAIC(fit2)   
head(predict(fit2))

## Survival regression modeling the time between the first 
## and second recurrence of an infection in kidney patients.
fit3 <- brm(time | cens(censored) ~ age * sex + disease + (1|patient), 
            data = kidney, family = lognormal())
summary(fit3) 
plot(fit3, ask = FALSE)
plot(marginal_effects(fit3), ask = FALSE)   

## Probit regression using the binomial family
n <- sample(1:10, 100, TRUE)  # number of trials
success <- rbinom(100, size = n, prob = 0.4)
x <- rnorm(100)
data4 <- data.frame(n, success, x)
fit4 <- brm(success | trials(n) ~ x, data = data4,
            family = binomial("probit"))
summary(fit4)

## Simple non-linear gaussian model
x <- rnorm(100)
y <- rnorm(100, mean = 2 - 1.5^x, sd = 1)
data5 <- data.frame(x, y)
fit5 <- brm(bf(y ~ a1 - a2^x, a1 + a2 ~ 1, nl = TRUE),  
            data = data5,
            prior = c(prior(normal(0, 2), nlpar = a1),
                      prior(normal(0, 2), nlpar = a2)))
summary(fit5)
plot(marginal_effects(fit5), ask = FALSE)

## Normal model with heterogeneous variances
data_het <- data.frame(y = c(rnorm(50), rnorm(50, 1, 2)),
                       x = factor(rep(c("a", "b"), each = 50)))
fit6 <- brm(bf(y ~ x, sigma ~ 0 + x), data = data_het)
summary(fit6)
plot(fit6)
marginal_effects(fit6)
# extract estimated residual SDs of both groups
sigmas <- exp(posterior_samples(fit6, "^b_sigma_"))
ggplot(stack(sigmas), aes(values)) + 
  geom_density(aes(fill = ind))
  
## Quantile regression predicting the 25%-quantile
fit7 <- brm(bf(y ~ x, quantile = 0.25), data = data_het, 
            family = asym_laplace())
summary(fit7)
marginal_effects(fit7)

## use the future package for parallelization
library(future)
plan(multiprocess)
fit7 <- update(fit7, future = TRUE)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab