Estimate excess variance compared to SRS in a multilevel model with brms

I started using brms very recently instead of stan, trying to estimate bias in election polls based on a model by Shirani-Mehr et al. (2018). For this purpose I have data on presidential state level polls nested in state level elections, including the republican two party vote share (poll), the actual election result (vote), the sample size(n) and some covariates. For this purpose I assume, that the poll result is the election result + x. Further, the excess variance above SRS should be estimated. The model looks as follows:

poll_i \sim\mathcal{N(p_i,\sigma_i^2)}
logit(p_{i}) = logit(vote_{state\_election[i]}) + x_{state\_election[i]}
\sigma_i^2 = \frac{p_i*(1-p_i)}{n} + excess_{state\_election[i]}

The model block of the stan code for it should look similar to:

model {
...
 for(i in 1:N){
       logit_p[i] = logit_vote[stateElection[i]] +  x[stateElection[i]] ;
       p[i] = inv_logit(logit_p[i]);
     poll[i] ~  normal(p[i],  sqrt(p[i] * (1 - p[i]) / n[i] + excess[stateElection[i]]));
   }

} 

I was wondering how to access mu (which corresponds to p in my model ) to estimate sigma

My attempts were not very fruitful so far:

formula <- bf(poll ~ 0 + logit_scaled(vote) + (1  |stateE[quote="Sina, post:1, topic:15575, full:true"]

I started using brms very recently instead of stan, trying to estimate bias in election polls based on a model by Shirani-Mehr et al. (2018). For this purpose I have data on presidential state level polls nested in state level elections, including the republican two party vote share (poll), the actual election result (vote), the sample size(n) and some covariates. For this purpose I assume, that the poll result is the election result + x. Further, the excess variance above SRS should be estimated. The model looks as follows:

poll_i \sim\mathcal{N(p_i,\sigma_i^2)}
logit(p_{i}) = logit(vote_{state\_election[i]}) + x_{state\_election[i]}
\sigma_i^2 = \frac{p_i*(1-p_i)}{n} + excess_{state\_election[i]}

The model block of the stan code for it should look similar to:

model {
...
 for(i in 1:N){
       logit_p[i] = logit_vote[stateElection[i]] +  x[stateElection[i]] ;
       p[i] = inv_logit(logit_p[i]);
     poll[i] ~  normal(p[i],  sqrt(p[i] * (1 - p[i]) / n[i] + excess[stateElection[i]]));
   }

} 

I was wondering how to access mu (which corresponds to p in my model ) to estimate sigma

My attempts were not very fruitful so far:

formula <- bf(poll ~ 0 + logit_scaled(vote) + (1 |stateElection),
                       sigma ~ 1 + (1| stateElection))

I think the “generated quantities” block should get you that. I don’t know enough Stan to be able to help you, but if you follow the Stan user guide on setting up the generated quantities block, you should be able to access any paramteter from your model.

You can use non-linear parameters for that purpose e.g. bf(mu ~ a, nl= TRUE, a ~ …, nlf(sigma ~ a * b), b ~ …)

Great, thank you very much!