6  Chapter17-PartB

6.1 Multilevel logistic regression

6.1.1 Data Preparation

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library ("arm")
Loading required package: MASS

Attaching package: 'MASS'

The following object is masked from 'package:dplyr':

    select

Loading required package: Matrix

Attaching package: 'Matrix'

The following objects are masked from 'package:tidyr':

    expand, pack, unpack

Loading required package: lme4

arm (Version 1.14-4, built: 2024-4-1)

Working directory is /home/lbarboza/Dropbox/Cursos/Actuales/SP1653/NotasClase/ModelosMixtos/ModelosMixtos
data (state)                  # "state" is an R data file
state.abbr <- c (state.abb[1:8], "DC", state.abb[9:50])
dc <- 9
not.dc <- c(1:8,10:51)
region <- c(3,4,4,3,4,4,1,1,5,3,3,4,4,2,2,2,2,3,3,1,1,1,2,2,3,2,4,2,4,1,1,4,1,3,2,2,3,4,1,1,3,2,3,3,4,1,3,4,1,2,4)

library(haven)
polls <- read_dta("data/ARM_Data/election88/polls.dta")


polls.subset <- polls %>% filter(survey == 8)
polls.subset <- polls.subset %>% rename (y = bush)

presvote <- read_dta('data/ARM_Data/election88/presvote.dta')
v.prev <- presvote$g76_84pr
not.dc <- c(1:8,10:51)
candidate.effects <- read.table("data/ARM_Data/election88/candidate_effects.dat",
                                header = T)

v.prev[not.dc] <- v.prev[not.dc] +
  (candidate.effects$X76 + candidate.effects$X80 + candidate.effects$X84)/3

n.edu <- max(polls.subset$edu)

# Reparametrizacion de la interacción
polls.subset <- polls.subset %>% mutate(age.edu = n.edu*(age-1) + edu,
                                        v.prev.full = v.prev[state],
                                        region.full = region[state])

6.1.2 Model Specification

Simple Model with Demographic and Geographic Variation

We model survey responses, where each response \(y_i\) is coded as: - \(y_i = 1\) for supporters of the Republican candidate - \(y_i = 0\) for supporters of the Democrat

Undecideds are excluded, and we assume the responses are independent, with: \[ \Pr(y_i = 1) = \text{logit}^{-1}(X_i \beta) \]

6.1.3 Model Inputs

The input variables include: - State index \(j[i]\): to account for geographic variation. - Demographic predictors: categorical variables for sex, ethnicity, age, and education (used by CBS in survey weighting).

6.1.4 Multilevel Logistic Regression Example

We demonstrate multilevel logistic regression with: - Two individual predictors: female and black - 51 states with varying intercepts

\[ \Pr(y_i = 1) = \text{logit}^{-1}(\alpha_{j[i]} + \beta_{\text{female}} \cdot \text{female}_i + \beta_{\text{black}} \cdot \text{black}_i), \quad \text{for } i = 1, \dots, n \]

where: - \(\alpha_j \sim N(\mu_\alpha, \sigma_{\text{state}}^2)\) for each state \(j = 1, \dots, 51\)

This model structure captures both individual demographic effects and state-level geographic variation in political support.

6.2 Fuller Model Including Non-Nested Factors

This expanded model incorporates a comprehensive set of demographic and geographic predictors used in CBS survey weighting.

6.2.1 Model Structure

We include: - Demographic Predictors: - Sex × Ethnicity and Age × Education interactions - Four age categories and four education categories with varying intercepts - A 16-level interaction between age and education - State-level Predictors: - Indicators for the 5 regions of the country (Northeast, Midwest, South, West, and D.C.) - v.prev: Average Republican vote share in the three previous elections, adjusted for home-state and home-region effects

The model uses indices \(j\), \(k\), \(l\), and \(m\) for state, age category, education category, and region, respectively.

6.2.2 Model Specification

The probability of supporting the Republican candidate is given by:

\[ \Pr(y_i = 1) = \text{logit}^{-1}(\beta_0 + \beta_{\text{female}} \cdot \text{female}_i + \beta_{\text{black}} \cdot \text{black}_i + \beta_{\text{female.black}} \cdot \text{female}_i \cdot \text{black}_i + \alpha^{\text{age}}_{k[i]} + \alpha^{\text{edu}}_{l[i]} + \alpha^{\text{age.edu}}_{k[i],l[i]} + \alpha^{\text{state}}_{j[i]}) \]

where: - \(\alpha^{\text{state}}_j \sim N(\alpha^{\text{region}}_{m[j]} + \beta_{\text{v.prev}} \cdot \text{v.prev}_j, \sigma_{\text{state}}^2)\)

6.2.3 Multilevel Coefficients

The multilevel coefficients have the following distributions:

\[ \alpha^{\text{age}}_k \sim N(0, \sigma_{\text{age}}^2), \quad \text{for } k = 1, \dots, 4 \] \[ \alpha^{\text{edu}}_l \sim N(0, \sigma_{\text{edu}}^2), \quad \text{for } l = 1, \dots, 4 \] \[ \alpha^{\text{age.edu}}_{k,l} \sim N(0, \sigma_{\text{age.edu}}^2), \quad \text{for } k, l = 1, \dots, 4 \] \[ \alpha^{\text{region}}_m \sim N(0, \sigma_{\text{region}}^2), \quad \text{for } m = 1, \dots, 5 \]

6.2.4 Summary

This model captures a complex structure of individual and group-level interactions, enabling us to account for detailed demographic and geographic variations in voting behavior.

6.2.5 Fitting the Model

# Load necessary library
library(R2jags)
Loading required package: rjags
Loading required package: coda

Attaching package: 'coda'
The following object is masked from 'package:arm':

    traceplot
Linked to JAGS 4.3.2
Loaded modules: basemod,bugs

Attaching package: 'R2jags'
The following object is masked from 'package:coda':

    traceplot
The following object is masked from 'package:arm':

    traceplot
attach(polls.subset)
The following object is masked from package:MASS:

    survey
# Adjust `state` to be within 1 to n.state if needed
state <- as.numeric(factor(state))  # Convert to consecutive integers starting from 1

data_jags <- list(
  y = y,
  female = female,
  black = black,
  age = age,
  edu = edu,
  state = state,  # Adjusted state
  region = region,
  v.prev = v.prev,
  n = length(y),
  n.age = length(unique(age)),
  n.edu = length(unique(edu)),
  n.state = length(unique(state)),
  n.region = length(unique(region))
)


# Initial values for the parameters
inits <- function() {
  list(
    b.0 = rnorm(1),
    b.female = rnorm(1),
    b.black = rnorm(1),
    b.female.black = rnorm(1),
    b.age = rnorm(data_jags$n.age),
    b.edu = rnorm(data_jags$n.edu),
    b.age.edu = matrix(rnorm(data_jags$n.age * data_jags$n.edu), nrow = data_jags$n.age),
    b.state = rnorm(data_jags$n.state),
    b.region = rnorm(data_jags$n.region),
    b.v.prev = rnorm(1),
    sigma.age = runif(1, 0, 100),
    sigma.edu = runif(1, 0, 100),
    sigma.age.edu = runif(1, 0, 100),
    sigma.state = runif(1, 0, 100),
    sigma.region = runif(1, 0, 100)
  )
}

# Parameters to monitor
params <- c("b.0", "b.female", "b.black", "b.female.black", "b.age", "b.edu", 
            "b.age.edu", "b.state", "b.region", "b.v.prev", 
            "sigma.age", "sigma.edu", "sigma.age.edu", "sigma.state", "sigma.region")

# Fit the model with JAGS
jags_fit <- jags(
  data = data_jags, 
  inits = inits, 
  parameters.to.save = params, 
  model.file = "codigoJAGS/logistic.jags", 
  n.chains = 3, 
  n.iter = 5000, 
  n.burnin = 1000, 
  n.thin = 10
)
module glm loaded
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 2015
   Unobserved stochastic nodes: 266
   Total graph size: 17210

Initializing model
# View summary of the model fit
print(jags_fit)
Inference for Bugs model at "codigoJAGS/logistic.jags", fit using jags,
 3 chains, each with 5000 iterations (first 1000 discarded), n.thin = 10
 n.sims = 1200 iterations saved
                mu.vect sd.vect     2.5%      25%      50%      75%    97.5%
b.0              -0.284   0.935   -2.336   -0.802   -0.147    0.405    1.096
b.age[1]          0.062   0.144   -0.217   -0.009    0.039    0.137    0.365
b.age[2]         -0.072   0.142   -0.401   -0.135   -0.049    0.004    0.183
b.age[3]          0.036   0.139   -0.245   -0.025    0.022    0.105    0.323
b.age[4]         -0.045   0.148   -0.364   -0.104   -0.023    0.025    0.214
b.age.edu[1,1]   -0.041   0.165   -0.422   -0.116   -0.015    0.040    0.265
b.age.edu[2,1]    0.083   0.165   -0.169   -0.010    0.045    0.162    0.501
b.age.edu[3,1]    0.018   0.146   -0.267   -0.052    0.008    0.081    0.359
b.age.edu[4,1]   -0.174   0.203   -0.679   -0.285   -0.120   -0.016    0.080
b.age.edu[1,2]    0.067   0.132   -0.141   -0.011    0.044    0.136    0.393
b.age.edu[2,2]   -0.102   0.143   -0.454   -0.186   -0.072   -0.003    0.128
b.age.edu[3,2]    0.016   0.131   -0.257   -0.048    0.006    0.080    0.315
b.age.edu[4,2]   -0.011   0.134   -0.317   -0.074   -0.005    0.057    0.263
b.age.edu[1,3]    0.069   0.154   -0.185   -0.015    0.035    0.138    0.448
b.age.edu[2,3]   -0.013   0.135   -0.313   -0.079   -0.007    0.054    0.274
b.age.edu[3,3]    0.012   0.133   -0.255   -0.053    0.003    0.068    0.326
b.age.edu[4,3]    0.071   0.167   -0.224   -0.020    0.038    0.145    0.481
b.age.edu[1,4]   -0.001   0.138   -0.299   -0.071    0.000    0.064    0.277
b.age.edu[2,4]   -0.032   0.126   -0.305   -0.100   -0.015    0.035    0.197
b.age.edu[3,4]   -0.002   0.134   -0.280   -0.065   -0.003    0.065    0.267
b.age.edu[4,4]    0.056   0.149   -0.174   -0.027    0.023    0.122    0.411
b.black          -1.658   0.323   -2.311   -1.873   -1.648   -1.453   -1.058
b.edu[1]         -0.182   0.307   -1.058   -0.287   -0.124   -0.013    0.268
b.edu[2]         -0.045   0.274   -0.778   -0.121   -0.019    0.057    0.445
b.edu[3]          0.142   0.287   -0.500    0.008    0.115    0.263    0.744
b.edu[4]          0.005   0.278   -0.734   -0.076    0.008    0.104    0.579
b.female         -0.090   0.100   -0.292   -0.157   -0.094   -0.025    0.104
b.female.black   -0.207   0.427   -1.079   -0.485   -0.221    0.081    0.636
b.region[1]      -0.225   0.327   -1.029   -0.383   -0.168   -0.028    0.273
b.region[2]       0.022   0.300   -0.683   -0.089    0.022    0.150    0.598
b.region[3]       0.071   0.302   -0.581   -0.054    0.057    0.212    0.643
b.region[4]      -0.013   0.309   -0.725   -0.121    0.003    0.115    0.542
b.region[5]       0.315   0.584   -0.397   -0.016    0.114    0.484    1.896
b.state[1]        1.385   1.014   -0.152    0.573    1.240    2.046    3.523
b.state[2]        1.030   1.009   -0.568    0.263    0.890    1.686    3.145
b.state[3]        0.682   1.043   -1.000   -0.103    0.524    1.374    2.878
b.state[4]        0.619   0.948   -0.860   -0.107    0.464    1.241    2.601
b.state[5]        0.665   0.977   -0.876   -0.068    0.515    1.310    2.701
b.state[6]        0.869   0.998   -0.728    0.085    0.725    1.555    2.949
b.state[7]        0.340   1.023   -1.352   -0.435    0.194    1.040    2.403
b.state[8]        0.446   1.027   -1.287   -0.329    0.346    1.127    2.583
b.state[9]        0.832   0.923   -0.499    0.093    0.647    1.414    2.749
b.state[10]       1.062   1.013   -0.506    0.292    0.903    1.733    3.214
b.state[11]       0.583   0.986   -1.165   -0.112    0.445    1.208    2.661
b.state[12]       0.435   0.943   -0.990   -0.295    0.286    1.048    2.388
b.state[13]       1.007   1.039   -0.636    0.206    0.884    1.709    3.165
b.state[14]       0.294   0.986   -1.278   -0.432    0.133    0.946    2.307
b.state[15]       1.153   1.018   -0.496    0.389    0.984    1.833    3.259
b.state[16]       0.910   0.959   -0.616    0.174    0.791    1.513    2.942
b.state[17]       0.938   1.020   -0.686    0.185    0.774    1.596    3.134
b.state[18]       0.591   0.968   -1.002   -0.126    0.454    1.234    2.603
b.state[19]       0.231   1.006   -1.433   -0.509    0.077    0.904    2.345
b.state[20]       0.194   0.959   -1.298   -0.541    0.060    0.880    2.184
b.state[21]       0.493   0.942   -0.964   -0.227    0.333    1.154    2.413
b.state[22]       0.156   0.935   -1.433   -0.524    0.018    0.735    2.086
b.state[23]       1.191   0.998   -0.396    0.445    1.048    1.848    3.314
b.state[24]       0.650   0.957   -0.857   -0.085    0.494    1.311    2.657
b.state[25]       0.731   1.034   -0.936   -0.007    0.575    1.446    2.934
b.state[26]       0.745   0.991   -0.828   -0.002    0.589    1.447    2.832
b.state[27]       0.861   1.018   -0.745    0.101    0.704    1.531    3.083
b.state[28]       0.957   1.186   -0.953    0.113    0.779    1.745    3.428
b.state[29]       0.808   0.981   -0.770    0.061    0.647    1.444    2.784
b.state[30]       0.467   1.048   -1.231   -0.315    0.315    1.188    2.662
b.state[31]       0.255   0.953   -1.217   -0.496    0.091    0.884    2.232
b.state[32]       1.151   0.958   -0.334    0.417    1.007    1.796    3.130
b.state[33]       0.529   0.982   -1.128   -0.198    0.410    1.228    2.598
b.state[34]       1.064   0.967   -0.392    0.324    0.916    1.704    3.086
b.state[35]       0.911   1.055   -0.705    0.094    0.754    1.597    3.104
b.state[36]       0.348   0.986   -1.289   -0.353    0.208    0.986    2.382
b.state[37]       0.600   0.974   -0.872   -0.133    0.420    1.230    2.632
b.state[38]       0.489   0.979   -1.172   -0.228    0.379    1.103    2.516
b.state[39]       0.983   0.983   -0.582    0.234    0.844    1.638    3.007
b.state[40]       0.432   0.948   -1.134   -0.254    0.314    1.086    2.352
b.state[41]       1.468   0.996   -0.089    0.711    1.319    2.123    3.589
b.state[42]       0.908   0.971   -0.572    0.143    0.753    1.559    2.874
b.state[43]       1.376   1.041   -0.270    0.609    1.211    2.094    3.596
b.state[44]       1.039   1.092   -0.705    0.224    0.882    1.774    3.376
b.state[45]       1.269   1.033   -0.363    0.448    1.110    1.962    3.422
b.state[46]       0.606   0.969   -0.926   -0.138    0.474    1.272    2.562
b.state[47]       0.840   1.028   -0.799    0.052    0.669    1.501    2.982
b.state[48]       0.440   0.951   -1.090   -0.275    0.303    1.059    2.386
b.state[49]       0.613   0.942   -1.005   -0.060    0.527    1.238    2.586
b.v.prev          1.389   1.688   -1.243    0.120    1.084    2.484    5.072
sigma.age         0.183   0.189    0.008    0.064    0.136    0.235    0.657
sigma.age.edu     0.150   0.103    0.010    0.070    0.133    0.208    0.389
sigma.edu         0.333   0.388    0.009    0.110    0.221    0.396    1.479
sigma.region      0.442   0.457    0.021    0.147    0.293    0.593    1.736
sigma.state       0.430   0.106    0.245    0.357    0.421    0.495    0.666
deviance       2609.503  11.547 2589.728 2600.830 2608.812 2616.853 2633.525
                Rhat n.eff
b.0            1.534     7
b.age[1]       1.008  1100
b.age[2]       1.020  1200
b.age[3]       1.013   480
b.age[4]       1.026   300
b.age.edu[1,1] 1.002  1200
b.age.edu[2,1] 1.002   870
b.age.edu[3,1] 1.001  1200
b.age.edu[4,1] 1.006   310
b.age.edu[1,2] 1.004   960
b.age.edu[2,2] 1.007   270
b.age.edu[3,2] 1.005   390
b.age.edu[4,2] 1.003  1200
b.age.edu[1,3] 1.012   160
b.age.edu[2,3] 1.001  1200
b.age.edu[3,3] 1.002  1200
b.age.edu[4,3] 1.008   320
b.age.edu[1,4] 1.001  1200
b.age.edu[2,4] 1.002   760
b.age.edu[3,4] 1.003  1200
b.age.edu[4,4] 1.004  1200
b.black        1.007   300
b.edu[1]       1.081    45
b.edu[2]       1.079    47
b.edu[3]       1.036    81
b.edu[4]       1.062    49
b.female       1.001  1200
b.female.black 1.001  1200
b.region[1]    1.023   860
b.region[2]    1.041   900
b.region[3]    1.039   630
b.region[4]    1.034  1200
b.region[5]    1.230    15
b.state[1]     1.586     7
b.state[2]     1.611     7
b.state[3]     1.574     7
b.state[4]     1.638     6
b.state[5]     1.594     7
b.state[6]     1.603     7
b.state[7]     1.553     7
b.state[8]     1.492     8
b.state[9]     1.642     6
b.state[10]    1.645     6
b.state[11]    1.526     7
b.state[12]    1.623     6
b.state[13]    1.620     7
b.state[14]    1.554     7
b.state[15]    1.582     7
b.state[16]    1.582     7
b.state[17]    1.587     7
b.state[18]    1.538     7
b.state[19]    1.576     7
b.state[20]    1.626     6
b.state[21]    1.649     6
b.state[22]    1.606     7
b.state[23]    1.570     7
b.state[24]    1.585     7
b.state[25]    1.509     7
b.state[26]    1.580     7
b.state[27]    1.514     7
b.state[28]    1.519     7
b.state[29]    1.653     6
b.state[30]    1.578     7
b.state[31]    1.632     6
b.state[32]    1.575     7
b.state[33]    1.518     7
b.state[34]    1.619     7
b.state[35]    1.569     7
b.state[36]    1.549     7
b.state[37]    1.637     6
b.state[38]    1.535     7
b.state[39]    1.611     7
b.state[40]    1.503     7
b.state[41]    1.585     7
b.state[42]    1.636     6
b.state[43]    1.531     7
b.state[44]    1.506     7
b.state[45]    1.638     6
b.state[46]    1.653     6
b.state[47]    1.630     6
b.state[48]    1.606     7
b.state[49]    1.450     8
b.v.prev       1.626     6
sigma.age      1.008   250
sigma.age.edu  1.016   220
sigma.edu      1.029   100
sigma.region   1.047    52
sigma.state    1.017  1000
deviance       1.002  1100

For each parameter, n.eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor (at convergence, Rhat=1).

DIC info (using the rule, pD = var(deviance)/2)
pD = 66.6 and DIC = 2676.2
DIC is an estimate of expected predictive error (lower deviance is better).

6.2.6 Plots

library(CalvinBayes)
Loading required package: ggformula
Loading required package: scales

Attaching package: 'scales'
The following object is masked from 'package:arm':

    rescale
The following object is masked from 'package:purrr':

    discard
The following object is masked from 'package:readr':

    col_factor
Loading required package: ggridges

New to ggformula?  Try the tutorials: 
    learnr::run_tutorial("introduction", package = "ggformula")
    learnr::run_tutorial("refining", package = "ggformula")
Loading required package: bayesplot
This is bayesplot version 1.11.1
- Online documentation and vignettes at mc-stan.org/bayesplot
- bayesplot theme set to bayesplot::theme_default()
   * Does _not_ affect other ggplot2 plots
   * See ?bayesplot_theme_set for details on theme setting

Attaching package: 'CalvinBayes'
The following object is masked from 'package:bayesplot':

    rhat
The following object is masked from 'package:datasets':

    HairEyeColor
diag_mcmc(as.mcmc(jags_fit),parName = "b.0")

diag_mcmc(as.mcmc(jags_fit),parName = "b.region[1]")

6.3 Logistic model with redundant parameters (centered)

# Prepare data for JAGS
data_jags <- list(
  y = y,
  female = female,
  black = black,
  age = age,
  edu = edu,
  state = state,
  region = region,
  v.prev = v.prev,
  n = length(y),
  n.age = length(unique(age)),
  n.edu = length(unique(edu)),
  n.state = length(unique(state)),
  n.region = length(unique(region))
)

# Initial values for the parameters
inits <- function() {
  list(
    b.0 = rnorm(1),
    b.female = rnorm(1),
    b.black = rnorm(1),
    b.female.black = rnorm(1),
    b.age = rnorm(data_jags$n.age),
    b.edu = rnorm(data_jags$n.edu),
    b.age.edu = matrix(rnorm(data_jags$n.age * data_jags$n.edu), nrow = data_jags$n.age),
    b.state = rnorm(data_jags$n.state),
    b.region = rnorm(data_jags$n.region),
    b.v.prev = rnorm(1),
    sigma.age = runif(1, 0, 100),
    sigma.edu = runif(1, 0, 100),
    sigma.age.edu = runif(1, 0, 100),
    sigma.state = runif(1, 0, 100),
    sigma.region = runif(1, 0, 100),
    mu.age = rnorm(1),
    mu.edu = rnorm(1),
    mu.age.edu = rnorm(1),
    mu.region = rnorm(1)
  )
}

# Parameters to monitor
params <- c("mu.adj", "b.0", "b.female", "b.black", "b.female.black", 
            "b.age.adj", "b.edu.adj", "b.age.edu.adj", "b.state", "b.region.adj", 
            "b.v.prev", "sigma.age", "sigma.edu", "sigma.age.edu", "sigma.state", 
            "sigma.region", "mu.age", "mu.edu", "mu.age.edu", "mu.region")

# Fit the model with JAGS
jags_fit <- jags(
  data = data_jags, 
  inits = inits, 
  parameters.to.save = params, 
  model.file = "codigoJAGS/logistic_centered.jags",  # Replace with the actual model file name
  n.chains = 3, 
  n.iter = 5000, 
  n.burnin = 1000, 
  n.thin = 10
)
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 2015
   Unobserved stochastic nodes: 270
   Total graph size: 17254

Initializing model
# View summary of the model fit
print(jags_fit)
Inference for Bugs model at "codigoJAGS/logistic_centered.jags", fit using jags,
 3 chains, each with 5000 iterations (first 1000 discarded), n.thin = 10
 n.sims = 1200 iterations saved
                    mu.vect sd.vect     2.5%      25%      50%      75%
b.0                  -2.247   2.524   -6.520   -4.758   -1.826   -0.098
b.age.adj[1]          0.068   0.091   -0.088    0.003    0.053    0.123
b.age.adj[2]         -0.060   0.087   -0.255   -0.114   -0.043   -0.002
b.age.adj[3]          0.038   0.085   -0.120   -0.010    0.024    0.084
b.age.adj[4]         -0.045   0.091   -0.259   -0.094   -0.026    0.009
b.age.edu.adj[1,1]   -0.045   0.146   -0.411   -0.112   -0.022    0.031
b.age.edu.adj[2,1]    0.078   0.150   -0.156   -0.011    0.046    0.145
b.age.edu.adj[3,1]    0.025   0.138   -0.264   -0.042    0.008    0.094
b.age.edu.adj[4,1]   -0.168   0.201   -0.659   -0.273   -0.115   -0.015
b.age.edu.adj[1,2]    0.069   0.124   -0.144   -0.006    0.044    0.135
b.age.edu.adj[2,2]   -0.104   0.133   -0.406   -0.181   -0.076   -0.007
b.age.edu.adj[3,2]    0.016   0.119   -0.228   -0.043    0.007    0.074
b.age.edu.adj[4,2]   -0.010   0.121   -0.269   -0.074   -0.004    0.056
b.age.edu.adj[1,3]    0.060   0.137   -0.164   -0.024    0.032    0.127
b.age.edu.adj[2,3]   -0.021   0.132   -0.322   -0.084   -0.012    0.045
b.age.edu.adj[3,3]    0.002   0.135   -0.284   -0.063   -0.001    0.070
b.age.edu.adj[4,3]    0.072   0.156   -0.193   -0.016    0.041    0.143
b.age.edu.adj[1,4]    0.002   0.126   -0.266   -0.063    0.001    0.065
b.age.edu.adj[2,4]   -0.038   0.118   -0.293   -0.105   -0.019    0.029
b.age.edu.adj[3,4]    0.004   0.123   -0.265   -0.060    0.000    0.066
b.age.edu.adj[4,4]    0.059   0.145   -0.199   -0.019    0.030    0.130
b.black              -1.656   0.340   -2.333   -1.883   -1.661   -1.425
b.edu.adj[1]         -0.166   0.137   -0.438   -0.264   -0.159   -0.059
b.edu.adj[2]         -0.034   0.086   -0.215   -0.084   -0.028    0.019
b.edu.adj[3]          0.172   0.118   -0.027    0.084    0.171    0.255
b.edu.adj[4]          0.027   0.096   -0.154   -0.031    0.020    0.083
b.female             -0.087   0.095   -0.273   -0.154   -0.086   -0.023
b.female.black       -0.211   0.432   -1.051   -0.508   -0.192    0.086
b.region.adj[1]      -0.224   0.198   -0.658   -0.360   -0.192   -0.058
b.region.adj[2]      -0.001   0.141   -0.306   -0.073    0.005    0.079
b.region.adj[3]       0.047   0.147   -0.269   -0.025    0.038    0.123
b.region.adj[4]      -0.025   0.159   -0.419   -0.092   -0.006    0.061
b.region.adj[5]       0.202   0.384   -0.309   -0.010    0.091    0.324
b.state[1]            1.989   2.293   -1.792   -0.031    1.980    4.145
b.state[2]            1.615   2.294   -2.083   -0.527    1.622    3.793
b.state[3]            1.270   2.309   -2.467   -0.776    1.277    3.398
b.state[4]            1.222   2.279   -2.439   -0.916    1.216    3.420
b.state[5]            1.256   2.295   -2.546   -0.918    1.288    3.413
b.state[6]            1.440   2.288   -2.294   -0.680    1.453    3.572
b.state[7]            0.944   2.295   -2.945   -1.148    0.932    3.040
b.state[8]            1.046   2.330   -2.828   -0.962    1.006    3.200
b.state[9]            1.432   2.278   -2.161   -0.754    1.447    3.582
b.state[10]           1.633   2.278   -2.079   -0.382    1.651    3.791
b.state[11]           1.192   2.326   -2.769   -0.865    1.230    3.390
b.state[12]           1.042   2.263   -2.619   -1.096    1.040    3.217
b.state[13]           1.599   2.290   -2.221   -0.392    1.620    3.720
b.state[14]           0.898   2.272   -2.922   -1.173    0.950    3.022
b.state[15]           1.744   2.292   -2.000   -0.242    1.734    3.856
b.state[16]           1.512   2.283   -2.220   -0.455    1.525    3.652
b.state[17]           1.543   2.291   -2.191   -0.549    1.516    3.683
b.state[18]           1.181   2.302   -2.647   -0.893    1.219    3.342
b.state[19]           0.819   2.297   -3.013   -1.197    0.834    2.962
b.state[20]           0.795   2.272   -2.870   -1.378    0.798    2.949
b.state[21]           1.103   2.276   -2.500   -0.966    1.083    3.265
b.state[22]           0.755   2.286   -2.986   -1.280    0.719    2.940
b.state[23]           1.798   2.282   -1.953   -0.182    1.764    3.993
b.state[24]           1.253   2.300   -2.492   -0.906    1.250    3.454
b.state[25]           1.303   2.298   -2.497   -0.713    1.303    3.490
b.state[26]           1.356   2.290   -2.454   -0.660    1.408    3.538
b.state[27]           1.456   2.320   -2.427   -0.505    1.495    3.601
b.state[28]           1.554   2.311   -2.335   -0.430    1.615    3.635
b.state[29]           1.397   2.284   -2.269   -0.697    1.401    3.556
b.state[30]           1.048   2.302   -2.684   -1.090    1.034    3.204
b.state[31]           0.859   2.272   -2.753   -1.275    0.838    3.024
b.state[32]           1.744   2.305   -1.926   -0.321    1.728    3.943
b.state[33]           1.162   2.294   -2.652   -0.928    1.090    3.299
b.state[34]           1.660   2.280   -1.976   -0.438    1.635    3.815
b.state[35]           1.500   2.296   -2.270   -0.684    1.491    3.634
b.state[36]           0.946   2.277   -2.769   -1.096    0.959    3.045
b.state[37]           1.196   2.266   -2.435   -0.910    1.223    3.359
b.state[38]           1.101   2.298   -2.623   -1.035    1.120    3.247
b.state[39]           1.619   2.310   -2.122   -0.455    1.602    3.821
b.state[40]           1.090   2.315   -2.766   -0.875    1.084    3.278
b.state[41]           2.054   2.291   -1.672   -0.038    2.064    4.230
b.state[42]           1.489   2.273   -2.128   -0.595    1.530    3.655
b.state[43]           1.961   2.304   -1.839    0.043    1.925    4.123
b.state[44]           1.620   2.294   -2.254   -0.386    1.627    3.750
b.state[45]           1.859   2.295   -1.874   -0.283    1.861    4.027
b.state[46]           1.215   2.302   -2.493   -0.917    1.189    3.406
b.state[47]           1.432   2.297   -2.303   -0.614    1.426    3.577
b.state[48]           1.049   2.263   -2.585   -0.981    1.063    3.206
b.state[49]           1.252   2.325   -2.619   -0.734    1.255    3.415
b.v.prev              1.011   1.310   -1.466    0.195    0.942    1.682
mu.adj                0.439   0.085    0.277    0.381    0.439    0.494
mu.age                0.822   1.960   -3.004   -0.546    1.496    2.129
mu.age.edu           -0.342   0.526   -1.182   -0.702   -0.410   -0.039
mu.edu                0.866   2.109   -2.890   -0.468    0.830    1.685
mu.region             0.844   2.383   -3.269   -1.027    0.756    2.896
sigma.age             0.192   0.208    0.005    0.061    0.130    0.240
sigma.age.edu         0.148   0.095    0.014    0.073    0.136    0.201
sigma.edu             0.346   0.379    0.019    0.128    0.238    0.412
sigma.region          0.382   0.514    0.015    0.122    0.248    0.441
sigma.state           0.429   0.102    0.241    0.356    0.424    0.493
deviance           2608.893  11.378 2589.207 2600.737 2608.226 2616.198
                      97.5%  Rhat n.eff
b.0                   1.760 4.193     3
b.age.adj[1]          0.272 1.002  1000
b.age.adj[2]          0.085 1.002  1200
b.age.adj[3]          0.222 1.002  1200
b.age.adj[4]          0.110 1.000  1200
b.age.edu.adj[1,1]    0.220 1.009  1200
b.age.edu.adj[2,1]    0.444 1.015   200
b.age.edu.adj[3,1]    0.342 1.009  1200
b.age.edu.adj[4,1]    0.081 1.009   220
b.age.edu.adj[1,2]    0.356 1.018   140
b.age.edu.adj[2,2]    0.105 1.013   220
b.age.edu.adj[3,2]    0.266 1.002  1200
b.age.edu.adj[4,2]    0.237 1.010   830
b.age.edu.adj[1,3]    0.386 1.005  1200
b.age.edu.adj[2,3]    0.252 1.008   420
b.age.edu.adj[3,3]    0.285 1.011   510
b.age.edu.adj[4,3]    0.453 1.001  1200
b.age.edu.adj[1,4]    0.266 1.009   670
b.age.edu.adj[2,4]    0.180 1.004   690
b.age.edu.adj[3,4]    0.273 1.004   500
b.age.edu.adj[4,4]    0.401 1.007   900
b.black              -1.011 1.003   650
b.edu.adj[1]          0.051 1.000  1200
b.edu.adj[2]          0.135 1.001  1200
b.edu.adj[3]          0.404 1.005   390
b.edu.adj[4]          0.225 1.003   640
b.female              0.096 1.002  1200
b.female.black        0.571 1.004   510
b.region.adj[1]       0.058 1.004   460
b.region.adj[2]       0.277 1.004   510
b.region.adj[3]       0.332 1.000  1200
b.region.adj[4]       0.272 1.002  1200
b.region.adj[5]       1.312 1.006  1100
b.state[1]            5.563 4.351     3
b.state[2]            5.181 4.291     3
b.state[3]            4.990 4.179     3
b.state[4]            4.756 4.505     3
b.state[5]            4.857 4.236     3
b.state[6]            4.957 4.244     3
b.state[7]            4.508 4.137     3
b.state[8]            4.708 4.137     3
b.state[9]            4.941 4.513     3
b.state[10]           5.102 4.330     3
b.state[11]           4.867 4.084     3
b.state[12]           4.553 4.476     3
b.state[13]           5.178 4.295     3
b.state[14]           4.422 4.237     3
b.state[15]           5.342 4.260     3
b.state[16]           5.071 4.345     3
b.state[17]           5.161 4.261     3
b.state[18]           4.783 4.241     3
b.state[19]           4.461 4.164     3
b.state[20]           4.284 4.369     3
b.state[21]           4.648 4.427     3
b.state[22]           4.267 4.357     3
b.state[23]           5.309 4.300     3
b.state[24]           4.805 4.378     3
b.state[25]           4.929 4.131     3
b.state[26]           4.924 4.198     3
b.state[27]           5.105 4.047     3
b.state[28]           5.271 3.872     3
b.state[29]           4.935 4.420     3
b.state[30]           4.635 4.259     3
b.state[31]           4.371 4.473     3
b.state[32]           5.358 4.439     3
b.state[33]           4.743 4.212     3
b.state[34]           5.237 4.435     3
b.state[35]           5.073 4.237     3
b.state[36]           4.521 4.250     3
b.state[37]           4.761 4.458     3
b.state[38]           4.791 4.237     3
b.state[39]           5.197 4.351     3
b.state[40]           4.674 4.176     3
b.state[41]           5.551 4.357     3
b.state[42]           5.014 4.437     3
b.state[43]           5.529 4.215     3
b.state[44]           5.245 4.094     3
b.state[45]           5.430 4.330     3
b.state[46]           4.808 4.396     3
b.state[47]           5.119 4.212     3
b.state[48]           4.533 4.427     3
b.state[49]           4.853 4.005     3
b.v.prev              4.366 1.003  1100
mu.adj                0.603 1.004   760
mu.age                3.746 2.782     4
mu.age.edu            0.956 1.151    21
mu.edu                5.967 1.611     7
mu.region             4.740 3.764     3
sigma.age             0.741 1.000  1200
sigma.age.edu         0.379 1.046    66
sigma.edu             1.437 1.017  1200
sigma.region          1.589 1.010   760
sigma.state           0.650 1.003   670
deviance           2633.799 1.003   720

For each parameter, n.eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor (at convergence, Rhat=1).

DIC info (using the rule, pD = var(deviance)/2)
pD = 64.7 and DIC = 2673.6
DIC is an estimate of expected predictive error (lower deviance is better).

6.3.1 Plots

diag_mcmc(as.mcmc(jags_fit),parName = "mu.adj")

diag_mcmc(as.mcmc(jags_fit),parName = "b.region.adj[1]")

6.4 Graphing the Estimated Model for a Binary Outcome”

6.4.1 Overview

In this analysis, we aim to create summary plots for a multilevel model with a binary outcome, similar to the multilevel models in Chapters 12 and 13. We make two main adjustments for these plots:

  1. Binary Outcome: Since the outcome is binary, we plot the predicted probability \(\Pr(y = 1) = E(y)\) as a function of the predictors, resulting in curved plots similar to those for generalized linear models.

  2. Combined Predictors: With multiple predictors in the model, we combine them into a single linear predictor for demographics, called linpred, rather than plotting each predictor individually.

6.4.1.1 Linear Predictor Definition

The combined demographic linear predictor for individual \(i\) is defined as:

\[ \text{linpred}_i = \beta_0 + \beta_{\text{female}} \cdot \text{female}_i + \beta_{\text{black}} \cdot \text{black}_i + \beta_{\text{female.black}} \cdot \text{female}_i \cdot \text{black}_i + \alpha^{\text{age}}_{k[i]} + \alpha^{\text{edu}}_{l[i]} + \alpha^{\text{age.edu}}_{k[i], l[i]} \]

6.4.1.2 Plotting the Demographic Effects

Estimates, along with their 50% and 95% intervals for each demographic coefficient, are shown below. These estimates can be interpreted directly as each predictor’s contribution to the sum \(X_i \beta\). For example, to predict the probability of a Republican vote for a female, aged 20, with no high school diploma, we would:

  • Sum the constant term and the estimates for the corresponding main effects and interactions.
  • Take the inverse-logit transformation to obtain the probability.

6.4.1.3 Regression Prediction for Survey Respondents

For each respondent \(i\), we can write the prediction as:

\[ \Pr(y_i = 1) = \text{logit}^{-1}(\text{linpred}_i + \alpha^{\text{state}}_{j[i]}) \]

where \(\text{linpred}_i\) is the demographic linear predictor and \(\alpha^{\text{state}}_{j[i]}\) is the state effect. This can then be plotted for each state.

6.4.2 R Code for Computing and Plotting linpred

After fitting the model in JAGS , we can compute linpred using the simulations of the fitted model

attach.jags(jags_fit)  # Attach model output for access to parameter arrays

attach(data_jags)
The following objects are masked _by_ .GlobalEnv:

    n.edu, region, state, v.prev
The following objects are masked from polls.subset:

    age, black, edu, female, state, y
# Compute linpred for each individual
linpred <- rep(NA, n)
for (i in 1:n) {
  linpred[i] <- mean(
    b.0 + b.female * female[i] + b.black * black[i] + 
    b.female.black * female[i] * black[i] + 
    b.age.adj[,age[i]] + b.edu.adj[,edu[i]] + b.age.edu.adj[,age[i], edu[i]]
  )
}

6.4.3 Plotting Predicted Probabilities by State

We create a plot for each displayed state showing the predicted probability of supporting Bush as a function of the linear predictor linpred.

par(mfrow = c(2, 4))  # Arrange plots in a 2x4 grid
displayed.states <- c(2,3,4,8,6,7,5,9)
for (j in displayed.states) {
  plot(0, 0, xlim = range(linpred), ylim = c(0, 1), yaxs = "i",
       xlab = "Linear predictor", ylab = "Pr (support Bush)",
       main = state.name[j], type = "n")

  # Plot 20 simulated probability curves for the state
  for (s in 1:20) {
    curve(invlogit(b.state[s, j] + x), lwd = 0.5, add = TRUE, col = "gray")
  }
  
  # Plot the median probability curve for the state
  curve(invlogit(median(b.state[, j]) + x), lwd = 2, add = TRUE)
  
  # Add observed points for the current state
  if (sum(state == j) > 0) {
    points(linpred[state == j], y[state == j])
  }
}

6.4.4 Interpretation

In the resulting plots: - The gray lines represent the simulated probability curves for each state. - The bold line represents the median probability curve. - Observed points are plotted for individuals in each state.

These plots illustrate how the demographic predictors and state effects contribute to the probability of supporting Bush.

6.5 Estimating Average Opinion by State Using Model Inferences

6.5.1 Overview

The logistic regression model provides a way to estimate the probability that any adult in a given demographic group and state will prefer Bush. Using these probabilities, we can compute weighted averages to estimate the proportion of Bush supporters in different population subsets.

6.5.1.1 Data Preparation

Using data from the U.S. Census, we create a dataset of 3264 cells (cross-classifications of demographics and states) with each cell representing a unique combination of: - Sex - Ethnicity - Age - Education level - State

Each cell contains the number of people fitting that combination, stored in the census data frame.

6.5.1.2 Calculating Expected Support for Bush (y.pred)

After fitting the model in JAGS and obtaining n.sims simulation draws, we calculate y.pred, the predicted probability of supporting Bush for each demographic cell in each simulation.

# Assuming `census` contains the demographic and state information for each cell
library (foreign)
library(tidyverse)
census <- read.dta ("data/ARM_Data/election88/census88.dta")

census <- census %>% filter(state <= 49)

L <- nrow(census)  # Number of census cells
y.pred <- array(NA, c(n.sims, L))  # Initialize a matrix to store predictions

for (l in 1:L) {
  y.pred[, l] <- invlogit(
    b.0 + b.female * census$female[l] +
    b.black * census$black[l] +
    b.female.black * census$female[l] * census$black[l] +
    b.age.adj[, census$age[l]] + b.edu.adj[, census$edu[l]] +
    b.age.edu.adj[, census$age[l], census$edu[l]] + b.state[, census$state[l]]
  )
}

6.5.2 Estimating Average Support by State

For each state \(j\), we estimate the average response by taking a weighted sum of predictions across the 64 demographic categories within the state. This weighted average reflects the expected proportion of Bush supporters in each state.

The weighted average for state \(j\) is calculated as:

\[ y^{\text{state}}_{\text{pred}, j} = \frac{\sum_{l \in j} N_l \theta_l}{\sum_{l \in j} N_l} \]

where: - \(N_l\) is the population count for demographic group \(l\) in state \(j\). - \(\theta_l\) is the predicted probability of support for Bush for group \(l\).

# Initialize an array to store state-level predictions
y.pred.state <- array(NA, c(n.sims, n.state))

# Compute state-level weighted averages of predictions
for (s in 1:n.sims) {
  for (j in 1:n.state) {
    ok <- census$state == j  # Identify cells corresponding to state j
    y.pred.state[s, j] <- sum(census$N[ok] * y.pred[s, ok]) / sum(census$N[ok])
  }
}

6.5.3 Summarizing State Predictions

For each state, we compute a point estimate and 50% prediction interval from the n.sims simulations. This provides a summary of the proportion of adults in each state who are predicted to support Bush.

# Initialize an array to store summary statistics
state.pred <- array(NA, c(3, n.state))

# Calculate 50% interval and median for each state
for (j in 1:n.state) {
  state.pred[, j] <- quantile(y.pred.state[, j], c(0.25, 0.5, 0.75))
}

6.5.4 Interpretation

The resulting state.pred array contains: - The 25th percentile (lower 50% interval bound), - The median (point prediction), and - The 75th percentile (upper 50% interval bound)

for the proportion of adults in each state who supported Bush. These estimates account for demographic variation within each state and provide insights into the predicted level of support by state. ```