Can a generated quantity be used in the model block

Can a generated quantity be used in a model block.

I have a mixture structure, and I can write the mixture in a marginalized form (as a sum) in the model block.

I can then generate the mixture component indicator in the generated quantities block from its posterior predictive distribution…

I would then like to use the generated mixture component indicator (from the posterior predictive distribution) to describe a separate part of the model.

Is that possible/allowed?


1 Like

no, but you can use the transformed parameters block to do the computation.
all variables declared in the model block are local variables, not available in GQs.
top-level variables (i.e., not declared inside a loop) in the transformed parameters block are global variables. the downside is that this increases the size of the output.csv file.

1 Like

I assume the mixture component indicator is generated via random number generation. This is disallowed in the model and transformed blocks because the resulting discrete parameter breaks the gradients that Hamiltonian Monte Carlo relies on to work its magic. To fit this model in Stan, you need to marginalize over the mixture component indicator in any downstream parts of the likelihood that depend on the indicator.