Hi,
I’m working on an election state-space model similar to the Peter Ellis example posted recently on Andrew’s blog, but expanded to include many elections instead of just one (and a different country). I’m running into some difficulty using ragged arrays, both in the different number of polls from each pollster and the different number of days between elections.
If I’m thinking about estimating a single party’s vote share in each of the 10 elections below (where n_days is the number of days since the last election), then I’d have to estimate a parameter mu of length 9442.
I’m trying to use the segment slicing suggested in chp 16 of the manual, but confused about how to incorporate a time series element. For example, if I wanted to mu parameter to be modeled as a random walk from the previous day in the same election, I imagined I would need something like below. This runs into two problems: first, I’m not allowed to declare an int in the transformed parameters block; second, stan doesn’t seem to like my attempt at indexing the result of the segment() function (segment(mu, pos, n_days[k])[1]). I’m trying to assign the first value of each election season to the previous result (mu_start), then a random walk after that. Is there a better way to proceed?
transformed parameters{
vector[total_days] mu;
int pos;
pos = 1;
for (k in 1:N_election) {
segment(mu, pos, n_days[k])[1] = mu_start[k,2];
for (t in 2:n_days[k]){
segment(mu, pos, n_days[k])[t] = segment(mu, pos, n_days[k])[t-1]+segment(innovation, pos, n_days[k])[t];
}
pos = pos + n_days[k];
}
}
model{
innovation ~ student_t(4, 0, 1);
int pos2;
pos2 = 1;
for (k in 1:N_poll_elec_prov_id) {
segment(y, pos2, s[k]) ~ normal(mu[segment(day_index, pos2, s[k])], y_se*sd_inflator);
pos2 = pos2 + s[k];
}
}