Hi!

Hope this message finds you well.

In order to propagate uncertainty, I am trying to find examples where all together in the same stan document, the output from the generated quantities of a model is used as the response variable for a second model.

I was originally doing this as separate steps:

- I get my observed data x (at a site level), fit a model to it where the likelihood is x[i]~N (mu[i],sigma), and then in the generated quantities I use the estimated parameters and data x to calculate a site-level metric, x2 (which has uncertainty).
- Outside of stan, per country, I calculate a weighted mean of x2, and classify a country as 1 or 0 is that weighted mean passes a threshold, so I end up having a variable x3 at a country level that is presence/absences.
- Then I run a logistic model to understand which country-level factors can predict that X3 variable. I get a distribution for the slopes of those country-factors (i.e., uncertainty in the estimated slope from all iterations).

However, someone pointed out that I should do it all together in stan to propagate uncertainty, and I would like to learn how to do it. What I described above would not propagate the uncertainty from X2 to the step 3 (i.e., slope uncertainty in step 3 does not include the uncertainty from X2).

So my question is, are there any examples out there that do this sort of thing (models on models within stan)? Alternatively, is there a way to add two model components in the same stan document. For example:

data{

observed data at a site level

site-level predictors of first model

country-level predictors of second model

}

parameters{

parameters for model 1

parameters for model 2

}

transformed parameters{

transformed parameters for model 1

transformed parameters for model 2

}

model1{

likelihood and priors for site-level response variable

}

generated quantities{

new variable based on observed data and parameters

calculate weighted mean per country and calculate presence/absence depending on threshold: second response variable

}

model 2{

likelihood and priors for second response variable (country-level)

}

Hope my message is clear and sorry for the matters. I do not have a reproducible example because I just want to know if this can be done and if there are any examples out there that do this so I can learn how to do it. Thank you in advance!

Best regards

Jessica