
Stan code and data of an mvgam model
Source:R/make_stan.R, R/print.mvgam.R
stancode.mvgam_formula.RdThe Stan program and data list of an mvgam model. For an
mvgam_formula() they are generated without fitting, as mvgam()
would build them from the same arguments. For an mvgam model they
are the ones the model was built with. Pass the two to rstan
or cmdstanr to run the model outside mvgam. The program
shows how the trend enters the linear predictor.
Usage
# S3 method for class 'mvgam_formula'
stancode(
object,
data,
family = gaussian(),
prior = NULL,
data2 = NULL,
sample_prior = "no",
knots = NULL,
drop_unused_levels = TRUE,
backend = "rstan",
threads = 1L,
normalize = TRUE,
save_model = NULL,
silent = 1L,
stanvars = NULL,
validate = TRUE,
...
)
# S3 method for class 'mvgam_formula'
standata(
object,
data,
family = gaussian(),
prior = NULL,
data2 = NULL,
sample_prior = "no",
knots = NULL,
drop_unused_levels = TRUE,
backend = "rstan",
threads = 1L,
normalize = TRUE,
save_model = NULL,
silent = 1L,
stanvars = NULL,
...
)
# S3 method for class 'mvgam'
stancode(object, ...)
# S3 method for class 'mvgam'
standata(object, ...)
# S3 method for class 'mvgamstancode'
print(x, ...)Arguments
- object
An
mvgam_formula(), or anmvgammodel fitted or built withrun_model = FALSE.- data
A data frame holding every variable the formulas use.
- family
A description of the response distribution and link function, given as a family function, a call to one or its name. Supports most brms families, including the ordinal families (
brms::cumulative(),brms::sratio(),brms::cratio(),brms::acat()). The brms families needing a separate linear predictor per response category,brms::categorical(),brms::multinomial()andbrms::dirichlet(), are refused: usecateg(),multi()anddiri(), which take the categories in long format and share one latent structure across them. A family whose mean has to stay positive needs a link that keeps it there once a trend enters the linear predictor: pairstats::Gamma(),brms::weibull(),brms::exponential(),brms::frechet()andstats::inverse.gaussian()withlink = "log". Their default links leave the mean positive only where the predictor is. A latent trend is centred near zero and reaches negative values, where the likelihood is undefined.mvgam()warns before fitting.brms::lognormal()is the exception: itsmuis a log-scale location that takes any sign.A family written inside a univariate
brms::bf()is the model's family, as in brms. In a multivariate model each response may take its own family, in which case name it inside that response'sbrms::bf():bf(count ~ x, family = poisson()) + bf(seen ~ x, family = bernoulli()). Passing a list of families to this argument, asbrms::brm()allows, is not supported; the family belongs to the response that names it. A single family given here applies to every response that does not name one of its own. Closure-unit and multi-response families (occ(),nmix(),mvn(),mvt(),diri(),multi(),categ()) lay their data out by unit. Each models its response alone and cannot be one response of a multivariate model.- prior
A
brmspriorof priors to set, frombrms::prior().get_prior.mvgam_formula()lists the classes.- data2
A named list of objects the model uses that are not variables in
data, as inbrms::brm().- sample_prior
"no"(the default),"yes"or"only", as inbrms::brm().- knots
Optional named list of knot positions for spline terms, used by the observation and trend formulas alike.
- drop_unused_levels
Logical.
TRUE(the default) drops factor levels without observations.- backend
"rstan"(the default) or"cmdstanr".- threads
Number of threads within each chain. Defaults to 1.
- normalize
Logical.
TRUE(the default) keeps the normalising constants of the sampling statements.- save_model
File path to write the Stan program to, or
NULL(the default) to write nothing.- silent
Integer verbosity, as in
brms::brm(). 0 prints Stan's exceptions and progress, 1 (the default) prints progress and 2 prints nothing.- stanvars
Optional
brms::stanvar()object of Stan code to add to the program, merged with the code mvgam writes for the trend.- validate
Logical.
TRUE(the default) checks the program with the Stan parser.stancode()alone takes it.- ...
For an
mvgam_formula(), other arguments ofmvgam(). A namemvgam()does not take is refused. For anmvgammodel, unused. Anything passed is refused.- x
An
mvgamstancodeobject.
Value
stancode() returns the Stan program as a character string
of class mvgamstancode, which prints line by line. standata()
returns the named list of Stan data.
See also
mvgam(), mvgam_formula(), get_prior.mvgam_formula().
methods_md() writes the same model as equations for a methods
section.
Other model fitting:
check_newdata(),
jsdgam(),
mvgam(),
mvgam_data(),
mvgam_multiple(),
pivot_detection_array(),
pivot_species_matrix(),
print.mvgam_formula(),
series_to_mvgam(),
update.mvgam()
Examples
set.seed(1)
dat <- sim_mvgam(family = poisson(), n_series = 2L,
n_timepoints = 40L, trend_model = AR())$data_train
mf <- mvgam_formula(y ~ s(x, k = 5), trend_formula = ~ AR(p = 1))
# The program of the model, before any fitting. The trend's
# parameters carry a `_trend` suffix.
code <- stancode(mf, data = dat, family = poisson())
code
#> // Generated with mvgam 2.0.0 using brms 2.23.2
#> functions {
#>
#> }
#> data {
#> int<lower=1> N; // total number of observations
#> array[N] int Y; // response variable
#>
#> // data for splines
#> int Ks; // number of linear effects
#> matrix[N, Ks] Xs; // design matrix for the linear effects
#>
#> // data for spline 1
#> int nb_1; // number of bases
#> array[nb_1] int knots_1; // number of knots
#>
#> // basis function matrices
#> matrix[N, knots_1[1]] Zs_1_1;
#> int prior_only; // should the likelihood be ignored?
#> int<lower=1> N_trend; // total number of observations
#> int<lower=1> N_series_trend;
#> int<lower=1> N_lv_trend;
#> int<lower=1> N_time_trend;
#> array[N_time_trend, N_series_trend] int times_trend;
#> array[N] int<lower=1, upper=N_time_trend> obs_trend_time;
#> array[N] int<lower=1, upper=N_series_trend> obs_trend_series;
#> }
#> transformed data {
#> matrix[N_series_trend, N_lv_trend] Z = diag_matrix(
#> rep_vector(1.0,
#> N_lv_trend));
#> array[N] int obs_trend_idx;
#> for (n in 1 : N) {
#> obs_trend_idx[n] = (obs_trend_series[n] - 1) * N_time_trend
#> + obs_trend_time[n];
#> }
#> }
#> parameters {
#> real Intercept; // temporary intercept for centered predictors
#> vector[Ks] bs; // unpenalized spline coefficients
#>
#> // parameters for spline 1
#> // standardized penalized spline coefficients
#> vector[knots_1[1]] zs_1_1;
#> vector<lower=0>[nb_1] sds_1; // SDs of penalized spline coefficients
#> vector<lower=0>[N_lv_trend] sigma_trend;
#> matrix[N_time_trend, N_lv_trend] innovations_trend;
#>
#> // AR coefficient parameters
#> vector<lower=-1, upper=1>[N_lv_trend] ar1_trend;
#> }
#> transformed parameters {
#> // penalized spline coefficients
#> vector[knots_1[1]] s_1_1;
#>
#> // prior contributions to the log posterior
#> real lprior = 0;
#> lprior += student_t_lpdf(Intercept | 3, 1.6, 2.5);
#> lprior += student_t_lpdf(sds_1 | 3, 0, 2.5)
#> - 1 * student_t_lccdf(0 | 3, 0, 2.5);
#>
#> matrix[N_time_trend, N_lv_trend] scaled_innovations_trend;
#> scaled_innovations_trend = diag_post_multiply(innovations_trend,
#> sigma_trend);
#> matrix[N_time_trend, N_lv_trend] lv_trend;
#>
#> // Stationary start: the t = 1 innovation scaled to the
#> // marginal SD sigma / sqrt(1 - ar1^2)
#> lv_trend[1] = scaled_innovations_trend[1] ./ sqrt(1 - square(ar1_trend))';
#>
#> // AR dynamics
#> for (i in 2 : N_time_trend) {
#> for (j in 1 : N_lv_trend) {
#> lv_trend[i, j] = ar1_trend[j] * lv_trend[i - 1, j]
#> + scaled_innovations_trend[i, j];
#> }
#> }
#>
#> // Each series' trend is the series' state
#> matrix[N_time_trend, N_series_trend] trend = lv_trend;
#>
#> // compute penalized spline coefficients
#> s_1_1 = sds_1[1] * zs_1_1;
#> }
#> model {
#> // likelihood including constants
#> if (!prior_only) {
#> // initialize linear predictor term
#> vector[N] mu = rep_vector(0.0, N);
#> mu += Intercept + Xs * bs + Zs_1_1 * s_1_1;
#> mu += to_vector(trend)[obs_trend_idx];
#> target += poisson_log_lpmf(Y | mu);
#> }
#>
#> // priors including constants
#> target += lprior;
#> // Shared innovation priors
#> target += inv_gamma_lpdf(sigma_trend | 3, 1.07);
#> target += std_normal_lpdf(to_vector(innovations_trend));
#> // AR coefficient priors
#> target += normal_lpdf(ar1_trend | 0, 0.5);
#> target += std_normal_lpdf(zs_1_1);
#> }
#> generated quantities {
#> // actual population-level intercept
#> real b_Intercept = Intercept;
#> }
#>
# The data passed to that program: 60 observations of 2 series
# over 30 time points
sdata <- standata(mf, data = dat, family = poisson())
sdata[c("N", "N_series_trend", "N_time_trend")]
#> $N
#> [1] 60
#>
#> $N_series_trend
#> [1] 2
#>
#> $N_time_trend
#> [1] 30
#>
# Next, fit the model with mvgam(), which compiles this program