-
Notifications
You must be signed in to change notification settings - Fork 23
/
prior.Rmd
76 lines (50 loc) · 4.81 KB
/
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
---
title: "Specifying Prior Distributions"
bibliography:
- refs.bib
---
```{r echo=FALSE, message=FALSE}
library(blavaan, quietly=TRUE)
library(lavaan, quietly=TRUE)
```
There are two ways to specify prior distributions in blavaan. First, each type of model parameter has a default prior distribution that may or may not be suitable for your specific situation. You are free to modify the defaults. Second, the priors for individual model parameters can be specified in the model syntax. Each is discussed below.
### Defaults
The default priors can be seen via
```{r}
dpriors()
```
It is important to note that these prior distributions correspond to Stan parameterizations. These are similar to R parameterizations but not necessarily exactly the same. The Greek(ish) names above correspond to the following parameter types (where MV is manifest/observed variable and LV is latent variable):
```{r, echo=FALSE}
pnames <- dpriors()
pdesc <- c("MV intercept", "LV intercept", "Loading", "Regression", "MV precision", "LV precision", "Correlation", "Covariance matrix", "Threshold")
pnames[1:9] <- pdesc
pnames
```
For further information about priors on thresholds, see the [ordinal modeling details](ordinal.html).
For `target = "stan"` (the default), priors are currently restricted to one distribution per parameter type. You can change the prior distribution parameters (for example, the mean and standard deviation of a normal), but you cannot change the prior distribution type. The only exceptions here are the "theta" and "psi" parameters: for those, you can use the modifiers "[sd]", "[var]", or "[prec]" to specify whether you want the priors to apply to the standard deviation, variance, or precision. If you require more flexibility in prior specification, you change the target to either `"stanclassic"` (the old Stan approach) or `"jags"` (the JAGS approach). Alternatively, you can export the Stan model via `mcmcfile = TRUE`, edit the file as needed, then fit it via the rstan package.
To modify prior distributions, we could simply supply a new text string to `dpriors()` like this:
```{r}
mydp <- dpriors(lambda="normal(1,2)")
mydp
```
so that the default prior for loadings is now normal with mean 1 and standard deviation 2, and the rest of the parameters remain at the original defaults. The next time we estimate a model (via `bsem()`, `bcfa()`, `bgrowth()`, or `blavaan()`), we would add the argument `dp=mydp` to use this new set of default priors.
### Individual Parameters
In addition to setting the prior for one type of model parameter, the user may wish to set the prior of a specific model parameter. This is accomplished by using the `prior()` modifier within the model specification. For example, consider the following syntax for the @holswi39 confirmatory factor model:
```{r eval=FALSE}
HS.model <- ' visual =~ x1 prior("normal(1,2)")*x2 x3
textual =~ x4 x5 prior("normal(3,1.5)")*x6
speed =~ x7 x8 x9
x1 ~~ prior("gamma(3,3)[sd]")*x1 '
```
The loading from `visual` to `x2` now has a normal prior with mean 1 and standard deviation 2, while the loading from `textual` to `x6` has a normal prior with mean 3 and standard deviation 1.5. All other loadings have the default prior distribution.
In the above syntax, we have additionally specified a gamma(3,3) prior associated with the residual of `x1`. The `[sd]` text at the end of the distribution says that this prior goes on the residual standard deviation, as opposed to the residual precision or residual variance. There exist two more options here: a `[var]` option for the residual variance, and no brackets for the precision (or you could also use `[prec]`). This bracketed text can be used for any model variance/SD/precision parameter and could also be used in default prior specification if desired.
### Covariance Parameters
One additional note on covariance parameters defined in the model syntax: the `prior()` syntax specifies a prior on the correlation associated with the covariance parameter, as opposed to the covariance itself. The specified distribution should have support on (0,1), and blavaan automatically translates the prior to an equivalent distribution with support on (-1,1). It is safest to stick with beta priors here. For example, the syntax
```{r eval=FALSE}
HS.model <- ' visual =~ x1 x2 x3
textual =~ x4 x5 x6
speed =~ x7 x8 x9
visual ~~ prior("beta(1,1)")*textual '
```
places a Beta(1,1) (uniform) prior on the correlation between the `visual` and `textual` factors. If desired, we could also specify priors on the standard deviations (or variances or precisions) of the `visual` and `textual` factors. Together with the prior on the correlation, these priors would imply a prior on the covariance between `visual` and `textual`.
### References