brms/ 0000755 0001762 0000144 00000000000 13624533242 011221 5 ustar ligges users brms/NAMESPACE 0000644 0001762 0000144 00000036614 13623751614 012456 0 ustar ligges users # Generated by roxygen2: do not edit by hand
S3method("+",bform)
S3method("+",brmsprior)
S3method("+",stanvars)
S3method(.compute_point_estimate,brmsdraws)
S3method(.compute_point_estimate,mvbrmsdraws)
S3method(.ndraws,brmsdraws)
S3method(.ndraws,mvbrmsdraws)
S3method(.thin_draws,brmsdraws)
S3method(.thin_draws,mvbrmsdraws)
S3method(LOO,brmsfit)
S3method(VarCorr,brmsfit)
S3method(WAIC,brmsfit)
S3method(add_criterion,brmsfit)
S3method(add_ic,brmsfit)
S3method(as.array,brmsfit)
S3method(as.data.frame,brmsfit)
S3method(as.matrix,brmsfit)
S3method(as.mcmc,brmsfit)
S3method(autocor,brmsfit)
S3method(bayes_R2,brmsfit)
S3method(bayes_factor,brmsfit)
S3method(bridge_sampler,brmsfit)
S3method(c,brmsprior)
S3method(c,stanvars)
S3method(change_effects,brmsterms)
S3method(change_effects,btl)
S3method(change_effects,default)
S3method(change_effects,mvbrmsterms)
S3method(check_prior_special,brmsprior)
S3method(check_prior_special,brmsterms)
S3method(check_prior_special,btl)
S3method(check_prior_special,btnl)
S3method(check_prior_special,default)
S3method(check_prior_special,mvbrmsterms)
S3method(coef,brmsfit)
S3method(compute_xi,brmsdraws)
S3method(compute_xi,brmsfit)
S3method(compute_xi,mvbrmsdraws)
S3method(conditional_effects,brmsfit)
S3method(conditional_effects,brmsterms)
S3method(conditional_effects,mvbrmsterms)
S3method(conditional_smooths,brmsfit)
S3method(conditional_smooths,brmsterms)
S3method(conditional_smooths,btl)
S3method(conditional_smooths,default)
S3method(conditional_smooths,mvbrmsterms)
S3method(control_params,brmsfit)
S3method(data_predictor,brmsterms)
S3method(data_predictor,btl)
S3method(data_predictor,btnl)
S3method(data_predictor,mvbrmsterms)
S3method(data_response,brmsterms)
S3method(data_response,mvbrmsterms)
S3method(def_scale_prior,brmsterms)
S3method(def_scale_prior,mvbrmsterms)
S3method(dpar_family,default)
S3method(dpar_family,mixfamily)
S3method(duplicated,brmsprior)
S3method(exclude_pars,brmsfit)
S3method(exclude_pars,brmsterms)
S3method(exclude_pars,btl)
S3method(exclude_pars,default)
S3method(exclude_pars,mvbrmsterms)
S3method(exclude_terms,brmsfit)
S3method(exclude_terms,brmsformula)
S3method(exclude_terms,mvbrmsformula)
S3method(expose_functions,brmsfit)
S3method(extract_draws,brmsfit)
S3method(extract_draws,brmsterms)
S3method(extract_draws,btl)
S3method(extract_draws,btnl)
S3method(extract_draws,default)
S3method(extract_old_standata,brmsterms)
S3method(extract_old_standata,btl)
S3method(extract_old_standata,btnl)
S3method(extract_old_standata,default)
S3method(extract_old_standata,mvbrmsterms)
S3method(family,brmsfit)
S3method(family_bounds,brmsterms)
S3method(family_bounds,mvbrmsterms)
S3method(family_info,brmsfamily)
S3method(family_info,brmsfit)
S3method(family_info,brmsformula)
S3method(family_info,brmsterms)
S3method(family_info,btl)
S3method(family_info,btnl)
S3method(family_info,default)
S3method(family_info,family)
S3method(family_info,list)
S3method(family_info,mixfamily)
S3method(family_info,mvbrmsformula)
S3method(family_info,mvbrmsterms)
S3method(fitted,brmsfit)
S3method(fixef,brmsfit)
S3method(formula,brmsfit)
S3method(getCall,brmsfit)
S3method(get_ad_vars,brmsterms)
S3method(get_ad_vars,mvbrmsterms)
S3method(get_all_effects,brmsterms)
S3method(get_all_effects,btl)
S3method(get_all_effects,btnl)
S3method(get_all_effects,default)
S3method(get_all_effects,mvbrmsterms)
S3method(get_data2_autocor,brmsformula)
S3method(get_data2_autocor,mvbrmsformula)
S3method(get_effect,brmsfit)
S3method(get_effect,brmsformula)
S3method(get_effect,brmsterms)
S3method(get_effect,btl)
S3method(get_effect,btnl)
S3method(get_effect,default)
S3method(get_effect,mvbrmsformula)
S3method(get_effect,mvbrmsterms)
S3method(get_element,default)
S3method(get_element,mvbrmsformula)
S3method(get_element,mvbrmsterms)
S3method(get_group_vars,brmsfit)
S3method(get_group_vars,brmsterms)
S3method(get_group_vars,default)
S3method(get_group_vars,mvbrmsterms)
S3method(get_int_vars,brmsterms)
S3method(get_int_vars,mvbrmsterms)
S3method(get_re,brmsterms)
S3method(get_re,btl)
S3method(get_re,default)
S3method(get_re,mvbrmsterms)
S3method(hypothesis,brmsfit)
S3method(hypothesis,default)
S3method(kfold,brmsfit)
S3method(launch_shinystan,brmsfit)
S3method(logLik,brmsfit)
S3method(log_lik,brmsdraws)
S3method(log_lik,brmsfit)
S3method(log_lik,mvbrmsdraws)
S3method(log_posterior,brmsfit)
S3method(loo,brmsfit)
S3method(loo_R2,brmsfit)
S3method(loo_compare,brmsfit)
S3method(loo_linpred,brmsfit)
S3method(loo_model_weights,brmsfit)
S3method(loo_predict,brmsfit)
S3method(loo_predictive_interval,brmsfit)
S3method(loo_subsample,brmsfit)
S3method(marginal_effects,brmsfit)
S3method(marginal_smooths,brmsfit)
S3method(mcmc_plot,brmsfit)
S3method(model.frame,brmsfit)
S3method(model_weights,brmsfit)
S3method(neff_ratio,brmsfit)
S3method(ngrps,brmsfit)
S3method(nobs,brmsfit)
S3method(nsamples,brmsfit)
S3method(nuts_params,brmsfit)
S3method(pairs,brmsfit)
S3method(parnames,brmsfit)
S3method(parnames,default)
S3method(parse_bf,brmsformula)
S3method(parse_bf,default)
S3method(parse_bf,mvbrmsformula)
S3method(plot,brmsMarginalEffects)
S3method(plot,brms_conditional_effects)
S3method(plot,brmsfit)
S3method(plot,brmshypothesis)
S3method(post_prob,brmsfit)
S3method(posterior_average,brmsfit)
S3method(posterior_interval,brmsfit)
S3method(posterior_linpred,brmsfit)
S3method(posterior_predict,brmsdraws)
S3method(posterior_predict,brmsfit)
S3method(posterior_predict,mvbrmsdraws)
S3method(posterior_samples,brmsfit)
S3method(posterior_samples,default)
S3method(posterior_summary,brmsfit)
S3method(posterior_summary,default)
S3method(pp_average,brmsfit)
S3method(pp_check,brmsfit)
S3method(pp_expect,brmsdraws)
S3method(pp_expect,brmsfit)
S3method(pp_expect,mvbrmsdraws)
S3method(pp_mixture,brmsfit)
S3method(predict,brmsfit)
S3method(predictive_error,brmsfit)
S3method(predictive_interval,brmsfit)
S3method(predictor,bdrawsl)
S3method(predictor,bdrawsnl)
S3method(print,brmsMarginalEffects)
S3method(print,brms_conditional_effects)
S3method(print,brmsfamily)
S3method(print,brmsfit)
S3method(print,brmsformula)
S3method(print,brmshypothesis)
S3method(print,brmsmodel)
S3method(print,brmsprior)
S3method(print,brmssummary)
S3method(print,cor_arma)
S3method(print,cor_brms_formula)
S3method(print,cor_car)
S3method(print,cor_cosy)
S3method(print,cor_empty)
S3method(print,cor_fixed)
S3method(print,cor_sar)
S3method(print,cov_fixed)
S3method(print,customfamily)
S3method(print,iclist)
S3method(print,loolist)
S3method(print,mixfamily)
S3method(print,mvbrmsformula)
S3method(prior_predictor,btl)
S3method(prior_predictor,btnl)
S3method(prior_predictor,default)
S3method(prior_samples,brmsfit)
S3method(prior_samples,default)
S3method(prior_summary,brmsfit)
S3method(ranef,brmsfit)
S3method(reloo,brmsfit)
S3method(reloo,loo)
S3method(rescale_old_mo,brmsfit)
S3method(rescale_old_mo,brmsterms)
S3method(rescale_old_mo,btl)
S3method(rescale_old_mo,btnl)
S3method(rescale_old_mo,mvbrmsterms)
S3method(residuals,brmsfit)
S3method(rhat,brmsfit)
S3method(stan_llh,brmsterms)
S3method(stan_llh,default)
S3method(stan_llh,mixfamily)
S3method(stan_llh,mvbrmsterms)
S3method(stan_predictor,brmsterms)
S3method(stan_predictor,btl)
S3method(stan_predictor,btnl)
S3method(stan_predictor,mvbrmsterms)
S3method(stancode,brmsfit)
S3method(standata,brmsfit)
S3method(stanplot,brmsfit)
S3method(summarise_families,brmsformula)
S3method(summarise_families,mvbrmsformula)
S3method(summarise_links,brmsformula)
S3method(summarise_links,mvbrmsformula)
S3method(summary,brmsfit)
S3method(summary,customfamily)
S3method(summary,family)
S3method(summary,mixfamily)
S3method(tidy_acef,"NULL")
S3method(tidy_acef,acef)
S3method(tidy_acef,brmsterms)
S3method(tidy_acef,btl)
S3method(tidy_acef,btnl)
S3method(tidy_acef,default)
S3method(tidy_acef,mvbrmsterms)
S3method(trunc_bounds,brmsterms)
S3method(trunc_bounds,mvbrmsterms)
S3method(update,brmsfit)
S3method(update,brmsfit_multiple)
S3method(update,brmsformula)
S3method(update,mvbrmsformula)
S3method(update_old_family,brmsfamily)
S3method(update_old_family,brmsformula)
S3method(update_old_family,customfamily)
S3method(update_old_family,default)
S3method(update_old_family,mixfamily)
S3method(update_old_family,mvbrmsformula)
S3method(update_re_terms,brmsformula)
S3method(update_re_terms,formula)
S3method(update_re_terms,mvbrmsformula)
S3method(valid_dpars,brmsfit)
S3method(valid_dpars,brmsformula)
S3method(valid_dpars,brmsterms)
S3method(valid_dpars,default)
S3method(valid_dpars,mixfamily)
S3method(valid_dpars,mvbrmsformula)
S3method(valid_dpars,mvbrmsterms)
S3method(validate_formula,brmsformula)
S3method(validate_formula,default)
S3method(validate_formula,mvbrmsformula)
S3method(vars_keep_na,brmsterms)
S3method(vars_keep_na,mvbrmsterms)
S3method(vcov,brmsfit)
S3method(waic,brmsfit)
export("add_ic<-")
export(Beta)
export(LOO)
export(VarCorr)
export(WAIC)
export(acat)
export(acformula)
export(add_criterion)
export(add_ic)
export(add_loo)
export(add_waic)
export(ar)
export(arma)
export(as.mcmc)
export(asym_laplace)
export(autocor)
export(bayes_R2)
export(bayes_factor)
export(bernoulli)
export(bf)
export(bridge_sampler)
export(brm)
export(brm_multiple)
export(brmsfamily)
export(brmsformula)
export(car)
export(categorical)
export(combine_models)
export(compare_ic)
export(conditional_effects)
export(conditional_smooths)
export(control_params)
export(cor_ar)
export(cor_arma)
export(cor_arr)
export(cor_bsts)
export(cor_car)
export(cor_cosy)
export(cor_errorsar)
export(cor_fixed)
export(cor_icar)
export(cor_lagsar)
export(cor_ma)
export(cor_sar)
export(cosy)
export(cratio)
export(cs)
export(cse)
export(cumulative)
export(custom_family)
export(dasym_laplace)
export(data_predictor)
export(data_response)
export(ddirichlet)
export(density_ratio)
export(dexgaussian)
export(dfrechet)
export(dgen_extreme_value)
export(dhurdle_gamma)
export(dhurdle_lognormal)
export(dhurdle_negbinomial)
export(dhurdle_poisson)
export(dinv_gaussian)
export(dirichlet)
export(dmulti_normal)
export(dmulti_student_t)
export(do_call)
export(dshifted_lnorm)
export(dskew_normal)
export(dstudent_t)
export(dvon_mises)
export(dwiener)
export(dzero_inflated_beta)
export(dzero_inflated_binomial)
export(dzero_inflated_negbinomial)
export(dzero_inflated_poisson)
export(empty_prior)
export(exgaussian)
export(exponential)
export(expose_functions)
export(expp1)
export(extract_draws)
export(fcor)
export(fixef)
export(frechet)
export(gen_extreme_value)
export(geometric)
export(get_prior)
export(get_y)
export(gp)
export(gr)
export(horseshoe)
export(hurdle_gamma)
export(hurdle_lognormal)
export(hurdle_negbinomial)
export(hurdle_poisson)
export(hypothesis)
export(inv_logit_scaled)
export(is.brmsfit)
export(is.brmsfit_multiple)
export(is.brmsformula)
export(is.brmsprior)
export(is.brmsterms)
export(is.cor_arma)
export(is.cor_brms)
export(is.cor_car)
export(is.cor_cosy)
export(is.cor_fixed)
export(is.cor_sar)
export(is.mvbrmsformula)
export(is.mvbrmsterms)
export(kfold)
export(kfold_predict)
export(lasso)
export(launch_shinystan)
export(lf)
export(log_lik)
export(log_posterior)
export(logit_scaled)
export(logm1)
export(lognormal)
export(loo)
export(loo_R2)
export(loo_compare)
export(loo_linpred)
export(loo_model_weights)
export(loo_predict)
export(loo_predictive_interval)
export(loo_subsample)
export(ma)
export(make_conditions)
export(make_stancode)
export(make_standata)
export(marginal_effects)
export(marginal_smooths)
export(mcmc_plot)
export(me)
export(mi)
export(mixture)
export(mm)
export(mmc)
export(mo)
export(model_weights)
export(multinomial)
export(mvbf)
export(mvbind)
export(mvbrmsformula)
export(neff_ratio)
export(negbinomial)
export(ngrps)
export(nlf)
export(nsamples)
export(nuts_params)
export(parnames)
export(parse_bf)
export(pasym_laplace)
export(pexgaussian)
export(pfrechet)
export(pgen_extreme_value)
export(phurdle_gamma)
export(phurdle_lognormal)
export(phurdle_negbinomial)
export(phurdle_poisson)
export(pinv_gaussian)
export(post_prob)
export(posterior_average)
export(posterior_interval)
export(posterior_linpred)
export(posterior_predict)
export(posterior_samples)
export(posterior_summary)
export(posterior_table)
export(pp_average)
export(pp_check)
export(pp_expect)
export(pp_mixture)
export(predictive_error)
export(predictive_interval)
export(prior)
export(prior_)
export(prior_samples)
export(prior_string)
export(prior_summary)
export(pshifted_lnorm)
export(pskew_normal)
export(pstudent_t)
export(pvon_mises)
export(pzero_inflated_beta)
export(pzero_inflated_binomial)
export(pzero_inflated_negbinomial)
export(pzero_inflated_poisson)
export(qasym_laplace)
export(qfrechet)
export(qshifted_lnorm)
export(qskew_normal)
export(qstudent_t)
export(ranef)
export(rasym_laplace)
export(rdirichlet)
export(reloo)
export(resp_cat)
export(resp_cens)
export(resp_dec)
export(resp_mi)
export(resp_rate)
export(resp_se)
export(resp_subset)
export(resp_thres)
export(resp_trials)
export(resp_trunc)
export(resp_vint)
export(resp_vreal)
export(resp_weights)
export(restructure)
export(rexgaussian)
export(rfrechet)
export(rgen_extreme_value)
export(rhat)
export(rinv_gaussian)
export(rmulti_normal)
export(rmulti_student_t)
export(rows2labels)
export(rshifted_lnorm)
export(rskew_normal)
export(rstudent_t)
export(rvon_mises)
export(rwiener)
export(s)
export(sar)
export(set_mecor)
export(set_nl)
export(set_prior)
export(set_rescor)
export(shifted_lognormal)
export(skew_normal)
export(sratio)
export(stancode)
export(standata)
export(stanplot)
export(stanvar)
export(student)
export(t2)
export(theme_black)
export(theme_default)
export(update_adterms)
export(validate_newdata)
export(von_mises)
export(waic)
export(weibull)
export(wiener)
export(zero_inflated_beta)
export(zero_inflated_binomial)
export(zero_inflated_negbinomial)
export(zero_inflated_poisson)
export(zero_one_inflated_beta)
import(Rcpp)
import(abind)
import(ggplot2)
import(methods)
import(parallel)
import(stats)
importFrom(bayesplot,log_posterior)
importFrom(bayesplot,neff_ratio)
importFrom(bayesplot,nuts_params)
importFrom(bayesplot,pp_check)
importFrom(bayesplot,rhat)
importFrom(bayesplot,theme_default)
importFrom(bridgesampling,bayes_factor)
importFrom(bridgesampling,bridge_sampler)
importFrom(bridgesampling,post_prob)
importFrom(coda,as.mcmc)
importFrom(grDevices,devAskNewPage)
importFrom(graphics,plot)
importFrom(loo,.compute_point_estimate)
importFrom(loo,.ndraws)
importFrom(loo,.thin_draws)
importFrom(loo,is.loo)
importFrom(loo,kfold)
importFrom(loo,loo)
importFrom(loo,loo_compare)
importFrom(loo,loo_model_weights)
importFrom(loo,loo_subsample)
importFrom(loo,waic)
importFrom(nlme,VarCorr)
importFrom(nlme,fixef)
importFrom(nlme,ranef)
importFrom(rstantools,bayes_R2)
importFrom(rstantools,log_lik)
importFrom(rstantools,loo_linpred)
importFrom(rstantools,loo_predict)
importFrom(rstantools,loo_predictive_interval)
importFrom(rstantools,nsamples)
importFrom(rstantools,posterior_interval)
importFrom(rstantools,posterior_linpred)
importFrom(rstantools,posterior_predict)
importFrom(rstantools,predictive_error)
importFrom(rstantools,predictive_interval)
importFrom(rstantools,prior_summary)
importFrom(shinystan,launch_shinystan)
importMethodsFrom(rstan,summary)
brms/README.md 0000644 0001762 0000144 00000036235 13606573732 012521 0 ustar ligges users
[
](http://mc-stan.org)
brms
====
[](https://travis-ci.org/paul-buerkner/brms)
[](https://codecov.io/github/paul-buerkner/brms?branch=master)
[](https://cran.r-project.org/package=brms)
[](https://CRAN.R-project.org/package=brms)
Overview
--------
The **brms** package provides an interface to fit Bayesian generalized
(non-)linear multivariate multilevel models using Stan, which is a C++
package for performing full Bayesian inference (see
http://mc-stan.org/). The
formula syntax is very similar to that of the package lme4 to provide a
familiar and simple interface for performing regression analyses. A wide
range of response distributions are supported, allowing users to fit –
among others – linear, robust linear, count data, survival, response
times, ordinal, zero-inflated, and even self-defined mixture models all
in a multilevel context. Further modeling options include non-linear and
smooth terms, auto-correlation structures, censored data, missing value
imputation, and quite a few more. In addition, all parameters of the
response distribution can be predicted in order to perform
distributional regression. Multivariate models (i.e., models with
multiple response variables) can be fit, as well. Prior specifications
are flexible and explicitly encourage users to apply prior distributions
that actually reflect their beliefs. Model fit can easily be assessed
and compared with posterior predictive checks, cross-validation, and
Bayes factors.
Resources
---------
- [Introduction to
brms](https://www.jstatsoft.org/article/view/v080i01) (Journal of
Statistical Software)
- [Advanced multilevel modeling with
brms](https://journal.r-project.org/archive/2018/RJ-2018-017/index.html)
(The R Journal)
- [Website](https://paul-buerkner.github.io/brms) (Website of brms
with documentation and vignettes)
- [Blog posts](https://paul-buerkner.github.io/blog/brms-blogposts/)
(List of blog posts about brms)
- [Ask a question](http://discourse.mc-stan.org/) (Stan Forums on
Discourse)
- [Open an issue](https://github.com/paul-buerkner/brms/issues)
(GitHub issues for bug reports and feature requests)
How to use brms
---------------
``` r
library(brms)
```
As a simple example, we use poisson regression to model the seizure
counts in epileptic patients to investigate whether the treatment
(represented by variable `Trt`) can reduce the seizure counts and
whether the effect of the treatment varies with the (standardized)
baseline number of seizures a person had before treatment (variable
`zBase`). As we have multiple observations per person, a group-level
intercept is incorporated to account for the resulting dependency in the
data.
``` r
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
data = epilepsy, family = poisson())
```
The results (i.e., posterior samples) can be investigated using
``` r
summary(fit1)
#> Family: poisson
#> Links: mu = log
#> Formula: count ~ zAge + zBase * Trt + (1 | patient)
#> Data: epilepsy (Number of observations: 236)
#> Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
#> total post-warmup samples = 4000
#>
#> Group-Level Effects:
#> ~patient (Number of levels: 59)
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> sd(Intercept) 0.58 0.07 0.46 0.73 1.01 757 1812
#>
#> Population-Level Effects:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Intercept 1.77 0.12 1.54 2.01 1.01 719 946
#> zAge 0.10 0.08 -0.07 0.27 1.00 778 1318
#> zBase 0.71 0.12 0.47 0.95 1.01 593 1323
#> Trt1 -0.27 0.17 -0.61 0.05 1.01 756 1270
#> zBase:Trt1 0.05 0.16 -0.26 0.37 1.01 764 1131
#>
#> Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
#> is a crude measure of effective sample size, and Rhat is the potential
#> scale reduction factor on split chains (at convergence, Rhat = 1).
```
On the top of the output, some general information on the model is
given, such as family, formula, number of iterations and chains. Next,
group-level effects are displayed seperately for each grouping factor in
terms of standard deviations and (in case of more than one group-level
effect per grouping factor; not displayed here) correlations between
group-level effects. On the bottom of the output, population-level
effects (i.e. regression coefficients) are displayed. If incorporated,
autocorrelation effects and family specific parameters (e.g. the
residual standard deviation ‘sigma’ in normal models) are also given.
In general, every parameter is summarized using the mean (‘Estimate’)
and the standard deviation (‘Est.Error’) of the posterior distribution
as well as two-sided 95% credible intervals (‘l-95% CI’ and ‘u-95% CI’)
based on quantiles. We see that the coefficient of `Trt` is negative
with a zero overlapping 95%-CI. This indicates that, on average, the
treatment may reduce seizure counts by some amount but the evidence
based on the data and applied model is not very strong and still
insufficient by standard decision rules. Further, we find little
evidence that the treatment effect varies with the baseline number of
seizures.
The last two values (‘Eff.Sample’ and ‘Rhat’) provide information on how
well the algorithm could estimate the posterior distribution of this
parameter. If ‘Rhat’ is considerably greater than 1, the algorithm has
not yet converged and it is necessary to run more iterations and / or
set stronger priors.
To visually investigate the chains as well as the posterior
distributions, we can use the `plot` method. If we just want to see
results of the regression coefficients of `Trt` and `zBase`, we go for
``` r
plot(fit1, pars = c("Trt", "zBase"))
```
A more detailed investigation can be performed by running
`launch_shinystan(fit1)`. To better understand the relationship of the
predictors with the response, I recommend the `conditional_effects`
method:
``` r
plot(conditional_effects(fit1, effects = "zBase:Trt"))
```
This method uses some prediction functionality behind the scenes, which
can also be called directly. Suppose that we want to predict responses
(i.e. seizure counts) of a person in the treatment group (`Trt = 1`) and
in the control group (`Trt = 0`) with average age and average number of
previous seizures. Than we can use
``` r
newdata <- data.frame(Trt = c(0, 1), zAge = 0, zBase = 0)
predict(fit1, newdata = newdata, re_formula = NA)
#> Estimate Est.Error Q2.5 Q97.5
#> [1,] 5.92175 2.487403 2 11
#> [2,] 4.57475 2.168084 1 9
```
We need to set `re_formula = NA` in order not to condition of the
group-level effects. While the `predict` method returns predictions of
the responses, the `fitted` method returns predictions of the regression
line.
``` r
fitted(fit1, newdata = newdata, re_formula = NA)
#> Estimate Est.Error Q2.5 Q97.5
#> [1,] 5.932893 0.6985409 4.663774 7.447649
#> [2,] 4.545819 0.5293890 3.574081 5.647261
```
Both methods return the same estimate (up to random error), while the
latter has smaller variance, because the uncertainty in the regression
line is smaller than the uncertainty in each response. If we want to
predict values of the original data, we can just leave the `newdata`
argument empty.
Suppose, we want to investigate whether there is overdispersion in the
model, that is residual variation not accounted for by the response
distribution. For this purpose, we include a second group-level
intercept that captures possible overdispersion.
``` r
fit2 <- brm(count ~ zAge + zBase * Trt + (1|patient) + (1|obs),
data = epilepsy, family = poisson())
```
We can then go ahead and compare both models via approximate
leave-one-out (LOO) cross-validation.
``` r
loo(fit1, fit2)
#> Output of model 'fit1':
#>
#> Computed from 4000 by 236 log-likelihood matrix
#>
#> Estimate SE
#> elpd_loo -672.1 36.5
#> p_loo 94.5 14.2
#> looic 1344.2 73.1
#> ------
#> Monte Carlo SE of elpd_loo is NA.
#>
#> Pareto k diagnostic values:
#> Count Pct. Min. n_eff
#> (-Inf, 0.5] (good) 209 88.6% 368
#> (0.5, 0.7] (ok) 19 8.1% 128
#> (0.7, 1] (bad) 6 2.5% 35
#> (1, Inf) (very bad) 2 0.8% 5
#> See help('pareto-k-diagnostic') for details.
#>
#> Output of model 'fit2':
#>
#> Computed from 4000 by 236 log-likelihood matrix
#>
#> Estimate SE
#> elpd_loo -594.9 13.8
#> p_loo 107.5 6.9
#> looic 1189.8 27.5
#> ------
#> Monte Carlo SE of elpd_loo is NA.
#>
#> Pareto k diagnostic values:
#> Count Pct. Min. n_eff
#> (-Inf, 0.5] (good) 84 35.6% 675
#> (0.5, 0.7] (ok) 100 42.4% 209
#> (0.7, 1] (bad) 45 19.1% 42
#> (1, Inf) (very bad) 7 3.0% 11
#> See help('pareto-k-diagnostic') for details.
#>
#> Model comparisons:
#> elpd_diff se_diff
#> fit2 0.0 0.0
#> fit1 -77.2 27.1
```
The `loo` output when comparing models is a little verbose. We first see
the individual LOO summaries of the two models and then the comparison
between them. Since higher `elpd` (i.e., expected log posterior density)
values indicate better fit, we see that the model accounting for
overdispersion (i.e., `fit2`) fits substantially better. However, we
also see in the individual LOO outputs that there are several
problematic observations for which the approximations may have not have
been very accurate. To deal with this appropriately, we need to fall
back to other methods such as `reloo` or `kfold` but this requires the
model to be refit several times which takes too long for the purpose of
a quick example. The post-processing methods we have shown above are
just the tip of the iceberg. For a full list of methods to apply on
fitted model objects, type `methods(class = "brmsfit")`.
Citing brms and related software
--------------------------------
Developing and maintaining open source software is an important yet
often underappreciated contribution to scientific progress. Thus,
whenever you are using open source software (or software in general),
please make sure to cite it appropriately so that developers get credit
for their work.
When using brms, please cite one or more of the following publications:
- Bürkner P. C. (2017). brms: An R Package for Bayesian Multilevel
Models using Stan. *Journal of Statistical Software*. 80(1), 1-28.
doi.org/10.18637/jss.v080.i01
- Bürkner P. C. (2018). Advanced Bayesian Multilevel Modeling with the
R Package brms. *The R Journal*. 10(1), 395-411.
doi.org/10.32614/RJ-2018-017
As brms is a high-level interface to Stan, please additionally cite
Stan:
- Carpenter B., Gelman A., Hoffman M. D., Lee D., Goodrich B.,
Betancourt M., Brubaker M., Guo J., Li P., and Riddell A. (2017).
Stan: A probabilistic programming language. *Journal of Statistical
Software*. 76(1). 10.18637/jss.v076.i01
Further, brms relies on several other R packages and, of course, on R
itself. To find out how to cite R and its packages, use the `citation`
function. There are some features of brms which specifically rely on
certain packages. The **rstan** package together with **Rcpp** makes
Stan conveniently accessible in R. Visualizations and
posterior-predictive checks are based on **bayesplot** and **ggplot2**.
Approximate leave-one-out cross-validation using `loo` and related
methods is done via the **loo** package. Marginal likelihood based
methods such as `bayes_factor` are realized by means of the
**bridgesampling** package. Splines specified via the `s` and `t2`
functions rely on **mgcv**. If you use some of these features, please
also consider citing the related packages.
FAQ
---
### How do I install brms?
To install the latest release version from CRAN use
``` r
install.packages("brms")
```
The current developmental version can be downloaded from github via
``` r
if (!requireNamespace("remotes")) {
install.packages("remotes")
}
remotes::install_github("paul-buerkner/brms")
```
Because brms is based on Stan, a C++ compiler is required. The program
Rtools (available on
https://cran.r-project.org/bin/windows/Rtools/)
comes with a C++ compiler for Windows. On Mac, you should install Xcode.
For further instructions on how to get the compilers running, see the
prerequisites section on
https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started.
### I am new to brms. Where can I start?
Detailed instructions and case studies are given in the package’s
extensive vignettes. See `vignette(package = "brms")` for an overview.
For documentation on formula syntax, families, and prior distributions
see `help("brm")`.
### Where do I ask questions, propose a new feature, or report a bug?
Questions can be asked on the [Stan
forums](http://discourse.mc-stan.org/) on Discourse. To propose a new
feature or report a bug, please open an issue on
[GitHub](https://github.com/paul-buerkner/brms).
### How can I extract the generated Stan code?
If you have already fitted a model, just apply the `stancode` method on
the fitted model object. If you just want to generate the Stan code
without any model fitting, use the `make_stancode` function.
### Can I avoid compiling models?
When you fit your model for the first time with brms, there is currently
no way to avoid compilation. However, if you have already fitted your
model and want to run it again, for instance with more samples, you can
do this without recompilation by using the `update` method. For more
details see `help("update.brmsfit")`.
### What is the difference between brms and rstanarm?
The rstanarm package is similar to brms in that it also allows to fit
regression models using Stan for the backend estimation. Contrary to
brms, rstanarm comes with precompiled code to save the compilation time
(and the need for a C++ compiler) when fitting a model. However, as brms
generates its Stan code on the fly, it offers much more flexibility in
model specification than rstanarm. Also, multilevel models are currently
fitted a bit more efficiently in brms. For detailed comparisons of brms
with other common R packages implementing multilevel models, see
`vignette("brms_multilevel")` and `vignette("brms_overview")`.
brms/data/ 0000755 0001762 0000144 00000000000 13450600656 012133 5 ustar ligges users brms/data/epilepsy.rda 0000644 0001762 0000144 00000005427 13442002443 014454 0 ustar ligges users BZh91AY&SY