Prior specifications for distributional regression parameters
Source:R/fixed-effect-priors.R
drm_priors.RdSpecify coefficient priors or penalties and natural-scale priors for
supported positive scales and correlations. Coefficient names refer to the final design matrix, so their
meaning depends on contrasts and predictor units. No automatic scaling is
applied. For coef = "all", state whether the intercept is included.
Usage
drm_priors(...)
drm_prior_sd_gamma(dpar = "mu", group, coef = "(Intercept)", shape, rate)
drm_prior_normal(dpar, coef, mean = 0, sd, intercept = NULL)
drm_prior_ridge(dpar, coef, lambda, intercept = NULL)Arguments
- ...
Prior specifications returned by
drm_prior_normal(),drm_prior_ridge(), ordrm_prior_sd_gamma().- dpar
For fixed-effect priors,
"mu"or"sigma"in univariate models. In a bivariate Gaussian model, fixed Normal and Student-t priors may target"mu1"or"mu2"when combined with onerho12LKJ prior. The SD prior currently supports"mu"only.- group
Exact random-effect grouping variable name.
- coef
For fixed-effect priors, one exact design-matrix column name or
"all". The SD prior currently supports"(Intercept)"only.- shape
Gamma shape, finite and greater than one.
- rate
Gamma rate, positive and finite.
- mean
Normal prior mean, a finite scalar.
- sd
Normal prior standard deviation, a positive finite scalar.
- intercept
For
coef = "all", either"include"or"exclude".- lambda
Nonnegative finite ridge precision. The penalty is
0.5 * lambda * sum(beta^2); zero contributes no penalty.
Value
drm_priors() returns a collection of prior specifications; the
other constructors return one specification.
Details
The prior-table interface accepts normal, Student-t, and ridge coefficient
rows, Gamma priors on supported random-effect and residual standard
deviations, and LKJ(eta) priors on supported correlations. Pass a table
directly to drmTMB(priors = ...) or drm_resolve_priors();
drm_priors() collects constructor specifications.
Gamma priors are densities on natural SD and LKJ priors are densities on
natural correlation; optimizing transformed coordinates adds no Jacobian.
In bivariate Gaussian models, exact Normal or Student-t fixed-effect rows
for mu1 and mu2 may be combined with one intercept-only rho12 LKJ row;
other bivariate prior combinations remain unsupported.
Shape must exceed one and rate must be positive. Normal, Student-t, Gamma,
and LKJ densities include their normalizing constants; ridge remains an
unnormalized penalty. logLik()
reports the data likelihood at the penalized estimates. Confidence intervals
and information criteria are not provided for active prior fits; local
penalized-Hessian curvature is not a calibrated confidence interval.
The Julia bridge currently refuses factor predictors in Gaussian mean
formulas with ordinary random effects; the native TMB route supports them.
Examples
drm_priors(drm_prior_normal("mu", "x", sd = 2))
#> [[1]]
#> $dpar
#> [1] "mu"
#>
#> $coef
#> [1] "x"
#>
#> $intercept
#> NULL
#>
#> $kind
#> [1] "normal"
#>
#> $mean
#> [1] 0
#>
#> $sd
#> [1] 2
#>
#> $precision
#> [1] 0.25
#>
#> $df
#> NULL
#>
#> attr(,"class")
#> [1] "drm_fixed_prior"
#>
#> attr(,"class")
#> [1] "drm_priors"
drm_priors(drm_prior_sd_gamma(group = "site", shape = 2, rate = 3))
#> [[1]]
#> $dpar
#> [1] "mu"
#>
#> $group
#> [1] "site"
#>
#> $coef
#> [1] "(Intercept)"
#>
#> $shape
#> [1] 2
#>
#> $rate
#> [1] 3
#>
#> attr(,"class")
#> [1] "drm_sd_prior"
#>
#> attr(,"class")
#> [1] "drm_priors"
# \donttest{
d <- data.frame(x = rep(c(-1, 0, 1), 6), group = factor(rep(1:6, each = 3)))
d$y <- 0.5 + 0.6 * d$x + rep(c(-0.5, 0.2, 0.7, -0.3, 0.4, -0.4), each = 3) +
rep(c(0.1, -0.2, 0.1), 6)
p <- drm_priors(drm_prior_normal("mu", "x", mean = 0.2, sd = 0.8))
fit <- drmTMB(bf(y ~ x + (1 | group), sigma ~ 1), data = d, priors = p)
coef(fit, dpar = "mu")
#> (Intercept) x
#> 0.5166667 0.5984435
logLik(fit) # data likelihood at the penalized estimates
#> 'log Lik.' -2.940245 (df=4)
sd_prior <- drm_priors(drm_prior_sd_gamma(group = "group", shape = 2, rate = 3))
formula <- bf(mu = y ~ x + (1 | group), sigma = ~ 1)
drm_resolve_priors(formula, gaussian(), d, sd_prior) # inspect the target
#> class dpar group block coefficient scale kind shape rate
#> 1 ranef_sd mu group (1 | group) (Intercept) natural_sd gamma 2 3
sd_fit <- drmTMB(formula, data = d, priors = sd_prior)
logLik(sd_fit) # data likelihood at the penalized estimates
#> 'log Lik.' -2.943036 (df=4)
d$observer <- rep(c("a", "b"), 9)
two_groups <- bf(mu = y ~ x + (1 | group) + (1 | observer), sigma = ~ 1)
two_priors <- drm_priors(drm_prior_sd_gamma(group = "group", shape = 2, rate = 3),
drm_prior_sd_gamma(group = "observer", shape = 3, rate = 2))
drm_resolve_priors(two_groups, gaussian(), d, two_priors)
#> class dpar group block coefficient scale kind shape rate
#> 1 ranef_sd mu group (1 | group) (Intercept) natural_sd gamma 2 3
#> 2 ranef_sd mu observer (1 | observer) (Intercept) natural_sd gamma 3 2
two_fit <- drmTMB(two_groups, data = d, priors = two_priors)
# }