-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_prior.Rmd
107 lines (88 loc) · 3.43 KB
/
set_prior.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
title: "``brms::set_prior``"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(brms)
```
Prior Definitions for brms Models
#### Description
Define priors for specific parameters or classes of parameters
#### Usage
<pre><code>
set_prior(prior, class = "b", coef = "", group = "", resp = "",
dpar = "", nlpar = "", lb = NA, ub = NA, check = TRUE)
prior(prior, ...)
prior_(prior, ...)
prior_string(prior, ...)
</code></pre>
#### Arguments
* ``prior``: A character string defining a distribution in Stan language
* ``class``: The parameter class. Defaults to "b" (i.e. population-level effects). See ’ Details’ for other valid parameter classes.
* ``coef``: Name of the (population- or group-level) parameter.
* ``group``: Grouping factor of group-level parameters.
* resp Name of the response variable / category. 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",
"ar", "ma", and "arr". Defaults to NULL, that is no restriction.
* ub Upper bound for parameter restriction. Currently only allowed for classes "b",
"ar", "ma", and "arr". Defaults to NULL, that is no restriction.
* ``check`` Logical; Indicates whether priors should be checked for validity (as far as possible). 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.
#### 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.
#### Examples
```{r}
## 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)
```
```{r}
## check which parameters can have priors
get_prior(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = cumulative())
```
```{r}
## define some priors
prior <- c(prior_string("normal(0,10)", class = "b"),
prior(normal(1,2), class = b, coef = treat),
prior_(~cauchy(0,2), class = ~sd,
group = ~subject, coef = ~Intercept))
```
```{r}
## 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 = prior)
```
```{r}
## use the horseshoe prior to model sparsity in population-level effects
make_stancode(count ~ log_Age_c + log_Base4_c * Trt_c,
data = epilepsy, family = poisson(),
prior = set_prior("horseshoe(3)"))
```
```{r}
## alternatively use the lasso prior
make_stancode(count ~ log_Age_c + log_Base4_c * Trt_c,
data = epilepsy, family = poisson(),
prior = set_prior("lasso(1)"))
```
```{r}
## pass priors to Stan without checking
prior <- prior_string("target += normal_lpdf(b[1] | 0, 1)", check = FALSE)
make_stancode(count ~ Trt_c, data = epilepsy, prior = prior)
```
#### References
Gelman A. (2006). Prior distributions for variance parameters in hierarchical models. Bayesian
analysis, 1(3), 515 – 534.
See Also
get_prior