I’m looking to replicate the below code snippet from a Stan example. I’m having a hard time figuring out the syntax to replicate the Turing predict() example for my logistic regression model and translating the Stan code to Julia/Turing. The issue seems to be translating the blank dependent, y, variable from the linear regression example into an equivalent for my logistic case. If I simply use, y = Vector{Union{Missing, Int64}}(undef, n)
, converting the Float64
into Int64
from the example to match the binary 0/1 for a logistic model, it gives me a MethodError
because there isn’t a Bernoulli model for ````Union{Missing, Int64}‘’'. I tested the linear model example seems to work on my version of Turing.
# Posterior_epred returns the posterior estimates for the different subgroups stored in the
# poststrat_df dataframe.
epred_mat <- posterior_epred(fit, newdata = poststrat_df, draws = 1000)
mrp_estimates_vector <- epred_mat %*% poststrat_df$n / sum(poststrat_df$n)
mrp_estimate <- c(mean = mean(mrp_estimates_vector), sd = sd(mrp_estimates_vector))
cat("MRP estimate mean, sd: ", round(mrp_estimate, 3))
I’m working on the predict()
example in Inference.jl, with key code from the example:
m_test = linear_reg(xs_test, Vector{Union{Missing, Float64}}(undef, length(ys_test)), σ);
predictions = predict(m_test, chain_lin_reg)
ys_pred = vec(mean(Array(group(predictions, :y)); dims = 1));
My Julia/Turing code to reproduce the Stan snippet is:
y = Vector{Union{Missing, Int64}}(undef, n) # This doesn't work (MethodError) as y but is based on Turing example in documentation for Inference.jl
y = ones(n) # This works as y
post_model = varying_intercept(
state_idx, eth_idx, male_eth_idx, educ_idx, educ_age_idx, educ_eth_idx, y, X, n, standardize = false
) # Model runs for training data set
epred_mat = predict(post_model, chains)
vec(mean(Array(group(epred_mat, :y)); dims = 1)) # Not the correct output