Learn R Programming

brms (version 0.5.0)

set_prior: Prior Deinitions for brms Models

Description

Define priors for specific parameters or classes of parameters

Usage

set_prior(prior, class = "b", coef = "", group = "")

Arguments

prior
A character string defining a distribution in Stan language
class
The parameter class. Defaults to "b" (fixed effects). See 'Details' for other valid parameter classes.
coef
Name of the (fixed, category specific, or random effects) parameter
group
Grouping factor for random effects parameters.

Value

  • An object of class brmsprior to be used in the prior argument of brm.

Details

set_prior is used to define prior distributions for parameters in brms models. Below, we explain its usage and list some common prior distributions for parameters. A complete overview on possible prior distributions is given in the Stan Reference Manual available at http://mc-stan.org/. To combine multiple priors, use c(...), e.g., c(set_prior(...), set_prior(...)). brms performs no checks if the priors are written in correct Stan language. Instead, Stan will check their correctness when the model is parsed to C++ and returns an error if they are not. Currently, there are five types of parameters in brms models, for which the user can specify prior distributions. 1. Fixed and category specific effects Every fixed (and category specific) effect has its corresponding regression parameter. These parameters are internally named as b_, where represents the name of the corresponding fixed effect. Suppose, for instance, that y is predicted by x1 and x2 (i.e. y ~ x1+x2 in formula syntax). Then, x1 and x2 have regression parameters b_x1 and b_x2 respectively. The default prior for fixed and category specific effects is an improper flat prior over the reals. Other common options are normal priors or uniform priors over a finite interval. If we want to have a normal prior with mean 0 and standard deviation 5 for x1, and a uniform prior between -10 and 10 for x2, we can specify this via set_prior("normal(0,5)", class = "b", coef = "x1") and set_prior("uniform(-10,10)", class = "b", coef = "x2"). To put the same prior on all fixed effects at once, we may write as a shortcut set_prior("", class = "b"). This also leads to faster sampling, because priors can be vectorized in this case. 2. Autocorrelation parameters The autocorrelation parameters currently implemented are named ar (autoregression) and ma (moving average). The default prior for autocorrelation parameters is an improper flat prior over the reals. Other priors can be defined with set_prior("", class = "ar") or set_prior("", class = "ma"). It should be noted that ar will only take one values between -1 and 1 if the response variable is wide-sence stationay, i.e. if there is no drift in the responses. 3. Standard deviations of random effects Each random effect of each grouping factor has a standard deviation named sd__. Consider, for instance, the formula y ~ x1+x2+(1+x1|g). We see that the intercept as well as x1 are random effects nested in the grouping factor g. The corresponding standard deviation parameters are named as sd_g_Intercept and sd_g_x1 respectively. These parameters are restriced to be non-negative and, by default, have a half cauchy prior with scale parameter 5. We could make this explicit by writing set_prior("cauchy(0,5)", class = "sd"). To define a prior distribution only for standard deviations of a specific grouping factor, use set_prior("", class = "sd", group = ""). To define a prior distribution only for a specific standard deviation of a specific grouping factor, you may write set_prior("", class = "sd", group = "", coef = ""). Recommendations on useful prior distributions for standard deviations are given in Gelman (2006). 4. Correlations of random effects If there is more than one random effect per grouping factor, the correlations between those random effects have to be estimated. The prior "lkj_corr_cholesky(eta)" or in short "lkj(eta)" with eta > 0 is essentially the only prior for (choelsky factors) of correlation matrices. If eta = 1 (the default) all correlations matrices are equally likely a priori. If eta > 1, extreme correlations become less likely, whereas 0 < eta < 1 results in higher probabilities for extreme correlations. Correlation matrix parameters in brms models are named as cor_(group), (e.g., cor_g if g is the grouping factor). To set the same prior on every correlation matrix, use for instance set_prior("lkj(2)", class = "cor"). 5. Parameters for specific families Some families need additional parameters to be estimated. Families gaussian, student, and cauchy need the parameter sigma to account for the standard deviation of the response variable around the regression line (not to be confused with the standard deviations of random effects). By default, sigma has a half cauchy prior with 'mean' 0 and 'standard deviation' 5. Furthermore, family student needs the parameter nu representing the degrees of freedom of students t distribution. By default, nu has prior "uniform(1,100)". Families gamma and weibull need the parameter shape that has a "gamma(0.01,0.01)" prior by default. For families cumulative, cratio, sratio, and acat, and only if threshold = "equidistant", the parameter delta is used to model the distance between to adjacent thresholds. By default, delta has an improper flat prior over the reals. Every family specific parameter has its own prior class, so that set_prior("", class = "") it the right way to go. Often, it may not be immediately clear, which parameters are present in the model. To get a full list of parameters and parameter classes for which priors can be specified (depending on the model) use function get_prior.

References

Gelman A (2006). Prior distributions for variance parameters in hierarchical models." Bayesian analysis, 1(3), 515 -- 534.

See Also

get_prior

Examples

Run this code
## check which parameters can have priors
get_prior(rating ~ treat + period + carry + (1|subject),
          data = inhaler, family = "cumulative",
          threshold = "equidistant")

## define some priors
prior <- c(set_prior("normal(0,10)", class = "b"),
           set_prior("normal(1,2)", class = "b", coef = "treat"),
           set_prior("cauchy(0,2)", class = "sd",
                     group = "subject", coef = "Intercept"),
           set_prior("uniform(-5,5)", class = "delta"))

## use the defined priors in the model
fit <- brm(rating ~ period + carry + (1|subject),
           data = inhaler, family = "sratio",
           partial = ~ treat, threshold = "equidistant",
           prior = prior, n.iter = 1000, n.cluster = 2)

## check that the priors found their way into Stan's model code
fit$model

Run the code above in your browser using DataLab