Ideal Point Modeling is useful when assessing political behaviour. I’m going to copy and explore the analysis that was developed by Robert Myles.

Generate the Data

Below is some of the data that was generated for simulation within a multiparty system. This is just a starting point.

library(dplyr)
set.seed(1834)
# simulate data: 100 legislators, 150 votes
N <- 50
M <- 150
m_votes <- matrix(NA, nrow = N, ncol = M)
# Liberals (majority Gov. party)
for(n in 1:20){
  m_votes[n, ] <- rbinom(M, size = 1, prob = 0.9)
}
# Conservatives (uneasy coalition)
for(n in 21:32){
  m_votes[n, ] <- rbinom(M, size = 1, prob = 0.7)
}
# Socialists (opposition)
for(n in 33:40){
  m_votes[n, ] <- rbinom(M, size = 1, prob = 0.3)
}
# Greens (opposition)
for(n in 41:45){
  m_votes[n, ] <- rbinom(M, size = 1, prob = 0.25)
}
# Religious (opposition)
for(n in 46:48){
  m_votes[n, ] <- rbinom(M, size = 1, prob = 0.01)
}
# Independents (random)
for(n in 49:50){
  m_votes[n, ] <- rbinom(M, size = 1, prob = 0.5)
}
rm(n)

votes_data <- data_frame(
  vote_id = rep(paste0("Vote_", 1:M), each = N),
  legislator_id = rep(1:N, times = M),
  vote = as.vector(m_votes),
  legislator_party = ""
) %>% 
  mutate(legislator_party = case_when(
    legislator_id <= 20 ~ "The Classic Liberal Party",
    legislator_id > 20 & legislator_id <= 32 ~ "The Conservative Party",
    legislator_id > 32 & legislator_id <= 40 ~ "The Socialist Party",
    legislator_id > 40 & legislator_id <= 45 ~ "The Green Party",
    legislator_id > 45 & legislator_id <= 48 ~ "The Religious Party",
    TRUE ~ "Independent"),
    legislator_id = paste0("Legislator_", legislator_id),
    government = ifelse(legislator_party %in% c("The Classic Liberal Party",
                                                "The Conservative Party"), 
                        "Government", "Opposition"),
    index = gsub("[A-Za-z_]*", "", vote_id),
    index = as.numeric(index),
    year = ifelse(index <= 75, "2017", "2018")) %>% 
  dplyr::select(-index)

dimnames(m_votes)[[1]] <- unique(votes_data$legislator_id)
dimnames(m_votes)[[2]] <- unique(votes_data$vote_id)

# make the first two voters roughly opposite:
# put voter from religious party in 2nd row
religious <- m_votes[46, ]
liberal <- m_votes[2, ]
m_votes[2, ] <- religious
m_votes[46, ] <- liberal
dimnames(m_votes)[[1]][2] <- "Legislator_46"
dimnames(m_votes)[[1]][46] <- "Legislator_2"

# and make a random subset NA (missed votes, common in real datasets):
m_votes[sample(seq(m_votes), 50)] <- NA

Generative Model

As with all 1- Dimensional Item Response Models the suggested form is:

\[y_i \sim bernouilli(invlog(\theta_j*\beta_k -\alpha_k))\] Where j represents the individual legislators, voting on k bills. In this model _j represents the latent ability of the indivudal legislator. One important component here is that IRT and specifically ideal point can be scale invariable. Gelman and Hill (2006) details this issue, but the idea is that the algorithms will generate the ideal point, but without an anchor the direction is uncertain. Gelman gets around this in an analysis of the US Supreme court by anchoring Scalia as the most conservative justice. In this example you can see that the for legistlators 1 and 2 are put into opposition. This is exactly to provide an anchoring of sorts.

Stan Code

data {
  int<lower=1> J; //Legislators
  int<lower=1> K; //Proposals/bills
  int<lower=1> N; //no. of observations
  int<lower=1, upper=J> j[N]; //Legislator for observation n
  int<lower=1, upper=K> k[N]; //proposal for observation n
  int<lower=0, upper=1> y[N]; //vote of observation n
}
parameters {
  vector[K] alpha;
  vector[K] beta;
  vector[J] theta;
}
model {
  //priors on parameters
  alpha ~ normal(0,10); 
  beta ~ normal(0,10); 
  theta ~ normal(0,1); 
  theta[1] ~  normal(1, .01);  //constraints
  theta[2] ~ normal(-1, .01);  //Important for identifiability problem
  for (n in 1:N)
  y[n] ~ bernoulli_logit(theta[j[n]] * beta[k[n]] - alpha[k[n]]);
}
library(rstan)
rstan_options(auto_write = TRUE)
id_fit <- stan_model("stan_ideal_point.stan")

Prep Data for Stan

Now we can prep the data removing the NA values from our data.

# take out NA:
nas <- which(is.na(m_votes))
votes <- m_votes[-nas]
N <- length(votes)
j <- rep(1:50, times = 150)
j <- j[-nas]
k <- rep(1:150, each = 50)
k <- k[-nas]
J <- max(j)
K <- max(k)


data <- list(J = J, K = K, N = N, j = j, k = k, y = votes)

Now we can fit the model:

fit <- sampling(id_fit, 
                 data = data, iter = 4000, 
                 chains = 2, cores = 2, refresh = 0)

Let’s check out the latent trait for our legislators:

print(fit, pars = "theta")
## Inference for Stan model: stan_ideal_point.
## 2 chains, each with iter=4000; warmup=2000; thin=1; 
## post-warmup draws per chain=2000, total post-warmup draws=4000.
## 
##            mean se_mean   sd  2.5%   25%   50%   75%   98% n_eff Rhat
## theta[1]   0.99    0.00 0.01  0.97  0.99  0.99  1.00  1.01    11  1.1
## theta[2]  -0.99    0.01 0.01 -1.02 -1.00 -0.99 -0.99 -0.97     2  1.2
## theta[3]  -0.51    1.23 1.24 -2.19 -1.73 -0.24  0.71  0.85     1  7.3
## theta[4]  -0.82    1.76 1.77 -3.20 -2.56 -0.48  0.92  1.11     1  7.6
## theta[5]  -0.74    1.58 1.59 -2.85 -2.30 -0.44  0.83  1.00     1  7.7
## theta[6]  -0.63    1.41 1.42 -2.53 -2.01 -0.36  0.77  0.92     1  7.4
## theta[7]  -0.51    1.24 1.24 -2.17 -1.73 -0.16  0.72  0.86     1  7.4
## theta[8]  -0.60    1.37 1.38 -2.47 -1.95 -0.35  0.76  0.90     1  7.4
## theta[9]  -0.66    1.46 1.47 -2.62 -2.11 -0.37  0.79  0.94     1  7.7
## theta[10] -0.65    1.45 1.46 -2.59 -2.08 -0.35  0.79  0.95     1  7.5
## theta[11] -0.44    1.14 1.15 -1.99 -1.57 -0.24  0.69  0.83     1  7.2
## theta[12] -0.61    1.40 1.41 -2.51 -1.98 -0.33  0.79  0.93     1  7.3
## theta[13] -0.55    1.29 1.30 -2.29 -1.82 -0.32  0.74  0.88     1  7.4
## theta[14] -0.69    1.51 1.52 -2.71 -2.18 -0.40  0.81  0.96     1  7.6
## theta[15] -0.58    1.34 1.35 -2.38 -1.91 -0.31  0.76  0.91     1  7.7
## theta[16] -0.66    1.48 1.49 -2.69 -2.12 -0.40  0.81  0.95     1  7.3
## theta[17] -0.46    1.16 1.17 -2.05 -1.61 -0.19  0.70  0.83     1  7.1
## theta[18] -0.52    1.24 1.25 -2.19 -1.76 -0.25  0.71  0.86     1  7.5
## theta[19] -0.66    1.46 1.47 -2.61 -2.11 -0.36  0.79  0.95     1  7.7
## theta[20] -0.54    1.27 1.28 -2.24 -1.79 -0.29  0.72  0.86     1  7.6
## theta[21]  0.01    0.47 0.48 -0.75 -0.45  0.15  0.47  0.59     1  4.1
## theta[22] -0.11    0.63 0.64 -1.05 -0.73  0.09  0.51  0.63     1  5.1
## theta[23] -0.06    0.56 0.57 -0.93 -0.61  0.11  0.50  0.62     1  4.7
## theta[24] -0.10    0.63 0.63 -1.02 -0.72  0.09  0.52  0.64     1  5.2
## theta[25] -0.02    0.51 0.52 -0.83 -0.51  0.14  0.48  0.60     1  4.3
## theta[26] -0.06    0.56 0.57 -0.91 -0.61  0.18  0.49  0.61     1  4.6
## theta[27] -0.19    0.75 0.75 -1.25 -0.92  0.02  0.55  0.67     1  6.0
## theta[28] -0.11    0.65 0.65 -1.06 -0.75  0.07  0.53  0.64     1  5.4
## theta[29] -0.07    0.58 0.59 -0.95 -0.64  0.09  0.51  0.63     1  4.8
## theta[30] -0.05    0.56 0.57 -0.91 -0.59  0.10  0.50  0.61     1  4.6
## theta[31] -0.06    0.56 0.57 -0.93 -0.60  0.08  0.50  0.62     1  4.7
## theta[32] -0.16    0.72 0.73 -1.20 -0.86  0.04  0.55  0.67     1  5.5
## theta[33]  0.67    0.51 0.52  0.04  0.17  0.42  1.16  1.54     1  3.6
## theta[34]  0.53    0.31 0.33  0.12  0.24  0.40  0.83  1.15     1  2.5
## theta[35]  0.68    0.53 0.54  0.04  0.17  0.46  1.19  1.56     1  3.8
## theta[36]  0.69    0.54 0.55  0.03  0.16  0.47  1.20  1.59     1  3.7
## theta[37]  0.58    0.38 0.40  0.09  0.22  0.41  0.95  1.29     1  2.9
## theta[38]  0.45    0.20 0.23  0.14  0.26  0.37  0.62  0.95     1  1.8
## theta[39]  0.58    0.38 0.40  0.09  0.21  0.41  0.95  1.29     1  2.9
## theta[40]  0.52    0.29 0.31  0.13  0.25  0.40  0.79  1.12     1  2.4
## theta[41]  0.74    0.61 0.62  0.01  0.14  0.52  1.33  1.69     1  4.3
## theta[42]  0.62    0.44 0.46  0.06  0.19  0.41  1.05  1.39     1  3.2
## theta[43]  0.74    0.62 0.63  0.00  0.13  0.46  1.34  1.74     1  4.1
## theta[44]  0.74    0.62 0.63  0.00  0.14  0.53  1.34  1.73     1  4.3
## theta[45]  0.71    0.57 0.58  0.02  0.15  0.50  1.26  1.61     1  4.1
## theta[46] -0.56    1.31 1.32 -2.34 -1.86 -0.31  0.74  0.88     1  7.3
## theta[47]  1.54    2.17 2.19 -0.99 -0.60  1.06  3.66  4.55     1  6.7
## theta[48]  1.48    2.01 2.03 -0.86 -0.50  1.05  3.46  4.31     1  6.4
## theta[49]  0.19    0.21 0.24 -0.29 -0.01  0.28  0.39  0.51     1  1.9
## theta[50]  0.24    0.15 0.18 -0.16  0.12  0.30  0.39  0.50     2  1.5
## 
## Samples were drawn using NUTS(diag_e) at Tue Feb 26 22:05:37 2019.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).

Let’s just extract the parameters from the model.

posterior <- rstan::extract(fit)[["theta"]]

And graph them in the classic way (note that our anchors are diametrically opposed):

library(tidyverse, quietly = TRUE)
posterior %>% 
  as_data_frame() %>% 
  set_names(sprintf("Legislator_%s", 1:50)) %>% 
  gather(legislator_id, value) %>% 
  group_by(legislator_id) %>% 
  summarise(med = median(value),
            q025 = quantile(value, 0.025),
            q975 = quantile(value, 0.975)) %>% 
  left_join(votes_data %>% dplyr::select(legislator_id, legislator_party) %>% unique()) %>% 
  ggplot(aes(reorder(legislator_id, -med), color = legislator_party))+
  geom_point(aes(y = med))+
  geom_errorbar(aes(ymin = q025, ymax = q975))+
  coord_flip()+
  theme_minimal()+
  labs(
    title = "1PL Ideal Point Analysis",
    subtitle = "95% Quantiles",
    x = NULL,
    color = "Party"
  )
## Warning: `as_data_frame()` is deprecated, use `as_tibble()` (but mind the new semantics).
## This warning is displayed once per session.
## Joining, by = "legislator_id"

References

Gelman, Andrew, and Jennifer Hill. 2006. Applied Regression and Multilevel/Hierarchical Models. Cambridge University Press.



Research and Methods Resources
me.dewitt.jr@gmail.com



Winston- Salem, NC

Michael DeWitt


Copyright © 2018 Michael DeWitt. All rights reserved.