Hi, I’m trying to do something which I don’t know is possible (I’m not a very advanced user).
The model is hierarchical and consists of a set of equations, the key thing is that I want to use the sum of an estimated (right word?) variable in a function. I figured that writing separate loops could possibly work, but I really don’t know if this is doable, or if there is some other way of achieving the desired result.
This what I’m trying to do conceptually:
for (i in 1:N){
// a bunch of equations here that end with something like;
est_variable1[i] = beta * variable2[i];
}
for (i in 1:N){
// here I want to sum est_variable1 (well a little more complicated);
}
for (i in 1:N){
// here I want to use the sum of variable1 in a function
variable3[i] = variable4[i] / sum_of_est_variable1;
// some more functions
}
The model can be fitted with a solver, but I want to se how stable it is. So basically, can this be done at all in STAN, and can I do it in the way suggested?
Based on what you’ve said here this is definitely something you can do in Stan. Whether you do it and end up with a model that works/makes sense I have no idea.
Thanks, it’s good to know I’m not attempting to do something impossible.
So there is nothing special about having two separate loops vs just one where values in the two are dependent? I get how sampling works conceptually but not quite how that relaters to loops written in c++. Like if things need to be in any particular order of if that doesn’t matter.
I see, the jump you need to make is that there’s nothing about sampling in the Stan model itself. All you are specifying is a procedure for calculating the log posterior density. Coming from BUGS sometimes people get confused and think you can write code out-of-order but Stan code must be in order (it really is just a procedure for calculating the log density).
I actually come from R and econometrics, so my handicap is even larger. Thank you for that explanation, I think I have a much better understanding of what’s going on now. Up to this point I’ve just been constructing models without really thinking much about how they are evaluated.
Stan’s an imperative programming language. Statements get executed in the order they’re encountered and variables have to be defined before they’re used. The whole program is just defining a big log density function over the parameters (and perhaps defining some derived quantities).
R’s another imperative language. So that’s not a bad place to start.
I wouldn’t name parameters est_variable. They’re just the variable—the estimation happens on the outside. I don’t write y ~ normal(mu_estimated, sigma_estimated), I just write normal(mu, sigma).
I can’t follow what you’re doing and how it relates to defining a log density.
You want to think of the models as defining a prior, likelihood, and from that you can calculate the posterior given data. So it’s all about getting the log densities defined the right way. Once you do that, Stan will handle the inference.