forked from JuanLopezMartin/MRPCaseStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-mrp_noncensus.Rmd
546 lines (444 loc) · 29.4 KB
/
02-mrp_noncensus.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# MRP with Noncensus Variables
<style type="text/css">
h1.title {
font-size: 32px;
text-align: center;
}
h2 {
padding-bottom: 4px;
}
h4.author {
padding-top: 22px;
text-align: center;
font-style: italic;
}
h4.date {
padding-top: 14px;
font-size: 14px;
text-align: center;
font-style: italic;
padding-bottom: 20px;
}
</style>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function myFunction2() {
var x = document.getElementById("myDIV2");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function myFunction3() {
var x = document.getElementById("myDIV3");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
<style>
#myDIV {
width: 100%;
padding: 20px 30px;
background-color: rgba(192,192,192,0.15);
margin-top: 10px;
border-radius: 4px;
}
#myButton{
border-color: #008CBA;
background-color: rgba(192,192,192,0.05);
color: #008CBA;
border-radius: 4px;
}
#myDIV2 {
width: 100%;
padding: 20px 30px;
background-color: rgba(192,192,192,0.15);
margin-top: 10px;
border-radius: 4px;
}
#myButton2{
border-color: #008CBA;
background-color: rgba(192,192,192,0.05);
color: #008CBA;
border-radius: 4px;
}
#myDIV3 {
width: 100%;
padding: 20px 30px;
background-color: rgba(192,192,192,0.15);
margin-top: 10px;
border-radius: 4px;
}
#myButton3{
border-color: #008CBA;
background-color: rgba(192,192,192,0.05);
color: #008CBA;
border-radius: 4px;
}
</style>
```{r packages-2, message=FALSE, echo = FALSE}
library(brms)
library(rstanarm)
library(data.table)
library(dplyr)
library(forcats)
library(tidyr)
library(reshape2)
library(kableExtra)
library(ggplot2)
library(bayesplot)
library(gridExtra)
library(ggalt)
library(scales)
library(usmap)
theme_set(bayesplot::theme_default())
Sys.setenv(LOCAL_CPPFLAGS = '-march=corei7 -mtune=corei7')
options(mc.cores = parallel::detectCores(logical = FALSE))
```
When our sample population is different than our target population, MRP can only adjust for the predictors included in the model. As these are restricted by the variables in the poststratification table, which in turn are limited by the questions asked in the census, the characteristics that we can use for poststratification are quite reduced. This is the reason researchers tend to use simple demographic and geographic variables, which unfortunately do not provide much help if the bias in the survey originates from non-response in voters of a certain party, for instance. As a potential solution, @kastellec2015polarizing propose extending the postratification table using a survey that contains one or multiple non-census variables that could help adjusting for the differences between the sample and the target population. For instance, if our survey asked for partisanship, we could use the CCES to extend the poststratification table such as that it also contains this variable. The extension is done in two steps. First, we fit a multilevel model in which we try to predict partisanship in the CCES based on the variables available in the census. Second, we use this model to predict, for each cell in the original poststratification table, what proportion of subjects are Democrats, Republicans, or Independents. This extended poststratification table that contains partisanship will allow us to (a) generate MRP estimates that adjust for differential party nonresponse in the original survey; and/or (b) obtain estimates outcome of interest by party.
For this case study we will continue using the previous example of studying support for the right of employers to exclude abortion coverage.
```{r, echo = FALSE}
# Reed CCES data again, but this time we also read the partisanship variable
clean_cces2 <- function(df, list_states_abb, list_states_num){
## Abortion -- dichotomous (0 - Oppose / 1 - Support)
df$abortion <- abs(df$CC18_321d-2)
## State -- factor
df$state <- df$inputstate
df$state <- factor(df$state, levels = list_states_num, labels = list_states_abb)
## Gender -- dichotomous (-0.5 Female, +0.5 Male)
df$male <- abs(df$gender-2)-0.5
## ethnicity -- factor
df$eth <- factor(df$race,
levels = 1:8,
labels = c("White", "Black", "Hispanic", "Asian", "Native American", "Mixed", "Other", "Middle Eastern"))
df$eth <- fct_collapse(df$eth, "Other" = c("Asian", "Other", "Middle Eastern", "Mixed", "Native American"))
## Age -- cut into factor
df$age <- 2018 - df$birthyr
df$age <- cut(as.integer(df$age), breaks = c(0, 29, 39, 49, 59, 69, 120),
labels = c("18-29","30-39","40-49","50-59","60-69","70+"),
ordered_result = TRUE)
## Education -- factor
df$educ <- factor(as.integer(df$educ),
levels = 1:6,
labels = c("No HS", "HS", "Some college", "Associates", "4-Year College", "Post-grad"), ordered = TRUE)
df$educ <- fct_collapse(df$educ, "Some college" = c("Some college", "Associates"))
# Party
df$party <- factor(df$pid3,
levels = 1:5,
labels = c("Democrat", "Republican", "Independent", "Other", "Not sure"),
ordered = TRUE)
df$party <- fct_collapse(df$party, "Independent" = c("Independent", "Other", "Not sure"))
# Clean and remove NAs
df <- df %>% select(abortion, state, eth, male, age, educ, party) %>% drop_na()
}
```
```{r}
## Read CCES data with the same outcome variable and predictors, but also including
## party
df_all <- read.csv("cces18_common_vv.csv")
list_states_abb <- datasets::state.abb
list_states_num <- c(1,2,4,5,6,8,9,10,12,13,15,16,17,18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,44,45,46,47,
48,49,50,51,53,54,55,56)
# Preprocessing. The clean_cces2 function also reads party
df_all <- clean_cces2(df_all, list_states_abb, list_states_num)
## Read poststratification table
poststrat_df <- read.csv("postrat_data.csv")
poststrat_df$state <- factor(poststrat_df$state,
levels = list_states_num,
labels = list_states_abb)
## Read state-level predictors and add them to the CCES and poststratification table
statelevel_predictors <- read.csv('statelevel_predictors.csv')
statelevel_predictors$state <- factor(statelevel_predictors$state,
levels = list_states_abb,
labels = list_states_abb)
df_all <- left_join(df_all, statelevel_predictors,
by = "state", keep = TRUE)
poststrat_df <- left_join(poststrat_df, statelevel_predictors,
by = "state", keep = TRUE)
```
## Model-based Extension of the Poststratification Table
As we have described, we start fitting a multilevel model to predict partisanship as a function of the same demographic and geographic variables used in the standard MRP model, which will allow us to predict the proportion of Republicans, Democrats, and Independents in each row of the poststratification table. As there are three levels for partisanship, we use a Bayesian multinomial (i.e. unordered) logistic regression which can be fitted in `brms` (currently, `rstanarm` does not support multinomial logistic regression).
For this extension step we should use a survey that we think is to some degree representative with respect to the variable that we are trying to include in the poststratification table. In our example, if we extended our census-based poststratification table using a highly non-representative survey with respect to party, we would indeed generated a biased poststratification table and ultimately obtain compromised MRP estimates. In other words, this is our opportunity to bring outside information in order to generate a richer poststratification table that can adjust for potential biases in the main survey, so we need to make sure that the survey we use to extend the poststratification table is trustworthy with respect to the non-census variable.
In this example, we will use a 10,000-person sample of the CCES to extend the poststratification table to include partisanship, which is addressed in the CCES:
> Generally speaking, do you think of yourself as a ...? (Democrat, Republican, Independent, Other, Not Sure)
For simplicity, we included the few respondents that indicated "Other" or "Not Sure" as Independents.
```{r}
# Setting seed to arbitrary number for reproducibility
set.seed(1010)
# Taking random sample from the CCES survey
df_random <- df_all %>% sample_n(5000)
# fit_party <- brm(party ~ (1 | state) + (1 | eth) + (1 | age) + (1 | educ) + male +
# (1 | male:eth) + (1 | educ:age) + (1 | educ:eth) +
# repvote + factor(region),
# family = "categorical",
# data = df_random,
# prior = c(prior(normal(0, 5), class = Intercept),
# prior(normal(0, 1), class = b),
# prior(exponential(0.5), class = sd, dpar = muIndependent),
# prior(exponential(0.5), class = sd, dpar = muRepublican)),
# control = list(adapt_delta = 0.9, max_treedepth = 10),
# seed = 1010)
#
# saveRDS(fit_party, file = "fit_party_example.rds")
fit_party <- readRDS("fit_party_example.rds")
```
This model gives us, for each poststratification cell $j$, an estimate for the proportion of Democrats ($\hat{\theta}_{{\rm Democrat}, j}$), Republicans ($\hat{\theta}_{{\rm Republican}, j}$), and Independents ($\hat{\theta}_{{\rm Independent}, j}$). We can multiply these quantities by the number of people in cell $j$ to estimate the number of Democrats ($N_j \: \hat{\theta}_{{\rm Democrat}, j}$), Republicans ($N_j \: \hat{\theta}_{{\rm Republican}, j}$), and Independents ($N_j \: \hat{\theta}_{{\rm Independent}, j}$), obtaining an extended poststratification table in which each cell has been expanded into three. That is, if the original poststratification table had $J$ rows (e.g. 12,000 in our case), the new one will have $3 J$ (e.g. 36,000). There is, however, a certain complication that must be taken into account. The model-based estimates for the proportion of Democrats, Republicans, and Independents are not single numbers, but several draws from the posterior distribution that capture the uncertainty about these estimates. For instance, if we have 500 draws for $\hat{\theta}_{{\rm Democrat}, j}$, $\hat{\theta}_{{\rm Republican}, j}$, and $\hat{\theta}_{{\rm Independent}, j}$, we can imagine 500 poststratification tables with different numbers for each cell.
```{r}
## Use pp_expect to predict partisanship for original poststratification table
posmat_party <- pp_expect(fit_party, newdata = poststrat_df, transform = TRUE)
# pp_expect does not allow us to select the number of draws. Therefore, we
# take a random sample of 500 draws from posterior distribution
posmat_party <- posmat_party[sample(nrow(posmat_party), size=500, replace=TRUE),,]
# Extend poststratification table
poststrat_df_threefold <- poststrat_df[rep(seq_len(nrow(poststrat_df)), each = 3), ]
poststrat_df_threefold$party <- rep(c("Democrat", "Republican", "Independent"), nrow(poststrat_df))
# Calculate new numbers for the cells of the new poststratification table. K
# is a matrix containing 36000 rows (one for each cell of the poststratification table)
# and 500 columns (corresponding to the 500 draws).
K_theta <- apply(posmat_party, 1, function(x){as.vector(t(x))})
K <- K_theta * rep(poststrat_df$n, each = 3)
```
<button id="myButton" onclick="myFunction()" >Show code explanation </button>
<div id="myDIV" style="display:none">
More generally, let's say that for each $\hat{\theta}_{{\rm Democrat}, j}$, $\hat{\theta}_{{\rm Republican}, j}$, and $\hat{\theta}_{{\rm Independent}, j}$ we have a vector that contains $D_1$ draws from the posterior distribution. In the code $D_1 = 500$, as we have randomly selected 500 draws from the `posmat_party` matrix. Therefore, `pp_expect` (the `brms` equivalent to `posterior_epred`) will return an array with $J$ (number of rows of the original poststratification table) $\times D_1$ (number of draws from posterior distribution) $\times 3$ (estimates for Democrats, Republicans, and Independents) dimensions.
The first step is to take this $J \times D_1 \times 3$ array and transform it to a $3J \times D_1$ matrix. Then, we multiply each row of the matrix by the corresponding $N_j$ of the original poststratification table. This gives us a new $3J \times D_1$ matrix that we will call $K$, and which contains the $D_1$ draws for each of the $3J$ cells of the new poststratification table.
</div>
<br>
In sum, we started with a poststratification table with 12,000 rows. Here we can see the first three rows:
```{r, echo=FALSE}
poststrat_df[1:3,2:7] %>% kable() %>% kable_styling(full_width = FALSE)
```
We have used a model-based approach to include partisanship in this poststratification table, that now has 36,000 rows (again, each row in the original table has been split into three). However, in order to consider the uncertainty in these model-based estimates we have actually built 500 different poststratification tables. Here we show the first 9 rows of one of these 500 poststratification tables:
```{r, echo=FALSE}
poststrat_df_threefold[1:9,c(2:6, 10)] %>% mutate(n = round(K[1:9, 1], 1)) %>% kable() %>% kable_styling(full_width = FALSE)
```
## Adjusting for Nonresponse Bias
We have described how to extend the poststratification table by including partisanship. Now, we will use this poststratification table to adjust for differential party nonresponse.
### Setting up example with an artificially nonrepresentative sample
To demostraty how non-census MRP can adjust for party, we will use a survey that is biased with respect to party. As we are already familiar with the CCES dataset, what we are going to do is to take a different sample of 10,000 respondents that simulates a high nonresponse rate among Republicans and, to a lesser degree, Independents.
```{r}
# Random sample of 10,000 that weights by party
df_nonrepresentative <- df_all %>% sample_n(5000, weight = I((df_all$party=="Democrat")*1 +
(df_all$party=="Independent")*0.75 +
(df_all$party=="Republican")*0.5))
```
Previously, we saw that the national average support for requiring companies to cover abortion in their insurance plans was around `r round(mean(df_all$abortion, na.rm = TRUE), 3)*100`% according to the CCES. Comparatively, this biased sample of the CCES gives an estimate of `r round(mean(df_nonrepresentative$abortion, na.rm = TRUE), 3)*100`%. This is not surprising, as missing Republicans and Independents in the survey should reduce support for the employers' right to decline abortion coverage.
### Standard MRP
We fit a standard MRP (i.e. without including party) on the nonrepresentative sample, using the same model as in the MRP introduction and the non-extended poststratification table.
```{r}
# fit_abortion_standard <- stan_glmer(abortion ~ (1 | state) + (1 | eth) + (1 | age) + (1 | educ) + male +
# (1 | male:eth) + (1 | educ:age) + (1 | educ:eth) +
# repvote + factor(region),
# family = binomial(link = "logit"),
# data = df_nonrepresentative,
# prior = normal(0, 1, autoscale = TRUE),
# prior_covariance = decov(scale = 0.50),
# adapt_delta = 0.99,
# seed = 1010)
#
# saveRDS(fit_abortion_standard, file = "fit_abortionstandard_example.rds")
fit_abortion_standard <- readRDS("fit_abortionstandard_example.rds")
```
```{r}
P_standardMRP <- rstanarm::posterior_epred(fit_abortion_standard, newdata = poststrat_df, draws = 500)
national_standardMRP <- (P_standardMRP %*% poststrat_df$n)/sum(poststrat_df$n)
```
The standard MRP with the nonrepresentative sample gives a national-level estimate of `r 100*round(mean(national_standardMRP), 3)`% ($\pm$ `r 100*round(sd(national_standardMRP), 3)`%). As this estimate does not consider partisanship, standard MRP is not being able to adjust for the smaller statement support that results from oversampling Democrats.
### Non-census MRP with partisanship as a predictor
In the first section we have created a poststratification table that contains partisanship. After doing this, the next step of the non-census MRP approach is to fit the same model as we did in the standard MRP, but also including party as a predictor:
$$
Pr(y_i = 1) = logit^{-1}(
\alpha_{\rm s[i]}^{\rm state}
+ \alpha_{\rm a[i]}^{\rm age}
+ \alpha_{\rm r[i]}^{\rm eth}
+ \alpha_{\rm e[i]}^{\rm educ}
+ \beta^{\rm male} \cdot {\rm Male}_{\rm i}
+ \alpha_{\rm g[i], r[i]}^{\rm male.eth}
+ \alpha_{\rm e[i], a[i]}^{\rm educ.age}
+ \alpha_{\rm e[i], r[i]}^{\rm educ.eth}
+ \alpha_{\rm p[i]}^{\rm party}
)
$$
$$
\begin{align*}
\alpha_{\rm s}^{\rm state} &\sim {\rm Normal}(\gamma^0 + \gamma^{\rm south} \cdot {\rm South}_{\rm s} + \gamma^{\rm midwest} \cdot {\rm Midwest}_{\rm s} + \gamma^{\rm west} \cdot {\rm West}_{\rm s} + \gamma^{\rm repvote} \cdot {\rm RepVote}_{\rm s}, \sigma_{\rm state}) \textrm{ for s = 1,...,50}\\
\alpha_{\rm a}^{\rm age} & \sim {\rm Normal}(0,\sigma_{\rm age}) \textrm{ for a = 1,...,6}\\
\alpha_{\rm r}^{\rm eth} & \sim {\rm Normal}(0,\sigma_{\rm eth}) \textrm{ for r = 1,...,4}\\
\alpha_{\rm e}^{\rm educ} & \sim {\rm Normal}(0,\sigma_{\rm educ}) \textrm{ for e = 1,...,5}\\
\alpha_{\rm g,r}^{\rm male.eth} & \sim {\rm Normal}(0,\sigma_{\rm male.eth}) \textrm{ for g = 1,2 and r = 1,...,4}\\
\alpha_{\rm e,a}^{\rm educ.age} & \sim {\rm Normal}(0,\sigma_{\rm educ.age}) \textrm{ for e = 1,...,5 and a = 1,...,6}\\
\alpha_{\rm e,r}^{\rm educ.eth} & \sim {\rm Normal}(0,\sigma_{\rm educ.eth}) \textrm{ for e = 1,...,5 and r = 1,...,4}\\
\alpha_{\rm p}^{\rm party} & \sim {\rm Normal}(0,\sigma_{\rm party}) \textrm{ for p = 1,2,3}\\
\end{align*}
$$
```{r}
# fit_abortion_noncensus <- stan_glmer(abortion ~ (1 | state) + (1 | eth) + (1 | age) + (1 | educ) + male +
# (1 | male:eth) + (1 | educ:age) + (1 | educ:eth) +
# repvote + factor(region) + (1 | party),
# family = binomial(link = "logit"),
# data = df_nonrepresentative,
# prior = normal(0, 1, autoscale = TRUE),
# prior_covariance = decov(scale = 0.50),
# adapt_delta = 0.99,
# seed = 1010)
#
# saveRDS(fit_abortion_noncensus, file = "fit_abortionnoncensus_example.rds")
fit_abortion_noncensus <- readRDS("fit_abortionnoncensus_example.rds")
```
Using `posterior_epred` allows us to estimate abortion coverage support for each of the cells in the extended poststratification table. As we set `draws = 500`, we obtain 500 estimates for each cell. In standard MRP, we will weight each the statement support estimates for each poststratification cell by the number of people in that cell according to the model-based estimates obtained in the previous section. However, as in this case the number of people in each cell was estimated with uncertainty, we need to propagate the uncertainty in the first (party prediction) model to the final MRP estimates. Essentially, what we can do is randomly pick one of the 500 statement support estimates for each poststratification cell (i.e. a 36,000 vector) we have just obtained and weight it by one of the 500 poststratification tables that resulted from the first model. Repeating the process for the remaining draws gives us a distribution of 500 MRP estimates for national support that correctly captures the uncertainty in the two models.
```{r}
# Use posterior_epred to predict stance on abortion insurance coverage for extended poststratification table
P <- rstanarm::posterior_epred(fit_abortion_noncensus, newdata = poststrat_df_threefold, draws = 500)
# Calculate national MRP estimates propagating uncertainty from the two models
national_noncensusMRP <- colSums(t(P)*K) / sum(K[,1])
```
<button id="myButton2" onclick="myFunction2()" >Show code explanation </button>
<div id="myDIV2" style="display:none">
More generally, `posterior_epred` returns a $D_2 \times 3J$ matrix we name $P$, where $D_2$ is the number of draws and $3J$ is, again, the number of cells in the extended poststratification table that includes partisanship.
With the two matrices $P$ a $K$, the poststratification step becomes:
$$\hat{\theta}_{MRP} = \frac{P \times K}{\sum_j^{3J} k_{j 1}} {\rm \: where \:} K = (k_{jd})$$
Were $\hat{\theta}_{MRP}$ is a matrix containing $D_2 \times D_1$ estimates for the proportion of statement support.
In case of $D_1 = D_2$, we can do an elementwise operation such that $\hat{\theta}_{MRP} = \frac{\sum_j^{3J} (P^T \odot K)_{jd}}{\sum_j^{3J} k_{j1}}$ in order for the operation to be less computationally expensive, as we only combine each draw (i.e. column) in matrix $P$ with one draw (i.e. column) in matrix $K$ (instead with all the draws in matrix $K$). This more efficient method, which is the one we implement, returns a vector of length $D_1 = D_2$. The only consideration is that we must make sure that $D_1 = D_2$, which in this case required randomly picking 500 draws from $K$ and only obtaining another 500 draws for $P$ (by specifying `draws = 500`).
</div>
<br>
Our national-level estimate for the right to exclude abortion coverage from employer-sponsored insurance resulting from this non-census variable MRP is `r 100*round(mean(national_noncensusMRP), 3)`% (`r 100*round(sd(national_noncensusMRP), 3)`%). Unsurprisingly, this is much closer to the full (unbiased) 60,000 participant survey (`r 100*round(mean(df_all$abortion), 3)` $\pm$ `r 100*round(sqrt(mean(df_all$abortion)*(1-mean(df_all$abortion))/nrow(df_all)), 3)`%) than the standard MRP estimate (`r 100*round(mean(national_standardMRP), 3)` $\pm$ `r 100*round(sd(national_standardMRP), 3)`%). Using an extended poststratification table that contained partisanship allowed us to adjust for differential partisan nonresponse.
Of course, we can also obtain state-level estimates and compare standard MRP with non-census MRP.
```{r}
n_states <- length(unique(poststrat_df_threefold$state))
state_df <- data.frame(
state = rep(NA, n_states),
standardMRP_mean = rep(NA, n_states),
standardMRP_se = rep(NA, n_states),
noncensusMRP_mean = rep(NA, n_states),
noncensusMRP_se = rep(NA, n_states),
all_mean = rep(NA, n_states),
all_se = rep(NA, n_states)
)
for(i in 1:n_states){
state <- unique(poststrat_df_threefold$state)[i]
state_df$state[i] <- as.character(state)
standardMRP_vector <- (P_standardMRP[, poststrat_df$state==state] %*% poststrat_df[poststrat_df$state==state, "n"])/
sum(poststrat_df[poststrat_df$state==state, "n"])
state_df$standardMRP_mean[i] <- mean(standardMRP_vector)
state_df$standardMRP_se[i] <- sd(standardMRP_vector)
noncensusMRP_vector <- colSums(t(P[ ,poststrat_df_threefold$state==state]) *
K[poststrat_df_threefold$state==state, ]) /
sum(K[poststrat_df_threefold$state==state, 1])
state_df$noncensusMRP_mean[i] <- mean(noncensusMRP_vector)
state_df$noncensusMRP_se[i] <- sd(noncensusMRP_vector)
state_df$all_mean[i] <- mean(df_all[df_all$state==state,"abortion"])
state_df$all_se[i] <- sqrt(state_df$all_mean[i]*(1-state_df$all_mean[i])/nrow(df_all[df_all$state==state,]))
}
```
```{r, fig.width=10, fig.height=3.5, warning=FALSE, message=FALSE, results = 'hide', echo=FALSE}
states_order <- poststrat_df %>% group_by(state) %>% summarise(repvote = first(repvote)) %>% arrange(repvote) %>% .$state
state_df$state <- factor(state_df$state, levels = states_order, ordered = TRUE)
compare1 <- ggplot(data=state_df) +
geom_point(aes(x=state, y=standardMRP_mean), color = "#E69F00", shape = 19, size = 1.5, alpha = 0.75) +
geom_errorbar(aes(ymin=standardMRP_mean - 2*standardMRP_se,
ymax=standardMRP_mean + 2*standardMRP_se,
x=state), alpha=.75, width = 0, color = "#E69F00") +
geom_point(aes(x=state, y=all_mean), color = "#02AD04", shape = 15, size = 1.25, alpha = 0.75) +
geom_errorbar(data=state_df, aes(ymin=all_mean + 2*all_se,
ymax=all_mean - 2*all_se,
x=state), alpha=.75, width = 0, color = "#02AD04") +
geom_point(data=state_df, aes(x=state, y=noncensusMRP_mean), color = "#1E88E5", shape = 19, size = 1.5, alpha = 0.75) +
geom_errorbar(data=state_df, aes(ymin=noncensusMRP_mean - 2*noncensusMRP_se,
ymax=noncensusMRP_mean + 2*noncensusMRP_se,
x=state), alpha=.75, width = 0, color = "#1E88E5") +
scale_y_continuous(breaks=c(0,.25,.5,.75,1),
labels=c("0%","25%","50%","75%","100%"),
expand=c(0,0)) +
coord_cartesian(ylim=c(0, 1)) +
theme_bw()+
labs(x="States",y="Support") +
theme(legend.position="none",
axis.title=element_text(size=10),
axis.text.y=element_text(size=10),
axis.text.x=element_text(angle=90,size=8, vjust=0.3),
legend.title=element_text(size=10),
legend.text=element_text(size=10))
compare2 <- ggplot(data = state_df)+
geom_point(aes(y=mean(national_standardMRP), x = .25), color = "#E69F00", shape = 19, size = 1.5, alpha = 0.75) +
geom_errorbar(data=state_df, aes(y = mean(national_standardMRP),
x = .25,
ymin = mean(national_standardMRP) - 2*sd(national_standardMRP),
ymax = mean(national_standardMRP) + 2*sd(national_standardMRP)),
width = 0, color = "#E69F00", alpha=.75) +
geom_text(data = data.frame(), aes(x = Inf, y = mean(national_standardMRP)-0.02, label = "Standard MRP"),
hjust = -.08, size = 4, color = "#E69F00") +
geom_point(aes(y = mean(national_noncensusMRP), x = .75), color = "#1E88E5", shape = 19, size = 1.5, alpha = 0.75) +
geom_errorbar(aes(y = mean(national_noncensusMRP),
x = .75,
ymin = mean(national_noncensusMRP) - 2*sd(national_noncensusMRP) ,
ymax = mean(national_noncensusMRP) + 2*sd(national_noncensusMRP) ),
width = 0, color = "#1E88E5", alpha=.75) +
geom_text(data = data.frame(), aes(x = Inf, y = mean(national_noncensusMRP), label = "Non-Census MRP"),
hjust = -.05, size = 4, color = "#1E88E5") +
geom_point(aes(y=mean(df_all$abortion), x = .5), color = "#02AD04", shape = 15, size = 1.25, alpha = 0.75) +
geom_errorbar(aes(y = mean(df_all$abortion),
x = .5,
ymin = mean(df_all$abortion) - 2*sqrt(mean(df_all$abortion)*(1-mean(df_all$abortion))/nrow(df_all)),
ymax = mean(df_all$abortion) + 2*sqrt(mean(df_all$abortion)*(1-mean(df_all$abortion))/nrow(df_all))),
width = 0, color = "#02AD04", alpha=.75) +
geom_text(data = data.frame(), aes(x = Inf, y = mean(df_all$abortion)+0.06, label = "Complete Survey"),
hjust = -.06, size = 4, color = "#02AD04") +
scale_y_continuous(breaks=c(0,.25,.5,.75,1),
labels=c("0%","25%","50%","75%","100%"),
limits=c(0,1),expand=c(0,0))+
scale_x_continuous(limits=c(0,1),expand=c(0,0), breaks=c(.25, .75)) +
coord_cartesian(clip = 'off') +
theme_bw() +
labs(x="Population",y="")+
theme(legend.position="none",
axis.title.y=element_blank(),
axis.title.x=element_text(size=10, margin = margin(t = 19, r = 0, b = , l = 0)),
axis.text=element_blank(),
axis.ticks=element_blank(),
legend.title=element_text(size=10),
legend.text=element_text(size=10),
plot.margin = margin(5.5, 100, 2.5, 5.5, "pt")
)
bayesplot_grid(compare1,compare2,
grid_args = list(nrow=1, widths = c(5,1.4)))
```
In general, we see that the estimates from the standard MRP are upwardly biased with respect to the 60,000 survey estimates. Conversely, the MRP with non-census variables is able to adjust for the differential partisan nonresponse.
## Obtaining Estimates for Non-census Variable Subgroups
Even if we do not suspect that our survey population is different from our target population with respect to a non-census variable, using non-census MRP can allow us to obtain different estimates for the levels of the non-census variable. Here, we obtain and plot support for declining coverage of abortions by state and party within state.
```{r, warning=FALSE}
state_df <- df_nonrepresentative %>% expand(state, party) %>%
mutate(model_mean = NA,
model_sd = NA)
for(i in 1:nrow(state_df)){
filtering_condition <- which(poststrat_df_threefold$state == state_df$state[i] &
poststrat_df_threefold$party == state_df$party[i])
noncensusMRP_vector <- colSums(t(P[ ,filtering_condition]) *
K[filtering_condition, ]) /
sum(K[filtering_condition, 1])
state_df$model_mean[i] <- mean(noncensusMRP_vector)
state_df$model_sd[i] <- sd(noncensusMRP_vector)
}
```
```{r, message=FALSE, warning=FALSE, echo=FALSE, cache=FALSE, fig.height=6, fig.width=4, fig.align = "center"}
states_map <- us_map(regions = "states")
state_df_melted <- state_df %>% select(state, model_mean, party)
states_map <- left_join(states_map, state_df_melted, by = c("abbr" = "state")) %>% drop_na()
ggplot(states_map, aes(x = x, y = y, group = group)) +
geom_polygon(colour = "lightgray") +
geom_polygon(aes(fill = model_mean)) + theme_void() + facet_grid(rows = vars(party)) +
scale_fill_gradient2(midpoint = 0.5, limits = c(0, 1), breaks = c(0, .5, 1),
name = "Support", low = muted("blue"), high = muted("red")) +
theme(legend.margin=margin(l = 0.5, unit='cm'))
```