Make intermediate parameters generated in the for loop accessible for return
and generated_quantities
#2408
-
In #2390 (reply in thread), it was suggested the following example to extract the parameters made in the main @model function model_Linear(y, x, participant, n)
# Priors - Fixed Effects
μ_intercept ~ Normal(0, 0.5)
μ_x ~ Normal(0, 0.5)
σ ~ truncated(Normal(0.0, 1), lower=0)
# Priors - Random Effects
μ_intercept_random_sd ~ truncated(Normal(0.0, 0.3), lower=0)
μ_x_random_sd ~ truncated(Normal(0, 0.3), lower=0)
# Participant-level priors (we only include these if `participant` is not `nothing`)
if participant !== nothing
μ_intercept_random ~ filldist(Normal(0, μ_intercept_random_sd), n)
μ_x_random ~ filldist(Normal(0, μ_x_random_sd), n)
end
for i in 1:length(y)
# Compute μ
μ = μ_intercept + μ_x * x[i, 1]
if participant !== nothing
μ += μ_intercept_random[participant[i]] + μ_x_random[participant[i]] * x[i, 1]
end
# Likelihood
y[i] ~ Normal(μ, σ)
end
# For `generated_quantities`
return (μ_fixed = μ_intercept .+ μ_x .* x[:, 1],)
end
# Convenience method for when participants aren't available.
model_Linear(y, x) = model_Linear(y, x, nothing, nothing)
# Predictions on new data "grid"
grid = DataFrame(x=[0, 50, 100], participant=[missing, missing, missing])
model_predict = model_Linear(fill(missing, 3), grid.x)
pred2 = predict(model_predict, post)
results = generated_quantities(model_predict, post) In the
Is there a workaround that to maybe make available the variables created during the for-loop and return them without explicitly recomputing them? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
A few things (sorry for being brief; a bit busy atm)
I've made an issue summarising these aspects of |
Beta Was this translation helpful? Give feedback.
To be concrete, using
generated_quantities
you could do: