-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixture.Rmd
92 lines (75 loc) · 2.93 KB
/
mixture.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
---
title: "``brms::mixture``"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(brms)
```
mixture Finite Mixture Families in brms
#### Description
Set up a finite mixture family for use in brms.
#### Usage
<pre><code>
mixture(..., flist = NULL, nmix = 1, order = NULL)
</code></pre>
#### 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 family 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, populationlevel intercepts of the mean parameters are ordered. 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.
#### Examples
```{r}
## 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)
)
```
```{r}
## fit a simple normal mixture model
mix <- mixture(gaussian, gaussian)
prior <- c(
prior(normal(0, 7), Intercept, dpar = mu1),
prior(normal(5, 7), Intercept, dpar = mu2)
)
```
```{r}
fit1 <- brm(bf(y ~ x + z), dat, family = mix,
prior = prior, chains = 2)
summary(fit1)
pp_check(fit1)
## use different predictors for the components
fit2 <- brm(bf(y ~ 1, mu1 ~ x, mu2 ~ z), dat, family = mix,
prior = prior, chains = 2)
summary(fit2)
```
```{r}
## fix the mixing proportions
fit3 <- brm(bf(y ~ x + z, theta1 = 1, theta2 = 2),
dat, family = mix, prior = prior,
inits = 0, chains = 2)
summary(fit3)
pp_check(fit3)
```
```{r}
## predict the mixing proportions
fit4 <- brm(bf(y ~ x + z, theta2 ~ x),
dat, family = mix, prior = prior,
inits = 0, chains = 2)
summary(fit4)
pp_check(fit4)
## compare model fit
LOO(fit1, fit2, fit3, fit4)
## End(Not run)
```
#### 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.
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.
#### Value
An object of class mixfamily.