brms
brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
Science Score: 49.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 10 DOI reference(s) in README -
○Academic publication links
-
✓Committers with academic emails
12 of 61 committers (19.7%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (19.3%) to scientific vocabulary
Keywords
bayesian-inference
brms
multilevel-models
r-package
stan
statistical-models
Keywords from Contributors
bayesian-data-analysis
psychology
report
bayes
multilevel-mediation-models
prior-distribution
bayesian-methods
data-manipulation
neuroscience
book
Last synced: 6 months ago
·
JSON representation
Repository
brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
Basic Info
- Host: GitHub
- Owner: paul-buerkner
- License: gpl-2.0
- Language: R
- Default Branch: master
- Homepage: https://paulbuerkner.com/brms/
- Size: 260 MB
Statistics
- Stars: 1,349
- Watchers: 38
- Forks: 202
- Open Issues: 124
- Releases: 56
Topics
bayesian-inference
brms
multilevel-models
r-package
stan
statistical-models
Created over 10 years ago
· Last pushed 6 months ago
Metadata Files
Readme
Changelog
Funding
License
README.Rmd
---
output:
md_document:
variant: markdown_github
---
```{r, include=FALSE}
stopifnot(require(knitr))
options(width = 90)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dev = "png",
dpi = 150,
fig.asp = 0.8,
fig.width = 5,
out.width = "60%",
fig.align = "center"
)
library(brms)
ggplot2::theme_set(bayesplot::theme_default())
set.seed(1234)
```
[
](https://mc-stan.org/)
# brms
[](https://github.com/paul-buerkner/brms/actions)
[](https://app.codecov.io/github/paul-buerkner/brms?branch=master)
[](https://cran.r-project.org/package=brms)
[](https://CRAN.R-project.org/package=brms)
## Overview
The **brms** package provides an interface to fit Bayesian generalized
(non-)linear multivariate multilevel models using Stan, which is a C++ package
for performing full Bayesian inference (see https://mc-stan.org/). The formula
syntax is very similar to that of the package lme4 to provide a familiar and
simple interface for performing regression analyses. A wide range of response
distributions are supported, allowing users to fit -- among others -- linear,
robust linear, count data, survival, response times, ordinal, zero-inflated, 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, missing value imputation, and quite a few more. In addition, all
parameters of the response distribution can be predicted in order to perform
distributional regression. Multivariate models (i.e., models with multiple
response variables) can be fit, as well. Prior specifications are flexible and
explicitly encourage users to apply prior distributions that actually reflect
their beliefs. Model fit can easily be assessed and compared with posterior
predictive checks, cross-validation, and Bayes factors.
## Resources
* [Introduction to brms](https://doi.org/10.18637/jss.v080.i01) (Journal of Statistical Software)
* [Advanced multilevel modeling with brms](https://journal.r-project.org/archive/2018/RJ-2018-017/index.html) (The R Journal)
* [Website](https://paulbuerkner.com/brms/) (Website of brms with documentation and vignettes)
* [Blog posts](http://paulbuerkner.com/software/brms-blogposts.html) (List of blog posts about brms)
* [Ask a question](https://discourse.mc-stan.org/) (Stan Forums on Discourse)
* [Open an issue](https://github.com/paul-buerkner/brms/issues) (GitHub issues for bug reports and feature requests)
## How to use brms
```{r load, message=FALSE}
library(brms)
```
As a simple example, we use poisson regression to model the seizure counts in
epileptic patients to investigate whether the treatment (represented by variable
`Trt`) can reduce the seizure counts and whether the effect of the treatment
varies with the (standardized) baseline number of seizures a person had before
treatment (variable `zBase`). As we have multiple observations per person, a
group-level intercept is incorporated to account for the resulting dependency in
the data.
```{r fit1, results='hide', message=FALSE}
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
data = epilepsy, family = poisson())
```
The results (i.e., posterior draws) can be investigated using
```{r summary}
summary(fit1)
```
On the top of the output, some general information on the model is given, such
as family, formula, number of iterations and chains. Next, group-level effects
are displayed separately for each grouping factor in terms of standard
deviations and (in case of more than one group-level effect per grouping factor;
not displayed here) correlations between group-level effects. On the bottom of
the output, population-level effects (i.e. regression coefficients) are
displayed. If incorporated, autocorrelation effects and family specific
parameters (e.g., the residual standard deviation 'sigma' in normal models) are
also given.
In general, every parameter is summarized using the mean ('Estimate') and the
standard deviation ('Est.Error') of the posterior distribution as well as
two-sided 95% credible intervals ('l-95% CI' and 'u-95% CI') based on quantiles.
We see that the coefficient of `Trt` is negative with a zero overlapping
95\%-CI. This indicates that, on average, the treatment may reduce seizure
counts by some amount but the evidence based on the data and applied model is
not very strong and still insufficient by standard decision rules. Further, we
find little evidence that the treatment effect varies with the baseline number
of seizures.
The last three values ('ESS_bulk', 'ESS_tail', and 'Rhat') provide information
on how well the algorithm could estimate the posterior distribution of this
parameter. If 'Rhat' is considerably greater than 1, the algorithm has not yet
converged and it is necessary to run more iterations and / or set stronger
priors.
To visually investigate the chains as well as the posterior distributions, we
can use the `plot` method. If we just want to see results of the regression
coefficients of `Trt` and `zBase`, we go for
```{r plot}
plot(fit1, variable = c("b_Trt1", "b_zBase"))
```
A more detailed investigation can be performed by running
`launch_shinystan(fit1)`. To better understand the relationship of the
predictors with the response, I recommend the `conditional_effects` method:
```{r conditional_effects}
plot(conditional_effects(fit1, effects = "zBase:Trt"))
```
This method uses some prediction functionality behind the scenes, which can also
be called directly. Suppose that we want to predict responses (i.e. seizure
counts) of a person in the treatment group (`Trt = 1`) and in the control group
(`Trt = 0`) with average age and average number of previous seizures. Than we
can use
```{r predict}
newdata <- data.frame(Trt = c(0, 1), zAge = 0, zBase = 0)
predict(fit1, newdata = newdata, re_formula = NA)
```
We need to set `re_formula = NA` in order not to condition of the group-level
effects. While the `predict` method returns predictions of the responses, the
`fitted` method returns predictions of the regression line.
```{r fitted}
fitted(fit1, newdata = newdata, re_formula = NA)
```
Both methods return the same estimate (up to random error), while the latter has
smaller variance, because the uncertainty in the regression line is smaller than
the uncertainty in each response. If we want to predict values of the original
data, we can just leave the `newdata` argument empty.
Suppose, we want to investigate whether there is overdispersion in the model,
that is residual variation not accounted for by the response distribution. For
this purpose, we include a second group-level intercept that captures possible
overdispersion.
```{r fit2, results='hide', message=FALSE}
fit2 <- brm(count ~ zAge + zBase * Trt + (1|patient) + (1|obs),
data = epilepsy, family = poisson())
```
We can then go ahead and compare both models via approximate leave-one-out (LOO)
cross-validation.
```{r loo, warning=FALSE}
loo(fit1, fit2)
```
The `loo` output when comparing models is a little verbose. We first see the
individual LOO summaries of the two models and then the comparison between them.
Since higher `elpd` (i.e., expected log posterior density) values indicate
better fit, we see that the model accounting for overdispersion (i.e., `fit2`)
fits substantially better. However, we also see in the individual LOO outputs
that there are several problematic observations for which the approximations may
have not have been very accurate. To deal with this appropriately, we need to fall
back to other methods such as `reloo` or `kfold` but this requires the model to
be refit several times which takes too long for the purpose of a quick example.
The post-processing methods we have shown above are just the tip of the
iceberg. For a full list of methods to apply on fitted model objects, type
`methods(class = "brmsfit")`.
## Citing brms and related software
Developing and maintaining open source software is an important yet often
underappreciated contribution to scientific progress. Thus, whenever you are
using open source software (or software in general), please make sure to cite it
appropriately so that developers get credit for their work.
When using brms, please cite one or more of the following publications:
- Bürkner P. C. (2017). brms: An R Package for Bayesian Multilevel Models
using Stan. *Journal of Statistical Software*. 80(1), 1-28.
doi.org/10.18637/jss.v080.i01
- Bürkner P. C. (2018). Advanced Bayesian Multilevel Modeling with the R
Package brms. *The R Journal*. 10(1), 395-411. doi.org/10.32614/RJ-2018-017
- Bürkner P. C. (2021). Bayesian Item Response Modeling in R with brms and Stan.
*Journal of Statistical Software*, 100(5), 1-54. doi.org/10.18637/jss.v100.i05
As brms is a high-level interface to Stan, please additionally cite Stan
(see also https://mc-stan.org/users/citations/):
- Stan Development Team. YEAR. Stan Modeling Language Users Guide and Reference
Manual, VERSION. https://mc-stan.org
- Carpenter B., Gelman A., Hoffman M. D., Lee D., Goodrich B., Betancourt M.,
Brubaker M., Guo J., Li P., and Riddell A. (2017). Stan: A probabilistic
programming language. *Journal of Statistical Software*. 76(1).
doi.org/10.18637/jss.v076.i01
Further, brms relies on several other R packages and, of course, on R itself. To
find out how to cite R and its packages, use the `citation` function. There are
some features of brms which specifically rely on certain packages. The **rstan**
package together with **Rcpp** makes Stan conveniently accessible in R.
Visualizations and posterior-predictive checks are based on **bayesplot** and
**ggplot2**. Approximate leave-one-out cross-validation using `loo` and related
methods is done via the **loo** package. Marginal likelihood based methods such
as `bayes_factor` are realized by means of the **bridgesampling** package.
Splines specified via the `s` and `t2` functions rely on **mgcv**. If you use
some of these features, please also consider citing the related packages.
## FAQ
### How do I install brms?
To install the latest release version from CRAN use
```{r install_brms, eval=FALSE}
install.packages("brms")
```
The current developmental version can be downloaded from GitHub via
```{r install_brms2, eval=FALSE}
if (!requireNamespace("remotes")) {
install.packages("remotes")
}
remotes::install_github("paul-buerkner/brms")
```
Because brms is based on Stan, a C++ compiler is required. The program Rtools
(available on https://cran.r-project.org/bin/windows/Rtools/) comes with a C++
compiler for Windows. On Mac, you should install Xcode. For further instructions
on how to get the compilers running, see the prerequisites section on
https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started.
### I am new to brms. Where can I start?
Detailed instructions and case studies are given in the package's extensive
vignettes. See `vignette(package = "brms")` for an overview. For documentation
on formula syntax, families, and prior distributions see `help("brm")`.
### Where do I ask questions, propose a new feature, or report a bug?
Questions can be asked on the [Stan forums](https://discourse.mc-stan.org/) on
Discourse. To propose a new feature or report a bug, please open an issue on
[GitHub](https://github.com/paul-buerkner/brms).
### How can I extract the generated Stan code?
If you have already fitted a model, apply the `stancode` method on the
fitted model object. If you just want to generate the Stan code without any
model fitting, use the `stancode` method on your model formula.
### Can I avoid compiling models?
When you fit your model for the first time with brms, there is currently no way
to avoid compilation. However, if you have already fitted your model and want to
run it again, for instance with more draws, you can do this without
recompilation by using the `update` method. For more details see
`help("update.brmsfit")`.
### What is the difference between brms and rstanarm?
The rstanarm package is similar to brms in that it also allows to fit regression
models using Stan for the backend estimation. Contrary to brms, rstanarm comes
with precompiled code to save the compilation time (and the need for a C++
compiler) when fitting a model. However, as brms generates its Stan code on the
fly, it offers much more flexibility in model specification than rstanarm. Also,
multilevel models are currently fitted a bit more efficiently in brms. For
detailed comparisons of brms with other common R packages implementing
multilevel models, see `vignette("brms_multilevel")` and
`vignette("brms_overview")`.
Owner
- Name: Paul-Christian Bürkner
- Login: paul-buerkner
- Kind: user
- Location: Department of Statistics, TU Dortmund University
- Website: https://paul-buerkner.github.io/
- Repositories: 22
- Profile: https://github.com/paul-buerkner
GitHub Events
Total
- Issues event: 129
- Watch event: 70
- Issue comment event: 452
- Push event: 77
- Pull request review event: 86
- Pull request review comment event: 102
- Pull request event: 58
- Fork event: 18
- Create event: 3
Last Year
- Issues event: 129
- Watch event: 70
- Issue comment event: 452
- Push event: 77
- Pull request review event: 86
- Pull request review comment event: 102
- Pull request event: 58
- Fork event: 18
- Create event: 3
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Paul-Christian Bürkner | p****r@g****m | 4,833 |
| fweber144 | f****4@p****m | 167 |
| Ioannis Kosmidis | i****s@w****k | 34 |
| Hamada S. Badr | h****r@g****m | 27 |
| Ven Popov | v****v@g****m | 18 |
| Andrew Johnson | a****n@p****u | 17 |
| jgabry | j****y@g****m | 17 |
| Andrew Johnson | a****n@a****m | 15 |
| Sebastian Weber | s****t@w****e | 14 |
| martinmodrak | m****r@g****m | 13 |
| hrabel | h****l@g****m | 12 |
| Aki Vehtari | A****i@a****i | 9 |
| Ben Schneider | B****r@g****m | 9 |
| SimonCMills | s****2@s****k | 7 |
| n-kall | n****l@g****m | 6 |
| Tom Peatman | t****n@g****m | 6 |
| mawilson1234 | m****n@y****u | 6 |
| Luna Fazio | b****l@g****m | 6 |
| Noa Kallioinen | 3****l | 5 |
| Rok Cesnovar | r****r@f****i | 4 |
| Marco Colombo | m****3@g****m | 3 |
| Jacob Socolar | j****r@g****m | 3 |
| Markus Gesmann | m****n@g****m | 3 |
| Nicholas Clark | u****2@u****u | 2 |
| Xiangyun Huang | 1****3@s****n | 2 |
| Peter Ralph | p****p@g****m | 2 |
| Vassilis Kehayas | v****s | 2 |
| Michael MacAskill | m****l@n****g | 1 |
| Russell V. Lenth | r****h@u****u | 1 |
| Sebastian Weber | s****r@n****m | 1 |
| and 31 more... | ||
Committer Domains (Top 20 + Academic)
gmx.de: 1
yandex.com: 1
lindeloev.dk: 1
cirad.fr: 1
gmx.net: 1
ur.rochester.edu: 1
astrazeneca.com: 1
hotmail.co.uk: 1
jhu.edu: 1
msu.edu: 1
columbia.edu: 1
adams-mac-mini.lan: 1
nantwichfarmvets.co.uk: 1
novartis.com: 1
uiowa.edu: 1
nzbri.org: 1
student.cumtb.edu.cn: 1
uq.edu.au: 1
fri.uni-lj.si: 1
yale.edu: 1
sheffield.ac.uk: 1
postgrad.curtin.edu.au: 1
warwick.ac.uk: 1
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 385
- Total pull requests: 136
- Average time to close issues: 7 months
- Average time to close pull requests: 3 months
- Total issue authors: 216
- Total pull request authors: 37
- Average comments per issue: 4.25
- Average comments per pull request: 4.11
- Merged pull requests: 100
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 89
- Pull requests: 69
- Average time to close issues: 8 days
- Average time to close pull requests: 7 days
- Issue authors: 62
- Pull request authors: 22
- Average comments per issue: 1.34
- Average comments per pull request: 4.26
- Merged pull requests: 48
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- avehtari (23)
- wds15 (23)
- venpopov (15)
- paul-buerkner (12)
- ASKurz (9)
- StaffanBetner (8)
- fweber144 (5)
- jflournoy (4)
- jsocolar (4)
- emstruong (4)
- SermetPekin (4)
- bmfazio (4)
- fusaroli (4)
- drjhw (4)
- n-kall (4)
Pull Request Authors
- venpopov (20)
- SermetPekin (20)
- paul-buerkner (14)
- avehtari (12)
- fweber144 (9)
- n-kall (9)
- bschneidr (4)
- jsocolar (3)
- andrjohns (3)
- bmfazio (3)
- yananlong (2)
- mcol (2)
- tom-peatman (2)
- wds15 (2)
- mages (2)
Top Labels
Issue Labels
feature (112)
bug (62)
family (24)
documentation (21)
efficiency (16)
post-processing (15)
good first issue (14)
formula (9)
priors (7)
autocorrelation (5)
Stan (4)
wontfix (3)
question (3)
duplicate (1)
objects (1)
Pull Request Labels
feature (11)
bug (1)
documentation (1)
Packages
- Total packages: 3
-
Total downloads:
- cran 27,065 last-month
- Total docker downloads: 55,954
-
Total dependent packages: 54
(may contain duplicates) -
Total dependent repositories: 188
(may contain duplicates) - Total versions: 135
- Total maintainers: 1
cran.r-project.org: brms
Bayesian Regression Models using 'Stan'
- Homepage: https://github.com/paul-buerkner/brms
- Documentation: http://cran.r-project.org/web/packages/brms/brms.pdf
- License: GPL-2
-
Latest release: 2.22.0
published over 1 year ago
Rankings
Stargazers count: 0.2%
Forks count: 0.3%
Dependent repos count: 1.4%
Dependent packages count: 1.7%
Downloads: 3.1%
Average: 4.8%
Docker downloads count: 21.8%
Maintainers (1)
Last synced:
6 months ago
proxy.golang.org: github.com/paul-buerkner/brms
- Documentation: https://pkg.go.dev/github.com/paul-buerkner/brms#section-documentation
- License: gpl-2.0
-
Latest release: v2.22.0+incompatible
published over 1 year ago
Rankings
Dependent packages count: 5.5%
Average: 5.6%
Dependent repos count: 5.8%
Last synced:
6 months ago
conda-forge.org: r-brms
- Homepage: http://discourse.mc-stan.org
- License: GPL-2.0-only
-
Latest release: 2.18.0
published over 3 years ago
Rankings
Dependent repos count: 20.1%
Average: 24.5%
Dependent packages count: 29.0%
Last synced:
6 months ago
Dependencies
.github/workflows/R-CMD-check.yaml
actions
- actions/checkout v2 composite
- n1hility/cancel-previous-runs v2 composite
- r-lib/actions/check-r-package v2 composite
- r-lib/actions/setup-pandoc v2 composite
- r-lib/actions/setup-r v2.2.6 composite
- r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml
actions
- actions/checkout v2 composite
- n1hility/cancel-previous-runs v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION
cran
- R >= 3.5.0 depends
- Rcpp >= 0.12.0 depends
- methods * depends
- Matrix >= 1.1.1 imports
- abind * imports
- backports * imports
- bayesplot >= 1.5.0 imports
- bridgesampling >= 0.3 imports
- coda * imports
- future >= 1.19.0 imports
- ggplot2 >= 2.0.0 imports
- glue >= 1.3.0 imports
- grDevices * imports
- loo >= 2.3.1 imports
- matrixStats * imports
- mgcv >= 1.8 imports
- nleqslv * imports
- nlme * imports
- parallel * imports
- posterior >= 1.0.0 imports
- rstan >= 2.19.2 imports
- rstantools >= 2.1.1 imports
- shinystan >= 2.4.0 imports
- stats * imports
- utils * imports
- MCMCglmm * suggests
- R.rsp * suggests
- RWiener * suggests
- ape * suggests
- arm * suggests
- cmdstanr >= 0.5.0 suggests
- diffobj * suggests
- digest * suggests
- emmeans >= 1.4.2 suggests
- extraDistr * suggests
- gtable * suggests
- knitr * suggests
- lme4 * suggests
- mice * suggests
- mnormt * suggests
- processx * suggests
- projpred >= 2.0.0 suggests
- rmarkdown * suggests
- rtdists * suggests
- shiny * suggests
- spdep * suggests
- splines2 * suggests
- statmod * suggests
- testthat >= 0.9.1 suggests