Generate Stan code for brms models
make_stancode( formula, data, family = gaussian(), prior = NULL, autocor = NULL, data2 = NULL, cov_ranef = NULL, sparse = NULL, sample_prior = "no", stanvars = NULL, stan_funs = NULL, knots = NULL, threads = NULL, normalize = getOption("brms.normalize", TRUE), save_model = NULL, ... )
formula  An object of class 

data  An object of class 
family  A description of the response distribution and link function to
be used in the model. This can be a family function, a call to a family
function or a character string naming the family. Every family function has
a 
prior  One or more 
autocor  (Deprecated) An optional 
data2  A named 
cov_ranef  (Deprecated) A list of matrices that are proportional to the
(within) covariance structure of the grouplevel effects. The names of the
matrices should correspond to columns in 
sparse  (Deprecated) Logical; indicates whether the populationlevel
design matrices should be treated as sparse (defaults to 
sample_prior  Indicate if draws from priors should be drawn
additionally to the posterior draws. Options are 
stanvars  An optional 
stan_funs  (Deprecated) An optional character string containing
selfdefined Stan functions, which will be included in the functions
block of the generated Stan code. It is now recommended to use the

knots  Optional list containing user specified knot values to be used
for basis construction of smoothing terms. See

threads  Number of threads to use in withinchain parallelization. For
more control over the threading process, 
normalize  Logical. Indicates whether normalization constants should
be included in the Stan code (defaults to 
save_model  Either 
...  Other arguments for internal usage only. 
A character string containing the fully commented Stan code to fit a brms model.
make_stancode(rating ~ treat + period + carry + (1subject), data = inhaler, family = "cumulative")#> // generated with brms 2.16.2 #> functions { #> /* cumulativelogit logPDF for a single response #> * Args: #> * y: response category #> * mu: latent mean parameter #> * disc: discrimination parameter #> * thres: ordinal thresholds #> * Returns: #> * a scalar to be added to the log posterior #> */ #> real cumulative_logit_lpmf(int y, real mu, real disc, vector thres) { #> int nthres = num_elements(thres); #> if (y == 1) { #> return log_inv_logit(disc * (thres[1]  mu)); #> } else if (y == nthres + 1) { #> return log1m_inv_logit(disc * (thres[nthres]  mu)); #> } else { #> return log_diff_exp( #> log_inv_logit(disc * (thres[y]  mu)), #> log_inv_logit(disc * (thres[y  1]  mu)) #> ); #> } #> } #> /* cumulativelogit logPDF for a single response and merged thresholds #> * Args: #> * y: response category #> * mu: latent mean parameter #> * disc: discrimination parameter #> * thres: vector of merged ordinal thresholds #> * j: start and end index for the applid threshold within 'thres' #> * Returns: #> * a scalar to be added to the log posterior #> */ #> real cumulative_logit_merged_lpmf(int y, real mu, real disc, vector thres, int[] j) { #> return cumulative_logit_lpmf(y  mu, disc, thres[j[1]:j[2]]); #> } #> /* orderedlogistic logPDF for a single response and merged thresholds #> * Args: #> * y: response category #> * mu: latent mean parameter #> * thres: vector of merged ordinal thresholds #> * j: start and end index for the applid threshold within 'thres' #> * Returns: #> * a scalar to be added to the log posterior #> */ #> real ordered_logistic_merged_lpmf(int y, real mu, vector thres, int[] j) { #> return ordered_logistic_lpmf(y  mu, thres[j[1]:j[2]]); #> } #> } #> data { #> int<lower=1> N; // total number of observations #> int Y[N]; // response variable #> int<lower=2> nthres; // number of thresholds #> int<lower=1> K; // number of populationlevel effects #> matrix[N, K] X; // populationlevel design matrix #> // data for grouplevel effects of ID 1 #> int<lower=1> N_1; // number of grouping levels #> int<lower=1> M_1; // number of coefficients per level #> int<lower=1> J_1[N]; // grouping indicator per observation #> // grouplevel predictor values #> vector[N] Z_1_1; #> int prior_only; // should the likelihood be ignored? #> } #> transformed data { #> int Kc = K; #> matrix[N, Kc] Xc; // centered version of X #> vector[Kc] means_X; // column means of X before centering #> for (i in 1:K) { #> means_X[i] = mean(X[, i]); #> Xc[, i] = X[, i]  means_X[i]; #> } #> } #> parameters { #> vector[Kc] b; // populationlevel effects #> ordered[nthres] Intercept; // temporary thresholds for centered predictors #> vector<lower=0>[M_1] sd_1; // grouplevel standard deviations #> vector[N_1] z_1[M_1]; // standardized grouplevel effects #> } #> transformed parameters { #> real<lower=0> disc = 1; // discrimination parameters #> vector[N_1] r_1_1; // actual grouplevel effects #> r_1_1 = (sd_1[1] * (z_1[1])); #> } #> model { #> // likelihood including constants #> if (!prior_only) { #> // initialize linear predictor term #> vector[N] mu = Xc * b; #> for (n in 1:N) { #> // add more terms to the linear predictor #> mu[n] += r_1_1[J_1[n]] * Z_1_1[n]; #> } #> for (n in 1:N) { #> target += ordered_logistic_lpmf(Y[n]  mu[n], Intercept); #> } #> } #> // priors including constants #> target += student_t_lpdf(Intercept  3, 0, 2.5); #> target += student_t_lpdf(sd_1  3, 0, 2.5) #>  1 * student_t_lccdf(0  3, 0, 2.5); #> target += std_normal_lpdf(z_1[1]); #> } #> generated quantities { #> // compute actual thresholds #> vector[nthres] b_Intercept = Intercept + dot_product(means_X, b); #> }make_stancode(count ~ zAge + zBase * Trt + (1patient), data = epilepsy, family = "poisson")#> // generated with brms 2.16.2 #> functions { #> } #> data { #> int<lower=1> N; // total number of observations #> int Y[N]; // response variable #> int<lower=1> K; // number of populationlevel effects #> matrix[N, K] X; // populationlevel design matrix #> // data for grouplevel effects of ID 1 #> int<lower=1> N_1; // number of grouping levels #> int<lower=1> M_1; // number of coefficients per level #> int<lower=1> J_1[N]; // grouping indicator per observation #> // grouplevel predictor values #> vector[N] Z_1_1; #> int prior_only; // should the likelihood be ignored? #> } #> transformed data { #> int Kc = K  1; #> matrix[N, Kc] Xc; // centered version of X without an intercept #> vector[Kc] means_X; // column means of X before centering #> for (i in 2:K) { #> means_X[i  1] = mean(X[, i]); #> Xc[, i  1] = X[, i]  means_X[i  1]; #> } #> } #> parameters { #> vector[Kc] b; // populationlevel effects #> real Intercept; // temporary intercept for centered predictors #> vector<lower=0>[M_1] sd_1; // grouplevel standard deviations #> vector[N_1] z_1[M_1]; // standardized grouplevel effects #> } #> transformed parameters { #> vector[N_1] r_1_1; // actual grouplevel effects #> r_1_1 = (sd_1[1] * (z_1[1])); #> } #> model { #> // likelihood including constants #> if (!prior_only) { #> // initialize linear predictor term #> vector[N] mu = Intercept + rep_vector(0.0, N); #> for (n in 1:N) { #> // add more terms to the linear predictor #> mu[n] += r_1_1[J_1[n]] * Z_1_1[n]; #> } #> target += poisson_log_glm_lpmf(Y  Xc, mu, b); #> } #> // priors including constants #> target += student_t_lpdf(Intercept  3, 1.4, 2.5); #> target += student_t_lpdf(sd_1  3, 0, 2.5) #>  1 * student_t_lccdf(0  3, 0, 2.5); #> target += std_normal_lpdf(z_1[1]); #> } #> generated quantities { #> // actual populationlevel intercept #> real b_Intercept = Intercept  dot_product(means_X, b); #> }