brms: Bayesian Multilevel Models using Stan Paul Brkner 2018-04-09 - - PowerPoint PPT Presentation

brms bayesian multilevel models using stan
SMART_READER_LITE
LIVE PREVIEW

brms: Bayesian Multilevel Models using Stan Paul Brkner 2018-04-09 - - PowerPoint PPT Presentation

brms: Bayesian Multilevel Models using Stan Paul Brkner 2018-04-09 1 Why using Multilevel Models? 2 Example: Effects of Sleep Deprivation on Reaction Times data ("sleepstudy", package = "lme4") head (sleepstudy, 10)


slide-1
SLIDE 1

brms: Bayesian Multilevel Models using Stan

Paul Bürkner 2018-04-09

1

slide-2
SLIDE 2

Why using Multilevel Models?

2

slide-3
SLIDE 3

Example: Effects of Sleep Deprivation on Reaction Times

data("sleepstudy", package = "lme4") head(sleepstudy, 10) Reaction Days Subject 249.5600 308 258.7047 1 308 250.8006 2 308 321.4398 3 308 356.8519 4 308 414.6901 5 308 382.2038 6 308 290.1486 7 308 430.5853 8 308 466.3535 9 308

3

slide-4
SLIDE 4

Linear Regression vs. Multilevel Regression

250 300 350 0 1 2 3 4 5 6 7 8 9

Days Reaction

250 300 350 0 1 2 3 4 5 6 7 8 9

Days Reaction

4

slide-5
SLIDE 5

Regression Lines for Specific Subjects

5

slide-6
SLIDE 6

Why going Bayesian?

6

slide-7
SLIDE 7

The Posterior Distribution

b_Days sd_Subject__Days

4 8 12 16 5 10 15

7

slide-8
SLIDE 8

Why using Stan?

8

slide-9
SLIDE 9

Multilevel Models in Classical Statistics with lme4

fit <- lmer(Reaction ~ 1 + Days + (1 + Days|Subject), data = sleepstudy)

9

slide-10
SLIDE 10

Multilevel Models in Bayesian Statistics with brms

fit <- brm(Reaction ~ 1 + Days + (1 + Days|Subject), data = sleepstudy)

10

slide-11
SLIDE 11

Post-Processing

methods(class = "brmsfit") ## [1] add_ic add_loo add_waic as.array ## [5] as.data.frame as.matrix as.mcmc coef ## [9] control_params expose_functions family fitted ## [13] fixef formula hypothesis kfold ## [17] launch_shiny log_lik log_posterior logLik ## [21] loo LOO loo_linpred loo_predict ## [25] loo_predictive_interval marginal_effects marginal_smooths model.frame ## [29] neff_ratio ngrps nobs nsamples ## [33] nuts_params pairs parnames plot ## [37] posterior_predict posterior_samples pp_check pp_mixture ## [41] predict predictive_error print prior_samples ## [45] prior_summary ranef reloo residuals ## [49] rhat stancode standata stanplot ## [53] summary update VarCorr vcov ## [57] waic WAIC ## see '?methods' for accessing help and source code

11

slide-12
SLIDE 12

The idea of brms: Fitting all kinds of regression models within one framework

12

slide-13
SLIDE 13

Example: Censored Recurrance Times of Kidney Infections

fitk <- brm(time | cens(censored) ~ age * sex + (1|patient), data = kidney, family = weibull()) marginal_effects(fitk, "age:sex")

200 400 600 20 40 60

age time | cens(censored) sex

male female

13

slide-14
SLIDE 14

Example: Complex Non-Linear Relationships

−2 −1 1 −2 −1 1 2

X y

Latent mean function Realized data

14

slide-15
SLIDE 15

Modeling Non-Linear Relationships with Gaussian Processes

fitgp <- brm(y ~ gp(x), bdata) marginal_effects(fitgp, nsamples = 100, spaghetti = TRUE)

−4 −2 2 −2 −1 1 2

x y

15

slide-16
SLIDE 16

Modeling Non-Linear Relationships with Splines

fits <- brm(y ~ s(x), bdata) marginal_effects(fits, nsamples = 100, spaghetti = TRUE)

−4 −2 2 −2 −1 1 2

x y

16

slide-17
SLIDE 17

Example: Number of Fish Caught at a Camping Place

50 100 5 10 15 20 25

nfish count

17

slide-18
SLIDE 18

Modeling Zero-Inflation

form <- bf(nfish ~ persons + child + camper, zi ~ child) fit_zinb <- brm(form, zinb, zero_inflated_poisson()) marginal_effects(fit_zinb, effects = "child")

1 2 3 4 1 2 3

child nfish

18

slide-19
SLIDE 19

Learn More about brms and Stan

  • Help within R: help("brms")
  • Overview of vignettes: vignette(package = "brms")
  • List of all methods: methods(class = "brmsfit")
  • Website of brms: https://github.com/paul-buerkner/brms
  • Website of Stan: http://mc-stan.org/
  • Contact me: paul.buerkner@gmail.com
  • Twitter: @paulbuerkner

19