data { int<lower=0> N; // sample wave_size int<lower=0> n_age; // number of age categories int<lower=0> n_race; // number of race categories int<lower=0> n_edu; // number of education catoegories int<lower=0> n_dma; // number of dmas int<lower=0> n_party_id; // number of party-categories int<lower=0> n_day; // number of party-categories
int<lower=0,upper=1> female[N]; // female each respondent int<lower=0,upper=n_age> age[N]; // age for each respondent int<lower=0,upper=n_edu> edu[N]; // edu for each respondent int<lower=0,upper=n_race> race[N]; // race for each respondent int<lower=0,upper=n_party_id> party_id[N]; // party_id for each respondent int<lower=0,upper=n_dma> dma[N]; // party_id for each respondent
real vote_prev_dma[N]; // 2012 state-vote at the individual-level int<lower=0,upper=1> y[N]; // vote-choice int<lower=0,upper=N> in_day[N]; // wave of respondents int<lower=0> day_size[n_day]; // N individuals per Wave int<lower=0> day_lims[n_day,2]; // first individual in wave and individuals per wave (5*2 matrix)}parameters { real beta [n_day]; // coefficient-array for female; wave vector[n_age] a_age [n_day]; // coefficient-array for age; wave vector[n_edu] a_edu [n_day]; // coefficient-array for edu; wave vector[n_race] a_race [n_day]; // coefficient-array for race; wave vector[n_party_id] a_party_id [n_day]; // coefficient-array for party_id; wave vector [n_dma] a_dma[n_day]; // coefficient-array for division; wave real gamma; // previous vote- slope real alpha; //intercept real <lower=0> sigma_dma; real <lower=0> sigma_age; real <lower=0> sigma_edu; real <lower=0> sigma_race; real <lower=0> sigma_party_id; real <lower=0> sigma_beta_evo; real <lower=0> sigma_dma_evo; real <lower=0> sigma_age_evo; real <lower=0> sigma_edu_evo; real <lower=0> sigma_race_evo; real <lower=0> sigma_party_id_evo;
}model { sigma_dma ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_age ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_edu ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_race ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_party_id ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_beta_evo ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_dma_evo ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_age_evo ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_edu_evo ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_race_evo ~normal(0,2.5); // within_division variance can move from 0.1% and 99% sigma_party_id_evo ~normal(0,2.5); // within_division variance can move from 0.1% and 99% gamma~normal(0,5); // embed 95% probability mass between 0.01% and 99.9% alpha~normal(0,5); // embed 95% probability mass between 0.01% and 99.9% // (Gelman 2008)// (Gelman 2008) beta[1]~normal(0,5); // embed 95% probability mass between 0.1% and 99% a_dma[1]~ normal(0,sigma_dma); // embed 95% probability mass between 70% and 30% a_age[1]~normal(0,sigma_age); // embed 95% probability mass between 0.01% and 99.9% a_edu[1]~normal(0,sigma_edu); // embed 95% probability mass between 0.01% and 99.9% a_race[1]~normal(0,sigma_race); // embed 95% probability mass between 0.01% and 99.9% a_party_id[1]~normal(0,sigma_party_id); // embed 95% probability mass between 0.01% and 99.9% for (k in 2:n_day) { beta[k] ~ cauchy(beta[k-1], sigma_beta_evo); // assume 5% shift per demographic per wave covers // 95% of probability mass a_age[k] ~ cauchy(a_age[k-1], sigma_age_evo); // assume 5% shift per demographic per wave covers // 95% of probability mass a_edu[k] ~ cauchy(a_edu[k-1], sigma_edu_evo); // assume 5% shift per demographic per wave covers // 95% of probability mass a_race[k] ~ cauchy(a_race[k-1],sigma_race_evo); // assume 5% shift per demographic per wave covers // 95% of probability mass a_party_id[k] ~ cauchy(a_party_id[k-1], sigma_party_id_evo); // assume 5% shift per demographic per wave covers // 95% of probability mass a_dma[k] ~ cauchy(a_dma[k-1], sigma_dma_evo); } //Likelihood for each i in each wave for(s in 1:n_day){ int obs_in_day[day_size[s]]; obs_in_day<-segment(in_day,day_lims[s,1],day_lims[s,2]) ; // find observations in each wave for (i in 1:day_size[s]) { real y_hat; y_hat <- alpha +beta[s]*female[obs_in_day[i]] +a_age[s,age[obs_in_day[i]]] +a_edu[s,edu[obs_in_day[i]]] +a_party_id[s,party_id[obs_in_day[i]]] +a_race[s,race[obs_in_day[i]]]+gamma*vote_prev_dma[i] +a_dma[s,dma[obs_in_day[i]]]; y[obs_in_day[i]] ~ bernoulli_logit(y_hat); } }}
data { int<lower=0> N; // sample wave_size int<lower=0> n_age; // number of age categories int<lower=0> n_race; // number of race categories int<lower=0> n_edu; // number of education catoegories int<lower=0> n_dma; // number of dmas int<lower=0> n_party_id; // number of party-categories int<lower=0> n_day; // number of party-categories
vector <lower=0,upper=1> [N]female; // female each respondent int<lower=0,upper=n_age> age[N]; // age for each respondent int<lower=0,upper=n_edu> edu[N]; // edu for each respondent int<lower=0,upper=n_race> race[N]; // race for each respondent int<lower=0,upper=n_party_id> party_id[N]; // party_id for each respondent int<lower=0,upper=n_dma> dma[N]; // party_id for each respondent
vector[N] vote_prev_dma; // 2012 state-vote at the individual-level int<lower=0,upper=1> y[N]; // vote-choice int<lower=0,upper=N> in_day[N]; // wave of respondents int<lower=0> day_size[n_day]; // N individuals per Wave int<lower=0> day_lims[n_day,2]; // first individual in wave and individuals per wave }parameters { real beta[n_day] ; // coefficient-array for female; wave matrix[n_day,n_age] a_age_raw; // coefficient-matrix for age; wave matrix[n_day,n_edu] a_edu_raw; // coefficient-matrix for edu; wave matrix[n_day,n_race] a_race_raw; // coefficient-matrix for race; wave matrix[n_day,n_party_id] a_party_id_raw ; // coefficient-matrix for party_id; wave matrix[n_day,n_dma] a_dma; // coefficient-matrix for DMA; wave real gamma; // previous vote- slope real alpha; //intercept real <lower=0> sigma_dma; real <lower=0> sigma_age; real <lower=0> sigma_edu; real <lower=0> sigma_race; real <lower=0> sigma_party_id; real <lower=0> sigma_beta_evo; real <lower=0> sigma_dma_evo; real <lower=0> sigma_age_evo; real <lower=0> sigma_edu_evo; real <lower=0> sigma_race_evo; real <lower=0> sigma_party_id_evo;
}transformed parameters{ matrix[n_day,n_age] a_age; //efficient non-centered paramterization matrix[n_day,n_edu] a_edu; matrix[n_day,n_race] a_race; matrix[n_day,n_party_id] a_party_id; a_age[1]<-a_age_raw[1]*sigma_age; a_edu[1]<-a_edu_raw[1]*sigma_edu; a_race[1]<-a_race_raw[1]*sigma_race; a_party_id[1]<-a_party_id_raw[1]*sigma_party_id;for (i in 2:n_day){ a_age[i]<-a_age_raw[i]; a_edu[i]<-a_edu_raw[i]; a_race[i]<-a_race_raw[i]; a_party_id[i]<-a_party_id_raw[i];}}
model { sigma_dma ~normal(0,2.5); // within_DMA variance can move from 0.1% and 99% sigma_age ~normal(0,2.5); // within_Age variance can move from 0.1% and 99% sigma_edu ~normal(0,2.5); // within_edu variance can move from 0.1% and 99% sigma_race ~normal(0,2.5); // within_race variance can move from 0.1% and 99% sigma_party_id ~normal(0,2.5); // within_party variance can move from 0.1% and 99% sigma_beta_evo ~normal(0,2.5); // Evolution Variance for Female sigma_dma_evo ~normal(0,2.5); // Evolution Variance for DMA sigma_age_evo ~normal(0,2.5); // Evolution Variance for Age sigma_edu_evo ~normal(0,2.5); // Evolution Variance for Edu sigma_race_evo ~normal(0,2.5); // Evolution Variance for race sigma_party_id_evo ~normal(0,2.5); // Evolution Variance for Party gamma~normal(0,5); // embed 95% probability mass between 0.01% and 99.9% alpha~normal(0,5); // embed 95% probability mass between 0.01% and 99.9% / beta[1]~normal(0,5); // embed 95% probability mass between 0.1% and 99% a_dma[1]~ normal(0,1); // embed 95% probability mass between 70% and 30% a_age[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% a_edu[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% a_race[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% a_party_id[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% // Evolution Sampling: beta[2:n_day] ~ cauchy( beta[1:(n_day - 1)], sigma_beta_evo); to_vector(a_age[2:n_day]) ~ cauchy( to_vector(a_age[1:(n_day - 1)]), sigma_age_evo); to_vector(a_edu[2:n_day]) ~ cauchy(to_vector( a_edu[1:(n_day - 1)]), sigma_edu_evo); to_vector(a_race[2:n_day]) ~ cauchy(to_vector( a_race[1:(n_day - 1)]), sigma_race_evo); to_vector(a_party_id[2:n_day]) ~ cauchy( to_vector(a_party_id[1:(n_day - 1)]), sigma_party_id_evo); to_vector(a_dma[2:n_day]) ~ cauchy( to_vector(a_dma[1:(n_day - 1)]), sigma_dma_evo); for(s in 1:n_day){ // Likelihood for each each wave vectorized int obs_in_day[day_size[s]]; // find observations in each wave: obs_in_day<-segment(in_day,day_lims[s,1],day_lims[s,2]); // Likelihood: y[obs_in_day] ~ bernoulli_logit(alpha +beta[s]*female[obs_in_day] +to_vector(a_age[s,age[obs_in_day]]) +to_vector(a_edu[s,edu[obs_in_day]]) +to_vector(a_party_id[s,party_id[obs_in_day]]) +to_vector(a_race[s,race[obs_in_day]]) +gamma*vote_prev_dma[obs_in_day] +to_vector(a_dma[s,dma[obs_in_day]])); }}data { int<lower=0> N; // sample wave_size int<lower=0> n_age; // number of age categories int<lower=0> n_race; // number of race categories int<lower=0> n_edu; // number of education catoegories int<lower=0> n_dma; // number of dmas int<lower=0> n_party_id; // number of party-categories int<lower=0> n_day; // number of party-categories int<lower=0> nu; // number of party-categories
vector <lower=0,upper=1> [N]female; // female each respondent int<lower=0,upper=n_age> age[N]; // age for each respondent int<lower=0,upper=n_edu> edu[N]; // edu for each respondent int<lower=0,upper=n_race> race[N]; // race for each respondent int<lower=0,upper=n_party_id> party_id[N]; // party_id for each respondent int<lower=0,upper=n_dma> dma[N]; // party_id for each respondent
vector[N] vote_prev_dma; // 2012 state-vote at the individual-level int<lower=0,upper=1> y[N]; // vote-choice int<lower=0,upper=N> in_day[N]; // wave of respondents int<lower=0> day_size[n_day]; // N individuals per Wave int<lower=0> day_lims[n_day,2]; // first individual in wave and individuals per wave }parameters { real beta[n_day] ; // coefficient-array for female; wave matrix[n_day,n_age] a_age_raw; // coefficient-matrix for age; wave matrix[n_day,n_edu] a_edu_raw; // coefficient-matrix for edu; wave matrix[n_day,n_race] a_race_raw; // coefficient-matrix for race; wave matrix[n_day,n_party_id] a_party_id_raw ; // coefficient-matrix for party_id; wave matrix[n_day,n_dma] a_dma_raw; // coefficient-matrix for DMA; wave real gamma; // previous vote- slope real alpha; //intercept real <lower=0> sigma_dma; real <lower=0> sigma_age; real <lower=0> sigma_edu; real <lower=0> sigma_race; real <lower=0> sigma_party_id; real <lower=0> sigma_beta_evo; real <lower=0> sigma_dma_evo; real <lower=0> sigma_age_evo; real <lower=0> sigma_edu_evo; real <lower=0> sigma_race_evo; real <lower=0> sigma_party_id_evo;
}transformed parameters{ matrix[n_day,n_age] a_age; //efficient non-centered paramterization matrix[n_day,n_edu] a_edu; matrix[n_day,n_race] a_race; matrix[n_day,n_party_id] a_party_id; matrix[n_day,n_dma] a_dma;
a_age[1]<-a_age_raw[1]*sigma_age; a_edu[1]<-a_edu_raw[1]*sigma_edu; a_race[1]<-a_race_raw[1]*sigma_race; a_party_id[1]<-a_party_id_raw[1]*sigma_party_id; a_dma[1]<-a_dma_raw[1]*sigma_dma;
for (i in 2:n_day){ a_age[i]<-a_age_raw[i]*sigma_age_evo; a_edu[i]<-a_edu_raw[i]*sigma_edu_evo; a_race[i]<-a_race_raw[i]*sigma_race_evo; a_party_id[i]<-a_party_id_raw[i]*sigma_party_id_evo; a_dma[i]<-a_dma_raw[i]*sigma_dma_evo;
}}
model { sigma_dma ~normal(0,2.5); // within_DMA variance can move from 0.1% and 99% sigma_age ~normal(0,2.5); // within_Age variance can move from 0.1% and 99% sigma_edu ~normal(0,2.5); // within_edu variance can move from 0.1% and 99% sigma_race ~normal(0,2.5); // within_race variance can move from 0.1% and 99% sigma_party_id ~normal(0,2.5); // within_party variance can move from 0.1% and 99% sigma_beta_evo ~normal(0,2.5); // Evolution Variance for Female sigma_dma_evo ~normal(0,2.5); // Evolution Variance for DMA sigma_age_evo ~normal(0,2.5); // Evolution Variance for Age sigma_edu_evo ~normal(0,2.5); // Evolution Variance for Edu sigma_race_evo ~normal(0,2.5); // Evolution Variance for race sigma_party_id_evo ~normal(0,2.5); // Evolution Variance for Party gamma~normal(0,5); // embed 95% probability mass between 0.01% and 99.9% alpha~normal(0,5); // embed 95% probability mass between 0.01% and 99.9% / beta[1]~normal(0,5); // embed 95% probability mass between 0.1% and 99% a_dma[1]~ normal(0,1); // embed 95% probability mass between 70% and 30% a_age[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% a_edu[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% a_race[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% a_party_id[1]~normal(0,1); // embed 95% probability mass between 0.01% and 99.9% // Evolution Sampling: beta[2:n_day] ~ student_t(nu, beta[1:(n_day - 1)], 1); to_vector(a_age[2:n_day]) ~ student_t(nu, to_vector(a_age[1:(n_day - 1)]), 1); to_vector(a_edu[2:n_day]) ~ student_t(nu,to_vector( a_edu[1:(n_day - 1)]), 1); to_vector(a_race[2:n_day]) ~ student_t(nu,to_vector( a_race[1:(n_day - 1)]), 1); to_vector(a_party_id[2:n_day]) ~ student_t(nu, to_vector(a_party_id[1:(n_day - 1)]), 1); to_vector(a_dma[2:n_day]) ~ student_t(nu, to_vector(a_dma[1:(n_day - 1)]), 1); data { int<lower=0> N; // sample wave_size int<lower=0> n_age; // number of age categories int<lower=0> n_race; // number of race categories int<lower=0> n_edu; // number of education catoegories int<lower=0> n_division; // number of Divisions int<lower=0> n_states; // number of States int<lower=0> n_party_id; // number of party-categories int<lower=0> n_waves; // number of Waves int<lower=0,upper=n_division> division[N]; // Division for each respondent int<lower=0,upper=1> gender[N]; // Gender each respondent int<lower=0,upper=n_age> age[N]; // age for each respondent int<lower=0,upper=n_edu> edu[N]; // edu for each respondent int<lower=0,upper=n_race> race[N]; // race for each respondent int<lower=0,upper=n_party_id> party_id[N]; // party_id for each respondent real vote_prev_state[N]; // 2012 state-vote at the individual-level int<lower=0,upper=1> y[N]; // vote-choice int<lower=0,upper=N> in_wave[N]; // wave of respondents int<lower=0> wave_size[n_waves]; // N individuals per Wave int<lower=0> wave_lims[n_waves,2]; // first individual in wave and individuals per wave (5*2 matrix)}parameters { real beta [n_waves]; // coefficient-array for female; wave vector[n_age] a_age [n_waves]; // coefficient-array for age; wave vector[n_edu] a_edu [n_waves]; // coefficient-array for edu; wave vector[n_race] a_race [n_waves]; // coefficient-array for race; wave vector[n_party_id] a_party_id [n_waves]; // coefficient-array for party_id; wave vector [n_division] a_division[n_waves]; // coefficient-array for division; wave real gamma; // previous vote- slope real <lower=0> sigma_division; real <lower=0> sigma_age; real <lower=0> sigma_edu; real <lower=0> sigma_race; real <lower=0> sigma_party_id; real <lower=0> sigma_evolution_beta; real <lower=0> sigma_evolution_age; real <lower=0> sigma_evolution_edu; real <lower=0> sigma_evolution_race; real <lower=0> sigma_evolution_party_id; real <lower=0> sigma_evolution_division;
}model { sigma_division ~normal(0,1.25); // within_division variance can move from 0.1% and 99% sigma_age ~normal(0,1.25); // within_division variance can move from 0.1% and 99% sigma_edu ~normal(0,1.25); // within_division variance can move from 0.1% and 99% sigma_race ~normal(0,1.25); // within_division variance can move from 0.1% and 99% sigma_party_id ~normal(0,1.25); // within_division variance can move from 0.1% and 99% sigma_evolution_beta~normal(0,0.1); sigma_evolution_age~normal(0,0.1); sigma_evolution_edu~normal(0,0.1); sigma_evolution_race~normal(0,0.1); sigma_evolution_party_id~normal(0,0.1); sigma_evolution_division~normal(0,0.1);
beta[1]~normal(0,2.5); // embed 95% probability mass between 0.1% and 99% gamma~normal(0,2.5); // embed 95% probability mass between 0.01% and 99.9% // (Gelman 2008)
a_division[1]~ normal(0,sigma_division); // embed 95% probability mass between 70% and 30% a_age[1]~normal(0,sigma_age); // embed 95% probability mass between 0.01% and 99.9% a_edu[1]~normal(0,sigma_edu); // embed 95% probability mass between 0.01% and 99.9% a_race[1]~normal(0,sigma_race); // embed 95% probability mass between 0.01% and 99.9% a_party_id[1]~normal(0,sigma_party_id); // embed 95% probability mass between 0.01% and 99.9%
for (k in 2:n_waves) { beta[k] ~ normal(beta[k-1], sigma_evolution_beta); // assume 5% shift per demographic per wave covers // 95% of probability mass a_age[k] ~ normal(a_age[k-1], sigma_evolution_age); // assume 5% shift per demographic per wave covers // 95% of probability mass a_edu[k] ~ normal(a_edu[k-1], sigma_evolution_edu); // assume 5% shift per demographic per wave covers // 95% of probability mass a_race[k] ~ normal(a_race[k-1],sigma_evolution_race); // assume 5% shift per demographic per wave covers // 95% of probability mass a_party_id[k] ~ normal(a_party_id[k-1], sigma_evolution_party_id); // assume 5% shift per demographic per wave covers // 95% of probability mass a_division[k] ~ normal(a_division[k-1], sigma_evolution_division);
} //Likelihood for each i in each wavefor(s in 1:n_waves){ int obs_in_wave[wave_size[s]];
obs_in_wave<-segment(in_wave,wave_lims[s,1],wave_lims[s,2]) ; // find observations in each wave for (i in 1:wave_size[s]) {real y_hat; y_hat <- beta[s]*gender[obs_in_wave[i]] +a_age[s,age[obs_in_wave[i]]] +a_edu[s,edu[obs_in_wave[i]]] +a_party_id[s,party_id[obs_in_wave[i]]] +a_race[s,race[obs_in_wave[i]]]+gamma*vote_prev_state[i] +a_division[s,division[obs_in_wave[i]]]; y[obs_in_wave[i]] ~ bernoulli_logit(y_hat); } } }