31.3 Poststratification in Stan
The maximum likelihood and Bayesian estimates can be handled with the same Stan program. The model of individual votes is collapsed to a binomial, where \(A_j\) is the number of voters from group \(j\), \(a_j\) is the number of positive responses from group \(j\), and \(N_j\) is the size of group \(j\) in the population.
data {
int<lower=1> J;
array[J] int<lower=0> A;
array[J] int<lower=0> a;
vector<lower=0>[J] N;
}parameters {
vector<lower=0, upper=1>[J] theta;
}model {
a ~ binomial(A, theta);
}generated quantities {t
real<lower=0, upper=1> phi = dot(N, theta) / sum(N);
}
The likelihood is vectorized, and implicitly sums over the \(j\).
The prior is implicitly uniform on \((0, 1),\) the support of \(\theta.\)
The summation is computed using a dot product and the sum function,
which is why N
was declared as a vector rather than as an array of
integers.