I’m doing a research study about bayesian modelling in R.
I’m using the brm function of brms package. Below is a part of my code where I obtain my model (chains=4, iter=2000).
However I have a doubt about the functionalities of the brm function.
I would like to ask if it is possible to obtain and see the intermediate models of my final model, and if so, how can I obtain them.
Example of code is as follow:
prior_intercept <- prior(normal(0, 200), class=Intercept)
prior_x1 <- prior(normal(-10,5), class=b, coef=x1)
prior_x2 <- prior(normal(0,2), class=b, coef=x2)
prior_x3 <- prior(normal(-10,5), class=b, coef=x3)
prior_x4 <- prior(normal(0.8,0.1), class=b, coef=x4)
model <- brm(y ~ x1 + x2 + x3 + x4 ,data = tes1, prior = c(prior_intercept, prior_x1, prior_x2, prior_x3, prior_x4))
What do you mean by “the intermediate models”?
When I ran my code I obtain this:
Whith “intermediate models” I mean the models from whitch these results are obtained.
stancode - Extract Stan model code — stancode.brmsfit • brms
and its sibling,
standata - Extract data passed to Stan — standata.brmsfit • brms
the names of the model parameters and input data objects are BRMS-generated. BRMS does a lot of name-munging magic to map from the variable names in your formula and data to the internal names used in the Stan code.
I’m still not entirely sure what need. There is just one model, which is a joint model for all of the parameters. The model is fully specified by your call to
brm. From there, it gets translated to Stan code, then compiled to C++, then executed, then returned and saved in memory in R as
model in your script. When you print
model, which is an object of class
brmsfit, you see the summary pasted above. There is quite a bit more information contained in
str(model)), and also quite a few functions with methods that can act on
brmsfit objects like this one to return various summaries, predictions, the posterior draws themselves, and so forth.
Does anything I mentioned above sound like what you are hoping to obtain?
So, if I have 2000 iterations on my brm, it produces just one final model of the “best combination” of my input parameters?
And, if my objective is to reduce the final error between the inputs and my target, do I have to call several times (as many as I want) by a cycle the brm function and therefore obtain several models and then analise wich one has the minor error?
One model has a posterior that is characterized by the ensemble of all of the post-warmup iterations (in brms by default this will be 4000 iterations: 1000 post-warmup iterations per chain and 4 chains).
Ok, and there is one way that I can access and visualize all iterations of each one of the chains?
Sure, take a look at
brms::as_draws (for use with
posterior package) or
brms::as.mcmc (for use with