Brms
Brms
1
2 R topics documented:
URL https://github.com/paul-buerkner/brms,
http://discourse.mc-stan.org
BugReports https://github.com/paul-buerkner/brms/issues
VignetteBuilder knitr, R.rsp
RoxygenNote 7.1.0
Author Paul-Christian Bürkner [aut, cre]
Maintainer Paul-Christian Bürkner <[email protected]>
Repository CRAN
Date/Publication 2020-05-27 05:30:08 UTC
R topics documented:
brms-package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
addition-terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
add_criterion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
add_loo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
ar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
arma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
as.mcmc.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
AsymLaplace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
autocor-terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
autocor.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
bayes_factor.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
bayes_R2.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
bridge_sampler.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
brm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
brmsfamily . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
brmsfit-class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
brmsformula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
brmsformula-helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
brmshypothesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
brmsterms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
brm_multiple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
coef.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
combine_models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
compare_ic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
conditional_effects.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
conditional_smooths.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
control_params . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
cor_ar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
cor_arma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
cor_brms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
cor_car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
cor_cosy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
R topics documented: 3
cor_fixed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
cor_ma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
cor_sar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
cosy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
custom_family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
density_ratio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
diagnostic-quantities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Dirichlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
emmeans-brms-helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
epilepsy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
ExGaussian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
expose_functions.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
expp1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
family.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
fcor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
fitted.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
fixef.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Frechet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
GenExtremeValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
get_prior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
gp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
gr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
horseshoe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Hurdle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
hypothesis.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
inhaler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
InvGaussian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
inv_logit_scaled . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
is.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
is.brmsfit_multiple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
is.brmsformula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
is.brmsprior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
is.brmsterms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
is.cor_brms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
is.mvbrmsformula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
is.mvbrmsterms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
kfold.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
kfold_predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
kidney . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
lasso . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
launch_shinystan.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
logit_scaled . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
logm1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
log_lik.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
loo.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
loo_compare.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
loo_model_weights.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
4 R topics documented:
loo_predict.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
loo_R2.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
loo_subsample.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
ma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
make_conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
make_stancode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
make_standata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
mcmc_plot.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
mi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
mixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
mm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
mmc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
mo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
model_weights.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
MultiNormal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
MultiStudentT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
mvbind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
mvbrmsformula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
ngrps.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
nsamples.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
pairs.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
parnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
plot.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
posterior_average.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
posterior_epred.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
posterior_interval.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
posterior_linpred.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
posterior_predict.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
posterior_samples.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
posterior_summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
posterior_table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
post_prob.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
pp_average.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
pp_check.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
pp_mixture.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
predict.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
predictive_error.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
predictive_interval.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
prepare_predictions.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
print.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
print.brmsprior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
prior_samples.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
prior_summary.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
ranef.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
reloo.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
rename_pars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
residuals.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
brms-package 5
restructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
rows2labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
sar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
set_prior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Shifted_Lognormal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
SkewNormal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
stancode.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
standata.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
stanvar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
StudentT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
summary.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
theme_black . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
theme_default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
update.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
update.brmsfit_multiple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
update_adterms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
validate_newdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
VarCorr.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
vcov.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
VonMises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
waic.brmsfit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Wiener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
ZeroInflated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Index 205
Description
The brms package provides an interface to fit Bayesian generalized multivariate (non-)linear mul-
tilevel models using Stan, which is a C++ package for obtaining full Bayesian inference (see
http://mc-stan.org/). The formula syntax is an extended version of the syntax applied in the
lme4 package to provide a familiar and simple interface for performing regression analyses.
Details
The main function of brms is brm, which uses formula syntax to specify a wide range of com-
plex Bayesian models (see brmsformula for details). Based on the supplied formulas, data, and
additional information, it writes the Stan code on the fly via make_stancode, prepares the data via
make_standata, and fits the model using Stan.
Subsequently, a large number of post-processing methods can be applied: To get an overview on
the estimated parameters, summary or conditional_effects are perfectly suited. Detailed visual
analyses can be performed by applying the pp_check and stanplot methods, which both rely on
the bayesplot package. Model comparsions can be done via loo and waic, which make use of the
6 addition-terms
loo package as well as via bayes_factor which relies on the bridgesampling package. For a full
list of methods to apply, type methods(class = "brmsfit").
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 Win-
dows. On Mac, you should use Xcode. For further instructions on how to get the compilers running,
see the prerequisites section at the RStan-Getting-Started page.
When comparing other packages fitting multilevel models to brms, keep in mind that the latter
needs to compile models before actually fitting them, which will require between 20 and 40 seconds
depending on your machine, operating system and overall model complexity.
Thus, fitting smaller models may be relatively slow as compilation time makes up the majority of
the whole running time. For larger / more complex models however, fitting my take several minutes
or even hours, so that the compilation time won’t make much of a difference for these models.
See vignette("brms_overview") and vignette("brms_multilevel") for a general introduc-
tion and overview of brms. For a full list of available vignettes, type vignette(package = "brms").
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
Paul-Christian Buerkner (2018). Advanced Bayesian Multilevel Modeling with the R Package
brms. The R Journal. 10(1), 395–411. doi:10.32614/RJ-2018-017
The Stan Development Team. Stan Modeling Language User’s Guide and Reference Manual.
http://mc-stan.org/users/documentation.
Stan Development Team (2018). RStan: the R interface to Stan. R package version 2.18.1. http:
//mc-stan.org
See Also
brm, brmsformula, brmsfamily, brmsfit
Description
Provide additional information on the response variable in brms models, such as censoring, trunca-
tion, or known measurement error.
Usage
resp_se(x, sigma = FALSE)
resp_trials(x)
addition-terms 7
resp_thres(x, gr = NA)
resp_cat(x)
resp_dec(x)
resp_cens(x, y2 = NA)
resp_mi(sdy = NA)
resp_rate(denom)
resp_subset(x)
resp_vreal(...)
resp_vint(...)
Arguments
x A vector; usually a variable defined in the data. Allowed values depend on the
function: resp_se and resp_weights require positive numeric values. resp_trials,
resp_thres, and resp_cat require positive integers. resp_dec requires 0 and
1, or alternatively 'lower' and 'upper'. resp_subset requires 0 and 1, or
alternatively FALSE and TRUE. resp_cens requires 'left', 'none', 'right',
and 'interval' (or equivalently -1, 0, 1, and 2) to indicate left, no, right, or
interval censoring.
sigma Logical; Indicates whether the residual standard deviation parameter sigma should
be included in addition to the known measurement error. Defaults to FALSE for
backwards compatibility, but setting it to TRUE is usually the better choice.
scale Logical; Indicates whether weights should be scaled so that the average weight
equals one. Defaults to FALSE.
gr A vector of grouping indicators.
y2 A vector specifying the upper bounds in interval censoring. Will be ignored
for non-interval censored observations. However, it should NOT be NA even
for non-interval censored observations to avoid accidental exclusion of these
observations.
lb A numeric vector or single numeric value specifying the lower truncation bound.
ub A numeric vector or single numeric value specifying the upper truncation bound.
sdy Optional known measurement error of the response treated as standard devia-
tion. If specified, handles measurement error and (completely) missing values
at the same time using the plausible-values-technique.
denom A vector of positive numeric values specifying the denominator values from
which the response rates are computed.
8 addition-terms
... For resp_vreal, vectors of real values. For resp_vint, vectors of integer val-
ues.
Details
These functions are almost solely useful when called in formulas passed to the brms package.
Within formulas, the resp_ prefix may be omitted. More information is given in the ’Details’
section of brmsformula.
Value
A list of additional response information to be processed further by brms.
See Also
brm, brmsformula
Examples
## Not run:
## Random effects meta-analysis
nstudies <- 20
true_effects <- rnorm(nstudies, 0.5, 0.2)
sei <- runif(nstudies, 0.05, 0.3)
outcomes <- rnorm(nstudies, true_effects, sei)
data1 <- data.frame(outcomes, sei)
fit1 <- brm(outcomes | se(sei, sigma = TRUE) ~ 1,
data = data1)
summary(fit1)
## End(Not run)
add_criterion 9
Description
Add model fit criteria to model objects
Usage
add_criterion(x, ...)
Arguments
x An R object typically of class brmsfit.
... Further arguments passed to the underlying functions computing the model fit
criteria.
criterion Names of model fit criteria to compute. Currently supported are "loo", "waic",
"kfold", "loo_subsample", "bayes_R2" (Bayesian R-squared), "loo_R2" (LOO-
adjusted R-squared), and "marglik" (log marginal likelihood).
model_name Optional name of the model. If NULL (the default) the name is taken from the
call to x.
overwrite Logical; Indicates if already stored fit indices should be overwritten. Defaults to
FALSE.
file Either NULL or a character string. In the latter case, the fitted model object
including the newly added criterion values is saved via saveRDS in a file named
after the string supplied in file. The .rds extension is added automatically. If
x was already stored in a file before, the file name will be reused automatically
(with a message) unless overwritten by file. In any case, file only applies if
new criteria were actually added via add_criterion or if force_save was set
to TRUE.
force_save Logical; only relevant if file is specified and ignored otherwise. If TRUE, the
fitted model object will be saved regardless of whether new criteria were added
via add_criterion.
10 add_loo
Details
Functions add_loo and add_waic are aliases of add_criterion with fixed values for the criterion
argument.
Value
An object of the same class as x, but with model fit criteria added for later usage.
Examples
## Not run:
fit <- brm(count ~ Trt, data = epilepsy)
# add both LOO and WAIC at once
fit <- add_criterion(fit, c("loo", "waic"))
print(fit$criteria$loo)
print(fit$criteria$waic)
## End(Not run)
Description
Deprecated aliases of add_criterion.
Usage
add_loo(x, model_name = NULL, ...)
add_ic(x, ...)
Arguments
x An R object typically of class brmsfit.
model_name Optional name of the model. If NULL (the default) the name is taken from the
call to x.
... Further arguments passed to the underlying functions computing the model fit
criteria.
ic, value Names of model fit criteria to compute. Currently supported are "loo", "waic",
"kfold", "R2" (R-squared), and "marglik" (log marginal likelihood).
ar 11
Value
An object of the same class as x, but with model fit criteria added for later usage. Previously
computed criterion objects will be overwritten.
Description
Set up an autoregressive (AR) term of order p in brms. The function does not evaluate its arguments
– it exists purely to help set up a model with AR terms.
Usage
Arguments
time An optional time variable specifying the time ordering of the observations. By
default, the existing order of the observations in the data is used.
gr An optional grouping variable. If specified, the correlation structure is assumed
to apply only to observations within the same grouping level.
p A non-negative integer specifying the autoregressive (AR) order of the ARMA
structure. Default is 1.
cov A flag indicating whether ARMA effects should be estimated by means of resid-
ual covariance matrices. This is currently only possible for stationary ARMA
effects of order 1. If the model family does not have natural residuals, latent
residuals are added automatically. If FALSE (the default), a regression formu-
lation is used that is considerably faster and allows for ARMA effects of order
higher than 1 but is only available for gaussian models and some of its gener-
alizations.
Value
See Also
autocor-terms, arma, ma
12 arma
Examples
## Not run:
data("LakeHuron")
LakeHuron <- as.data.frame(LakeHuron)
fit <- brm(x ~ ar(p = 2), data = LakeHuron)
summary(fit)
## End(Not run)
Description
Set up an autoregressive moving average (ARMA) term of order (p, q) in brms. The function does
not evaluate its arguments – it exists purely to help set up a model with ARMA terms.
Usage
arma(time = NA, gr = NA, p = 1, q = 1, cov = FALSE)
Arguments
time An optional time variable specifying the time ordering of the observations. By
default, the existing order of the observations in the data is used.
gr An optional grouping variable. If specified, the correlation structure is assumed
to apply only to observations within the same grouping level.
p A non-negative integer specifying the autoregressive (AR) order of the ARMA
structure. Default is 1.
q A non-negative integer specifying the moving average (MA) order of the ARMA
structure. Default is 1.
cov A flag indicating whether ARMA effects should be estimated by means of resid-
ual covariance matrices. This is currently only possible for stationary ARMA
effects of order 1. If the model family does not have natural residuals, latent
residuals are added automatically. If FALSE (the default), a regression formu-
lation is used that is considerably faster and allows for ARMA effects of order
higher than 1 but is only available for gaussian models and some of its gener-
alizations.
Value
An object of class 'arma_term', which is a list of arguments to be interpreted by the formula
parsing functions of brms.
as.mcmc.brmsfit 13
See Also
autocor-terms, ar, ma,
Examples
## Not run:
data("LakeHuron")
LakeHuron <- as.data.frame(LakeHuron)
fit <- brm(x ~ arma(p = 2, q = 1), data = LakeHuron)
summary(fit)
## End(Not run)
as.mcmc.brmsfit Extract posterior samples for use with the coda package
Description
Extract posterior samples for use with the coda package
Usage
## S3 method for class 'brmsfit'
as.mcmc(
x,
pars = NA,
fixed = FALSE,
combine_chains = FALSE,
inc_warmup = FALSE,
...
)
Arguments
x An R object typically of class brmsfit
pars Names of parameters for which posterior samples should be returned, as given
by a character vector or regular expressions. By default, all posterior samples of
all parameters are extracted.
fixed Indicates whether parameter names should be matched exactly (TRUE) or treated
as regular expressions (FALSE). Default is FALSE.
combine_chains Indicates whether chains should be combined.
inc_warmup Indicates if the warmup samples should be included. Default is FALSE. Warmup
samples are used to tune the parameters of the sampling algorithm and should
not be analyzed.
... currently unused
14 AsymLaplace
Value
If combine_chains = TRUE an mcmc object is returned. If combine_chains = FALSE an mcmc.list
object is returned.
Description
Density, distribution function, quantile function and random generation for the asymmetric Laplace
distribution with location mu, scale sigma and asymmetry parameter quantile.
Usage
dasym_laplace(x, mu = 0, sigma = 1, quantile = 0.5, log = FALSE)
pasym_laplace(
q,
mu = 0,
sigma = 1,
quantile = 0.5,
lower.tail = TRUE,
log.p = FALSE
)
qasym_laplace(
p,
mu = 0,
sigma = 1,
quantile = 0.5,
lower.tail = TRUE,
log.p = FALSE
)
Arguments
x, q Vector of quantiles.
mu Vector of locations.
sigma Vector of scales.
quantile Asymmetry parameter corresponding to quantiles in quantile regression (hence
the name).
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
autocor-terms 15
Details
Description
Specify autocorrelation terms in brms models. Currently supported terms are arma, ar, ma, cosy,
sar, car, and fcor. Terms can be directly specified within the formula, or passed to the autocor
argument of brmsformula in the form of a one-sided formula. For deprecated ways of specifying
autocorrelation terms, see cor_brms.
Details
The autocor term functions are almost solely useful when called in formulas passed to the brms
package. They do not evaluate its arguments – but exist purely to help set up a model with autocor-
relation terms.
See Also
Examples
Description
(Deprecated) Extract Autocorrelation Objects
Usage
## S3 method for class 'brmsfit'
autocor(object, resp = NULL, ...)
autocor(object, ...)
Arguments
object An object of class brmsfit.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
... Currently unused.
Value
A cor_brms object or a list of such objects for multivariate models. Not supported for models fitted
with brms 2.11.1 or higher.
Description
Compute Bayes factors from marginal likelihoods.
Usage
## S3 method for class 'brmsfit'
bayes_factor(x1, x2, log = FALSE, ...)
Arguments
x1 A brmsfit object
x2 Another brmsfit object based on the same responses.
log Report Bayes factors on the log-scale?
... Additional arguments passed to bridge_sampler.
bayes_R2.brmsfit 17
Details
Computing the marginal likelihood requires samples of all variables defined in Stan’s parameters
block to be saved. Otherwise bayes_factor cannot be computed. Thus, please set save_all_pars
= TRUE in the call to brm, if you are planning to apply bayes_factor to your models.
The computation of Bayes factors based on bridge sampling requires a lot more posterior samples
than usual. A good conservative rule of thumb is perhaps 10-fold more samples (read: the default
of 4000 samples may not be enough in many cases). If not enough posterior samples are provided,
the bridge sampling algorithm tends to be unstable, leading to considerably different results each
time it is run. We thus recommend running bayes_factor multiple times to check the stability of
the results.
More details are provided under bridgesampling:bayes_factor.
See Also
bridge_sampler,post_prob
Examples
## Not run:
# model with the treatment effect
fit1 <- brm(
count ~ zAge + zBase + Trt,
data = epilepsy, family = negbinomial(),
prior = prior(normal(0, 1), class = b),
save_all_pars = TRUE
)
summary(fit1)
## End(Not run)
Description
Compute a Bayesian version of R-squared for regression models
18 bayes_R2.brmsfit
Usage
Arguments
Details
For an introduction to the approach, see Gelman et al. (2018) and https://github.com/jgabry/
bayes_R2/.
Value
References
Andrew Gelman, Ben Goodrich, Jonah Gabry & Aki Vehtari. (2018). R-squared for Bayesian re-
gression models, The American Statistician. https://doi.org/10.1080/00031305.2018.1549100.
(Preprint available at https://stat.columbia.edu/~gelman/research/published/bayes_R2_
v3.pdf.)
bridge_sampler.brmsfit 19
Examples
## Not run:
fit <- brm(mpg ~ wt + cyl, data = mtcars)
summary(fit)
bayes_R2(fit)
## End(Not run)
bridge_sampler.brmsfit
Log Marginal Likelihood via Bridge Sampling
Description
Computes log marginal likelihood via bridge sampling, which can be used in the computation of
bayes factors and posterior model probabilities. The brmsfit method is just a thin wrapper around
the corresponding method for stanfit objects.
Usage
## S3 method for class 'brmsfit'
bridge_sampler(samples, ...)
Arguments
samples A brmsfit object.
... Additional arguments passed to bridge_sampler.stanfit.
Details
Computing the marginal likelihood requires samples of all variables defined in Stan’s parameters
block to be saved. Otherwise bridge_sampler cannot be computed. Thus, please set save_all_pars
= TRUE in the call to brm, if you are planning to apply bridge_sampler to your models.
The computation of marginal likelihoods based on bridge sampling requires a lot more posterior
samples than usual. A good conservative rule of thump is perhaps 10-fold more samples (read:
the default of 4000 samples may not be enough in many cases). If not enough posterior samples
are provided, the bridge sampling algorithm tends to be unstable leading to considerably different
results each time it is run. We thus recommend running bridge_sampler multiple times to check
the stability of the results.
More details are provided under bridgesampling:bridge_sampler.
20 brm
See Also
bayes_factor,post_prob
Examples
## Not run:
# model with the treatment effect
fit1 <- brm(
count ~ zAge + zBase + Trt,
data = epilepsy, family = negbinomial(),
prior = prior(normal(0, 1), class = b),
save_all_pars = TRUE
)
summary(fit1)
bridge_sampler(fit1)
## End(Not run)
Description
Fit Bayesian generalized (non-)linear multivariate 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 stan-
dard errors, and quite a few more. In addition, all parameters of the response distributions can
be predicted in order to perform distributional regression. Prior specifications are flexible and ex-
plicitly 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.
brm 21
Usage
brm(
formula,
data,
family = gaussian(),
prior = NULL,
autocor = NULL,
data2 = NULL,
cov_ranef = NULL,
sample_prior = "no",
sparse = NULL,
knots = NULL,
stanvars = 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 = NA,
save_model = NULL,
stan_model_args = list(),
save_dso = TRUE,
file = NULL,
empty = FALSE,
rename = TRUE,
...
)
Arguments
formula An object of class formula, brmsformula, or mvbrmsformula (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) con-
taining 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
22 brm
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. In multivariate models, family
might also be a list of families.
prior One or more brmsprior objects created by set_prior or related functions and
combined using the c method or the + operator. See also get_prior for more
help.
autocor (Deprecated) 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. In multivariate models, autocor might
also be a list of autocorrelation structures. It is now recommend to specify auto-
correlation terms directly within formula. See brmsformula for more details.
data2 A named list of objects containing data, which cannot be passed via argument
data. Required for some objects used in autocorrelation structures to specify
dependency structures as well as for within-group covariance matrices.
cov_ranef (Deprecated) A list of matrices that are proportional to the (within) covariance
structure of the group-level effects. The names of the matrices should corre-
spond 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 ef-
fects. It is now recommended to specify those matrices in the formula interface
using the gr and related functions. See vignette("brms_phylogenetics")
for more details.
sample_prior Indicate if samples from priors should be drawn additionally to the posterior
samples. Options are "no" (the default), "yes", and "only". Among others,
these samples can be used to calculate Bayes factors for point hypotheses via
hypothesis. Please note that improper priors are not sampled, including the
default improper priors used by brm. See set_prior on how to set (proper) pri-
ors. Please also note that prior samples for the overall intercept are not obtained
by default for technical reasons. See brmsformula how to obtain prior samples
for the intercept. If sample_prior is set to "only", samples are drawn solely
from the priors ignoring the likelihood, which allows among others to generate
samples from the prior predictive distribution. In this case, all parameters must
have proper priors.
sparse (Deprecated) Logical; indicates whether the population-level design matrices
should be treated as sparse (defaults to FALSE). For design matrices with many
zeros, this can considerably reduce required memory. Sampling speed is cur-
rently not improved or even slightly decreased. It is now recommended to use
the sparse argument of brmsformula and related functions.
knots Optional list containing user specified knot values to be used for basis construc-
tion of smoothing terms. See gamm for more details.
stanvars An optional stanvars object generated by function stanvar to define additional
variables for use in Stan’s program blocks.
stan_funs (Deprecated) An optional character string containing self-defined Stan func-
tions, which will be included in the functions block of the generated Stan code.
It is now recommended to use the stanvars argument for this purpose instead.
brm 23
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.
save_mevars A flag to indicate if samples of latent noise-free variables obtained by using me
and mi terms should be saved (default is FALSE). Saving these samples allows to
better use methods such as predict with the latent 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 initialized
to zero. This option is sometimes useful for certain families, 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.
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 indepen-
dent normal distributions, or "fullrank" for variational inference with a multi-
variate 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
24 brm
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. To stop Stan from opening additional
progress bars, set open_progress = FALSE.
seed The seed for random number generation to make results reproducible. If NA (the
default), Stan will set the seed randomly.
save_model Either NULL or a character string. In the latter case, the model’s Stan code is
saved via cat in a text file named after the string supplied in save_model.
stan_model_args
A list of further arguments passed to stan_model.
save_dso (Deprecated) 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).
file Either NULL or a character string. In the latter case, the fitted model object is
saved via saveRDS in a file named after the string supplied in file. The .rds
extension is added automatically. If the file already exists, brm will load and
return the saved model object instead of refitting the model. As existing files
won’t be overwritten, you have to manually remove the file in order to refit
and save the model under an existing file name. The file name is stored in the
brmsfit object for later usage.
empty Logical. If TRUE, the Stan model is not created and compiled and the corre-
sponding 'fit' slot of the brmsfit object will be empty. This is useful if you
have estimated a brms-created Stan model outside of brms and want to feed it
back into the package.
rename For internal use only.
... Further arguments passed to Stan that is to sampling or vb.
Details
Fit a generalized (non-)linear multivariate 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 in-
formation 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 infor-
mative so that their influence on the results will be negligible and you usually don’t have to worry
brm 25
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.
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.
Author(s)
Paul-Christian Buerkner <[email protected]>
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
Paul-Christian Buerkner (2018). Advanced Bayesian Multilevel Modeling with the R Package
brms. The R Journal. 10(1), 395–411. doi:10.32614/RJ-2018-017
See Also
brms, brmsformula, brmsfamily, brmsfit
Examples
## 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
bprior1 <- prior(student_t(5,0,10), class = b) +
prior(cauchy(0,2), class = sd)
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
26 brm
## End(Not run)
Description
Family objects provide a convenient way to specify the details of the models used by many model
fitting functions. The family functions presented here are for use with brms only and will **not**
work with other model fitting functions such as glm or glmer. However, the standard family func-
tions as described in family will work with brms. You can also specify custom families for use in
brms with the custom_family function.
Usage
brmsfamily(
family,
link = NULL,
link_sigma = "log",
link_shape = "log",
link_nu = "logm1",
link_phi = "log",
link_kappa = "log",
link_beta = "log",
link_zi = "logit",
link_hu = "logit",
link_zoi = "logit",
link_coi = "logit",
link_disc = "log",
link_bs = "log",
link_ndt = "log",
link_bias = "logit",
link_xi = "log1p",
link_alpha = "identity",
link_quantile = "logit",
threshold = "flexible",
refcat = NULL,
bhaz = NULL
)
bernoulli(link = "logit")
geometric(link = "log")
exponential(link = "log")
wiener(
link = "identity",
link_bs = "log",
link_ndt = "log",
link_bias = "logit"
)
hurdle_poisson(link = "log")
zero_one_inflated_beta(
link = "logit",
link_phi = "log",
link_zoi = "logit",
link_coi = "logit"
)
Arguments
family A character string naming the distribution of the response variable be used in
the model. Currently, the following families are supported: gaussian, student,
binomial, bernoulli, poisson, negbinomial, geometric, Gamma, skew_normal,
lognormal, shifted_lognormal, exgaussian, wiener, inverse.gaussian,
exponential, weibull, frechet, Beta, dirichlet, von_mises, asym_laplace,
gen_extreme_value, categorical, multinomial, cumulative, cratio, sratio,
acat, hurdle_poisson, hurdle_negbinomial, hurdle_gamma, hurdle_lognormal,
zero_inflated_binomial, zero_inflated_beta, zero_inflated_negbinomial,
zero_inflated_poisson, and zero_one_inflated_beta.
link A specification for the model link function. This can be a name/expression or
character string. See the ’Details’ section for more information on link functions
supported by each family.
link_sigma Link of auxiliary parameter sigma if being predicted.
link_shape Link of auxiliary parameter shape if being predicted.
link_nu Link of auxiliary parameter nu if being predicted.
link_phi Link of auxiliary parameter phi if being predicted.
link_kappa Link of auxiliary parameter kappa if being predicted.
link_beta Link of auxiliary parameter beta if being predicted.
link_zi Link of auxiliary parameter zi if being predicted.
link_hu Link of auxiliary parameter hu if being predicted.
link_zoi Link of auxiliary parameter zoi if being predicted.
link_coi Link of auxiliary parameter coi if being predicted.
link_disc Link of auxiliary parameter disc if being predicted.
link_bs Link of auxiliary parameter bs if being predicted.
link_ndt Link of auxiliary parameter ndt if being predicted.
link_bias Link of auxiliary parameter bias if being predicted.
link_xi Link of auxiliary parameter xi if being predicted.
link_alpha Link of auxiliary parameter alpha if being predicted.
link_quantile Link of auxiliary parameter quantile if being predicted.
brmsfamily 31
threshold A character string indicating the type of thresholds (i.e. intercepts) used in
an ordinal model. "flexible" provides the standard unstructured thresholds,
"equidistant" restricts the distance between consecutive thresholds to the
same value, and "sum_to_zero" ensures the thresholds sum to zero.
refcat Optional name of the reference response category used in categorical, multino-
mial, and dirichlet models. If NULL (the default), the first category is used as the
reference. If NA, all categories will be predicted, which requires strong priors or
carefully specified predictor terms in order to lead to an identified model.
bhaz Currently for experimental purposes only.
Details
Below, we list common use cases for the different families. This list is not ment to be exhaustive.
Below, we list all possible links for each family. The first link mentioned for each family is the
default.
Please note that when calling the Gamma family function of the stats package, the default link will
be inverse instead of log although the latter is the default in brms. Also, when using the family
functions gaussian, binomial, poisson, and Gamma of the stats package (see family), special link
functions such as softplus or cauchit won’t work. In this case, you have to use brmsfamily to
specify the family with corresponding link function.
See Also
Examples
Description
Models fitted with the brms package are represented as a brmsfit object, which contains the pos-
terior samples, model formula, Stan code, relevant data, and other information.
Details
See methods(class = "brmsfit") for an overview of available methods.
Slots
formula A brmsformula object.
data A data.frame containing all variables used in the model.
data2 A list of data objects which cannot be passed via data.
prior A brmsprior object containing information on the priors used in the model.
stanvars A stanvars object.
model The model code in Stan language.
ranef A data.frame containing the group-level structure.
exclude The names of the parameters for which samples are not saved.
algorithm The name of the algorithm used to fit the model.
fit An object of class stanfit among others containing the posterior samples.
criteria An empty list for adding model fit criteria after estimation of the model.
file Optional name of a file in which the model object was stored in or loaded from.
version The versions of brms and rstan with which the model was fitted.
family (Deprecated) A brmsfamily object.
autocor (Deprecated) An cor_brms object containing the autocorrelation structure if specified.
cov_ranef (Deprecated) A list of customized group-level covariance matrices.
stan_funs (Deprecated) A character string of length one or NULL.
data.name (Deprecated) The name of data as specified by the user.
See Also
brms, brm, brmsformula, brmsfamily
34 brmsformula
Description
Set up a model formula for use in the brms package allowing to define (potentially non-linear)
additive multilevel models for all parameters of the assumed response distribution.
Usage
brmsformula(
formula,
...,
flist = NULL,
family = NULL,
autocor = NULL,
nl = NULL,
loop = NULL,
center = NULL,
cmc = NULL,
sparse = NULL,
decomp = NULL
)
Arguments
formula An object of class formula (or one that can be coerced to that class): a symbolic
description of the model to be fitted. The details of model specification are given
in ’Details’.
... Additional formula objects to specify predictors of non-linear and distributional
parameters. Formulas can either be named directly or contain names on their
left-hand side. Alternatively, it is possible to fix parameters to certain values by
passing numbers or character strings in which case arguments have to be named
to provide the parameter names. See ’Details’ for more information.
flist Optional list of formulas, which are treated in the same way as formulas passed
via the ... argument.
family Same argument as in brm. If family is specified in brmsformula, it will over-
write the value specified in other functions.
autocor An optional formula which contains autocorrelation terms as described in autocor-terms
or alternatively a cor_brms object (deprecated). If autocor is specified in
brmsformula, it will overwrite the value specified in other functions.
nl Logical; Indicates whether formula should be treated as specifying a non-linear
model. By default, formula is treated as an ordinary linear model formula.
loop Logical; Only used in non-linear models. Indicates if the computation of the
non-linear formula should be done inside (TRUE) or outside (FALSE) a loop over
observations. Defaults to TRUE.
brmsformula 35
Details
General formula structure
The formula argument accepts formulas of the following syntax:
response | aterms ~ pterms + (gterms | group)
The pterms part contains effects that are assumed to be the same across observations. We call them
’population-level’ or ’overall’ effects, or (adopting frequentist vocabulary) ’fixed’ effects. The op-
tional gterms part may contain effects that are assumed to vary across grouping variables specified
in group. We call them ’group-level’ or ’varying’ effects, or (adopting frequentist vocabulary) ’ran-
dom’ effects, although the latter name is misleading in a Bayesian context. For more details type
vignette("brms_overview") and vignette("brms_multilevel").
Group-level terms
Multiple grouping factors each with multiple group-level effects are possible. (Of course we can
also run models without any group-level effects.) Instead of | you may use || in grouping terms to
prevent correlations from being modeled. Equivalently, the cor argument of the gr function can be
used for this purpose, for example, (1 + x || g) is equivalent to (1 + x | gr(g,cor = FALSE)).
It is also possible to model different group-level terms of the same grouping factor as correlated
(even across different formulas, e.g., in non-linear models) by using |<ID>| instead of |. All group-
level terms sharing the same ID will be modeled as correlated. If, for instance, one specifies the
terms (1+x|i|g) and (1+z|i|g) somewhere in the formulas passed to brmsformula, correlations
between the corresponding group-level effects will be estimated. In the above example, i is not a
variable in the data but just a symbol to indicate correlations between multiple group-level terms.
Equivalently, the id argument of the gr function can be used as well, for example, (1 + x | gr(g,id
= "i")).
If levels of the grouping factor belong to different sub-populations, it may be reasonable to assume
a different covariance matrix for each of the sub-populations. For instance, the variation within the
treatment group and within the control group in a randomized control trial might differ. Suppose
that y is the outcome, and x is the factor indicating the treatment and control group. Then, we
could estimate different hyper-parameters of the varying effects (in this case a varying intercept) for
treatment and control group via y ~ x + (1 | gr(subject,by = x)).
36 brmsformula
You can specify multi-membership terms using the mm function. For instance, a multi-membership
term with two members could be (1 | mm(g1,g2)), where g1 and g2 specify the first and second
member, respectively. Moreover, if a covariate x varies across the levels of the grouping-factors g1
and g2, we can save the respective covariate values in the variables x1 and x2 and then model the
varying effect as (1 + mmc(x1,x2) | mm(g1,g2)).
Special predictor terms
Flexible non-linear smooth terms can modeled using the s and t2 functions in the pterms part of
the model formula. This allows to fit generalized additive mixed models (GAMMs) with brms. The
implementation is similar to that used in the gamm4 package. For more details on this model class
see gam and gamm.
Gaussian process terms can be fitted using the gp function in the pterms part of the model formula.
Similar to smooth terms, Gaussian processes can be used to model complex non-linear relationships,
for instance temporal or spatial autocorrelation. However, they are computationally demanding and
are thus not recommended for very large datasets or approximations need to be used.
The pterms and gterms parts may contain four non-standard effect types namely monotonic, mea-
surement error, missing value, and category specific effects, which can be specified using terms of
the form mo(predictor), me(predictor,sd_predictor), mi(predictor), and cs(<predictors>),
respectively. Category specific effects can only be estimated in ordinal models and are explained in
more detail in the package’s main vignette (type vignette("brms_overview")). The other three
effect types are explained in the following.
A monotonic predictor must either be integer valued or an ordered factor, which is the first dif-
ference to an ordinary continuous predictor. More importantly, predictor categories (or integers)
are not assumed to be equidistant with respect to their effect on the response variable. Instead,
the distance between adjacent predictor categories (or integers) is estimated from the data and may
vary across categories. This is realized by parameterizing as follows: One parameter takes care of
the direction and size of the effect similar to an ordinary regression parameter, while an additional
parameter vector estimates the normalized distances between consecutive predictor categories. A
main application of monotonic effects are ordinal predictors that can this way be modeled without
(falsely) treating them as continuous or as unordered categorical predictors. For more details and
examples see vignette("brms_monotonic").
Quite often, predictors are measured and as such naturally contain measurement error. Although
most researchers are well aware of this problem, measurement error in predictors is ignored in
most regression analyses, possibly because only few packages allow for modeling it. Notably,
measurement error can be handled in structural equation models, but many more general regression
models (such as those featured by brms) cannot be transferred to the SEM framework. In brms,
effects of noise-free predictors can be modeled using the me (for ’measurement error’) function. If,
say, y is the response variable and x is a measured predictor with known measurement error sdx, we
can simply include it on the right-hand side of the model formula via y ~ me(x,sdx). This can easily
be extended to more general formulas. If x2 is another measured predictor with corresponding error
sdx2 and z is a predictor without error (e.g., an experimental setting), we can model all main effects
and interactions of the three predictors in the well known manner: y ~ me(x,sdx) * me(x2,sdx2)
* z. In future version of brms, a vignette will be added to explain more details about these so called
’error-in-variables’ models and provide real world examples.
When a variable contains missing values, the corresponding rows will be excluded from the data by
default (row-wise exclusion). However, quite often we want to keep these rows and instead estimate
the missing values. There are two approaches for this: (a) Impute missing values before the model
brmsformula 37
fitting for instance via multiple imputation (see brm_multiple for a way to handle multiple imputed
datasets). (b) Impute missing values on the fly during model fitting. The latter approach is explained
in the following. Using a variable with missing values as predictors requires two things, First, we
need to specify that the predictor contains missings that should to be imputed. If, say, y is the
primary response, x is a predictor with missings and z is a predictor without missings, we go for y
~ mi(x) + z. Second, we need to model x as an additional response with corresponding predictors
and the addition term mi(). In our example, we could write x | mi() ~ z. See mi for examples with
real data.
Autocorrelation terms
Autocorrelation terms can be directly specified inside the pterms part as well. Details can be found
in autocor-terms.
Additional response information
Another special of the brms formula syntax is the optional aterms part, which may contain multiple
terms of the form fun(<variable>) separated by + each providing special information on the
response variable. fun can be replaced with either se, weights, subset, cens, trunc, trials,
cat, dec, rate, vreal, or vint. Their meanings are explained below. (see also addition-terms).
For families gaussian, student and skew_normal, it is possible to specify standard errors of the
observations, thus allowing to perform meta-analysis. Suppose that the variable yi contains the
effect sizes from the studies and sei the corresponding standard errors. Then, fixed and random
effects meta-analyses can be conducted using the formulas yi | se(sei) ~ 1 and yi | se(sei) ~ 1
+ (1|study), respectively, where study is a variable uniquely identifying every study. If desired,
meta-regression can be performed via yi | se(sei) ~ 1 + mod1 + mod2 + (1|study) or
yi | se(sei) ~ 1 + mod1 + mod2 + (1 + mod1 + mod2|study), where mod1 and mod2 represent mod-
erator variables. By default, the standard errors replace the parameter sigma. To model sigma in
addition to the known standard errors, set argument sigma in function se to TRUE, for instance, yi
| se(sei,sigma = TRUE) ~ 1.
For all families, weighted regression may be performed using weights in the aterms part. In-
ternally, this is implemented by multiplying the log-posterior values of each observation by their
corresponding weights. Suppose that variable wei contains the weights and that yi is the response
variable. Then, formula yi | weights(wei) ~ predictors implements a weighted regression.
For multivariate models, subset may be used in the aterms part, to use different subsets of the data
in different univariate models. For instance, if sub is a logical variable and y is the response of one
of the univariate models, we may write y | subset(sub) ~ predictors so that y is predicted only
for those observations for which sub evaluates to TRUE.
For log-linear models such as poisson models, rate may be used in the aterms part to specify the
denomintor of a response that is expressed as a rate. The numerator is given by the actual response
variable and has a distribution according to the family as usual. Using rate(denom) is equivalent
to adding offset(log(denom)) to the linear predictor of the main parameter but the former is
arguably more convenient and explicit.
With the exception of categorical, ordinal, and mixture families, left, right, and interval censor-
ing can be modeled through y | cens(censored) ~ predictors. The censoring variable (named
censored in this example) should contain the values 'left', 'none', 'right', and 'interval'
(or equivalently -1, 0, 1, and 2) to indicate that the corresponding observation is left censored,
not censored, right censored, or interval censored. For interval censored data, a second vari-
able (let’s call it y2) has to be passed to cens. In this case, the formula has the structure y |
cens(censored,y2) ~ predictors. While the lower bounds are given in y, the upper bounds are
38 brmsformula
given in y2 for interval censored data. Intervals are assumed to be open on the left and closed on
the right: (y,y2].
With the exception of categorical, ordinal, and mixture families, the response distribution can be
truncated using the trunc function in the addition part. If the response variable is truncated be-
tween, say, 0 and 100, we can specify this via yi | trunc(lb = 0,ub = 100) ~ predictors. Instead
of numbers, variables in the data set can also be passed allowing for varying truncation points across
observations. Defining only one of the two arguments in trunc leads to one-sided truncation.
For all continuous families, missing values in the responses can be imputed within Stan by using
the addition term mi. This is mostly useful in combination with mi predictor terms as explained
above under ’Special predictor terms’.
For families binomial and zero_inflated_binomial, addition should contain a variable indicat-
ing the number of trials underlying each observation. In lme4 syntax, we may write for instance
cbind(success,n -success), which is equivalent to success | trials(n) in brms syntax. If
the number of trials is constant across all observations, say 10, we may also write success |
trials(10). Please note that the cbind() syntax will not work in brms in the expected way
because this syntax is reserved for other purposes.
For all ordinal families, aterms may contain a term thres(number) to specify the number thresh-
olds (e.g, thres(6)), which should be equal to the total number of response categories - 1. If not
given, the number of thresholds is calculated from the data. If different threshold vectors should be
used for different subsets of the data, the gr argument can be used to provide the grouping variable
(e.g, thres(6,gr = item), if item is the grouping variable). In this case, the number of thresholds
can also be a variable in the data with different values per group.
A deprecated quasi alias of thres() is cat() with which the total number of response categories
(i.e., number of thresholds + 1) can be specified.
In Wiener diffusion models (family wiener) the addition term dec is mandatory to specify the
(vector of) binary decisions corresponding to the reaction times. Non-zero values will be treated as
a response on the upper boundary of the diffusion process and zeros will be treated as a response
on the lower boundary. Alternatively, the variable passed to dec might also be a character vector
consisting of 'lower' and 'upper'.
For custom families, it is possible to pass an abitrary number of real and integer vectors via the addi-
tion terms vreal and vint, respectively. An example is provided in vignette('brms_customfamilies').
Multiple addition terms may be specified at the same time using the + operator. For example, the
formula formula = yi | se(sei) + cens(censored) ~ 1 implies a censored meta-analytic model.
The addition argument disp (short for dispersion) has been removed in version 2.0. You may
instead use the distributional regression approach by specifying sigma ~ 1 + offset(log(xdisp))
or shape ~ 1 + offset(log(xdisp)), where xdisp is the variable being previously passed to disp.
Parameterization of the population-level intercept
By default, the population-level intercept (if incorporated) is estimated separately and not as part
of population-level parameter vector b As a result, priors on the intercept also have to be speci-
fied separately. Furthermore, to increase sampling efficiency, the population-level design matrix
X is centered around its column means X_means if the intercept is incorporated. This leads to a
temporary bias in the intercept equal to <X_means,b>, where <,> is the scalar product. The bias
is corrected after fitting the model, but be aware that you are effectively defining a prior on the
intercept of the centered design matrix not on the real intercept. You can turn off this special han-
dling of the intercept by setting argument center to FALSE. For more details on setting priors on
population-level intercepts, see set_prior.
brmsformula 39
This behavior can be avoided by using the reserved (and internally generated) variable Intercept.
Instead of y ~ x, you may write y ~ 0 + Intercept + x. This way, priors can be defined on the real
intercept, directly. In addition, the intercept is just treated as an ordinary population-level effect and
thus priors defined on b will also apply to it. Note that this parameterization may be less efficient
than the default parameterization discussed above.
Formula syntax for non-linear models
In brms, it is possible to specify non-linear models of arbitrary complexity. The non-linear model
can just be specified within the formula argument. Suppose, that we want to predict the response
y through the predictor x, where x is linked to y through y = alpha -beta * lambda^x, with pa-
rameters alpha, beta, and lambda. This is certainly a non-linear model being defined via formula
= y ~ alpha -beta * lambda^x (addition arguments can be added in the same way as for ordinary
formulas). To tell brms that this is a non-linear model, we set argument nl to TRUE. Now we have
to specify a model for each of the non-linear parameters. Let’s say we just want to estimate those
three parameters with no further covariates or random effects. Then we can pass alpha + beta +
lambda ~ 1 or equivalently (and more flexible) alpha ~ 1,beta ~ 1,lambda ~ 1 to the ... argu-
ment. This can, of course, be extended. If we have another predictor z and observations nested
within the grouping factor g, we may write for instance alpha ~ 1,beta ~ 1 + z + (1|g),lambda ~
1. The formula syntax described above applies here as well. In this example, we are using z and
g only for the prediction of beta, but we might also use them for the other non-linear parameters
(provided that the resulting model is still scientifically reasonable).
By default, non-linear covariates are treated as real vectors in Stan. However, if the data of the
covariates is of type ‘integer‘ in R (which can be enforced by the ‘as.integer‘ function), the Stan
type will be changed to an integer array. That way, covariates can also be used for indexing purposes
in Stan.
Non-linear models may not be uniquely identified and / or show bad convergence. For this reason
it is mandatory to specify priors on the non-linear parameters. For instructions on how to do that,
see set_prior. For some examples of non-linear models, see vignette("brms_nonlinear").
Formula syntax for predicting distributional parameters
It is also possible to predict parameters of the response distribution such as the residual standard
deviation sigma in gaussian models or the hurdle probability hu in hurdle models. The syntax
closely resembles that of a non-linear parameter, for instance sigma ~ x + s(z) + (1+x|g). For
some examples of distributional models, see vignette("brms_distreg").
Parameter mu exists for every family and can be used as an alternative to specifying terms in
formula. If both mu and formula are given, the right-hand side of formula is ignored. Accordingly,
specifying terms on the right-hand side of both formula and mu at the same time is deprecated. In
future versions, formula might be updated by mu.
The following are distributional parameters of specific families (all other parameters are treated
as non-linear parameters): sigma (residual standard deviation or scale of the gaussian, student,
skew_normal, lognormal exgaussian, and asym_laplace families); shape (shape parameter of
the Gamma, weibull, negbinomial, and related zero-inflated / hurdle families); nu (degrees of
freedom parameter of the student and frechet families); phi (precision parameter of the beta
and zero_inflated_beta families); kappa (precision parameter of the von_mises family); beta
(mean parameter of the exponential component of the exgaussian family); quantile (quantile pa-
rameter of the asym_laplace family); zi (zero-inflation probability); hu (hurdle probability); zoi
(zero-one-inflation probability); coi (conditional one-inflation probability); disc (discrimination)
for ordinal models; bs, ndt, and bias (boundary separation, non-decision time, and initial bias of
40 brmsformula
the wiener diffusion model). By default, distributional parameters are modeled on the log scale if
they can be positive only or on the logit scale if the can only be within the unit interval.
Alternatively, one may fix distributional parameters to certain values. However, this is mainly useful
when models become too complicated and otherwise have convergence issues. We thus suggest to
be generally careful when making use of this option. The quantile parameter of the asym_laplace
distribution is a good example where it is useful. By fixing quantile, one can perform quantile
regression for the specified quantile. For instance, quantile = 0.25 allows predicting the 25%-
quantile. Furthermore, the bias parameter in drift-diffusion models, is assumed to be 0.5 (i.e. no
bias) in many applications. To achieve this, simply write bias = 0.5. Other possible applications
are the Cauchy distribution as a special case of the Student-t distribution with nu = 1, or the geomet-
ric distribution as a special case of the negative binomial distribution with shape = 1. Furthermore,
the parameter disc (’discrimination’) in ordinal models is fixed to 1 by default and not estimated,
but may be modeled as any other distributional parameter if desired (see examples). For reasons of
identification, 'disc' can only be positive, which is achieved by applying the log-link.
In categorical models, distributional parameters do not have fixed names. Instead, they are named
after the response categories (excluding the first one, which serves as the reference category), with
the prefix 'mu'. If, for instance, categories are named cat1, cat2, and cat3, the distributional
parameters will be named mucat2 and mucat3.
Some distributional parameters currently supported by brmsformula have to be positive (a negative
standard deviation or precision parameter does not make any sense) or are bounded between 0 and
1 (for zero-inflated / hurdle probabilities, quantiles, or the initial bias parameter of drift-diffusion
models). However, linear predictors can be positive or negative, and thus the log link (for positive
parameters) or logit link (for probability parameters) are used by default to ensure that distributional
parameters are within their valid intervals. This implies that, by default, effects for such distribu-
tional parameters are estimated on the log / logit scale and one has to apply the inverse link function
to get to the effects on the original scale. Alternatively, it is possible to use the identity link to
predict parameters on their original scale, directly. However, this is much more likely to lead to
problems in the model fitting, if the parameter actually has a restricted range.
See also brmsfamily for an overview of valid link functions.
Formula syntax for mixture models
The specification of mixture models closely resembles that of non-mixture models. If not specified
otherwise (see below), all mean parameters of the mixture components are predicted using the right-
hand side of formula. All types of predictor terms allowed in non-mixture models are allowed in
mixture models as well.
Distributional parameters of mixture distributions have the same name as those of the corresponding
ordinary distributions, but with a number at the end to indicate the mixture component. For instance,
if you use family mixture(gaussian,gaussian), the distributional parameters are sigma1 and
sigma2. Distributional parameters of the same class can be fixed to the same value. For the above
example, we could write sigma2 = "sigma1" to make sure that both components have the same
residual standard deviation, which is in turn estimated from the data.
In addition, there are two types of special distributional parameters. The first are named mu<ID>, that
allow for modeling different predictors for the mean parameters of different mixture components.
For instance, if you want to predict the mean of the first component using predictor x and the mean
of the second component using predictor z, you can write mu1 ~ x as well as mu2 ~ z. The second
are named theta<ID>, which constitute the mixing proportions. If the mixing proportions are fixed
to certain values, they are internally normalized to form a probability vector. If one seeks to predict
brmsformula 41
the mixing proportions, all but one of the them has to be predicted, while the remaining one is
used as the reference category to identify the model. The softmax function is applied on the linear
predictor terms to form a probability vector.
For more information on mixture models, see the documentation of mixture.
Formula syntax for multivariate models
Multivariate models may be specified using mvbind notation or with help of the mvbf function. Sup-
pose that y1 and y2 are response variables and x is a predictor. Then mvbind(y1,y2) ~ x specifies a
multivariate model. The effects of all terms specified at the RHS of the formula are assumed to vary
across response variables. For instance, two parameters will be estimated for x, one for the effect
on y1 and another for the effect on y2. This is also true for group-level effects. When writing, for
instance, mvbind(y1,y2) ~ x + (1+x|g), group-level effects will be estimated separately for each
response. To model these effects as correlated across responses, use the ID syntax (see above). For
the present example, this would look as follows: mvbind(y1,y2) ~ x + (1+x|2|g). Of course, you
could also use any value other than 2 as ID.
It is also possible to specify different formulas for different responses. If, for instance, y1 should be
predicted by x and y2 should be predicted by z, we could write mvbf(y1 ~ x,y2 ~ z). Alternatively,
multiple brmsformula objects can be added to specify a joint multivariate model (see ’Examples’).
Value
An object of class brmsformula, which is essentially a list containing all model formulas as well
as some additional information.
See Also
mvbrmsformula, brmsformula-helpers
Examples
# multilevel model with smoothing terms
brmsformula(y ~ x1*x2 + s(z) + (1+x1|1) + (1|g2))
# incorporate censoring
bf(y | cens(censor_variable) ~ predictors)
Description
Helper functions to specify linear and non-linear formulas for use with brmsformula.
Usage
nlf(formula, ..., flist = NULL, dpar = NULL, resp = NULL, loop = NULL)
lf(
...,
flist = NULL,
dpar = NULL,
resp = NULL,
center = NULL,
cmc = NULL,
sparse = NULL,
decomp = NULL
)
set_rescor(rescor = TRUE)
set_mecor(mecor = TRUE)
44 brmsformula-helpers
Arguments
formula Non-linear formula for a distributional parameter. The name of the distribu-
tional parameter can either be specified on the left-hand side of formula or via
argument dpar.
... Additional formula objects to specify predictors of non-linear and distributional
parameters. Formulas can either be named directly or contain names on their
left-hand side. Alternatively, it is possible to fix parameters to certain values by
passing numbers or character strings in which case arguments have to be named
to provide the parameter names. See ’Details’ for more information.
flist Optional list of formulas, which are treated in the same way as formulas passed
via the ... argument.
dpar Optional character string specifying the distributional parameter to which the
formulas passed via ... and flist belong.
resp Optional character string specifying the response variable to which the formulas
passed via ... and flist belong. Only relevant in multivariate models.
loop Logical; Only used in non-linear models. Indicates if the computation of the
non-linear formula should be done inside (TRUE) or outside (FALSE) a loop over
observations. Defaults to TRUE.
center Logical; Indicates if the population-level design matrix should be centered,
which usually increases sampling efficiency. See the ’Details’ section for more
information. Defaults to TRUE for distributional parameters and to FALSE for
non-linear parameters.
cmc Logical; Indicates whether automatic cell-mean coding should be enabled when
removing the intercept by adding 0 to the right-hand of model formulas. De-
faults to TRUE to mirror the behavior of standard R formula parsing.
sparse Logical; indicates whether the population-level design matrices should be treated
as sparse (defaults to FALSE). For design matrices with many zeros, this can con-
siderably reduce required memory. Sampling speed is currently not improved or
even slightly decreased.
decomp Optional name of the decomposition used for the population-level design matrix.
Defaults to NULL that is no decomposition. Other options currently available are
"QR" for the QR decomposition that helps in fitting models with highly corre-
lated predictors.
autocor A one sided formula containing autocorrelation terms. All none autocorrelation
terms in autocor will be silently ignored.
nl Logical; Indicates whether formula should be treated as specifying a non-linear
model. By default, formula is treated as an ordinary linear model formula.
rescor Logical; Indicates if residual correlation between the response variables should
be modeled. Currently this is only possible in multivariate gaussian and student
models. Only relevant in multivariate models.
mecor Logical; Indicates if correlations between latent variables defined by me terms
should be modeled. Defaults to TRUE.
brmshypothesis 45
Value
For lf and nlf a list that can be passed to brmsformula or added to an existing brmsformula
or mvbrmsformula object. For set_nl and set_rescor a logical value that can be added to an
existing brmsformula or mvbrmsformula object.
See Also
brmsformula, mvbrmsformula
Examples
# add more formulas to the model
bf(y ~ 1) +
nlf(sigma ~ a * exp(b * x)) +
lf(a ~ x, b ~ z + (1|g)) +
gaussian()
Description
A brmshypothesis object contains posterior samples as well as summary statistics of non-linear
hypotheses as returned by hypothesis.
Usage
## S3 method for class 'brmshypothesis'
print(x, digits = 2, chars = 20, ...)
chars = 40,
colors = NULL,
theme = NULL,
ask = TRUE,
plot = TRUE,
...
)
Arguments
Details
The two most important elements of a brmshypothesis object are hypothesis, which is a data.frame
containing the summary estimates of the hypotheses, and samples, which is a data.frame containing
the corresponding posterior samples.
See Also
hypothesis
brmsterms 47
Description
Parse formulas objects for use in brms.
Usage
brmsterms(formula, ...)
## Default S3 method:
brmsterms(formula, ...)
Arguments
formula An object of class formula, brmsformula, or mvbrmsformula (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.
... Further arguments passed to or from other methods.
check_response Logical; Indicates whether the left-hand side of formula (i.e. response variables
and addition arguments) should be parsed. If FALSE, formula may also be one-
sided.
resp_rhs_all Logical; Indicates whether to also include response variables on the right-hand
side of formula .$allvars, where . represents the output of brmsterms.
Details
This is the main formula parsing function of brms. It should usually not be called directly, but
is exported to allow package developers making use of the formula syntax implemented in brms.
As long as no other packages depend on this functions, it may be changed without deprecation
warnings, when new features make this necessary.
Value
An object of class brmsterms or mvbrmsterms (for multivariate models), which is a list containing
all required information initially stored in formula in an easier to use format, basically a list of
formulas (not an abstract syntax tree).
48 brm_multiple
See Also
brm, brmsformula, mvbrmsformula
Description
Run the same brms model on multiple datasets and then combine the results into one fitted model
object. This is useful in particular for multiple missing value imputation, where the same model is
fitted on multiple imputed data sets. Models can be run in parallel using the future package.
Usage
brm_multiple(
formula,
data,
family = gaussian(),
prior = NULL,
data2 = NULL,
autocor = NULL,
cov_ranef = NULL,
sample_prior = c("no", "yes", "only"),
sparse = NULL,
knots = NULL,
stanvars = NULL,
stan_funs = NULL,
recompile = FALSE,
combine = TRUE,
fit = NA,
seed = NA,
file = NULL,
...
)
Arguments
formula An object of class formula, brmsformula, or mvbrmsformula (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 A list of data.frames each of which will be used to fit a separate model. Alter-
natively, a mids object from the mice package.
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,
brm_multiple 49
default links are used. For details of supported families see brmsfamily. By
default, a linear gaussian model is applied. In multivariate models, family
might also be a list of families.
prior One or more brmsprior objects created by set_prior or related functions and
combined using the c method or the + operator. See also get_prior for more
help.
data2 A list of named lists each of which will be used to fit a separate model. Each
of the named lists contains objects representing data which cannot be passed via
argument data (see brm for examples). The length of the outer list should match
the length of the list passed to the data argument.
autocor (Deprecated) 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. In multivariate models, autocor might
also be a list of autocorrelation structures. It is now recommend to specify auto-
correlation terms directly within formula. See brmsformula for more details.
cov_ranef (Deprecated) A list of matrices that are proportional to the (within) covariance
structure of the group-level effects. The names of the matrices should corre-
spond 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 ef-
fects. It is now recommended to specify those matrices in the formula interface
using the gr and related functions. See vignette("brms_phylogenetics")
for more details.
sample_prior Indicate if samples from priors should be drawn additionally to the posterior
samples. Options are "no" (the default), "yes", and "only". Among others,
these samples can be used to calculate Bayes factors for point hypotheses via
hypothesis. Please note that improper priors are not sampled, including the
default improper priors used by brm. See set_prior on how to set (proper) pri-
ors. Please also note that prior samples for the overall intercept are not obtained
by default for technical reasons. See brmsformula how to obtain prior samples
for the intercept. If sample_prior is set to "only", samples are drawn solely
from the priors ignoring the likelihood, which allows among others to generate
samples from the prior predictive distribution. In this case, all parameters must
have proper priors.
sparse (Deprecated) Logical; indicates whether the population-level design matrices
should be treated as sparse (defaults to FALSE). For design matrices with many
zeros, this can considerably reduce required memory. Sampling speed is cur-
rently not improved or even slightly decreased. It is now recommended to use
the sparse argument of brmsformula and related functions.
knots Optional list containing user specified knot values to be used for basis construc-
tion of smoothing terms. See gamm for more details.
stanvars An optional stanvars object generated by function stanvar to define additional
variables for use in Stan’s program blocks.
stan_funs (Deprecated) An optional character string containing self-defined Stan func-
tions, which will be included in the functions block of the generated Stan code.
It is now recommended to use the stanvars argument for this purpose instead.
50 brm_multiple
recompile Logical, indicating whether the Stan model should be recompiled for every im-
puted data set. Defaults to FALSE. If NULL, brm_multiple tries to figure out
internally, if recompilation is necessary, for example because data-dependent
priors have changed. Using the default of no recompilation should be fine in
most cases.
combine Logical; Indicates if the fitted models should be combined into a single fitted
model object via combine_models. Defaults to TRUE.
fit An instance of S3 class brmsfit_multiple derived from a previous fit; defaults
to NA. If fit is of class brmsfit_multiple, 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.
seed The seed for random number generation to make results reproducible. If NA (the
default), Stan will set the seed randomly.
file Either NULL or a character string. In the latter case, the fitted model object is
saved via saveRDS in a file named after the string supplied in file. The .rds
extension is added automatically. If the file already exists, brm will load and
return the saved model object instead of refitting the model. As existing files
won’t be overwritten, you have to manually remove the file in order to refit
and save the model under an existing file name. The file name is stored in the
brmsfit object for later usage.
... Further arguments passed to brm.
Details
The combined model may issue false positive convergence warnings, as the MCMC chains corre-
sponding to different datasets may not necessarily overlap, even if each of the original models did
converge. To find out whether each of the original models converged, investigate fit$rhats, where
fit denotes the output of brm_multiple.
Value
If combine = TRUE a brmsfit_multiple object, which inherits from class brmsfit and behaves
essentially the same. If combine = FALSE a list of brmsfit objects.
Author(s)
Paul-Christian Buerkner <[email protected]>
Examples
## Not run:
library(mice)
imp <- mice(nhanes2)
## End(Not run)
Description
Set up an spatial conditional autoregressive (CAR) term in brms. The function does not evaluate
its arguments – it exists purely to help set up a model with CAR terms.
Usage
car(M, gr = NA, type = "escar")
Arguments
M Adjacency matrix of locations. All non-zero entries are treated as if the two
locations are adjacent. If gr is specified, the row names of W have to match the
levels of the grouping factor.
gr An optional grouping factor mapping observations to spatial locations. If not
specified, each observation is treated as a separate location. It is recommended
to always specify a grouping factor to allow for handling of new data in post-
processing methods.
type Type of the CAR structure. Currently implemented are "escar" (exact sparse
CAR), "esicar" (exact sparse intrinsic CAR), "icar" (intrinsic CAR), and
"bym2". More information is provided in the ’Details’ section.
Details
The escar and esicar types are implemented based on the case study of Max Joseph (https://
github.com/mbjoseph/CARstan). The icar and bym2 type is implemented based on the case study
of Mitzi Morris (http://mc-stan.org/users/documentation/case-studies/icar_stan.html).
52 coef.brmsfit
Value
An object of class 'car_term', which is a list of arguments to be interpreted by the formula parsing
functions of brms.
See Also
autocor-terms
Examples
## Not run:
# generate some spatial data
east <- north <- 1:10
Grid <- expand.grid(east, north)
K <- nrow(Grid)
## End(Not run)
Description
Extract model coefficients, which are the sum of population-level effects and corresponding group-
level effects
combine_models 53
Usage
## S3 method for class 'brmsfit'
coef(object, summary = TRUE, robust = FALSE, probs = c(0.025, 0.975), ...)
Arguments
object An object of class brmsfit.
summary Should summary statistics be returned instead of the raw values? Default is
TRUE.
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead. Only used if summary is
TRUE.
probs The percentiles to be computed by the quantile function. Only used if summary
is TRUE.
... Further arguments passed to fixef.brmsfit and ranef.brmsfit.
Value
A list of 3D arrays (one per grouping factor). If summary is TRUE, the 1st dimension contains the
factor levels, the 2nd dimension contains the summary statistics (see posterior_summary), and the
3rd dimension contains the group-level effects. If summary is FALSE, the 1st dimension contains the
posterior draws, the 2nd dimension contains the factor levels, and the 3rd dimension contains the
group-level effects.
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
data = epilepsy, family = gaussian(), chains = 2)
## extract population and group-level coefficients separately
fixef(fit)
ranef(fit)
## extract combined coefficients
coef(fit)
## End(Not run)
Description
Combine multiple brmsfit objects, which fitted the same model. This is usefuly for instance when
having manually run models in parallel.
54 compare_ic
Usage
combine_models(..., mlist = NULL, check_data = TRUE)
Arguments
... One or more brmsfit objects.
mlist Optional list of one or more brmsfit objects.
check_data Logical; indicates if the data should be checked for being the same across mod-
els (defaults to TRUE). Setting it to FALSE may be useful for instance when com-
bining models fitted on multiple imputed data sets.
Details
This function just takes the first model and replaces its stanfit object (slot fit) by the combined
stanfit objects of all models.
Value
A brmsfit object.
Description
Compare information criteria of different models fitted with waic or loo. Deprecated and will be
removed in the future. Please use loo_compare instead.
Usage
compare_ic(..., x = NULL, ic = c("loo", "waic", "kfold"))
Arguments
... At least two objects returned by waic or loo. Alternatively, brmsfit objects
with information criteria precomputed via add_ic may be passed, as well.
x A list containing the same types of objects as can be passed via ....
ic The name of the information criterion to be extracted from brmsfit objects.
Ignored if information criterion objects are only passed directly.
Details
See loo_compare for the recommended way of comparing models with the loo package.
Value
An object of class iclist.
conditional_effects.brmsfit 55
See Also
loo, loo_compare add_criterion
Examples
## Not run:
# model with population-level effects only
fit1 <- brm(rating ~ treat + period + carry,
data = inhaler)
waic1 <- waic(fit1)
## End(Not run)
conditional_effects.brmsfit
Display Conditional Effects of Predictors
Description
Display conditional effects of one or more numeric and/or categorical predictors including two-way
interaction effects.
Usage
## S3 method for class 'brmsfit'
conditional_effects(
x,
effects = NULL,
conditions = NULL,
int_conditions = NULL,
re_formula = NA,
robust = TRUE,
probs = c(0.025, 0.975),
method = "posterior_epred",
spaghetti = FALSE,
surface = FALSE,
categorical = FALSE,
ordinal = FALSE,
transform = NULL,
56 conditional_effects.brmsfit
resolution = 100,
select_points = 0,
too_far = 0,
...
)
conditional_effects(x, ...)
Arguments
x An object of class brmsfit.
effects An optional character vector naming effects (main effects or interactions) for
which to compute conditional plots. Interactions are specified by a : between
variable names. If NULL (the default), plots are generated for all main effects and
two-way interactions estimated in the model. When specifying effects manu-
ally, all two-way interactions may be plotted even if not originally modeled.
conditions An optional data.frame containing variable values to condition on. Each effect
defined in effects will be plotted separately for each row of conditions. Val-
ues in the cond__ column will be used as titles of the subplots. If cond__ is not
given, the row names will be used for this purpose instead. It is recommended
to only define a few rows in order to keep the plots clear. See make_conditions
for an easy way to define conditions. If NULL (the default), numeric variables
will be conditionalized by using their means and factors will get their reference
level assigned. NA values within factors are interpreted as if all dummy variables
of this factor are zero. This allows, for instance, to make predictions of the grand
conditional_effects.brmsfit 57
too_far Positive number. For surface plots only: Grid points that are too far away from
the actual data points can be excluded from the plot. too_far determines what
is too far. The grid is scaled into the unit square and then grid points more than
too_far from the predictor variables are excluded. By default, all grid points
are used. Ignored for non-surface plots.
... Further arguments such as subset or nsamples passed to posterior_predict
or posterior_epred.
ncol Number of plots to display per column for each effect. If NULL (default), ncol
is computed internally based on the number of rows of conditions.
points Logical; indicating whether the original data points should be added via geom_jitter.
Default is FALSE. Note that only those data points will be added that match the
specified conditions defined in conditions. For categorical predictors, the con-
ditions have to match exactly. For numeric predictors, argument select_points
is used to determine, which points do match a condition.
rug Logical; indicating whether a rug representation of predictor values should be
added via geom_rug. Default is FALSE. Depends on select_points in the same
way as points does.
mean Logical; only relevant for spaghetti plots. If TRUE (the default), display the mean
regression line on top of the regression lines for each sample.
jitter_width Only used if points = TRUE: Amount of horizontal jittering of the data points.
Mainly useful for ordinal models. Defaults to 0 that is no jittering.
stype Indicates how surface plots should be displayed. Either "contour" or "raster".
line_args Only used in plots of continuous predictors: A named list of arguments passed
to geom_smooth.
cat_args Only used in plots of categorical predictors: A named list of arguments passed
to geom_point.
errorbar_args Only used in plots of categorical predictors: A named list of arguments passed
to geom_errorbar.
surface_args Only used in surface plots: A named list of arguments passed to geom_contour
or geom_raster (depending on argument stype).
spaghetti_args Only used in spaghetti plots: A named list of arguments passed to geom_smooth.
point_args Only used if points = TRUE: A named list of arguments passed to geom_jitter.
rug_args Only used if rug = TRUE: A named list of arguments passed to geom_rug.
facet_args Only used if if multiple condtions are provided: A named list of arguments
passed to facet_wrap.
theme A theme object modifying the appearance of the plots. For some basic themes
see ggtheme and theme_default.
ask Logical; indicates if the user is prompted before a new page is plotted. Only
used if plot is TRUE.
plot Logical; indicates if plots should be plotted directly in the active graphic device.
Defaults to TRUE.
conditional_effects.brmsfit 59
Details
When creating conditional_effects for a particular predictor (or interaction of two predictors),
one has to choose the values of all other predictors to condition on. By default, the mean is used
for continuous variables and the reference category is used for factors, but you may change these
values via argument conditions. This also has an implication for the points argument: In the
created plots, only those points will be shown that correspond to the factor levels actually used in
the conditioning, in order not to create the false impression of bad model fit, where it is just due to
conditioning on certain factor levels.
To fully change colors of the created plots, one has to amend both scale_colour and scale_fill.
See scale_colour_grey or scale_colour_gradient for more details.
Value
An object of class 'brms_conditional_effects', which is a named list with one data.frame per
effect containing all information required to generate conditional effects plots. Among others, these
data.frames contain some special variables, namely estimate__ (predicted values of the response),
se__ (standard error of the predicted response), lower__ and upper__ (lower and upper bounds
of the uncertainty interval of the response), as well as cond__ (used in faceting when conditions
contains multiple rows).
The corresponding plot method returns a named list of ggplot objects, which can be further cus-
tomized using the ggplot2 package.
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt + (1 | patient),
data = epilepsy, family = poisson())
## End(Not run)
conditional_smooths.brmsfit
Display Smooth Terms
Description
Display smooth s and t2 terms of models fitted with brms.
Usage
## S3 method for class 'brmsfit'
conditional_smooths(
x,
smooths = NULL,
int_conditions = NULL,
probs = c(0.025, 0.975),
spaghetti = FALSE,
resolution = 100,
too_far = 0,
subset = NULL,
nsamples = NULL,
...
)
conditional_smooths(x, ...)
conditional_smooths.brmsfit 61
Arguments
Details
Two-dimensional smooth terms will be visualized using either contour or raster plots.
Value
Examples
## Not run:
set.seed(0)
dat <- mgcv::gamSim(1, n = 200, scale = 2)
fit <- brm(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat)
# show all smooth terms
plot(conditional_smooths(fit), rug = TRUE, ask = FALSE)
# show only the smooth term s(x2)
plot(conditional_smooths(fit, smooths = "s(x2)"), ask = FALSE)
## End(Not run)
Description
Extract control parameters of the NUTS sampler such as adapt_delta or max_treedepth.
Usage
control_params(x, ...)
Arguments
x An R object
... Currently ignored.
pars Optional names of the control parameters to be returned. If NULL (the default)
all control parameters are returned. See stan for more details.
Value
A named list with control parameter values.
cor_ar 63
Description
This function is deprecated. Please see ar for the new syntax. This function is a constructor for the
cor_arma class, allowing for autoregression terms only.
Usage
Arguments
Details
Value
See Also
cor_arma
64 cor_arma
Examples
cor_ar(~visit|patient, p = 2)
Description
This function is deprecated. Please see arma for the new syntax. This functions is a constructor for
the cor_arma class, representing an autoregression-moving average correlation structure of order
(p, q).
Usage
cor_arma(formula = ~1, p = 0, q = 0, r = 0, cov = FALSE)
Arguments
formula A one sided formula of the form ~ t, or ~ t | g, specifying a time covariate
t and, optionally, a grouping factor g. A covariate for this correlation struc-
ture must be integer valued. When a grouping factor is present in formula, the
correlation structure is assumed to apply only to observations within the same
grouping level; observations with different grouping levels are assumed to be
uncorrelated. Defaults to ~ 1, which corresponds to using the order of the obser-
vations in the data as a covariate, and no groups.
p A non-negative integer specifying the autoregressive (AR) order of the ARMA
structure. Default is 0.
q A non-negative integer specifying the moving average (MA) order of the ARMA
structure. Default is 0.
r No longer supported.
cov A flag indicating whether ARMA effects should be estimated by means of resid-
ual covariance matrices. This is currently only possible for stationary ARMA
effects of order 1. If the model family does not have natural residuals, latent
residuals are added automatically. If FALSE (the default) a regression formula-
tion is used that is considerably faster and allows for ARMA effects of order
higher than 1 but is only available for gaussian models and some of its gener-
alizations.
Value
An object of class cor_arma, representing an autoregression-moving-average correlation structure.
See Also
cor_ar, cor_ma
cor_brms 65
Examples
cor_arma(~ visit | patient, p = 2, q = 2)
Description
Classes of correlation structures available in the brms package. cor_brms is not a correlation
structure itself, but the class common to all correlation structures implemented in brms.
See Also
cor_arma,cor_ar,cor_ma,cor_car,cor_sar,cor_fixed
Description
These function are deprecated. Please see car for the new syntax. These functions are constructors
for the cor_car class implementing spatial conditional autoregressive structures.
Usage
cor_car(W, formula = ~1, type = "escar")
Arguments
W Adjacency matrix of locations. All non-zero entries are treated as if the two
locations are adjacent. If formula contains a grouping factor, the row names of
W have to match the levels of the grouping factor.
formula An optional one-sided formula of the form ~ 1 | g, where g is a grouping factor
mapping observations to spatial locations. If not specified, each observation is
treated as a separate location. It is recommended to always specify a grouping
factor to allow for handling of new data in post-processing methods.
type Type of the CAR structure. Currently implemented are "escar" (exact sparse
CAR), "esicar" (exact sparse intrinsic CAR), "icar" (intrinsic CAR), and
"bym2". More information is provided in the ’Details’ section.
Details
The escar and esicar types are implemented based on the case study of Max Joseph (https://
github.com/mbjoseph/CARstan). The icar and bym2 type is implemented based on the case study
of Mitzi Morris (http://mc-stan.org/users/documentation/case-studies/icar_stan.html).
Examples
## Not run:
# generate some spatial data
east <- north <- 1:10
Grid <- expand.grid(east, north)
K <- nrow(Grid)
## End(Not run)
cor_cosy 67
Description
This function is deprecated. Please see cosy for the new syntax. This functions is a constructor
for the cor_cosy class, representing a compound symmetry structure corresponding to uniform
correlation.
Usage
cor_cosy(formula = ~1)
Arguments
formula A one sided formula of the form ~ t, or ~ t | g, specifying a time covariate
t and, optionally, a grouping factor g. A covariate for this correlation struc-
ture must be integer valued. When a grouping factor is present in formula, the
correlation structure is assumed to apply only to observations within the same
grouping level; observations with different grouping levels are assumed to be
uncorrelated. Defaults to ~ 1, which corresponds to using the order of the obser-
vations in the data as a covariate, and no groups.
Value
An object of class cor_cosy, representing a compound symmetry correlation structure.
Examples
cor_cosy(~ visit | patient)
Description
This function is deprecated. Please see fcor for the new syntax. Define a fixed covariance matrix
of the response variable for instance to model multivariate effect sizes in meta-analysis.
Usage
cor_fixed(V)
68 cor_ma
Arguments
V Known covariance matrix of the response variable. If a vector is passed, it will
be used as diagonal entries (variances) and covariances will be set to zero.
Value
An object of class cor_fixed.
Examples
## Not run:
dat <- data.frame(y = rnorm(3))
V <- cbind(c(0.5, 0.3, 0.2), c(0.3, 1, 0.1), c(0.2, 0.1, 0.2))
fit <- brm(y~1, data = dat, autocor = cor_fixed(V))
## End(Not run)
Description
This function is deprecated. Please see ma for the new syntax. This function is a constructor for the
cor_arma class, allowing for moving average terms only.
Usage
cor_ma(formula = ~1, q = 1, cov = FALSE)
Arguments
formula A one sided formula of the form ~ t, or ~ t | g, specifying a time covariate
t and, optionally, a grouping factor g. A covariate for this correlation struc-
ture must be integer valued. When a grouping factor is present in formula, the
correlation structure is assumed to apply only to observations within the same
grouping level; observations with different grouping levels are assumed to be
uncorrelated. Defaults to ~ 1, which corresponds to using the order of the obser-
vations in the data as a covariate, and no groups.
q A non-negative integer specifying the moving average (MA) order of the ARMA
structure. Default is 1.
cov A flag indicating whether ARMA effects should be estimated by means of resid-
ual covariance matrices. This is currently only possible for stationary ARMA
effects of order 1. If the model family does not have natural residuals, latent
residuals are added automatically. If FALSE (the default) a regression formula-
tion is used that is considerably faster and allows for ARMA effects of order
higher than 1 but is only available for gaussian models and some of its gener-
alizations.
cor_sar 69
Value
An object of class cor_arma containing solely moving average terms.
See Also
cor_arma
Examples
cor_ma(~visit|patient, q = 2)
Description
Thse functions are deprecated. Please see sar for the new syntax. These functions are constructors
for the cor_sar class implementing spatial simultaneous autoregressive structures. The lagsar
structure implements SAR of the response values:
y = ρW y + η + e
y = η + u, u = ρW u + e
In the above equations, η is the predictor term and e are independent normally or t-distributed
residuals.
Usage
cor_sar(W, type = c("lag", "error"))
cor_lagsar(W)
cor_errorsar(W)
Arguments
W An object specifying the spatial weighting matrix. Can be either the spatial
weight matrix itself or an object of class listw or nb, from which the spatial
weighting matrix can be computed.
type Type of the SAR structure. Either "lag" (for SAR of the response values) or
"error" (for SAR of the residuals).
Details
Currently, only families gaussian and student support SAR structures.
70 cosy
Value
An object of class cor_sar to be used in calls to brm.
Examples
## Not run:
data(oldcol, package = "spdep")
fit1 <- brm(CRIME ~ INC + HOVAL, data = COL.OLD,
autocor = cor_lagsar(COL.nb),
chains = 2, cores = 2)
summary(fit1)
plot(fit1)
## End(Not run)
Description
Set up a compounds symmetry (COSY) term in brms. The function does not evaluate its arguments
– it exists purely to help set up a model with COSY terms.
Usage
cosy(time = NA, gr = NA)
Arguments
time An optional time variable specifying the time ordering of the observations. By
default, the existing order of the observations in the data is used.
gr An optional grouping variable. If specified, the correlation structure is assumed
to apply only to observations within the same grouping level.
Value
An object of class 'cosy_term', which is a list of arguments to be interpreted by the formula
parsing functions of brms.
See Also
autocor-terms
cs 71
Examples
## Not run:
data("lh")
lh <- as.data.frame(lh)
fit <- brm(x ~ cosy(), data = lh)
summary(fit)
## End(Not run)
Description
Category Specific Predictors in brms Models
Usage
cs(expr)
Arguments
expr Expression containing predictors, for which category specific effects should be
estimated. For evaluation, R formula syntax is applied.
Details
For detailed documentation see help(brmsformula) as well as vignette("brms_overview").
This function is almost solely useful when called in formulas passed to the brms package.
See Also
brmsformula
Examples
## Not run:
fit <- brm(rating ~ period + carry + cs(treat),
data = inhaler, family = sratio("cloglog"),
prior = set_prior("normal(0,5)"), chains = 2)
summary(fit)
plot(fit, ask = FALSE)
## End(Not run)
72 custom_family
Description
Define custom families (i.e. response distribution) for use in brms models. It allows users to benefit
from the modeling flexibility of brms, while applying their self-defined likelihood functions. All
of the post-processing methods for brmsfit objects can be made compatible with custom fami-
lies. See vignette("brms_customfamilies") for more details. For a list of built-in families see
brmsfamily.
Usage
custom_family(
name,
dpars = "mu",
links = "identity",
type = c("real", "int"),
lb = NA,
ub = NA,
vars = NULL,
specials = NULL,
threshold = "flexible",
log_lik = NULL,
posterior_predict = NULL,
posterior_epred = NULL,
predict = NULL,
fitted = NULL,
env = parent.frame()
)
Arguments
vars Names of variables, which are part of the likelihood function without being dis-
tributional parameters. That is, vars can be used to pass data to the likelihood.
See stanvar for details about adding self-defined data to the generated Stan
model.
specials A character vector of special options to enable for this custom family. Currently
for internal use only.
threshold Optional threshold type for custom ordinal families. Ignored for non-ordinal
families.
log_lik Optional function to compute log-likelihood values of the model in R. This is
only relevant if one wants to ensure compatibility with method log_lik.
posterior_predict
Optional function to compute posterior prediction of the model in R. This is only
relevant if one wants to ensure compatibility with method posterior_predict.
posterior_epred
Optional function to compute expected values of the posterior predictive distri-
bution of the model in R. This is only relevant if one wants to ensure compati-
bility with method posterior_epred.
predict Deprecated alias of ‘posterior_predict‘.
fitted Deprecated alias of ‘posterior_epred‘.
env An environment in which certain post-processing functions related to the cus-
tom family can be found, if there were not directly passed to custom_family.
This is only relevant if one wants to ensure compatibility with the methods
log_lik, posterior_predict, or posterior_epred. By default, env is the
enviroment from which custom_family is called.
Details
The corresponding probability density or mass Stan functions need to have the same name as the
custom family. That is if a family is called myfamily, then the Stan functions should be called
myfamily_lpdf or myfamily_lpmf depending on whether it defines a continuous or discrete distri-
bution.
Value
An object of class customfamily inheriting from class brmsfamily.
See Also
brmsfamily, stanvar
Examples
## Not run:
## demonstrate how to fit a beta-binomial model
## generate some fake data
phi <- 0.7
n <- 300
z <- rnorm(n, sd = 0.2)
74 density_ratio
## End(Not run)
Description
Compute the ratio of two densities at given points based on samples of the corresponding distribu-
tions.
Usage
density_ratio(x, y = NULL, point = 0, n = 4096, ...)
Arguments
x Vector of samples from the first distribution, usually the posterior distribution of
the quantity of interest.
y Optional vector of samples from the second distribution, usually the prior distri-
bution of the quantity of interest. If NULL (the default), only the density of x will
be evaluated.
diagnostic-quantities 75
point Numeric values at which to evaluate and compare the densities. Defaults to 0.
n Single numeric value. Influences the accuracy of the density estimation. See
density for details.
... Further arguments passed to density.
Details
In order to achieve sufficient accuracy in the density estimation, more samples than usual are re-
quired. That is you may need an effective sample size of 10,000 or more to reliably estimate the
densities.
Value
Examples
x <- rnorm(10000)
y <- rnorm(10000, mean = 1)
density_ratio(x, y, point = c(0, 1))
Description
Extract quantities that can be used to diagnose sampling behavior of the algorithms applied by Stan
at the back-end of brms.
Usage
Arguments
object A brmsfit object.
... Arguments passed to individual methods.
pars An optional character vector of parameter names. For nuts_params these will
be NUTS sampler parameter names rather than model parameters. If pars is
omitted all parameters are included.
Details
For more details see bayesplot-extractors.
Value
The exact form of the output depends on the method.
Examples
## Not run:
fit <- brm(time ~ age * sex, data = kidney)
lp <- log_posterior(fit)
head(lp)
np <- nuts_params(fit)
str(np)
# extract the number of divergence transitions
sum(subset(np, Parameter == "divergent__")$Value)
head(rhat(fit))
head(neff_ratio(fit))
## End(Not run)
Description
Density function and random number generation for the dirichlet distribution with shape parameter
vector alpha.
Usage
ddirichlet(x, alpha, log = FALSE)
rdirichlet(n, alpha)
emmeans-brms-helpers 77
Arguments
x Matrix of quantiles. Each row corresponds to one probability vector.
alpha Matrix of positive shape parameters. Each row corresponds to one probability
vector.
log Logical; If TRUE, values are returned on the log scale.
n Number of samples to draw from the distribution.
Details
See vignette("brms_families") for details on the parameterization.
Description
Functions required for compatibility of brms with emmeans. Users are not required to call these
functions themselves. Instead, they will be called automatically by the emmeans function of the
emmeans package.
Usage
recover_data.brmsfit(object, data, resp = NULL, dpar = NULL, nlpar = NULL, ...)
emm_basis.brmsfit(
object,
trms,
xlev,
grid,
vcov.,
resp = NULL,
dpar = NULL,
nlpar = NULL,
...
)
Arguments
object An object of class brmsfit.
data, trms, xlev, grid, vcov.
Arguments required by emmeans.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
dpar Optional name of a predicted distributional parameter. If specified, expected
predictions of this parameters are returned.
78 epilepsy
Examples
## Not run:
fit <- brm(time | cens(censored) ~ age * sex + disease + (1|patient),
data = kidney, family = lognormal())
summary(fit)
## End(Not run)
Description
Breslow and Clayton (1993) analyze data initially provided by Thall and Vail (1990) concerning
seizure counts in a randomized trial of anti-convulsant therapy in epilepsy. Covariates are treatment,
8-week baseline seizure counts, and age of the patients in years.
Usage
epilepsy
Format
A data frame of 236 observations containing information on the following 9 variables.
Source
Thall, P. F., & Vail, S. C. (1990). Some covariance models for longitudinal count data with overdis-
persion. Biometrics, 46(2), 657-671.
Breslow, N. E., & Clayton, D. G. (1993). Approximate inference in generalized linear mixed mod-
els. Journal of the American Statistical Association, 88(421), 9-25.
Examples
## Not run:
## poisson regression without random effects.
fit1 <- brm(count ~ zAge + zBase * Trt,
data = epilepsy, family = poisson())
summary(fit1)
plot(fit1)
## End(Not run)
Description
Density, distribution function, and random generation for the exponentially modified Gaussian dis-
tribution with mean mu and standard deviation sigma of the gaussian component, as well as scale
beta of the exponential component.
Usage
Arguments
x, q Vector of quantiles.
mu Vector of means of the combined distribution.
sigma Vector of standard deviations of the gaussian component.
beta Vector of scales of the exponential component.
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
n Number of samples to draw from the distribution.
Details
expose_functions.brmsfit
Expose user-defined Stan functions
Description
Export user-defined Stan function and optionally vectorize them. For more details see expose_stan_functions.
Usage
expose_functions(x, ...)
Arguments
Description
Computes exp(x) + 1.
Usage
expp1(x)
Arguments
Description
Usage
Arguments
Value
Description
Set up a fixed residual correlation (FCOR) term in brms. The function does not evaluate its argu-
ments – it exists purely to help set up a model with FCOR terms.
Usage
fcor(M)
Arguments
Value
See Also
autocor-terms
Examples
## Not run:
dat <- data.frame(y = rnorm(3))
V <- cbind(c(0.5, 0.3, 0.2), c(0.3, 1, 0.1), c(0.2, 0.1, 0.2))
fit <- brm(y ~ 1 + fcor(V), data = dat, data2 = list(V = V))
## End(Not run)
fitted.brmsfit 83
Description
This method is an alias of posterior_epred.brmsfit with additional arguments for obtaining
summaries of the computed samples.
Usage
## S3 method for class 'brmsfit'
fitted(
object,
newdata = NULL,
re_formula = NULL,
scale = c("response", "linear"),
resp = NULL,
dpar = NULL,
nlpar = NULL,
nsamples = NULL,
subset = NULL,
sort = FALSE,
summary = TRUE,
robust = FALSE,
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
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
scale Either "response" or "linear". If "response", results are returned on the
scale of the response variable. If "linear", results are returned on the scale of
the linear predictor term, that is without applying the inverse link function or
other transformations.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
dpar Optional name of a predicted distributional parameter. If specified, expected
predictions of this parameters are returned.
84 fitted.brmsfit
Value
An array of predicted mean response values. If summary = FALSE the output resembles those of
posterior_epred.brmsfit.
If summary = TRUE the output depends on the family: For categorical and ordinal families, the output
is an N x E x C array, where N is the number of observations, E is the number of summary statistics,
and C is the number of categories. For all other families, the output is an N x E matrix. The num-
ber of summary statistics E is equal to 2 + length(probs): The Estimate column contains point
estimates (either mean or median depending on argument robust), while the Est.Error column
contains uncertainty estimates (either standard deviation or median absolute deviation depending on
argument robust). The remaining columns starting with Q contain quantile estimates as specifed
via argument probs.
In multivariate models, an additional dimension is added to the output which indexes along the
different response variables.
See Also
posterior_epred.brmsfit
Examples
## Not run:
## fit a model
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler)
## End(Not run)
Description
Usage
Arguments
Value
If summary is TRUE, a matrix returned by posterior_summary for the population-level effects. If
summary is FALSE, a matrix with one row per posterior draw and one column per population-level
effect.
Examples
## Not run:
fit <- brm(time | cens(censored) ~ age + sex + disease,
data = kidney, family = "exponential")
fixef(fit)
# extract only some coefficients
fixef(fit, pars = c("age", "sex"))
## End(Not run)
Description
Density, distribution function, quantile function and random generation for the Frechet distribution
with location loc, scale scale, and shape shape.
Usage
dfrechet(x, loc = 0, scale = 1, shape = 1, log = FALSE)
Arguments
x, q Vector of quantiles.
loc Vector of locations.
scale Vector of scales.
shape Vector of shapes.
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
p Vector of probabilities.
n Number of samples to draw from the distribution.
GenExtremeValue 87
Details
See vignette("brms_families") for details on the parameterization.
Description
Density, distribution function, and random generation for the generalized extreme value distribution
with location mu, scale sigma and shape xi.
Usage
dgen_extreme_value(x, mu = 0, sigma = 1, xi = 0, log = FALSE)
pgen_extreme_value(
q,
mu = 0,
sigma = 1,
xi = 0,
lower.tail = TRUE,
log.p = FALSE
)
rgen_extreme_value(n, mu = 0, sigma = 1, xi = 0)
Arguments
x, q Vector of quantiles.
mu Vector of locations.
sigma Vector of scales.
xi Vector of shapes.
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
n Number of samples to draw from the distribution.
Details
See vignette("brms_families") for details on the parameterization.
88 get_prior
Description
Get information on all parameters (and parameter classes) for which priors may be specified includ-
ing default priors.
Usage
get_prior(
formula,
data,
family = gaussian(),
autocor = NULL,
knots = NULL,
sparse = NULL,
...
)
Arguments
formula An object of class formula, brmsformula, or mvbrmsformula (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) con-
taining 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. In multivariate models, family
might also be a list of families.
autocor (Deprecated) 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. In multivariate models, autocor might
also be a list of autocorrelation structures. It is now recommend to specify auto-
correlation terms directly within formula. See brmsformula for more details.
knots Optional list containing user specified knot values to be used for basis construc-
tion of smoothing terms. See gamm for more details.
sparse (Deprecated) Logical; indicates whether the population-level design matrices
should be treated as sparse (defaults to FALSE). For design matrices with many
gp 89
zeros, this can considerably reduce required memory. Sampling speed is cur-
rently not improved or even slightly decreased. It is now recommended to use
the sparse argument of brmsformula and related functions.
... Other arguments for internal usage only.
Value
A data.frame with columns prior, class, coef, and group and several rows, each providing infor-
mation on a parameter (or parameter class) on which priors can be specified. The prior column is
empty except for internal default priors.
See Also
set_prior
Examples
## get all parameters and parameters classes to define priors on
(prior <- get_prior(count ~ zAge + zBase * Trt + (1|patient) + (1|obs),
data = epilepsy, family = poisson()))
## verify that the priors indeed found their way into Stan's model code
make_stancode(count ~ zAge + zBase * Trt + (1|patient) + (1|obs),
data = epilepsy, family = poisson(),
prior = prior)
Description
Set up a Gaussian process (GP) term in brms. The function does not evaluate its arguments – it
exists purely to help set up a model with GP terms.
Usage
gp(
...,
by = NA,
k = NA,
cov = "exp_quad",
90 gp
iso = TRUE,
gr = TRUE,
cmc = TRUE,
scale = TRUE,
c = NULL
)
Arguments
... One or more predictors for the GP.
by A numeric or factor variable of the same length as each predictor. In the numeric
vector case, the elements multiply the values returned by the GP. In the factor
variable case, a separate GP is fitted for each factor level.
k Optional number of basis functions for computing approximate GPs. If NA (the
default), exact GPs are computed.
cov Name of the covariance kernel. By default, the exponentiated-quadratic kernel
"exp_quad" is used.
iso A flag to indicate whether an isotropic (TRUE; the default) of a non-isotropic GP
should be used. In the former case, the same amount of smoothing is applied
to all predictors. In the latter case, predictors may have different smoothing.
Ignored if only a single predictors is supplied.
gr Logical; Indicates if auto-grouping should be used (defaults to TRUE). If enabled,
observations sharing the same predictor values will be represented by the same
latent variable in the GP. This will improve sampling efficiency drastically if
the number of unique predictor combinations is small relative to the number of
observations.
cmc Logical; Only relevant if by is a factor. If TRUE (the default), cell-mean coding
is used for the by-factor, that is one GP per level is estimated. If FALSE, contrast
GPs are estimated according to the contrasts set for the by-factor.
scale Logical; If TRUE (the default), predictors are scaled so that the maximum Eu-
clidean distance between two points is 1. This often improves sampling speed
and convergence.
c Numeric value only used in approximate GPs. Defines the multiplicative con-
stant of the predictors’ range over which predictions should be computed. A
good default could be c = 5/4 but we are still working on providing better rec-
ommendations.
Details
A GP is a stochastic process, which describes the relation between one or more predictors x =
(x1 , ..., xd ) and a response f (x), where d is the number of predictors. A GP is the generalization
of the multivariate normal distribution to an infinite number of dimensions. Thus, it can be inter-
preted as a prior over functions. Any finite sample realized from this stochastic process is jointly
multivariate normal, with a covariance matrix defined by the covariance kernel kp (x), where p is
the vector of parameters of the GP:
The smoothness and general behavior of the function f depends only on the choice of covariance
kernel. For a more detailed introduction to Gaussian processes, see https://en.wikipedia.org/
wiki/Gaussian_process.
Below, we describe the currently supported covariance kernels:
In the current implementation, "exp_quad" is the only supported covariance kernel. More options
will follow in the future.
Value
An object of class 'gp_term', which is a list of arguments to be interpreted by the formula parsing
functions of brms.
See Also
brmsformula
Examples
## Not run:
# simulate data using the mgcv package
dat <- mgcv::gamSim(1, n = 30, scale = 2)
## End(Not run)
Description
Function used to set up a basic grouping term in brms. The function does not evaluate its arguments
– it exists purely to help set up a model with grouping terms. gr is called implicitly inside the
package and there is usually no need to call it directly.
Usage
Arguments
See Also
brmsformula
horseshoe 93
Examples
## Not run:
# model using basic lme4-style formula
fit1 <- brm(count ~ Trt + (1|patient), data = epilepsy)
summary(fit1)
## End(Not run)
Description
Function used to set up regularized horseshoe priors and related hierarchical shrinkage priors for
population-level effects in brms. The function does not evaluate its arguments – it exists purely to
help set up the model.
Usage
horseshoe(
df = 1,
scale_global = 1,
df_global = 1,
scale_slab = 2,
df_slab = 4,
par_ratio = NULL,
autoscale = TRUE
)
Arguments
df_global Degrees of freedom of student-t prior of the global shrinkage parameter. De-
faults to 1. If df_global is greater 1, the shape of the prior will no longer
resemble a horseshoe and it may be more appropriately called an hierarchical
shrinkage prior in this case.
scale_slab Scale of the student-t prior of the regularization parameter. Defaults to 2. The
original unregularized horseshoe prior is obtained by setting scale_slab to in-
finite, which we can approximate in practice by setting it to a very large real
value.
df_slab Degrees of freedom of the student-t prior of the regularization parameter. De-
faults to 4.
par_ratio Ratio of the expected number of non-zero coefficients to the expected number of
zero coefficients. If specified, scale_global is ignored and internally computed
as par_ratio / sqrt(N), where N is the total number of observations in the
data.
autoscale Logical; indicating whether the horseshoe prior should be scaled using the resid-
ual standard deviation sigma if possible and sensible (defaults to TRUE). Au-
toscaling is not applied for distributional parameters or when the model does
not contain the parameter sigma.
Details
The horseshoe prior is a special shrinkage prior initially proposed by Carvalho et al. (2009). It is
symmetric around zero with fat tails and an infinitely large spike at zero. This makes it ideal for
sparse models that have many regression coefficients, although only a minority of them is non-zero.
The horseshoe prior can be applied on all population-level effects at once (excluding the inter-
cept) by using set_prior("horseshoe(1)"). The 1 implies that the student-t prior of the local
shrinkage parameters has 1 degrees of freedom. This may, however, lead to an increased number
of divergent transition in Stan. Accordingly, increasing the degrees of freedom to slightly higher
values (e.g., 3) may often be a better option, although the prior no longer resembles a horseshoe in
this case. Further, the scale of the global shrinkage parameter plays an important role in amount
of shrinkage applied. It defaults to 1, but this may result in too few shrinkage (Piironen & Vehtari,
2016). It is thus possible to change the scale using argument scale_global of the horseshoe prior,
for instance horseshoe(1,scale_global = 0.5). In linear models, scale_global will internally
be multiplied by the residual standard deviation parameter sigma. See Piironen and Vehtari (2016)
for recommendations how to properly set the global scale. The degrees of freedom of the global
shrinkage prior may also be adjusted via argument df_global. Piironen and Vehtari (2017) recom-
mend to specifying the ratio of the expected number of non-zero coefficients to the expected number
of zero coefficients par_ratio rather than scale_global directly. As proposed by Piironen and
Vehtari (2017), an additional regularization is applied that only affects non-zero coefficients. The
amount of regularization can be controlled via scale_slab and df_slab. To make sure that shrink-
age can equally affect all coefficients, predictors should be one the same scale. Generally, models
with horseshoe priors a more likely than other models to have divergent transitions so that increas-
ing adapt_delta from 0.8 to values closer to 1 will often be necessary. See the documentation of
brm for instructions on how to increase adapt_delta.
Value
A character string obtained by match.call() with additional arguments.
Hurdle 95
References
Carvalho, C. M., Polson, N. G., & Scott, J. G. (2009). Handling sparsity via the horseshoe. In
International Conference on Artificial Intelligence and Statistics (pp. 73-80).
Piironen J. & Vehtari A. (2016). On the Hyperprior Choice for the Global Shrinkage Parameter in
the Horseshoe Prior. https://arxiv.org/pdf/1610.05559v1.pdf
Piironen, J., and Vehtari, A. (2017). Sparsity information and regularization in the horseshoe and
other shrinkage priors. https://arxiv.org/abs/1707.01694
See Also
set_prior
Examples
Description
Usage
Arguments
x Vector of quantiles.
hu hurdle propability
log Logical; If TRUE, values are returned on the log scale.
q Vector of quantiles.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
mu, lambda location parameter
shape shape parameter
sigma, scale scale parameter
Details
The density of a hurdle distribution can be specified as follows. If x = 0 set f (x) = θ. Else set
f (x) = (1 − θ) ∗ g(x)/(1 − G(0)) where g(x) and G(x) are the density and distribution function
of the non-hurdle part, respectively.
Description
Perform non-linear hypothesis testing for all model parameters.
Usage
## S3 method for class 'brmsfit'
hypothesis(
x,
hypothesis,
class = "b",
group = "",
scope = c("standard", "ranef", "coef"),
alpha = 0.05,
seed = NULL,
...
)
hypothesis(x, ...)
## Default S3 method:
hypothesis(x, hypothesis, alpha = 0.05, ...)
hypothesis.brmsfit 97
Arguments
x An R object. If it is no brmsfit object, it must be coercible to a data.frame.
hypothesis A character vector specifying one or more non-linear hypothesis concerning
parameters of the model.
class A string specifying the class of parameters being tested. Default is "b" for
population-level effects. Other typical options are "sd" or "cor". If class =
NULL, all parameters can be tested against each other, but have to be specified
with their full name (see also parnames)
group Name of a grouping factor to evaluate only group-level effects parameters re-
lated to this grouping factor.
scope Indicates where to look for the variables specified in hypothesis. If "standard",
use the full parameter names (subject to the restriction given by class and
group). If "coef" or "ranef", compute the hypothesis for all levels of the
grouping factor given in "group", based on the output of coef.brmsfit and
ranef.brmsfit, respectively.
alpha The alpha-level of the tests (default is 0.05; see ’Details’ for more information).
seed A single numeric value passed to set.seed to make results reproducible.
... Currently ignored.
Details
Among others, hypothesis computes an evidence ratio (Evid.Ratio) for each hypothesis. For a
one-sided hypothesis, this is just the posterior probability (Post.Prob) under the hypothesis against
its alternative. That is, when the hypothesis is of the form a > b, the evidence ratio is the ratio of the
posterior probability of a > b and the posterior probability of a < b. In this example, values greater
than one indicate that the evidence in favor of a > b is larger than evidence in favor of a < b. For
an two-sided (point) hypothesis, the evidence ratio is a Bayes factor between the hypothesis and its
alternative computed via the Savage-Dickey density ratio method. That is the posterior density at
the point of interest divided by the prior density at that point. Values greater than one indicate that
evidence in favor of the point hypothesis has increased after seeing the data. In order to calculate
this Bayes factor, all parameters related to the hypothesis must have proper priors and argument
sample_prior of function brm must be set to "yes". Otherwise Evid.Ratio (and Post.Prob) will
be NA. Please note that, for technical reasons, we cannot sample from priors of certain parameters
classes. Most notably, these include overall intercept parameters (prior class "Intercept") as
well as group-level coefficients. When interpreting Bayes factors, make sure that your priors are
reasonable and carefully chosen, as the result will depend heavily on the priors. In particular, avoid
using default priors.
The Evid.Ratio may sometimes be 0 or Inf implying very small or large evidence, respectively, in
favor of the tested hypothesis. For one-sided hypotheses pairs, this basically means that all posterior
samples are on the same side of the value dividing the two hypotheses. In that sense, instead of 0 or
Inf, you may rather read it as Evid.Ratio smaller 1 / S or greater S, respectively, where S denotes
the number of posterior samples used in the computations.
The argument alpha specifies the size of the credible interval (i.e., Bayesian confidence interval).
For instance, if we tested a two-sided hypothesis and set alpha = 0.05 (5%) an, the credible interval
will contain 1 -alpha = 0.95 (95%) of the posterior values. Hence, alpha * 100% of the posterior
98 hypothesis.brmsfit
values will lie outside of the credible interval. Although this allows testing of hypotheses in a
similar manner as in the frequentist null-hypothesis testing framework, we strongly argue against
using arbitrary cutoffs (e.g., p < .05) to determine the ’existence’ of an effect.
Value
A brmshypothesis object.
Author(s)
Paul-Christian Buerkner <[email protected]>
See Also
brmshypothesis
Examples
## Not run:
## define priors
prior <- c(set_prior("normal(0,2)", class = "b"),
set_prior("student_t(10,0,1)", class = "sigma"),
set_prior("student_t(10,0,1)", class = "sd"))
## End(Not run)
Description
Ezzet and Whitehead (1991) analyze data from a two-treatment, two-period crossover trial to com-
pare 2 inhalation devices for delivering the drug salbutamol in 286 asthma patients. Patients were
asked to rate the clarity of leaflet instructions accompanying each device, using a 4-point ordinal
scale.
Usage
inhaler
Format
A data frame of 572 observations containing information on the following 5 variables.
subject The subject number
rating The rating of the inhaler instructions on a scale ranging from 1 to 4
treat A contrast to indicate which of the two inhaler devices was used
period A contrast to indicate the time of administration
carry A contrast to indicate possible carry over effects
Source
Ezzet, F., & Whitehead, J. (1991). A random effects model for ordinal responses from a crossover
trial. Statistics in Medicine, 10(6), 901-907.
Examples
## Not run:
## ordinal regression with family "sratio"
fit1 <- brm(rating ~ treat + period + carry,
data = inhaler, family = sratio(),
prior = set_prior("normal(0,5)"))
summary(fit1)
plot(fit1)
100 InvGaussian
## End(Not run)
Description
Density, distribution function, and random generation for the inverse Gaussian distribution with
location mu, and shape shape.
Usage
rinv_gaussian(n, mu = 1, shape = 1)
Arguments
x, q Vector of quantiles.
mu Vector of locations.
shape Vector of shapes.
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
n Number of samples to draw from the distribution.
Details
Description
Usage
inv_logit_scaled(x, lb = 0, ub = 1)
Arguments
Value
Description
Usage
is.brmsfit(x)
Arguments
x An R object
102 is.brmsprior
Description
Checks if argument is a brmsfit_multiple object
Usage
is.brmsfit_multiple(x)
Arguments
x An R object
Description
Checks if argument is a brmsformula object
Usage
is.brmsformula(x)
Arguments
x An R object
Description
Checks if argument is a brmsprior object
Usage
is.brmsprior(x)
Arguments
x An R object
is.brmsterms 103
Description
Checks if argument is a brmsterms object
Usage
is.brmsterms(x)
Arguments
x An R object
See Also
brmsterms
Description
Check if argument is one of the correlation structures used in brms.
Usage
is.cor_brms(x)
is.cor_arma(x)
is.cor_cosy(x)
is.cor_sar(x)
is.cor_car(x)
is.cor_fixed(x)
Arguments
x An R object.
104 is.mvbrmsterms
Description
Usage
is.mvbrmsformula(x)
Arguments
x An R object
Description
Usage
is.mvbrmsterms(x)
Arguments
x An R object
See Also
brmsterms
kfold.brmsfit 105
Description
Perform exact K-fold cross-validation by refitting the model K times each leaving out one-Kth of
the original data. Folds can be run in parallel using the future package.
Usage
## S3 method for class 'brmsfit'
kfold(
x,
...,
K = 10,
Ksub = NULL,
folds = NULL,
group = NULL,
exact_loo = NULL,
compare = TRUE,
resp = NULL,
model_names = NULL,
save_fits = FALSE
)
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
K The number of subsets of equal (if possible) size into which the data will be
partitioned for performing K-fold cross-validation. The model is refit K times,
each time leaving out one of the K subsets. If K is equal to the total number
of observations in the data then K-fold cross-validation is equivalent to exact
leave-one-out cross-validation.
Ksub Optional number of subsets (of those subsets defined by K) to be evaluated. If
NULL (the default), K-fold cross-validation will be performed on all subsets.
If Ksub is a single integer, Ksub subsets (out of all K) subsets will be randomly
chosen. If Ksub consists of multiple integers or a one-dimensional array (created
via as.array) potentially of length one, the corresponding subsets will be used.
This argument is primarily useful, if evaluation of all subsets is infeasible for
some reason.
folds Determines how the subsets are being constructed. Possible values are NULL
(the default), "stratified", "grouped", or "loo". May also be a vector of
length equal to the number of observations in the data. Alters the way group is
handled. More information is provided in the ’Details’ section.
106 kfold.brmsfit
group Optional name of a grouping variable or factor in the model. What exactly
is done with this variable depends on argument folds. More information is
provided in the ’Details’ section.
exact_loo Deprecated! Please use folds = "loo" instead.
compare A flag indicating if the information criteria of the models should be compared
to each other via loo_compare.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
save_fits If TRUE, a component fits is added to the returned object to store the cross-
validated brmsfit objects and the indices of the omitted observations for each
fold. Defaults to FALSE.
Details
The kfold function performs exact K-fold cross-validation. First the data are partitioned into K
folds (i.e. subsets) of equal (or as close to equal as possible) size by default. Then the model is refit
K times, each time leaving out one of the K subsets. If K is equal to the total number of observations
in the data then K-fold cross-validation is equivalent to exact leave-one-out cross-validation (to
which loo is an efficient approximation). The compare_ic function is also compatible with the
objects returned by kfold.
The subsets can be constructed in multiple different ways:
• If both folds and group are NULL, the subsets are randomly chosen so that they have equal
(or as close to equal as possible) size.
• If folds is NULL but group is specified, the data is split up into subsets, each time omitting all
observations of one of the factor levels, while ignoring argument K.
• If folds = "stratified" the subsets are stratified after group using loo::kfold_split_stratified.
• If folds = "grouped" the subsets are split by group using loo::kfold_split_grouped.
• If folds = "loo" exact leave-one-out cross-validation will be performed and K will be ignored.
Further, if group is specified, all observations corresponding to the factor level of the currently
predicted single value are omitted. Thus, in this case, the predicted values are only a subset of
the omitted ones.
• If folds is a numeric vector, it must contain one element per observation in the data. Each
element of the vector is an integer in 1:K indicating to which of the K folds the corresponding
observation belongs. There are some convenience functions available in the loo package that
create integer vectors to use for this purpose (see the Examples section below and also the
kfold-helpers page).
Value
kfold returns an object that has a similar structure as the objects returned by the loo and waic
methods and can be used with the same post-processing functions.
kfold_predict 107
See Also
loo, reloo
Examples
## Not run:
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient) + (1|obs),
data = epilepsy, family = poisson())
# throws warning about some pareto k estimates being too high
(loo1 <- loo(fit1))
# perform 10-fold cross validation
(kfold1 <- kfold(fit1, chains = 1))
## End(Not run)
Description
Compute and evaluate predictions after performing K-fold cross-validation via kfold.
Usage
kfold_predict(x, method = c("predict", "fitted"), resp = NULL, ...)
Arguments
x Object of class 'kfold' computed by kfold. For kfold_predict to work, the
fitted model objects need to have been stored via argument save_fits of kfold.
method The method used to make predictions. Either "predict" or "fitted". See
predict.brmsfit for details.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
Value
A list with two slots named 'y' and 'yrep'. Slot y contains the vector of observed responses.
Slot yrep contains the matrix of predicted responses, with rows being posterior draws and columns
being observations.
108 kidney
See Also
kfold
Examples
## Not run:
fit <- brm(count ~ zBase * Trt + (1|patient),
data = epilepsy, family = poisson())
## End(Not run)
Description
This dataset, originally discussed in McGilchrist and Aisbett (1991), describes the first and second
(possibly right censored) recurrence time of infection in kidney patients using portable dialysis
equipment. In addition, information on the risk variables age, sex and disease type is provided.
Usage
kidney
Format
A data frame of 76 observations containing information on the following 7 variables.
time The time to first or second recurrence of the infection, or the time of censoring
recur A factor of levels 1 or 2 indicating if the infection recurred for the first or second time for
this patient
censored Either 0 or 1, where 0 indicates no censoring of recurrence time and 1 indicates right
censoring
patient The patient number
lasso 109
Source
McGilchrist, C. A., & Aisbett, C. W. (1991). Regression with frailty in survival analysis. Biomet-
rics, 47(2), 461-466.
Examples
## Not run:
## performing surivival analysis using the "weibull" family
fit1 <- brm(time | cens(censored) ~ age + sex + disease,
data = kidney, family = weibull, inits = "0")
summary(fit1)
plot(fit1)
## End(Not run)
Description
Function used to set up a lasso prior for population-level effects in brms. The function does not
evaluate its arguments – it exists purely to help set up the model.
Usage
lasso(df = 1, scale = 1)
Arguments
df Degrees of freedom of the chi-square prior of the inverse tuning parameter. De-
faults to 1.
scale Scale of the lasso prior. Defaults to 1.
110 launch_shinystan.brmsfit
Details
The lasso prior is the Bayesian equivalent to the LASSO method for performing variable selection
(Park & Casella, 2008). With this prior, independent Laplace (i.e. double exponential) priors are
placed on the population-level effects. The scale of the Laplace priors depends on a tuning param-
eter that controls the amount of shrinkage. In brms, the inverse of the tuning parameter is used
so that smaller values imply more shrinkage. The inverse tuning parameter has a chi-square dis-
tribution and with degrees of freedom controlled via argument df of function lasso (defaults to
1). For instance, one can specify a lasso prior using set_prior("lasso(1)"). To make sure that
shrinkage can equally affect all coefficients, predictors should be one the same scale. If you do not
want to standardized all variables, you can adjust the general scale of the lasso prior via argument
scale, for instance, lasso(1,scale = 10).
Value
References
Park, T., & Casella, G. (2008). The Bayesian Lasso. Journal of the American Statistical Associa-
tion, 103(482), 681-686.
See Also
set_prior
Examples
set_prior(lasso(df = 1, scale = 10))
launch_shinystan.brmsfit
Interface to shinystan
Description
Usage
Arguments
object A fitted model object typically of class brmsfit.
rstudio Only relevant for RStudio users. The default (rstudio=FALSE) is to launch the
app in the default web browser rather than RStudio’s pop-up Viewer. Users can
change the default to TRUE by setting the global option
options(shinystan.rstudio = TRUE).
... Optional arguments to pass to runApp
Value
An S4 shinystan object
See Also
launch_shinystan
Examples
## Not run:
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = "gaussian")
launch_shinystan(fit)
## End(Not run)
Description
Computes logit((x -lb) / (ub -lb))
Usage
logit_scaled(x, lb = 0, ub = 1)
Arguments
x A numeric or complex vector.
lb Lower bound defaulting to 0.
ub Upper bound defaulting to 1.
Value
A numeric or complex vector.
112 log_lik.brmsfit
Description
Usage
Arguments
Description
Usage
Arguments
object A fitted model object of class brmsfit.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
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.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
pointwise A flag indicating whether to compute the full log-likelihood matrix at once (the
default), or just return the likelihood function along with all data and samples
required to compute the log-likelihood separately for each observation. The
latter option is rarely useful when calling log_lik directly, but rather when
computing waic or loo.
combine Only relevant in multivariate models. Indicates if the log-likelihoods of the sub-
models should be combined per observation (i.e. added together; the default) or
if the log-likelihoods should be returned separately.
add_point_estimate
For internal use only. Ensures compatibility with the loo_subsample method.
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
Value
Usually, an S x N matrix containing the pointwise log-likelihood samples, where S is the number of
samples and N is the number of observations in the data. For multivariate models and if combine is
FALSE, an S x N x R array is returned, where R is the number of response variables. If pointwise
= TRUE, the output is a function with a draws attribute containing all relevant data and posterior
samples.
Description
Perform approximate leave-one-out cross-validation based on the posterior likelihood using the loo
package. For more details see loo.
114 loo.brmsfit
Usage
## S3 method for class 'brmsfit'
loo(
x,
...,
compare = TRUE,
resp = NULL,
pointwise = FALSE,
reloo = FALSE,
k_threshold = 0.7,
reloo_args = list(),
model_names = NULL
)
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
compare A flag indicating if the information criteria of the models should be compared
to each other via loo_compare.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
pointwise A flag indicating whether to compute the full log-likelihood matrix at once or
separately for each observation. The latter approach is usually considerably
slower but requires much less working memory. Accordingly, if one runs into
memory issues, pointwise = TRUE is the way to go.
reloo Logical; Indicate whether reloo should be applied on problematic observations.
Defaults to FALSE.
k_threshold The threshold at which pareto k estimates are treated as problematic. Defaults to
0.7. Only used if argument reloo is TRUE. See pareto_k_ids for more details.
reloo_args Optional list of additional arguments passed to reloo.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
Details
See loo_compare for details on model comparisons. For brmsfit objects, LOO is an alias of loo.
Use method add_criterion to store information criteria in the fitted model object for later usage.
Value
If just one object is provided, an object of class loo. If multiple objects are provided, an object of
class loolist.
loo_compare.brmsfit 115
References
Vehtari, A., Gelman, A., & Gabry J. (2016). Practical Bayesian model evaluation using leave-
one-out cross-validation and WAIC. In Statistics and Computing, doi:10.1007/s11222-016-9696-4.
arXiv preprint arXiv:1507.04544.
Gelman, A., Hwang, J., & Vehtari, A. (2014). Understanding predictive information criteria for
Bayesian models. Statistics and Computing, 24, 997-1016.
Watanabe, S. (2010). Asymptotic equivalence of Bayes cross validation and widely applicable
information criterion in singular learning theory. The Journal of Machine Learning Research, 11,
3571-3594.
Examples
## Not run:
# model with population-level effects only
fit1 <- brm(rating ~ treat + period + carry,
data = inhaler)
(loo1 <- loo(fit1))
## End(Not run)
Description
For more details see loo_compare.
Usage
## S3 method for class 'brmsfit'
loo_compare(x, ..., criterion = c("loo", "waic", "kfold"), model_names = NULL)
Arguments
x A brmsfit object.
... More brmsfit objects.
criterion The name of the criterion to be extracted from brmsfit objects.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
116 loo_model_weights.brmsfit
Details
All brmsfit objects should contain precomputed criterion objects. See add_criterion for more
help.
Value
An object of class "compare.loo".
Examples
## Not run:
# model with population-level effects only
fit1 <- brm(rating ~ treat + period + carry,
data = inhaler)
fit1 <- add_criterion(fit1, "waic")
## End(Not run)
loo_model_weights.brmsfit
Model averaging via stacking or pseudo-BMA weighting.
Description
Compute model weights for brmsfit objects via stacking or pseudo-BMA weighting. For more
details, see loo::loo_model_weights.
Usage
## S3 method for class 'brmsfit'
loo_model_weights(x, ..., model_names = NULL)
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
loo_predict.brmsfit 117
Value
A named vector of model weights.
Examples
## Not run:
# model with population-level effects only
fit1 <- brm(rating ~ treat + period + carry,
data = inhaler, family = "gaussian")
# model with an additional varying intercept for subjects
fit2 <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = "gaussian")
loo_model_weights(fit1, fit2)
## End(Not run)
Description
These functions are wrappers around the E_loo function of the loo package.
Usage
## S3 method for class 'brmsfit'
loo_predict(
object,
type = c("mean", "var", "quantile"),
probs = 0.5,
psis_object = NULL,
resp = NULL,
...
)
Arguments
object An object of class brmsfit.
type The statistic to be computed on the results. Can by either "mean" (default),
"var", or "quantile".
probs A vector of quantiles to compute. Only used if type = quantile.
psis_object An optional object returend by psis. If psis_object is missing then psis is
executed internally, which may be time consuming for models fit to very large
datasets.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
... Optional arguments passed to the underlying methods that is log_lik, as well
as posterior_predict or posterior_linpred.
prob For loo_predictive_interval, a scalar in (0, 1) indicating the desired proba-
bility mass to include in the intervals. The default is prob = 0.9 (90% intervals).
Value
loo_predict and loo_linpred return a vector with one element per observation. The only excep-
tion is if type = "quantile" and length(probs) >= 2, in which case a separate vector for each
element of probs is computed and they are returned in a matrix with length(probs) rows and one
column per observation.
loo_predictive_interval returns a matrix with one row per observation and two columns. loo_predictive_interval(..
= p) is equivalent to loo_predict(...,type = "quantile",probs = c(a,1-a)) with a = (1 -p)/2,
except it transposes the result and adds informative column names.
Examples
## Not run:
## data from help("lm")
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
d <- data.frame(
weight = c(ctl, trt),
group = gl(2, 10, 20, labels = c("Ctl", "Trt"))
)
fit <- brm(weight ~ group, data = d)
loo_predictive_interval(fit, prob = 0.8)
## End(Not run)
loo_R2.brmsfit 119
Description
Compute a LOO-adjusted R-squared for regression models
Usage
## S3 method for class 'brmsfit'
loo_R2(object, resp = NULL, ...)
Arguments
object An object of class brmsfit.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
... Further arguments passed to posterior_epred and log_lik, which are used in
the computation of the R-squared values.
Value
A real value per response variable indicating the LOO-adjusted R-squared.
Examples
## Not run:
fit <- brm(mpg ~ wt + cyl, data = mtcars)
summary(fit)
loo_R2(fit)
## End(Not run)
Description
Efficient approximate leave-one-out cross-validation (LOO) using subsampling
120 ma
Usage
## S3 method for class 'brmsfit'
loo_subsample(x, ..., compare = TRUE, resp = NULL, model_names = NULL)
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
compare A flag indicating if the information criteria of the models should be compared
to each other via loo_compare.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
Details
More details can be found on loo_subsample.
Examples
## Not run:
# model with population-level effects only
fit1 <- brm(rating ~ treat + period + carry,
data = inhaler)
(loo1 <- loo_subsample(fit1))
## End(Not run)
Description
Set up a moving average (MA) term of order q in brms. The function does not evaluate its argu-
ments – it exists purely to help set up a model with MA terms.
make_conditions 121
Usage
ma(time = NA, gr = NA, q = 1, cov = FALSE)
Arguments
time An optional time variable specifying the time ordering of the observations. By
default, the existing order of the observations in the data is used.
gr An optional grouping variable. If specified, the correlation structure is assumed
to apply only to observations within the same grouping level.
q A non-negative integer specifying the moving average (MA) order of the ARMA
structure. Default is 1.
cov A flag indicating whether ARMA effects should be estimated by means of resid-
ual covariance matrices. This is currently only possible for stationary ARMA
effects of order 1. If the model family does not have natural residuals, latent
residuals are added automatically. If FALSE (the default), a regression formu-
lation is used that is considerably faster and allows for ARMA effects of order
higher than 1 but is only available for gaussian models and some of its gener-
alizations.
Value
An object of class 'arma_term', which is a list of arguments to be interpreted by the formula
parsing functions of brms.
See Also
autocor-terms, arma, ar
Examples
## Not run:
data("LakeHuron")
LakeHuron <- as.data.frame(LakeHuron)
fit <- brm(x ~ ma(p = 2), data = LakeHuron)
summary(fit)
## End(Not run)
Description
This is a helper function to prepare fully crossed conditions primarily for use with the conditions
argument of conditional_effects. Automatically creates labels for each row in the cond__ col-
umn.
122 make_stancode
Usage
make_conditions(x, vars, ...)
Arguments
x An R object from which to extract the variables that should be part of the con-
ditions.
vars Names of the variables that should be part of the conditions.
... Arguments passed to rows2labels.
Details
For factor like variables, all levels are used as conditions. For numeric variables, mean + (-1:1) *
SD are used as conditions.
Value
A data.frame where each row indicates a condition.
See Also
conditional_effects, rows2labels
Examples
df <- data.frame(x = c("a", "b"), y = rnorm(10))
make_conditions(df, vars = c("x", "y"))
Description
Generate Stan code for brms models
Usage
make_stancode(
formula,
data,
family = gaussian(),
prior = NULL,
autocor = NULL,
cov_ranef = NULL,
sparse = NULL,
sample_prior = "no",
make_stancode 123
stanvars = NULL,
stan_funs = NULL,
knots = NULL,
save_model = NULL,
...
)
Arguments
formula An object of class formula, brmsformula, or mvbrmsformula (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) con-
taining 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. In multivariate models, family
might also be a list of families.
prior One or more brmsprior objects created by set_prior or related functions and
combined using the c method or the + operator. See also get_prior for more
help.
autocor (Deprecated) 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. In multivariate models, autocor might
also be a list of autocorrelation structures. It is now recommend to specify auto-
correlation terms directly within formula. See brmsformula for more details.
cov_ranef (Deprecated) A list of matrices that are proportional to the (within) covariance
structure of the group-level effects. The names of the matrices should corre-
spond 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 ef-
fects. It is now recommended to specify those matrices in the formula interface
using the gr and related functions. See vignette("brms_phylogenetics")
for more details.
sparse (Deprecated) Logical; indicates whether the population-level design matrices
should be treated as sparse (defaults to FALSE). For design matrices with many
zeros, this can considerably reduce required memory. Sampling speed is cur-
rently not improved or even slightly decreased. It is now recommended to use
the sparse argument of brmsformula and related functions.
sample_prior Indicate if samples from priors should be drawn additionally to the posterior
samples. Options are "no" (the default), "yes", and "only". Among others,
these samples can be used to calculate Bayes factors for point hypotheses via
hypothesis. Please note that improper priors are not sampled, including the
124 make_standata
default improper priors used by brm. See set_prior on how to set (proper) pri-
ors. Please also note that prior samples for the overall intercept are not obtained
by default for technical reasons. See brmsformula how to obtain prior samples
for the intercept. If sample_prior is set to "only", samples are drawn solely
from the priors ignoring the likelihood, which allows among others to generate
samples from the prior predictive distribution. In this case, all parameters must
have proper priors.
stanvars An optional stanvars object generated by function stanvar to define additional
variables for use in Stan’s program blocks.
stan_funs (Deprecated) An optional character string containing self-defined Stan func-
tions, which will be included in the functions block of the generated Stan code.
It is now recommended to use the stanvars argument for this purpose instead.
knots Optional list containing user specified knot values to be used for basis construc-
tion of smoothing terms. See gamm for more details.
save_model Either NULL or a character string. In the latter case, the model’s Stan code is
saved via cat in a text file named after the string supplied in save_model.
... Other arguments for internal usage only.
Value
A character string containing the fully commented Stan code to fit a brms model.
Examples
make_stancode(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = "cumulative")
Description
Generate data for brms models to be passed to Stan
Usage
make_standata(
formula,
data,
family = gaussian(),
prior = NULL,
autocor = NULL,
make_standata 125
data2 = NULL,
cov_ranef = NULL,
sample_prior = "no",
stanvars = NULL,
knots = NULL,
...
)
Arguments
formula An object of class formula, brmsformula, or mvbrmsformula (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) con-
taining 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. In multivariate models, family
might also be a list of families.
prior One or more brmsprior objects created by set_prior or related functions and
combined using the c method or the + operator. See also get_prior for more
help.
autocor (Deprecated) 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. In multivariate models, autocor might
also be a list of autocorrelation structures. It is now recommend to specify auto-
correlation terms directly within formula. See brmsformula for more details.
data2 A named list of objects containing data, which cannot be passed via argument
data. Required for some objects used in autocorrelation structures to specify
dependency structures as well as for within-group covariance matrices.
cov_ranef (Deprecated) A list of matrices that are proportional to the (within) covariance
structure of the group-level effects. The names of the matrices should corre-
spond 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 ef-
fects. It is now recommended to specify those matrices in the formula interface
using the gr and related functions. See vignette("brms_phylogenetics")
for more details.
sample_prior Indicate if samples from priors should be drawn additionally to the posterior
samples. Options are "no" (the default), "yes", and "only". Among others,
these samples can be used to calculate Bayes factors for point hypotheses via
hypothesis. Please note that improper priors are not sampled, including the
126 mcmc_plot.brmsfit
default improper priors used by brm. See set_prior on how to set (proper) pri-
ors. Please also note that prior samples for the overall intercept are not obtained
by default for technical reasons. See brmsformula how to obtain prior samples
for the intercept. If sample_prior is set to "only", samples are drawn solely
from the priors ignoring the likelihood, which allows among others to generate
samples from the prior predictive distribution. In this case, all parameters must
have proper priors.
stanvars An optional stanvars object generated by function stanvar to define additional
variables for use in Stan’s program blocks.
knots Optional list containing user specified knot values to be used for basis construc-
tion of smoothing terms. See gamm for more details.
... Other arguments for internal use.
Value
A named list of objects containing the required data to fit a brms model with Stan.
Author(s)
Paul-Christian Buerkner <[email protected]>
Examples
sdata1 <- make_standata(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = "cumulative")
str(sdata1)
Description
Convenient way to call MCMC plotting functions implemented in the bayesplot package.
Usage
## S3 method for class 'brmsfit'
mcmc_plot(
object,
pars = NA,
type = "intervals",
fixed = FALSE,
mcmc_plot.brmsfit 127
exact_match = FALSE,
...
)
mcmc_plot(object, ...)
Arguments
object An R object typically of class brmsfit
pars Names of parameters to be plotted, as given by a character vector or regular ex-
pressions. By default, all parameters except for group-level and smooth effects
are plotted. May be ignored for some plots.
type The type of the plot. Supported types are (as names) hist, dens, hist_by_chain,
dens_overlay, violin, intervals, areas, acf, acf_bar,trace, trace_highlight,
scatter, rhat, rhat_hist, neff, neff_hist nuts_acceptance, nuts_divergence,
nuts_stepsize, nuts_treedepth, and nuts_energy. For an overview on the
various plot types see MCMC-overview.
fixed Indicates whether parameter names should be matched exactly (TRUE) or treated
as regular expressions (FALSE). Default is FALSE.
exact_match Deprecated alias of argument fixed.
... Additional arguments passed to the plotting functions. See MCMC-overview for
more details.
Details
Also consider using the shinystan package available via method launch_shinystan in brms for
flexible and interactive visual analysis.
Value
A ggplot object that can be further customized using the ggplot2 package.
Examples
## Not run:
model <- brm(count ~ zAge + zBase * Trt + (1|patient),
data = epilepsy, family = "poisson")
## End(Not run)
Description
Specify predictors with measurement error. The function does not evaluate its arguments – it exists
purely to help set up a model.
Usage
me(x, sdx, gr = NULL)
Arguments
x The variable measured with error.
sdx Known measurement error of x treated as standard deviation.
gr Optional grouping factor to specify which values of x correspond to the same
value of the latent variable. If NULL (the default) each observation will have its
own value of the latent variable.
Details
For detailed documentation see help(brmsformula).
By default, latent noise-free variables are assumed to be correlated. To change that, add set_mecor(FALSE)
to your model formula object (see examples).
See Also
brmsformula, brmsformula-helpers
Examples
## Not run:
# sample some data
N <- 100
dat <- data.frame(
y = rnorm(N), x1 = rnorm(N),
x2 = rnorm(N), sdx = abs(rnorm(N, 1))
)
mi 129
## End(Not run)
Description
Specify predictor term with missing values in brms. The function does not evaluate its arguments
– it exists purely to help set up a model.
Usage
mi(x)
Arguments
x The variable containing missings.
Details
For detailed documentation see help(brmsformula).
See Also
brmsformula
Examples
## Not run:
data("nhanes", package = "mice")
bform <- bf(bmi | mi() ~ age * mi(chl)) +
bf(chl | mi() ~ age) + set_rescor(FALSE)
fit <- brm(bform, data = nhanes)
summary(fit)
plot(conditional_effects(fit, resp = "bmi"), ask = FALSE)
LOO(fit, newdata = na.omit(fit$data))
## End(Not run)
130 mixture
Description
Set up a finite mixture family for use in brms.
Usage
mixture(..., flist = NULL, nmix = 1, order = NULL)
Arguments
... One or more objects providing a description of the response distributions to be
combined in the mixture model. These can be family functions, calls to fam-
ily functions or character strings naming the families. For details of supported
families see brmsfamily.
flist Optional list of objects, which are treated in the same way as objects passed via
the ... argument.
nmix Optional numeric vector specifying the number of times each family is repeated.
If specified, it must have the same length as the number of families passed via
... and flist.
order Ordering constraint to identify mixture components. If 'mu' or TRUE, population-
level intercepts of the mean parameters are ordered in non-ordinal models and
fixed to the same value in ordinal models (see details). If 'none' or FALSE, no
ordering constraint is applied. If NULL (the default), order is set to 'mu' if all
families are the same and 'none' otherwise. Other ordering constraints may be
implemented in the future.
Details
Most families supported by brms can be used to form mixtures. The response variable has to be
valid for all components of the mixture family. Currently, the number of mixture components has to
be specified by the user. It is not yet possible to estimate the number of mixture components from
the data.
Ordering intercepts in mixtures of ordinal families is not possible as each family has itself a set of
vector of intercepts (i.e. ordinal thresholds). Instead, brms will fix the vector of intercepts across
components in ordinal mixtures, if desired, so that users can try to identify the mixture model via
selective inclusion of predictors.
For most mixture models, you may want to specify priors on the population-level intercepts via
set_prior to improve convergence. In addition, it is sometimes necessary to set inits = 0 in the
call to brm to allow chains to initialize properly.
For more details on the specification of mixture models, see brmsformula.
mixture 131
Value
Examples
## Not run:
## simulate some data
set.seed(1234)
dat <- data.frame(
y = c(rnorm(200), rnorm(100, 6)),
x = rnorm(300),
z = sample(0:1, 300, TRUE)
)
## End(Not run)
132 mm
Description
Function to set up a multi-membership grouping term in brms. The function does not evaluate its
arguments – it exists purely to help set up a model with grouping terms.
Usage
mm(
...,
weights = NULL,
scale = TRUE,
cor = TRUE,
id = NA,
cov = NULL,
dist = "gaussian"
)
Arguments
... One or more terms containing grouping factors.
weights A matrix specifying the weights of each member. It should have as many
columns as grouping terms specified in .... If NULL (the default), equally
weights are used.
scale Logical; if TRUE (the default), weights are standardized in order to sum to one
per row. If negative weights are specified, scale needs to be set to FALSE.
cor Logical. If TRUE (the default), group-level terms will be modelled as correlated.
id Optional character string. All group-level terms across the model with the same
id will be modeled as correlated (if cor is TRUE). See brmsformula for more
details.
cov An optional matrix which is proportional to the withon-group covariance matrix
of the group-level effects. All levels of the grouping factor should appear as row-
names of the corresponding matrix. This argument can be used, among others, to
model pedigrees and phylogenetic effects. See vignette("brms_phylogenetics")
for more details. By default, levels of the same grouping factor are modeled as
independent of each other.
dist Name of the distribution of the group-level effects. Currently "gaussian" is the
only option.
See Also
brmsformula, mmc
mmc 133
Examples
## Not run:
# simulate some data
dat <- data.frame(
y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100),
g1 = sample(1:10, 100, TRUE), g2 = sample(1:10, 100, TRUE)
)
# multi-membership model with two members per group and equal weights
fit1 <- brm(y ~ x1 + (1|mm(g1, g2)), data = dat)
summary(fit1)
# weight the first member two times for than the second member
dat$w1 <- rep(2, 100)
dat$w2 <- rep(1, 100)
fit2 <- brm(y ~ x1 + (1|mm(g1, g2, weights = cbind(w1, w2))), data = dat)
summary(fit2)
## End(Not run)
Description
Specify covarariates that vary over different levels of multi-membership grouping factors thus re-
quiring special treatment. This function is almost solely useful, when called in combination with
mm. Outside of multi-membership terms it will behave very much like cbind.
Usage
mmc(...)
Arguments
... One or more terms containing covariates corresponding to the grouping levels
specified in mm.
Value
A matrix with covariates as columns.
134 mo
See Also
mm
Examples
## Not run:
# simulate some data
dat <- data.frame(
y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100),
g1 = sample(1:10, 100, TRUE), g2 = sample(1:10, 100, TRUE)
)
## End(Not run)
Description
Specify a monotonic predictor term in brms. The function does not evaluate its arguments – it
exists purely to help set up a model.
Usage
mo(x)
Arguments
x An integer variable or an ordered factor to be modeled as monotonic.
Details
For detailed documentation see help(brmsformula) as well as vignette("brms_monotonic").
References
Bürkner P. C. & Charpentier, E. (in review). Monotonic Effects: A Principled Approach for Includ-
ing Ordinal Predictors in Regression Models. PsyArXiv preprint.
See Also
brmsformula
model_weights.brmsfit 135
Examples
## Not run:
# generate some data
income_options <- c("below_20", "20_to_40", "40_to_100", "greater_100")
income <- factor(sample(income_options, 100, TRUE),
levels = income_options, ordered = TRUE)
mean_ls <- c(30, 60, 70, 75)
ls <- mean_ls[income] + rnorm(100, sd = 7)
dat <- data.frame(income, ls)
## End(Not run)
Description
Compute model weights in various ways, for instance via stacking of predictive distributions,
Akaike weights, or marginal likelihoods.
Usage
## S3 method for class 'brmsfit'
model_weights(x, ..., weights = "stacking", model_names = NULL)
model_weights(x, ...)
Arguments
x A brmsfit object.
136 MultiNormal
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
weights Name of the criterion to compute weights from. Should be one of "loo",
"waic", "kfold", "stacking" (current default), or "bma", "pseudobma", For
the former three options, Akaike weights will be computed based on the infor-
mation criterion values returned by the respective methods. For "stacking"
and "pseudobma" method loo_model_weights will be used to obtain weights.
For "bma", method post_prob will be used to compute Bayesian model aver-
aging weights based on log marginal likelihood values (make sure to specify
reasonable priors in this case). Some method, weights may also be a numeric
vector of pre-specified weights.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
Value
A numeric vector of weights for the models.
Examples
## Not run:
# model with 'treat' as predictor
fit1 <- brm(rating ~ treat + period + carry, data = inhaler)
summary(fit1)
## End(Not run)
Description
Density function and random generation for the multivariate normal distribution with mean vector
mu and covariance matrix Sigma.
Usage
dmulti_normal(x, mu, Sigma, log = FALSE, check = FALSE)
Arguments
x Vector or matrix of quantiles. If x is a matrix, each row is taken to be a quantile.
mu Mean vector with length equal to the number of dimensions.
Sigma Covariance matrix.
log Logical; If TRUE, values are returned on the log scale.
check Logical; Indicates whether several input checks should be performed. Defaults
to FALSE to improve efficiency.
n Number of samples to draw from the distribution.
Details
See the Stan user’s manual http://mc-stan.org/documentation/ for details on the parameteri-
zation
Description
Density function and random generation for the multivariate Student-t distribution with location
vector mu, covariance matrix Sigma, and degrees of freedom df.
Usage
dmulti_student_t(x, df, mu, Sigma, log = FALSE, check = FALSE)
Arguments
x Vector or matrix of quantiles. If x is a matrix, each row is taken to be a quantile.
df Vector of degrees of freedom.
mu Location vector with length equal to the number of dimensions.
Sigma Covariance matrix.
log Logical; If TRUE, values are returned on the log scale.
check Logical; Indicates whether several input checks should be performed. Defaults
to FALSE to improve efficiency.
n Number of samples to draw from the distribution.
Details
See the Stan user’s manual http://mc-stan.org/documentation/ for details on the parameteri-
zation
138 mvbrmsformula
Description
Can be used to specify a multivariate brms model within a single formula. Outside of brmsformula,
it just behaves like cbind.
Usage
mvbind(...)
Arguments
... Same as in cbind
See Also
brmsformula, mvbrmsformula
Examples
bf(mvbind(y1, y2) ~ x)
Description
Set up a multivariate model formula for use in the brms package allowing to define (potentially
non-linear) additive multilevel models for all parameters of the assumed response distributions.
Usage
mvbrmsformula(..., flist = NULL, rescor = NULL)
Arguments
... Objects of class formula or brmsformula, each specifying a univariate model.
See brmsformula for details on how to specify univariate models.
flist Optional list of formulas, which are treated in the same way as formulas passed
via the ... argument.
rescor Logical; Indicates if residual correlation between the response variables should
be modeled. Currently, this is only possible in multivariate gaussian and student
models. If NULL (the default), rescor is internally set to TRUE when possible.
ngrps.brmsfit 139
Details
Value
An object of class mvbrmsformula, which is essentially a list containing all model formulas as
well as some additional information for multivariate models.
See Also
brmsformula, brmsformula-helpers
Examples
bf1 <- bf(y1 ~ x + (1|g))
bf2 <- bf(y2 ~ s(z))
mvbf(bf1, bf2)
Description
Usage
ngrps(object, ...)
Arguments
object An R object.
... Currently ignored.
Value
Description
Extract the number of posterior samples stored in a fitted Bayesian model.
Usage
## S3 method for class 'brmsfit'
nsamples(object, subset = NULL, incl_warmup = FALSE, ...)
Arguments
object An object of class brmsfit.
subset An optional integer vector defining a subset of samples to be considered.
incl_warmup A flag indicating whether to also count warmup / burn-in samples.
... Currently ignored.
Description
A pairs method that is customized for MCMC output.
Usage
## S3 method for class 'brmsfit'
pairs(x, pars = NA, fixed = FALSE, exact_match = FALSE, ...)
Arguments
x An object of class brmsfit
pars Names of the parameters to plot, as given by a character vector or a regular
expression. By default, all parameters except for group-level and smooth effects
are plotted.
fixed Indicates whether parameter names should be matched exactly (TRUE) or treated
as regular expressions (FALSE). Default is FALSE.
exact_match Deprecated alias of argument fixed.
... Further arguments to be passed to mcmc_pairs.
parnames 141
Details
For a detailed description see mcmc_pairs.
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt
+ (1|patient) + (1|visit),
data = epilepsy, family = "poisson")
pairs(fit, pars = parnames(fit)[1:3], fixed = TRUE)
pairs(fit, pars = "^sd_")
## End(Not run)
Description
Extract all parameter names of a given model.
Usage
parnames(x, ...)
Arguments
x An R object
... Further arguments passed to or from other methods.
Value
A character vector containing the parameter names of the model.
Description
Trace and Density Plots for MCMC Samples
142 plot.brmsfit
Usage
Arguments
Value
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt
+ (1|patient) + (1|visit),
data = epilepsy, family = "poisson")
plot(fit)
## plot population-level effects only
plot(fit, pars = "^b_")
## End(Not run)
posterior_average.brmsfit
Posterior samples of parameters averaged across models
Description
Extract posterior samples of parameters averaged across models. Weighting can be done in various
ways, for instance using Akaike weights based on information criteria or marginal likelihoods.
Usage
## S3 method for class 'brmsfit'
posterior_average(
x,
...,
pars = NULL,
weights = "stacking",
nsamples = NULL,
missing = NULL,
model_names = NULL,
control = list(),
seed = NULL
)
posterior_average(x, ...)
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
pars Names of parameters for which to average across models. Only those param-
eters can be averaged that appear in every model. Defaults to all overlapping
parameters.
144 posterior_average.brmsfit
weights Name of the criterion to compute weights from. Should be one of "loo",
"waic", "kfold", "stacking" (current default), or "bma", "pseudobma", For
the former three options, Akaike weights will be computed based on the infor-
mation criterion values returned by the respective methods. For "stacking"
and "pseudobma" method loo_model_weights will be used to obtain weights.
For "bma", method post_prob will be used to compute Bayesian model aver-
aging weights based on log marginal likelihood values (make sure to specify
reasonable priors in this case). Some method, weights may also be a numeric
vector of pre-specified weights.
nsamples Total number of posterior samples to use.
missing An optional numeric value or a named list of numeric values to use if a model
does not contain a parameter for which posterior samples should be averaged.
Defaults to NULL, in which case only those parameters can be averaged that are
present in all of the models.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
control Optional list of further arguments passed to the function specified in weights.
seed A single numeric value passed to set.seed to make results reproducible.
Details
Weights are computed with the model_weights method.
Value
A data.frame of posterior samples. Samples are rows and parameters are columns.
See Also
model_weights, pp_average
Examples
## Not run:
# model with 'treat' as predictor
fit1 <- brm(rating ~ treat + period + carry, data = inhaler)
summary(fit1)
## End(Not run)
posterior_epred.brmsfit 145
posterior_epred.brmsfit
Expected Values of the Posterior Predictive Distribution
Description
Compute posterior samples of the expected value/mean of the posterior predictive distribution. 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 posterior predictions performed by the
posterior_predict.brmsfit method. This is because only the uncertainty in the mean is incorpo-
rated in the samples computed by posterior_epred while any residual error is ignored. However,
the estimated means of both methods averaged across samples should be very similar.
Usage
## S3 method for class 'brmsfit'
posterior_epred(
object,
newdata = NULL,
re_formula = NULL,
re.form = NULL,
resp = NULL,
dpar = NULL,
nlpar = NULL,
nsamples = NULL,
subset = NULL,
sort = FALSE,
...
)
posterior_epred(object, ...)
Arguments
object An object of class brmsfit.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
re.form Alias of re_formula.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
146 posterior_interval.brmsfit
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.
Value
An array of predicted mean response values. For categorical and ordinal models, the output is
an S x N x C array. Otherwise, the output is an S x N matrix, where S is the number of posterior
samples, N is the number of observations, and C is the number of categories. In multivariate models,
an additional dimension is added to the output which indexes along the different response variables.
Examples
## Not run:
## fit a model
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler)
## End(Not run)
posterior_interval.brmsfit
Compute posterior uncertainty intervals
Description
Compute posterior uncertainty intervals for brmsfit objects.
posterior_linpred.brmsfit 147
Usage
## S3 method for class 'brmsfit'
posterior_interval(object, pars = NA, prob = 0.95, ...)
Arguments
object An object of class brmsfit
pars Names of parameters for which posterior samples should be returned, as given
by a character vector or regular expressions. By default, all posterior samples of
all parameters are extracted.
prob A value between 0 and 1 indicating the desired probability to be covered by the
uncertainty intervals. The default is 0.95.
... More arguments passed to as.matrix.brmsfit.
Value
A matrix with lower and upper interval bounds as columns and as many rows as selected parame-
ters.
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt,
data = epilepsy, family = negbinomial())
posterior_interval(fit)
## End(Not run)
posterior_linpred.brmsfit
Posterior Samples of the Linear Predictor
Description
Compute posterior samples of the linear predictor, that is samples before applying any link functions
or other transformations. Can be performed for the data used to fit the model (posterior predictive
checks) or for new data.
Usage
## S3 method for class 'brmsfit'
posterior_linpred(
object,
transform = FALSE,
newdata = NULL,
re_formula = NULL,
148 posterior_linpred.brmsfit
re.form = NULL,
resp = NULL,
dpar = NULL,
nlpar = NULL,
nsamples = NULL,
subset = NULL,
sort = FALSE,
...
)
Arguments
object An object of class brmsfit.
transform (Deprecated) Logical; if FALSE (the default), samples of the linear predictor are
returned. If TRUE, samples of transformed linear predictor, that is, the mean of
the posterior predictive distribution are returned instead (see posterior_epred
for details). Only implemented for compatibility with the posterior_linpred
generic.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
re.form Alias of re_formula.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
dpar Optional name of a predicted distributional parameter. If specified, expected
predictions of this parameters are returned.
nlpar Optional name of a predicted non-linear parameter. If specified, expected pre-
dictions of this parameters are returned.
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.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
sort Logical. Only relevant for time series models. Indicating whether to return
predicted values in the original order (FALSE; default) or in the order of the time
series (TRUE).
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
See Also
posterior_epred.brmsfit
posterior_predict.brmsfit 149
Examples
## Not run:
## fit a model
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler)
## End(Not run)
posterior_predict.brmsfit
Samples from the Posterior Predictive Distribution
Description
Compute posterior samples of the posterior predictive distribution. Can be performed for the
data used to fit the model (posterior predictive checks) or for new data. By definition, these
samples have higher variance than samples of the means of the posterior predictive distribution
computed by posterior_epred.brmsfit. This is because the residual error is incorporated in
posterior_predict. However, the estimated means of both methods averaged across samples
should be very similar.
Usage
## S3 method for class 'brmsfit'
posterior_predict(
object,
newdata = NULL,
re_formula = NULL,
re.form = NULL,
transform = NULL,
resp = NULL,
negative_rt = FALSE,
nsamples = NULL,
subset = NULL,
sort = FALSE,
ntrys = 5,
...
)
Arguments
object An object of class brmsfit.
150 posterior_predict.brmsfit
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
re.form Alias of re_formula.
transform (Deprecated) A function or a character string naming a function to be applied
on the predicted responses before summary statistics are computed.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
negative_rt Only relevant for Wiener diffusion models. A flag indicating whether response
times of responses on the lower boundary should be returned as negative values.
This allows to distinguish responses on the upper and lower boundary. Defaults
to FALSE.
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.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
sort Logical. Only relevant for time series models. Indicating whether to return
predicted values in the original order (FALSE; default) or in the order of the time
series (TRUE).
ntrys Parameter used in rejection sampling for truncated discrete models only (de-
faults to 5). See Details for more information.
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
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 truncated discrete models only: In the absence of any general algorithm to sample from trun-
cated 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 sam-
pling 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
occur suggesting to increase argument ntrys.
Value
An array of predicted response values. If summary = FALSE, the output is as an S x N matrix, where
S is the number of posterior samples and N is the number of observations. In multivariate models,
an additional dimension is added to the output which indexes along the different response variables.
posterior_samples.brmsfit 151
Examples
## Not run:
## fit a model
fit <- brm(time | cens(censored) ~ age + sex + (1 + age || patient),
data = kidney, family = "exponential", inits = "0")
## predicted responses
pp <- posterior_predict(fit)
str(pp)
## End(Not run)
posterior_samples.brmsfit
Extract Posterior Samples
Description
Extract posterior samples of specified parameters.
Usage
## S3 method for class 'brmsfit'
posterior_samples(
x,
pars = NA,
fixed = FALSE,
add_chain = FALSE,
subset = NULL,
as.matrix = FALSE,
as.array = FALSE,
...
)
152 posterior_samples.brmsfit
Arguments
x An R object typically of class brmsfit
pars Names of parameters for which posterior samples should be returned, as given
by a character vector or regular expressions. By default, all posterior samples of
all parameters are extracted.
fixed Indicates whether parameter names should be matched exactly (TRUE) or treated
as regular expressions (FALSE). Default is FALSE.
add_chain A flag indicating if the returned data.frame should contain two additional
columns. The chain column indicates the chain in which each sample was
generated, the iter column indicates the iteration number within each chain.
subset A numeric vector indicating the rows (i.e., posterior samples) to be returned. If
NULL (the default), all posterior samples are returned.
as.matrix Should the output be a matrix instead of a data.frame? Defaults to FALSE.
as.array Should the output be an array instead of a data.frame? Defaults to FALSE.
... For as.data.frame, as.matrix, and as.array: Further arguments to be passed
to posterior_samples.
row.names, optional
See as.data.frame.
Details
Currently there are methods for brmsfit objects. as.data.frame.brmsfit, as.matrix.brmsfit,
and as.array.brmsfit are basically aliases of posterior_samples.brmsfit and differ from
each other only in type of the returned object.
Value
A data.frame (matrix or array) containing the posterior samples, with one column per parameter. In
case an array is returned, it contains one additional dimension for the chains.
Examples
## Not run:
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = "cumulative")
posterior_summary 153
## End(Not run)
Description
Summarizes posterior samples based on point estimates (mean or median), estimation errors (SD or
MAD) and quantiles.
Usage
posterior_summary(x, ...)
## Default S3 method:
posterior_summary(x, probs = c(0.025, 0.975), robust = FALSE, ...)
Arguments
x An R object.
... More arguments passed to or from other methods.
probs The percentiles to be computed by the quantile function.
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead.
pars Names of parameters for which posterior samples should be returned, as given
by a character vector or regular expressions. By default, all posterior samples of
all parameters are extracted.
Value
A matrix where rows indicate parameters and columns indicate the summary estimates.
154 posterior_table
Examples
## Not run:
fit <- brm(time ~ age * sex, data = kidney)
posterior_summary(fit)
## End(Not run)
Description
Create a table for unique values of posterior samples. This is usually only useful when summarizing
predictions of ordinal models.
Usage
Arguments
x A matrix of posterior samples where rows indicate samples and columns indicate
parameters.
levels Optional values of possible posterior values. Defaults to all unique values in x.
Value
A matrix where rows indicate parameters and columns indicate the unique values of posterior sam-
ples.
Examples
## Not run:
fit <- brm(rating ~ period + carry + treat,
data = inhaler, family = cumulative())
pr <- predict(fit, summary = FALSE)
posterior_table(pr)
## End(Not run)
post_prob.brmsfit 155
Description
Compute posterior model probabilities from marginal likelihoods. The brmsfit method is just a
thin wrapper around the corresponding method for bridge objects.
Usage
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
prior_prob Numeric vector with prior model probabilities. If omitted, a uniform prior is
used (i.e., all models are equally likely a priori). The default NULL corresponds
to equal prior model weights.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
Details
Computing the marginal likelihood requires samples of all variables defined in Stan’s parameters
block to be saved. Otherwise post_prob cannot be computed. Thus, please set save_all_pars =
TRUE in the call to brm, if you are planning to apply post_prob to your models.
The computation of model probabilities based on bridge sampling requires a lot more posterior
samples than usual. A good conservative rule of thump is perhaps 10-fold more samples (read:
the default of 4000 samples may not be enough in many cases). If not enough posterior samples
are provided, the bridge sampling algorithm tends to be unstable leading to considerably different
results each time it is run. We thus recommend running post_prob multiple times to check the
stability of the results.
More details are provided under bridgesampling:post_prob.
See Also
bridge_sampler,bayes_factor
156 pp_average.brmsfit
Examples
## Not run:
# model with the treatment effect
fit1 <- brm(
count ~ zAge + zBase + Trt,
data = epilepsy, family = negbinomial(),
prior = prior(normal(0, 1), class = b),
save_all_pars = TRUE
)
summary(fit1)
## End(Not run)
Description
Compute posterior predictive samples averaged across models. Weighting can be done in various
ways, for instance using Akaike weights based on information criteria or marginal likelihoods.
Usage
## S3 method for class 'brmsfit'
pp_average(
x,
...,
weights = "stacking",
method = "posterior_predict",
nsamples = NULL,
summary = TRUE,
probs = c(0.025, 0.975),
robust = FALSE,
pp_average.brmsfit 157
model_names = NULL,
control = list(),
seed = NULL
)
pp_average(x, ...)
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
weights Name of the criterion to compute weights from. Should be one of "loo",
"waic", "kfold", "stacking" (current default), or "bma", "pseudobma", For
the former three options, Akaike weights will be computed based on the infor-
mation criterion values returned by the respective methods. For "stacking"
and "pseudobma" method loo_model_weights will be used to obtain weights.
For "bma", method post_prob will be used to compute Bayesian model aver-
aging weights based on log marginal likelihood values (make sure to specify
reasonable priors in this case). Some method, weights may also be a numeric
vector of pre-specified weights.
method Method used to obtain predictions to average over. Should be one of "posterior_predict"
(default), "pp_expect", or "predictive_error".
nsamples Total number of posterior samples to use.
summary Should summary statistics (i.e. means, sds, and 95% intervals) be returned in-
stead of the raw values? Default is TRUE.
probs The percentiles to be computed by the quantile function. Only used if summary
is TRUE.
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead. Only used if summary is
TRUE.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
control Optional list of further arguments passed to the function specified in weights.
seed A single numeric value passed to set.seed to make results reproducible.
Details
Weights are computed with the model_weights method.
Value
Same as the output of the method specified in argument method.
158 pp_check.brmsfit
See Also
model_weights, posterior_average
Examples
## Not run:
# model with 'treat' as predictor
fit1 <- brm(rating ~ treat + period + carry, data = inhaler)
summary(fit1)
## End(Not run)
Description
Perform posterior predictive checks with the help of the bayesplot package.
Usage
Arguments
object An object of class brmsfit.
type Type of the ppc plot as given by a character string. See PPC for an overview of
currently supported types. You may also use an invalid type (e.g. type = "xyz")
to get a list of supported types in the resulting error message.
nsamples Positive integer indicating how many posterior samples should be used. If NULL
all samples are used. If not specified, the number of posterior samples is chosen
automatically. Ignored if subset is not NULL.
group Optional name of a factor variable in the model by which to stratify the ppc plot.
This argument is required for ppc *_grouped types and ignored otherwise.
x Optional name of a variable in the model. Only used for ppc types having an x
argument and ignored otherwise.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
... Further arguments passed to predict.brmsfit as well as to the PPC function
specified in type.
Details
For a detailed explanation of each of the ppc functions, see the PPC documentation of the bayesplot
package.
Value
A ggplot object that can be further customized using the ggplot2 package.
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt
+ (1|patient) + (1|obs),
data = epilepsy, family = poisson())
## End(Not run)
Description
Compute the posterior probabilities of mixture component memberships for each observation in-
cluding uncertainty estimates.
Usage
## S3 method for class 'brmsfit'
pp_mixture(
x,
newdata = NULL,
re_formula = NULL,
resp = NULL,
nsamples = NULL,
subset = NULL,
log = FALSE,
summary = TRUE,
robust = FALSE,
probs = c(0.025, 0.975),
...
)
pp_mixture(x, ...)
Arguments
x An R object usually of class brmsfit.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
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.
pp_mixture.brmsfit 161
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
log Logical; Indicates whether to return probabilities on the log-scale.
summary Should summary statistics be returned instead of the raw values? Default is
TRUE.
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead. Only used if summary is
TRUE.
probs The percentiles to be computed by the quantile function. Only used if summary
is TRUE.
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
Details
The returned probabilities can be written as P (Kn = k|Y n), that is the posterior probability that
observation n originates from component k. They are computed using Bayes’ Theorem
is a normalizing constant.
Value
If summary = TRUE, an N x E x K array, where N is the number of observations, K is the number
of mixture components, and E is equal to length(probs) + 2. If summary = FALSE, an S x N x K
array, where S is the number of posterior samples.
Examples
## Not run:
## simulate some data
set.seed(1234)
dat <- data.frame(
y = c(rnorm(100), rnorm(50, 2)),
x = rnorm(150)
)
## fit a simple normal mixture model
mix <- mixture(gaussian, nmix = 2)
prior <- c(
prior(normal(0, 5), Intercept, nlpar = mu1),
prior(normal(0, 5), Intercept, nlpar = mu2),
prior(dirichlet(2, 2), theta)
162 predict.brmsfit
)
fit1 <- brm(bf(y ~ x), dat, family = mix,
prior = prior, chains = 2, inits = 0)
summary(fit1)
## End(Not run)
Description
Usage
Arguments
object An object of class brmsfit.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
transform (Deprecated) A function or a character string naming a function to be applied
on the predicted responses before summary statistics are computed.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
negative_rt Only relevant for Wiener diffusion models. A flag indicating whether response
times of responses on the lower boundary should be returned as negative values.
This allows to distinguish responses on the upper and lower boundary. Defaults
to FALSE.
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.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
sort Logical. Only relevant for time series models. Indicating whether to return
predicted values in the original order (FALSE; default) or in the order of the time
series (TRUE).
ntrys Parameter used in rejection sampling for truncated discrete models only (de-
faults to 5). See Details for more information.
summary Should summary statistics be returned instead of the raw values? Default is
TRUE.
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead. Only used if summary is
TRUE.
probs The percentiles to be computed by the quantile function. Only used if summary
is TRUE.
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
Value
An array of predicted response values. If summary = FALSE the output resembles those of posterior_predict.brmsfit.
If summary = TRUE the output depends on the family: For categorical and ordinal families, the output
is an N x C matrix, where N is the number of observations, C is the number of categories, and the
values are predicted category probabilites. For all other families, the output is a N x E matrix where
E = 2 + length(probs) is the number of summary statistics: The Estimate column contains point
164 predictive_error.brmsfit
estimates (either mean or median depending on argument robust), while the Est.Error column
contains uncertainty estimates (either standard deviation or median absolute deviation depending on
argument robust). The remaining columns starting with Q contain quantile estimates as specifed
via argument probs.
See Also
posterior_predict.brmsfit
Examples
## Not run:
## fit a model
fit <- brm(time | cens(censored) ~ age + sex + (1 + age || patient),
data = kidney, family = "exponential", inits = "0")
## predicted responses
pp <- predict(fit)
head(pp)
## End(Not run)
predictive_error.brmsfit
Posterior Samples of Predictive Errors
Description
Compute posterior samples of predictive errors, that is, observed minus predicted responses. Can
be performed for the data used to fit the model (posterior predictive checks) or for new data.
Usage
## S3 method for class 'brmsfit'
predictive_error(
object,
predictive_error.brmsfit 165
newdata = NULL,
re_formula = NULL,
re.form = NULL,
resp = NULL,
nsamples = NULL,
subset = NULL,
sort = FALSE,
...
)
Arguments
object An object of class brmsfit.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
re.form Alias of re_formula.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
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.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
sort Logical. Only relevant for time series models. Indicating whether to return
predicted values in the original order (FALSE; default) or in the order of the time
series (TRUE).
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
Value
An S x N array of predictive error samples, where S is the number of posterior samples and N is
the number of observations.
Examples
## Not run:
## fit a model
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler, cores = 2)
str(pe)
## End(Not run)
predictive_interval.brmsfit
Predictive Intervals
Description
Usage
Arguments
Value
A matrix with 2 columns for the lower and upper bounds of the intervals, respectively, and as many
rows as observations being predicted.
Examples
## Not run:
fit <- brm(count ~ zBase, data = epilepsy, family = poisson())
predictive_interval(fit)
## End(Not run)
prepare_predictions.brmsfit 167
prepare_predictions.brmsfit
Prepare Predictions
Description
This method helps in preparing brms models for certin post-processing tasks most notably various
forms of predictions. Unless you are a package developer, you will rarely need to call prepare_predictions
directly.
Usage
## S3 method for class 'brmsfit'
prepare_predictions(
x,
newdata = NULL,
re_formula = NULL,
allow_new_levels = FALSE,
sample_new_levels = "uncertainty",
incl_autocor = TRUE,
oos = NULL,
resp = NULL,
nsamples = NULL,
subset = NULL,
nug = NULL,
smooths_only = FALSE,
offset = TRUE,
newdata2 = NULL,
new_objects = NULL,
point_estimate = NULL,
...
)
prepare_predictions(x, ...)
Arguments
x An R object typically of class 'brmsfit'.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
168 prepare_predictions.brmsfit
allow_new_levels
A flag indicating if new levels of group-level effects are allowed (defaults to
FALSE). Only relevant if newdata is provided.
sample_new_levels
Indicates how to sample new levels for grouping factors specified in re_formula.
This argument is only relevant if newdata is provided and allow_new_levels is
set to TRUE. If "uncertainty" (default), include group-level uncertainty in the
predictions based on the variation of the existing levels. If "gaussian", sample
new levels from the (multivariate) normal distribution implied by the group-level
standard deviations and correlations. This options may be useful for conducting
Bayesian power analysis. If "old_levels", directly sample new levels from the
existing levels.
incl_autocor A flag indicating if correlation structures originally specified via autocor should
be included in the predictions. Defaults to TRUE.
oos Optional indices of observations for which to compute out-of-sample rather than
in-sample predictions. Only required in models that make use of response values
to make predictions, that is currently only ARMA models.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
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.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
nug Small positive number for Gaussian process terms only. For numerical rea-
sons, the covariance matrix of a Gaussian process might not be positive definite.
Adding a very small number to the matrix’s diagonal often solves this problem.
If NULL (the default), nug is chosen internally.
smooths_only Logical; If TRUE only predictions related to the computation of smooth terms
will be prepared.
offset Logical; Indicates if offsets should be included in the predictions. Defaults to
TRUE.
newdata2 A named list of objects containing new data, which cannot be passed via ar-
gument newdata. Required for some objects used in autocorrelation structures,
or stanvars.
new_objects Deprecated alias of newdata2.
point_estimate Shall the returned object contain only point estimates of the parameters instead
of their posterior samples? Defaults to NULL in which case no point estimate
is computed. Alternatively, may be set to "mean" or "median". This argu-
ment is primarily implemented to ensure compatibility with the loo_subsample
method.
... Further arguments passed to validate_newdata.
Value
An object of class 'brmsprep' or 'mvbrmsprep', depending on whether a univariate or multivariate
model is passed.
print.brmsfit 169
Description
Print a summary for a fitted model represented by a brmsfit object
Usage
## S3 method for class 'brmsfit'
print(x, digits = 2, ...)
Arguments
x An object of class brmsfit
digits The number of significant digits for printing out the summary; defaults to 2. The
effective sample size is always rounded to integers.
... Additional arguments that would be passed to method summary of brmsfit.
See Also
summary.brmsfit
Description
Print method for brmsprior objects
Usage
## S3 method for class 'brmsprior'
print(x, show_df, ...)
Arguments
x An object of class brmsprior.
show_df Logical; Print priors as a single data.frame (TRUE) or as a sequence of sampling
statements (FALSE)?
... Currently ignored.
170 prior_samples.brmsfit
Description
Extract prior samples of specified parameters
Usage
## S3 method for class 'brmsfit'
prior_samples(x, pars = NA, ...)
Arguments
x An R object typically of class brmsfit
pars Names of parameters for which prior samples should be returned, as given by
a character vector or regular expressions. By default, all prior samples are ex-
tracted
... Currently ignored
Details
To make use of this function, the model must contain samples of prior distributions. This can be
ensured by setting sample_prior = TRUE in function brm. Priors of certain parameters cannot be
saved for technical reasons. For instance, this is the case for the population-level intercept, which
is only computed after fitting the model by default. If you want to treat the intercept as part of all
the other regression coefficients, so that sampling from its prior becomes possible, use ... ~ 0 +
Intercept + ... in the formulas.
Value
A data frame containing the prior samples.
Examples
## Not run:
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = "cumulative",
prior = set_prior("normal(0,2)", class = "b"),
sample_prior = TRUE)
## End(Not run)
Description
Extract Priors of a Bayesian Model Fitted with brms
Usage
## S3 method for class 'brmsfit'
prior_summary(object, all = TRUE, ...)
Arguments
object A brmsfit object
all Logical; Show all parameters in the model which may have priors (TRUE) or only
those with proper priors (FALSE)?
... Further arguments passed to or from other methods.
Value
For brmsfit objects, an object of class brmsprior.
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * 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)))
prior_summary(fit)
prior_summary(fit, all = FALSE)
print(prior_summary(fit, all = FALSE), show_df = FALSE)
## End(Not run)
172 ranef.brmsfit
Description
Extract the group-level (’random’) effects of each level from a brmsfit object.
Usage
## S3 method for class 'brmsfit'
ranef(
object,
summary = TRUE,
robust = FALSE,
probs = c(0.025, 0.975),
pars = NULL,
groups = NULL,
...
)
Arguments
object An object of class brmsfit.
summary Should summary statistics be returned instead of the raw values? Default is
TRUE.
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead. Only used if summary is
TRUE.
probs The percentiles to be computed by the quantile function. Only used if summary
is TRUE.
pars Optional names of coefficients to extract. By default, all coefficients are ex-
tracted.
groups Optional names of grouping variables for which to extract effects.
... Currently ignored.
Value
A list of 3D arrays (one per grouping factor). If summary is TRUE, the 1st dimension contains the
factor levels, the 2nd dimension contains the summary statistics (see posterior_summary), and the
3rd dimension contains the group-level effects. If summary is FALSE, the 1st dimension contains the
posterior draws, the 2nd dimension contains the factor levels, and the 3rd dimension contains the
group-level effects.
reloo.brmsfit 173
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
data = epilepsy, family = gaussian(), chains = 2)
ranef(fit)
## End(Not run)
Description
Compute exact cross-validation for problematic observations for which approximate leave-one-out
cross-validation may return incorrect results. Models for problematic observations can be run in
parallel using the future package.
Usage
## S3 method for class 'brmsfit'
reloo(
x,
loo,
k_threshold = 0.7,
newdata = NULL,
resp = NULL,
check = TRUE,
...
)
reloo(x, ...)
Arguments
x An R object of class brmsfit or loo depending on the method.
loo An R object of class loo.
k_threshold The threshold at which pareto k estimates are treated as problematic. Defaults
to 0.7. See pareto_k_ids for more details.
newdata An optional data.frame for which to evaluate predictions. If NULL (default), the
original data of the model is used. NA values within factors 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.
174 rename_pars
Details
Warnings about Pareto k estimates indicate observations for which the approximation to LOO is
problematic (this is described in detail in Vehtari, Gelman, and Gabry (2017) and the loo package
documentation). If there are J observations with k estimates above k_threshold, then reloo will
refit the original model J times, each time leaving out one of the J problematic observations. The
pointwise contributions of these observations to the total ELPD are then computed directly and
substituted for the previous estimates from these J observations that are stored in the original loo
object.
Value
An object of the class loo.
See Also
loo, kfold
Examples
## Not run:
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
data = epilepsy, family = poisson())
# throws warning about some pareto k estimates being too high
(loo1 <- loo(fit1))
(reloo1 <- reloo(fit1, loo = loo1, chains = 1))
## End(Not run)
Description
Rename parameters within the stanfit object after model fitting to ensure reasonable parameter
names. This function is usally called automatically by brm and users will rarely be required to call
it themselves.
residuals.brmsfit 175
Usage
rename_pars(x)
Arguments
x A brmsfit object.
Value
A brmfit object with adjusted parameter names.
Examples
## Not run:
# fit a model manually via rstan
scode <- make_stancode(count ~ Trt, data = epilepsy)
sdata <- make_standata(count ~ Trt, data = epilepsy)
stanfit <- rstan::stan(model_code = scode, data = sdata)
## End(Not run)
Description
This method is an alias of predictive_error.brmsfit with additional arguments for obtaining
summaries of the computed samples.
Usage
## S3 method for class 'brmsfit'
residuals(
object,
newdata = NULL,
re_formula = NULL,
method = "pp_expect",
type = c("ordinary", "pearson"),
resp = NULL,
nsamples = NULL,
subset = NULL,
176 residuals.brmsfit
sort = FALSE,
summary = TRUE,
robust = FALSE,
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
original data of the model is used. NA values within factors 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.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
method Method use to obtain predictions. Either "pp_expect" (the default) or "posterior_predict".
Using "posterior_predict" is recommended but "pp_expect" is the current
default for reasons of backwards compatibility.
type The type of the residuals, either "ordinary" or "pearson". More information
is provided under ’Details’.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
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.
subset A numeric vector specifying the posterior samples to be used. If NULL (the
default), all samples are used.
sort Logical. Only relevant for time series models. Indicating whether to return
predicted values in the original order (FALSE; default) or in the order of the time
series (TRUE).
summary Should summary statistics be returned instead of the raw values? Default is
TRUE..
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead. Only used if summary is
TRUE.
probs The percentiles to be computed by the quantile function. Only used if summary
is TRUE.
... Further arguments passed to prepare_predictions that control several aspects
of data validation and prediction.
Details
Residuals of type 'ordinary' are of the form R = Y − Y rep, where Y is the observed and Y rep
is the predicted response. Residuals of type pearson are of the form R = (Y − Y rep)/SD(Y ),
where SD(Y ) is an estimation of the standard deviation of Y .
restructure 177
Value
An array of predictive error/residual samples. If summary = FALSE the output resembles those of
predictive_error.brmsfit. If summary = TRUE the output is an N x E matrix, where N is the
number of observations and E denotes the summary statistics computed from the samples.
Examples
## Not run:
## fit a model
fit <- brm(rating ~ treat + period + carry + (1|subject),
data = inhaler, cores = 2)
## End(Not run)
Description
Restructure old brmsfit objects to work with the latest brms version. This function is called
internally when applying post-processing methods. However, in order to avoid unnecessary run
time caused by the restructuring, I recommend explicitly calling restructure once per model after
updating brms.
Usage
Arguments
Value
Description
Convert information in rows to labels for each row.
Usage
rows2labels(x, digits = 2, sep = " & ", incl_vars = TRUE, ...)
Arguments
x A data.frame for which to extract labels.
digits Minimal number of decimal places shown in the labels of numeric variables.
sep A single character string defining the separator between variables used in the
labels.
incl_vars Indicates if variable names should be part of the labels. Defaults to TRUE.
... Currently unused.
Value
A character vector of the same length as the number of rows of x.
See Also
make_conditions, conditional_effects
Description
Functions used in definition of smooth terms within a model formulas. The function does not
evaluate a (spline) smooth - it exists purely to help set up a model using spline based smooths.
Usage
s(...)
t2(...)
Arguments
... Arguments passed to mgcv::s or mgcv::t2.
sar 179
Details
The function defined here are just simple wrappers of the respective functions of the mgcv package.
See Also
brmsformula, mgcv::s, mgcv::t2
Examples
## Not run:
# simulate some data
dat <- mgcv::gamSim(1, n = 200, scale = 2)
## End(Not run)
Description
Set up an spatial simultaneous autoregressive (SAR) term in brms. The function does not evaluate
its arguments – it exists purely to help set up a model with SAR terms.
Usage
sar(M, type = "lag")
Arguments
M An object specifying the spatial weighting matrix. Can be either the spatial
weight matrix itself or an object of class listw or nb, from which the spatial
weighting matrix can be computed.
type Type of the SAR structure. Either "lag" (for SAR of the response values) or
"error" (for SAR of the residuals). More information is provided in the ’De-
tails’ section.
180 set_prior
Details
The lagsar structure implements SAR of the response values:
y = ρW y + η + e
y = η + u, u = ρW u + e
In the above equations, η is the predictor term and e are independent normally or t-distributed
residuals. Currently, only families gaussian and student support SAR structures.
Value
An object of class 'sar_term', which is a list of arguments to be interpreted by the formula parsing
functions of brms.
See Also
autocor-terms
Examples
## Not run:
data(oldcol, package = "spdep")
fit1 <- brm(CRIME ~ INC + HOVAL + sar(COL.nb, type = "lag"),
data = COL.OLD, data2 = list(COL.nb = COL.nb),
chains = 2, cores = 2)
summary(fit1)
plot(fit1)
## End(Not run)
Description
Define priors for specific parameters or classes of parameters.
set_prior 181
Usage
set_prior(
prior,
class = "b",
coef = "",
group = "",
resp = "",
dpar = "",
nlpar = "",
lb = NA,
ub = NA,
check = TRUE
)
prior(prior, ...)
prior_(prior, ...)
prior_string(prior, ...)
empty_prior()
Arguments
prior A character string defining a distribution in Stan language
class The parameter class. Defaults to "b" (i.e. population-level effects). See ’De-
tails’ for other valid parameter classes.
coef Name of the coefficient within the parameter class.
group Grouping factor for group-level parameters.
resp Name of the response variable. Only used in multivariate models.
dpar Name of a distributional parameter. Only used in distributional models.
nlpar Name of a non-linear parameter. Only used in non-linear models.
lb Lower bound for parameter restriction. Currently only allowed for classes "b".
Defaults to NULL, that is no restriction.
ub Upper bound for parameter restriction. Currently only allowed for classes "b".
Defaults to NULL, that is no restriction.
check Logical; Indicates whether priors should be checked for validity (as far as pos-
sible). Defaults to TRUE. If FALSE, prior is passed to the Stan code as is, and all
other arguments are ignored.
... Arguments passed to set_prior.
Details
set_prior is used to define prior distributions for parameters in brms models. The functions
prior, prior_, and prior_string are aliases of set_prior each allowing for a different kind
182 set_prior
When defining priors on group-level parameters in non-linear models, please make sure to spec-
ify the corresponding non-linear parameter through the nlpar argument in the same way as for
population-level effects.
3. Correlations of group-level (’random’) effects
If there is more than one group-level effect per grouping factor, the correlations between those ef-
fects have to be estimated. The prior lkj_corr_cholesky(eta) or in short lkj(eta) with eta >
0 is essentially the only prior for (Cholesky factors) of correlation matrices. If eta = 1 (the default)
all correlations matrices are equally likely a priori. If eta > 1, extreme correlations become less
likely, whereas 0 < eta < 1 results in higher probabilities for extreme correlations. Correlation ma-
trix parameters in brms models are named as cor_<group>, (e.g., cor_g if g is the grouping factor).
To set the same prior on every correlation matrix, use for instance set_prior("lkj(2)",class =
"cor"). Internally, the priors are transformed to be put on the Cholesky factors of the correlation
matrices to improve efficiency and numerical stability. The corresponding parameter class of the
Cholesky factors is L, but it is not recommended to specify priors for this parameter class directly.
4. Splines
Splines are implemented in brms using the ’random effects’ formulation as explained in gamm).
Thus, each spline has its corresponding standard deviations modeling the variability within this
term. In brms, this parameter class is called sds and priors can be specified via set_prior("<prior>",class
184 set_prior
= "sds",coef = "<term label>"). The default prior is the same as for standard deviations of
group-level effects.
5. Gaussian processes
Gaussian processes as currently implemented in brms have two parameters, the standard deviation
parameter sdgp, and characteristic length-scale parameter lscale (see gp for more details). The
default prior of sdgp is the same as for standard deviations of group-level effects. The default prior
of lscale is an informative inverse-gamma prior specifically tuned to the covariates of the Gaus-
sian process (for more details see https://betanalpha.github.io/assets/case_studies/gp_
part3/part3.html). This tuned prior may be overly informative in some cases, so please consider
other priors as well to make sure inference is robust to the prior specification. If tuning fails, a
half-normal prior is used instead.
6. Autocorrelation parameters
The autocorrelation parameters currently implemented are named ar (autoregression), ma (moving
average), arr (autoregression of the response), car (spatial conditional autoregression), as well as
lagsar and errorsar (Spatial simultaneous autoregression).
Priors can be defined by set_prior("<prior>",class = "ar") for ar and similar for other auto-
correlation parameters. By default, ar and ma are bounded between -1 and 1, car, lagsar, and
errorsar are bounded between 0, and 1, and arr is unbounded (you may change this by using the
arguments lb and ub). The default prior is flat over the definition area.
7. Distance parameters of monotonic effects
As explained in the details section of brm, monotonic effects make use of a special parameter vector
to estimate the ’normalized distances’ between consecutive predictor categories. This is realized in
Stan using the simplex parameter type. This class is named "simo" (short for simplex monotonic)
in brms. The only valid prior for simplex parameters is the dirichlet prior, which accepts a vector
of length K -1 (K = number of predictor categories) as input defining the ’concentration’ of the
distribution. Explaining the dirichlet prior is beyond the scope of this documentation, but we want
to describe how to define this prior syntactically correct. If a predictor x with K categories is modeled
as monotonic, we can define a prior on its corresponding simplex via
prior(dirichlet(<vector>),class = simo,coef = mox1). The 1 in the end of coef indicates
that this is the first simplex in this term. If interactions between multiple monotonic variables are
modeled, multiple simplexes per term are required. For <vector>, we can put in any R expression
defining a vector of length K -1. The default is a uniform prior (i.e. <vector> = rep(1,K-1)) over
all simplexes of the respective dimension.
8. Parameters for specific families
Some families need additional parameters to be estimated. Families gaussian, student, skew_normal,
lognormal, and gen_extreme_value need the parameter sigma to account for the residual standard
deviation. By default, sigma has a half student-t prior that scales in the same way as the group-level
standard deviations. Further, family student needs the parameter nu representing the degrees of
freedom of students-t distribution. By default, nu has prior gamma(2,0.1) and a fixed lower bound
of 1. Families gamma, weibull, inverse.gaussian, and negbinomial need a shape parameter
that has a gamma(0.01,0.01) prior by default. For families cumulative, cratio, sratio, and
acat, and only if threshold = "equidistant", the parameter delta is used to model the distance
between two adjacent thresholds. By default, delta has an improper flat prior over the reals. The
von_mises family needs the parameter kappa, representing the concentration parameter. By de-
fault, kappa has prior gamma(2,0.01).
Every family specific parameter has its own prior class, so that set_prior("<prior>",class =
set_prior 185
"<parameter>") is the right way to go. All of these priors are chosen to be weakly informative,
having only minimal influence on the estimations, while improving convergence and sampling effi-
ciency.
Fixing parameters to constants is possible by using the constant function, for example, constant(1)
to fix a parameter to 1. Broadcasting to vectors and matrices is done automatically. A limitation
of the current implementation is that the same parameter vector cannot contain estimated and fixed
values at the same time, but this will be possible in the future.
Often, it may not be immediately clear, which parameters are present in the model. To get a full list
of parameters and parameter classes for which priors can be specified (depending on the model) use
function get_prior.
Value
An object of class brmsprior to be used in the prior argument of brm.
Functions
• prior: Alias of set_prior allowing to specify arguments as expressions without quotation
marks.
• prior_: Alias of set_prior allowing to specify arguments as as one-sided formulas or
wrapped in quote.
• prior_string: Alias of set_prior allowing to specify arguments as strings.
• empty_prior: Create an empty brmsprior object.
References
Gelman A. (2006). Prior distributions for variance parameters in hierarchical models. Bayesian
analysis, 1(3), 515 – 534.
See Also
get_prior
Examples
## use alias functions
(prior1 <- prior(cauchy(0, 1), class = sd))
(prior2 <- prior_(~cauchy(0, 1), class = ~sd))
(prior3 <- prior_string("cauchy(0, 1)", class = "sd"))
identical(prior1, prior2)
identical(prior1, prior3)
# verify that the priors indeed found their way into Stan's model code
make_stancode(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = cumulative(),
prior = bprior)
Description
Density, distribution function, quantile function and random generation for the shifted log normal
distribution with mean meanlog, standard deviation sdlog, and shift parameter shift.
Usage
dshifted_lnorm(x, meanlog = 0, sdlog = 1, shift = 0, log = FALSE)
pshifted_lnorm(
q,
meanlog = 0,
sdlog = 1,
shift = 0,
lower.tail = TRUE,
log.p = FALSE
)
qshifted_lnorm(
p,
meanlog = 0,
SkewNormal 187
sdlog = 1,
shift = 0,
lower.tail = TRUE,
log.p = FALSE
)
Arguments
x, q Vector of quantiles.
meanlog Vector of means.
sdlog Vector of standard deviations.
shift Vector of shifts.
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
p Vector of probabilities.
n Number of samples to draw from the distribution.
Details
See vignette("brms_families") for details on the parameterization.
Description
Density, distribution function, and random generation for the skew-normal distribution with mean
mu, standard deviation sigma, and skewness alpha.
Usage
dskew_normal(
x,
mu = 0,
sigma = 1,
alpha = 0,
xi = NULL,
omega = NULL,
log = FALSE
)
pskew_normal(
188 SkewNormal
q,
mu = 0,
sigma = 1,
alpha = 0,
xi = NULL,
omega = NULL,
lower.tail = TRUE,
log.p = FALSE
)
qskew_normal(
p,
mu = 0,
sigma = 1,
alpha = 0,
xi = NULL,
omega = NULL,
lower.tail = TRUE,
log.p = FALSE,
tol = 1e-08
)
Arguments
x, q Vector of quantiles.
mu Vector of mean values.
sigma Vector of standard deviation values.
alpha Vector of skewness values.
xi Optional vector of location values. If NULL (the default), will be computed inter-
nally.
omega Optional vector of scale values. If NULL (the default), will be computed inter-
nally.
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
p Vector of probabilities.
tol Tolerance of the approximation used in the computation of quantiles.
n Number of samples to draw from the distribution.
Details
See vignette("brms_families") for details on the parameterization.
stancode.brmsfit 189
Description
Extract Stan code that was used to specify the model.
Usage
## S3 method for class 'brmsfit'
stancode(object, version = TRUE, ...)
stancode(object, ...)
Arguments
object An object of class brmsfit.
version Logical; indicates if the first line containing the brms version number should be
included. Defaults to TRUE.
... Currently ignored.
Value
Stan model code for further processing.
Description
Extract all data that was used by Stan to fit the model.
Usage
## S3 method for class 'brmsfit'
standata(
object,
newdata = NULL,
re_formula = NULL,
newdata2 = NULL,
new_objects = NULL,
incl_autocor = TRUE,
...
)
standata(object, ...)
190 stanvar
Arguments
Value
Description
Prepare user-defined variables to be passed to one of Stan’s program blocks. This is primarily use-
ful for defining more complex priors, for refitting models without recompilation despite changing
priors, or for defining custom Stan functions.
Usage
stanvar(
x = NULL,
name = NULL,
scode = NULL,
block = "data",
position = "start"
)
StudentT 191
Arguments
x An R object containing data to be passed to Stan. Only required if block =
'data' and ignored otherwise.
name Optional character string providing the desired variable name of the object in x.
If NULL (the default) the variable name is directly infered from x.
scode Line of Stan code to define the variable in Stan language. If block = 'data',
the Stan code is inferred based on the class of x by default.
block Name of one of Stan’s program blocks in which the variable should be defined.
Can be 'data', 'tdata' (transformed data), 'parameters', 'tparameters'
(transformed parameters), 'model', 'genquant' (generated quantities) or 'functions'.
position Name of the position within the block where the Stan code should be placed.
Currently allowed are 'start' (the default) and 'end' of the block.
Value
An object of class stanvars.
Examples
bprior <- prior(normal(mean_intercept, 10), class = "Intercept")
stanvars <- stanvar(5, name = "mean_intercept")
make_stancode(count ~ Trt, epilepsy, prior = bprior,
stanvars = stanvars)
Description
Density, distribution function, quantile function and random generation for the Student-t distribution
with location mu, scale sigma, and degrees of freedom df.
192 summary.brmsfit
Usage
Arguments
x, q Vector of quantiles.
df Vector of degrees of freedom.
mu Vector of location values.
sigma Vector of scale values.
log, log.p Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
p Vector of probabilities.
n Number of samples to draw from the distribution.
Details
See Also
TDist
Description
Usage
Arguments
object An object of class brmsfit
priors Logical; Indicating if priors should be included in the summary. Default is
FALSE.
prob A value between 0 and 1 indicating the desired probability to be covered by the
uncertainty intervals. The default is 0.95.
mc_se Logical; Indicating if the uncertainty caused by the MCMC sampling should be
shown in the summary. Defaults to FALSE.
... Other potential arguments
Details
The convergence diagnostics Rhat, Bulk_ESS, and Tail_ESS are described in detail in Vehtari et
al. (2019).
References
Aki Vehtari, Andrew Gelman, Daniel Simpson, Bob Carpenter, and Paul-Christian Bürkner (2019).
Rank-normalization, folding, and localization: An improved R-hat for assessing convergence of
MCMC. *arXiv preprint* ‘arXiv:1903.08008‘.
Description
A black theme for ggplot graphics inspired by a blog post of Jon Lefcheck (https://jonlefcheck.
net/2013/03/11/black-theme-for-ggplot2-2/).
Usage
theme_black(base_size = 12, base_family = "")
Arguments
base_size base font size
base_family base font family
Details
When using theme_black in plots powered by the bayesplot package such as pp_check or stanplot,
I recommend using the "viridisC" color scheme (see examples).
Value
A theme object used in ggplot2 graphics.
194 update.brmsfit
Examples
## Not run:
# change default ggplot theme
ggplot2::theme_set(theme_black())
## End(Not run)
Description
This theme is imported from the bayesplot package. See theme_default for a complete documen-
tation.
Arguments
base_size base font size
base_family base font family
Value
A theme object used in ggplot2 graphics.
Description
This method allows to update an existing brmsfit object.
update.brmsfit_multiple 195
Usage
## S3 method for class 'brmsfit'
update(object, formula., newdata = NULL, recompile = NULL, ...)
Arguments
object An object of class brmsfit.
formula. Changes to the formula; for details see update.formula and brmsformula.
newdata Optional data.frame to update the model with new data. Data-dependent de-
fault priors will not be updated automatically.
recompile Logical, indicating whether the Stan model should be recompiled. If NULL (the
default), update tries to figure out internally, if recompilation is necessary. Set-
ting it to FALSE will cause all Stan code changing arguments to be ignored.
... Other arguments passed to brm.
Examples
## Not run:
fit1 <- brm(time | cens(censored) ~ age * sex + disease + (1|patient),
data = kidney, family = gaussian("log"))
summary(fit1)
## End(Not run)
update.brmsfit_multiple
Update brms models based on multiple data sets
Description
This method allows to update an existing brmsfit_multiple object.
196 update_adterms
Usage
## S3 method for class 'brmsfit_multiple'
update(object, formula., newdata = NULL, ...)
Arguments
object An object of class brmsfit_multiple.
formula. Changes to the formula; for details see update.formula and brmsformula.
newdata List of data.frames to update the model with new data. Currently required
even if the original data should be used.
... Other arguments passed to update.brmsfit and brm_multiple.
Examples
## Not run:
library(mice)
imp <- mice(nhanes2)
## End(Not run)
Description
Update additions terms used in formulas of brms. See addition-terms for details.
Usage
update_adterms(formula, adform, action = c("update", "replace"))
Arguments
formula Two-sided formula to be updated.
adform One-sided formula containing addition terms to update formula with.
action Indicates what should happen to the existing addition terms in formula. If
"update" (the default), old addition terms that have no corresponding term in
adform will be kept. If "replace", all old addition terms will be removed.
validate_newdata 197
Value
An object of class formula.
Examples
form <- y | trials(size) ~ x
update_adterms(form, ~ trials(10))
update_adterms(form, ~ weights(w))
update_adterms(form, ~ weights(w), action = "replace")
update_adterms(y ~ x, ~ trials(10))
Description
Validate new data passed to post-processing methods of brms. Unless you are a package developer,
you will rarely need to call validate_newdata directly.
Usage
validate_newdata(
newdata,
object,
re_formula = NULL,
allow_new_levels = FALSE,
resp = NULL,
check_response = TRUE,
incl_autocor = TRUE,
all_group_vars = NULL,
...
)
Arguments
newdata A data.frame containing new data to be validated.
object A brmsfit object.
re_formula formula containing group-level effects to be considered in the prediction. If
NULL (default), include all group-level effects; if NA, include no group-level ef-
fects.
allow_new_levels
A flag indicating if new levels of group-level effects are allowed (defaults to
FALSE). Only relevant if newdata is provided.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
198 VarCorr.brmsfit
Value
A validated 'data.frame' based on newdata.
Description
This function calculates the estimated standard deviations, correlations and covariances of the
group-level terms in a multilevel model of class brmsfit. For linear models, the residual stan-
dard deviations, correlations and covariances are also returned.
Usage
## S3 method for class 'brmsfit'
VarCorr(
x,
sigma = 1,
summary = TRUE,
robust = FALSE,
probs = c(0.025, 0.975),
...
)
Arguments
x An object of class brmsfit.
sigma Ignored (included for compatibility with VarCorr).
summary Should summary statistics be returned instead of the raw values? Default is
TRUE.
robust If FALSE (the default) the mean is used as the measure of central tendency and
the standard deviation as the measure of variability. If TRUE, the median and the
median absolute deviation (MAD) are applied instead. Only used if summary is
TRUE.
probs The percentiles to be computed by the quantile function. Only used if summary
is TRUE.
... Currently ignored.
vcov.brmsfit 199
Value
A list of lists (one per grouping factor), each with three elements: a matrix containing the standard
deviations, an array containing the correlation matrix, and an array containing the covariance matrix
with variances on the diagonal.
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
data = epilepsy, family = gaussian(), chains = 2)
VarCorr(fit)
## End(Not run)
Description
Get a point estimate of the covariance or correlation matrix of population-level parameters
Usage
## S3 method for class 'brmsfit'
vcov(object, correlation = FALSE, pars = NULL, ...)
Arguments
object An object of class brmsfit.
correlation Logical; if FALSE (the default), compute the covariance matrix, if TRUE, compute
the correlation matrix.
pars Optional names of coefficients to extract. By default, all coefficients are ex-
tracted.
... Currently ignored.
Details
Estimates are obtained by calculating the maximum likelihood covariances (correlations) of the
posterior samples.
Value
covariance or correlation matrix of population-level parameters
200 VonMises
Examples
## Not run:
fit <- brm(count ~ zAge + zBase * Trt + (1+Trt|visit),
data = epilepsy, family = gaussian(), chains = 2)
vcov(fit)
## End(Not run)
Description
Density, distribution function, and random generation for the von Mises distribution with location
mu, and precision kappa.
Usage
Arguments
x, q Vector of quantiles.
mu Vector of location values.
kappa Vector of precision values.
log Logical; If TRUE, values are returned on the log scale.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
acc Accuracy of numerical approximations.
n Number of samples to draw from the distribution.
Details
Description
Compute the widely applicable information criterion (WAIC) based on the posterior likelihood
using the loo package. For more details see waic.
Usage
## S3 method for class 'brmsfit'
waic(
x,
...,
compare = TRUE,
resp = NULL,
pointwise = FALSE,
model_names = NULL
)
Arguments
x A brmsfit object.
... More brmsfit objects or further arguments passed to the underlying post-processing
functions. In particular, see prepare_predictions for further supported argu-
ments.
compare A flag indicating if the information criteria of the models should be compared
to each other via loo_compare.
resp Optional names of response variables. If specified, predictions are performed
only for the specified response variables.
pointwise A flag indicating whether to compute the full log-likelihood matrix at once or
separately for each observation. The latter approach is usually considerably
slower but requires much less working memory. Accordingly, if one runs into
memory issues, pointwise = TRUE is the way to go.
model_names If NULL (the default) will use model names derived from deparsing the call. Oth-
erwise will use the passed values as model names.
Details
See loo_compare for details on model comparisons. For brmsfit objects, WAIC is an alias of waic.
Use method add_criterion to store information criteria in the fitted model object for later usage.
Value
If just one object is provided, an object of class loo. If multiple objects are provided, an object of
class loolist.
202 Wiener
References
Vehtari, A., Gelman, A., & Gabry J. (2016). Practical Bayesian model evaluation using leave-
one-out cross-validation and WAIC. In Statistics and Computing, doi:10.1007/s11222-016-9696-4.
arXiv preprint arXiv:1507.04544.
Gelman, A., Hwang, J., & Vehtari, A. (2014). Understanding predictive information criteria for
Bayesian models. Statistics and Computing, 24, 997-1016.
Watanabe, S. (2010). Asymptotic equivalence of Bayes cross validation and widely applicable
information criterion in singular learning theory. The Journal of Machine Learning Research, 11,
3571-3594.
Examples
## Not run:
# model with population-level effects only
fit1 <- brm(rating ~ treat + period + carry,
data = inhaler)
(waic1 <- waic(fit1))
## End(Not run)
Description
Density function and random generation for the Wiener diffusion model distribution with boundary
separation alpha, non-decision time tau, bias beta and drift rate delta.
Usage
Arguments
x Vector of quantiles.
alpha Boundary separation parameter.
tau Non-decision time parameter.
beta Bias parameter.
delta Drift rate parameter.
resp Response: "upper" or "lower". If no character vector, it is coerced to logical
where TRUE indicates "upper" and FALSE indicates "lower".
log Logical; If TRUE, values are returned on the log scale.
n Number of samples to draw from the distribution.
types Which types of responses to return? By default, return both the response times
"q" and the dichotomous responses "resp". If either "q" or "resp", return only
one of the two types.
Details
These are wrappers around functions of the RWiener package. See vignette("brms_families")
for details on the parameterization.
See Also
wienerdist
Description
Density and distribution functions for zero-inflated distributions.
Usage
dzero_inflated_poisson(x, lambda, zi, log = FALSE)
Arguments
x Vector of quantiles.
zi zero-inflation propability
log Logical; If TRUE, values are returned on the log scale.
q Vector of quantiles.
lower.tail Logical; If TRUE (default), return P(X <= x). Else, return P(X > x) .
log.p Logical; If TRUE, values are returned on the log scale.
mu, lambda location parameter
shape, shape1, shape2
shape parameter
size number of trials
prob probability of success on each trial
Details
The density of a zero-inflated distribution can be specified as follows. If x = 0 set f (x) = θ + (1 −
θ) ∗ g(0). Else set f (x) = (1 − θ) ∗ g(x), where g(x) is the density of the non-zero-inflated part.
Index
205
206 INDEX
t2, 36
t2 (s), 178
TDist, 192
theme, 46, 58, 142
theme_black, 193
theme_default, 46, 58, 142, 194, 194
update, 23, 50
update.brmsfit, 174, 194, 196
update.brmsfit_multiple, 195