get_mvgam_priors {mvgam} | R Documentation |
Extract information on default prior distributions for an mvgam model
Description
This function lists the parameters that can have their prior distributions
changed for a given mvgam
model, as well listing their default distributions
Usage
get_mvgam_priors(
formula,
trend_formula,
data,
data_train,
family = "poisson",
knots,
use_lv = FALSE,
n_lv,
use_stan = TRUE,
trend_model = "None",
trend_map,
drift = FALSE
)
Arguments
formula |
A |
trend_formula |
An optional |
data |
A
Should also include any other variables to be included in the linear predictor of |
data_train |
Deprecated. Still works in place of |
family |
Note that only |
knots |
An optional |
use_lv |
|
n_lv |
|
use_stan |
Logical. If |
trend_model |
For all trend types apart from |
trend_map |
Optional |
drift |
|
Details
Users can supply a model formula, prior to fitting the model, so that default priors can be inspected and
altered. To make alterations, change the contents of the prior
column and supplying this
data.frame
to the mvgam
function using the argument priors
. If using Stan
as the backend,
users can also modify the parameter bounds by modifying the new_lowerbound
and/or new_upperbound
columns.
This will be necessary if using restrictive distributions on some parameters, such as a Beta distribution
for the trend sd parameters for example (Beta only has support on (0,1)
), so the upperbound cannot
be above 1
. Another option is to make use of the prior modification functions in brms
(i.e. prior
) to change prior distributions and bounds (just use the name of the parameter that
you'd like to change as the class
argument; see examples below)
Value
either a data.frame
containing the prior definitions (if any suitable
priors can be altered by the user) or NULL
, indicating that no priors in the model
can be modified through the mvgam
interface
Note
Only the prior
, new_lowerbound
and/or new_upperbound
columns of the output
should be altered when defining the user-defined priors for the mvgam
model. Use only if you are
familiar with the underlying probabilistic programming language. There are no sanity checks done to
ensure that the code is legal (i.e. to check that lower bounds are smaller than upper bounds, for
example)
Author(s)
Nicholas J Clark
See Also
Examples
# Simulate three integer-valued time series
library(mvgam)
dat <- sim_mvgam(trend_rel = 0.5)
# Get a model file that uses default mvgam priors for inspection (not always necessary,
# but this can be useful for testing whether your updated priors are written correctly)
mod_default <- mvgam(y ~ s(series, bs = 're') +
s(season, bs = 'cc') - 1,
family = nb(),
data = dat$data_train,
trend_model = AR(p = 2),
run_model = FALSE)
# Inspect the model file with default mvgam priors
code(mod_default)
# Look at which priors can be updated in mvgam
test_priors <- get_mvgam_priors(y ~ s(series, bs = 're') +
s(season, bs = 'cc') - 1,
family = nb(),
data = dat$data_train,
trend_model = AR(p = 2))
test_priors
# Make a few changes; first, change the population mean for the series-level
# random intercepts
test_priors$prior[2] <- 'mu_raw ~ normal(0.2, 0.5);'
# Now use stronger regularisation for the series-level AR2 coefficients
test_priors$prior[5] <- 'ar2 ~ normal(0, 0.25);'
# Check that the changes are made to the model file without any warnings by
# setting 'run_model = FALSE'
mod <- mvgam(y ~ s(series, bs = 're') +
s(season, bs = 'cc') - 1,
family = nb(),
data = dat$data_train,
trend_model = AR(p = 2),
priors = test_priors,
run_model = FALSE)
code(mod)
# No warnings, the model is ready for fitting now in the usual way with the addition
# of the 'priors' argument
# The same can be done using 'brms' functions; here we will also change the ar1 prior
# and put some bounds on the ar coefficients to enforce stationarity; we set the
# prior using the 'class' argument in all brms prior functions
brmsprior <- c(prior(normal(0.2, 0.5), class = mu_raw),
prior(normal(0, 0.25), class = ar1, lb = -1, ub = 1),
prior(normal(0, 0.25), class = ar2, lb = -1, ub = 1))
brmsprior
mod <- mvgam(y ~ s(series, bs = 're') +
s(season, bs = 'cc') - 1,
family = nb(),
data = dat$data_train,
trend_model = AR(p = 2),
priors = brmsprior,
run_model = FALSE)
code(mod)
# Look at what is returned when an incorrect spelling is used
test_priors$prior[5] <- 'ar2_bananas ~ normal(0, 0.25);'
mod <- mvgam(y ~ s(series, bs = 're') +
s(season, bs = 'cc') - 1,
family = nb(),
data = dat$data_train,
trend_model = AR(p = 2),
priors = test_priors,
run_model = FALSE)
code(mod)
# Example of changing parametric (fixed effect) priors
simdat <- sim_mvgam()
# Add a fake covariate
simdat$data_train$cov <- rnorm(NROW(simdat$data_train))
priors <- get_mvgam_priors(y ~ cov + s(season),
data = simdat$data_train,
family = poisson(),
trend_model = AR())
# Change priors for the intercept and fake covariate effects
priors$prior[1] <- '(Intercept) ~ normal(0, 1);'
priors$prior[2] <- 'cov ~ normal(0, 0.1);'
mod2 <- mvgam(y ~ cov + s(season),
data = simdat$data_train,
trend_model = AR(),
family = poisson(),
priors = priors,
run_model = FALSE)
code(mod2)
# Likewise using 'brms' utilities (note that you can use
# Intercept rather than `(Intercept)`) to change priors on the intercept
brmsprior <- c(prior(normal(0.2, 0.5), class = cov),
prior(normal(0, 0.25), class = Intercept))
brmsprior
mod2 <- mvgam(y ~ cov + s(season),
data = simdat$data_train,
trend_model = AR(),
family = poisson(),
priors = brmsprior,
run_model = FALSE)
code(mod2)
# The "class = 'b'" shortcut can be used to put the same prior on all
# 'fixed' effect coefficients (apart from any intercepts)
set.seed(0)
dat <- mgcv::gamSim(1, n = 200, scale = 2)
dat$time <- 1:NROW(dat)
mod <- mvgam(y ~ x0 + x1 + s(x2) + s(x3),
priors = prior(normal(0, 0.75), class = 'b'),
data = dat,
family = gaussian(),
run_model = FALSE)
code(mod)