Returns the Stan code of the model. Mostly useful for debugging or for building a customized version of the model.
Usage
get_code(x, ...)
# S3 method for class 'dynamiteformula'
get_code(x, data, time, group = NULL, blocks = NULL, ...)
# S3 method for class 'dynamitefit'
get_code(x, blocks = NULL, ...)
Arguments
- x
[
dynamiteformula
ordynamitefit
]
The model formula or an existingdynamitefit
object. Seedynamiteformula()
anddynamite()
.- ...
Ignored.
- data
[
data.frame
,tibble::tibble
, ordata.table::data.table
]
The data that contains the variables in the model in long format. Supported column types areinteger
,logical
,double
, andfactor
. Columns of typecharacter
will be converted to factors. Unused factor levels will be dropped. Thedata
can contain missing values which will simply be ignored in the estimation in a case-wise fashion (per time-point and per channel). Inputdata
is converted to channel specific matrix representations viastats::model.matrix.lm()
.- time
[
character(1)
]
A column name ofdata
that denotes the time index of observations. If this variable is a factor, the integer representation of its levels are used internally for defining the time indexing.- group
[
character(1)
]
A column name ofdata
that denotes the unique groups orNULL
corresponding to a scenario without any groups. Ifgroup
isNULL
, a new column.group
is created with constant value1L
is created indicating that all observations belong to the same group. In case of name conflicts withdata
, see thegroup_var
element of the return object to get the column name of the new variable.- blocks
[
character()
]
Stan block names to extract. IfNULL
, extracts the full model code.
See also
Model outputs
as.data.frame.dynamitefit()
,
as.data.table.dynamitefit()
,
as_draws_df.dynamitefit()
,
coef.dynamitefit()
,
confint.dynamitefit()
,
dynamite()
,
get_data()
,
get_parameter_dims()
,
get_parameter_names()
,
get_parameter_types()
,
ndraws.dynamitefit()
,
nobs.dynamitefit()
Examples
data.table::setDTthreads(1) # For CRAN
d <- data.frame(y = rnorm(10), x = 1:10, time = 1:10, id = 1)
cat(get_code(obs(y ~ x, family = "gaussian"),
data = d, time = "time", group = "id"
))
#> functions {
#> }
#> data {
#> int<lower=1> T; // number of time points
#> int<lower=1> N; // number of individuals
#> int<lower=0> K; // total number of covariates across all channels
#> array[T] matrix[N, K] X; // covariates as an array of N x K matrices
#> row_vector[K] X_m; // Means of all covariates at first time point
#> // number of fixed, varying and random coefficients, and related indices
#> int<lower=0> K_fixed_y;
#> int<lower=0> K_y; // K_fixed + K_varying
#> array[K_fixed_y] int J_fixed_y;
#> array[K_y] int J_y; // fixed and varying
#> array[K_fixed_y] int L_fixed_y;
#> // Parameters of vectorized priors
#> matrix[K_fixed_y, 2] beta_prior_pars_y;
#> matrix[N, T] y_y;
#> }
#> transformed data {
#> }
#> parameters {
#> vector[K_fixed_y] beta_y; // Fixed coefficients
#> real a_y; // Mean of the first time point
#> real<lower=0> sigma_y; // SD of the normal distribution
#> }
#> transformed parameters {
#> // Time-invariant intercept
#> real alpha_y;
#> // Define the first alpha using mean a_y
#> {
#> vector[K_y] gamma__y;
#> gamma__y[L_fixed_y] = beta_y;
#> alpha_y = a_y - X_m[J_y] * gamma__y;
#> }
#> }
#> model {
#> a_y ~ normal(0.61, 2.4);
#> beta_y ~ normal(beta_prior_pars_y[, 1], beta_prior_pars_y[, 2]);
#> sigma_y ~ exponential(0.83);
#> {
#> real ll = 0.0;
#> vector[K_y] gamma__y;
#> gamma__y[L_fixed_y] = beta_y;
#> for (t in 1:T) {
#> real intercept_y = alpha_y;
#> ll += normal_id_glm_lupdf(y_y[, t] | X[t][, J_y], intercept_y, gamma__y, sigma_y);
#> }
#> target += ll;
#> }
#> }
#> generated quantities {
#> }
# same as
cat(dynamite(obs(y ~ x, family = "gaussian"),
data = d, time = "time", group = "id",
debug = list(model_code = TRUE, no_compile = TRUE)
)$model_code)
#> functions {
#> }
#> data {
#> int<lower=1> T; // number of time points
#> int<lower=1> N; // number of individuals
#> int<lower=0> K; // total number of covariates across all channels
#> array[T] matrix[N, K] X; // covariates as an array of N x K matrices
#> row_vector[K] X_m; // Means of all covariates at first time point
#> // number of fixed, varying and random coefficients, and related indices
#> int<lower=0> K_fixed_y;
#> int<lower=0> K_y; // K_fixed + K_varying
#> array[K_fixed_y] int J_fixed_y;
#> array[K_y] int J_y; // fixed and varying
#> array[K_fixed_y] int L_fixed_y;
#> // Parameters of vectorized priors
#> matrix[K_fixed_y, 2] beta_prior_pars_y;
#> matrix[N, T] y_y;
#> }
#> transformed data {
#> }
#> parameters {
#> vector[K_fixed_y] beta_y; // Fixed coefficients
#> real a_y; // Mean of the first time point
#> real<lower=0> sigma_y; // SD of the normal distribution
#> }
#> transformed parameters {
#> // Time-invariant intercept
#> real alpha_y;
#> // Define the first alpha using mean a_y
#> {
#> vector[K_y] gamma__y;
#> gamma__y[L_fixed_y] = beta_y;
#> alpha_y = a_y - X_m[J_y] * gamma__y;
#> }
#> }
#> model {
#> a_y ~ normal(0.61, 2.4);
#> beta_y ~ normal(beta_prior_pars_y[, 1], beta_prior_pars_y[, 2]);
#> sigma_y ~ exponential(0.83);
#> {
#> real ll = 0.0;
#> vector[K_y] gamma__y;
#> gamma__y[L_fixed_y] = beta_y;
#> for (t in 1:T) {
#> real intercept_y = alpha_y;
#> ll += normal_id_glm_lupdf(y_y[, t] | X[t][, J_y], intercept_y, gamma__y, sigma_y);
#> }
#> target += ll;
#> }
#> }
#> generated quantities {
#> }