-
Notifications
You must be signed in to change notification settings - Fork 449
/
anes.Rmd
480 lines (339 loc) · 14 KB
/
anes.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
# American National Election Studies (ANES) {-}
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <img src='https://img.shields.io/badge/Tested%20Locally-Windows%20Laptop-brightgreen' alt='Local Testing Badge'>
A time series recording belief, public opinion, and political participation back to Dewey vs. Truman.
* Most tables contain one row per sampled eligible voter, varying weights like pre- and post-election.
* A complex sample generalizing to eligible voters in the U.S. with some panels to follow individuals.
* Core studies released quadrennially (presidential elections), plus pilot studies (often at midterms).
* Administered by a consortium of universities and funded by the [National Science Foundation](https://www.nsf.gov).
---
## Recommended Reading {-}
Four Example Strengths & Limitations:
✔️ [Time series studies interview both before and after quadrennial elections](https://electionstudies.org/data-center/2024-time-series-study/)
✔️ [Instrument design tested in smaller study prior to inclusion](https://electionstudies.org/data-center/2024-pilot-study/)
❌ [Turnout errors are part of a long-standing problem of turnout over-estimation in surveys](https://electionstudies.org/wp-content/uploads/2022/03/DeBell-Maisel-Benchmark-Report-for-the-ANES-2016-Time-Series-Study.pdf#page=5)
❌ [Prior survey questions not always asked again](https://electionstudies.org/data-tools/anes-continuity-guide/)
<br>
Three Example Findings:
1. [Younger Americans were less politically polarized than older Americans in 2020](https://doi.org/10.1371/journal.pone.0302434).
2. [In 2020, 90% of Biden and Trump voters also opted for a congressional candidate of the same party](https://rollcall.com/2024/05/14/will-the-last-ticket-splitting-voter-please-turn-out-the-light/).
3. [Between 1996 and 2016, demographic groups least likely to use the Internet and social media experienced larger changes in political polarization than those more likely to use the Internet](https://doi.org/10.1073/pnas.1706588114).
<br>
Two Methodology Documents:
> [ANES 2020 Time Series Study Full Release: User Guide and Codebook](https://electionstudies.org/wp-content/uploads/2022/02/anes_timeseries_2020_userguidecodebook_20220210.pdf)
> [How to Analyze ANES Survey Data](https://electionstudies.org/wp-content/uploads/2018/04/nes012492.pdf)
<br>
One Haiku:
```{r}
# chez sacrificed queen
# quadrennial bloodless coup
# knight churchill's least worst
```
---
## Function Definitions {-}
Define a function to import a stata file as a data.frame:
```{r eval = FALSE , results = "hide" }
library(haven)
anes_import_dta <-
function( this_fn ){
this_tbl <- read_dta( this_fn )
this_tbl <- zap_labels( this_tbl )
this_df <- data.frame( this_tbl )
names( this_df ) <- tolower( names( this_df ) )
this_df
}
```
---
## Download, Import, Preparation {-}
1. Register for the ANES Data Center at https://electionstudies.org/
2. Choose `2020 Time Series Study`
3. Download the `STATA` version of the February 10, 2022 file:
```{r eval = FALSE , results = "hide" }
library(haven)
anes_fn <-
file.path(
path.expand( "~" ) ,
"anes_timeseries_2020_stata_20220210.dta"
)
anes_df <- anes_import_dta( anes_fn )
```
### Save Locally \ {-}
Save the object at any point:
```{r eval = FALSE , results = "hide" }
# anes_fn <- file.path( path.expand( "~" ) , "ANES" , "this_file.rds" )
# saveRDS( anes_df , file = anes_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# anes_df <- readRDS( anes_fn )
```
### Survey Design Definition {-}
Construct a complex sample survey design:
```{r eval = FALSE , results = "hide" }
library(survey)
anes_design <-
svydesign(
ids = ~ v200010c ,
strata = ~ v200010d ,
weights = ~ v200010a ,
data = subset( anes_df , v200010a > 0 ) ,
nest = TRUE
)
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
anes_design <-
update(
anes_design ,
one = 1 ,
democratic_party_rating = ifelse( v201156 %in% 0:100 , v201156 , NA ) ,
republican_party_rating = ifelse( v201157 %in% 0:100 , v201157 , NA ) ,
primary_voter = ifelse( v201020 %in% 1:2 , as.numeric( v201020 == 1 ) , NA ) ,
think_gov_spend_least =
factor( v201645 , levels = 1:4 , labels =
c( 'foreign aid (correct)' , 'medicare' , 'national defense' , 'social security' )
) ,
undoc_kids =
factor( v201423x , levels = 1:6 , labels =
c( 'should sent back - favor a great deal' ,
'should sent back - favor a moderate amount' ,
'should sent back - favor a little' ,
'should allow to stay - favor a little' ,
'should allow to stay - favor a moderate amount' ,
'should allow to stay - favor a great deal' )
)
)
```
---
## Analysis Examples with the `survey` library \ {-}
### Unweighted Counts {-}
Count the unweighted number of records in the survey sample, overall and by groups:
```{r eval = FALSE , results = "hide" }
sum( weights( anes_design , "sampling" ) != 0 )
svyby( ~ one , ~ undoc_kids , anes_design , unwtd.count )
```
### Weighted Counts {-}
Count the weighted size of the generalizable population, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ one , anes_design )
svyby( ~ one , ~ undoc_kids , anes_design , svytotal )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ republican_party_rating , anes_design , na.rm = TRUE )
svyby( ~ republican_party_rating , ~ undoc_kids , anes_design , svymean , na.rm = TRUE )
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ think_gov_spend_least , anes_design , na.rm = TRUE )
svyby( ~ think_gov_spend_least , ~ undoc_kids , anes_design , svymean , na.rm = TRUE )
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ republican_party_rating , anes_design , na.rm = TRUE )
svyby( ~ republican_party_rating , ~ undoc_kids , anes_design , svytotal , na.rm = TRUE )
```
Calculate the weighted sum of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ think_gov_spend_least , anes_design , na.rm = TRUE )
svyby( ~ think_gov_spend_least , ~ undoc_kids , anes_design , svytotal , na.rm = TRUE )
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svyquantile( ~ republican_party_rating , anes_design , 0.5 , na.rm = TRUE )
svyby(
~ republican_party_rating ,
~ undoc_kids ,
anes_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE
)
```
Estimate a ratio:
```{r eval = FALSE , results = "hide" }
svyratio(
numerator = ~ republican_party_rating ,
denominator = ~ democratic_party_rating ,
anes_design ,
na.rm = TRUE
)
```
### Subsetting {-}
Restrict the survey design to party id: independent:
```{r eval = FALSE , results = "hide" }
sub_anes_design <- subset( anes_design , v201231x == 4 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
svymean( ~ republican_party_rating , sub_anes_design , na.rm = TRUE )
```
### Measures of Uncertainty {-}
Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
```{r eval = FALSE , results = "hide" }
this_result <- svymean( ~ republican_party_rating , anes_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ republican_party_rating ,
~ undoc_kids ,
anes_design ,
svymean ,
na.rm = TRUE
)
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )
```
Calculate the degrees of freedom of any survey design object:
```{r eval = FALSE , results = "hide" }
degf( anes_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ republican_party_rating , anes_design , na.rm = TRUE )
```
Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# SRS without replacement
svymean( ~ republican_party_rating , anes_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ republican_party_rating , anes_design , na.rm = TRUE , deff = "replace" )
```
Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See `?svyciprop` for alternatives:
```{r eval = FALSE , results = "hide" }
svyciprop( ~ primary_voter , anes_design ,
method = "likelihood" , na.rm = TRUE )
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( republican_party_rating ~ primary_voter , anes_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq(
~ primary_voter + think_gov_spend_least ,
anes_design
)
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
svyglm(
republican_party_rating ~ primary_voter + think_gov_spend_least ,
anes_design
)
summary( glm_result )
```
---
## Replication Example {-}
This example matches statistics and standard errors in the Age rows of the `ANES respondents (weighted)` column of `Table 1A` from [Benchmark and Attrition Report for the ANES 2016 Time Series Study](https://electionstudies.org/wp-content/uploads/2022/03/DeBell-Maisel-Benchmark-Report-for-the-ANES-2016-Time-Series-Study.pdf#page=4):
1. Log in to the ANES Data Center at https://electionstudies.org/
2. Choose `2016 Time Series Study`.
3. Download the `DTA` version of the September 4, 2019 file
4. Download the `DTA` version of the `Methodology File December 10, 2018`
```{r eval = FALSE , results = "hide" }
anes2016_fn <-
file.path(
path.expand( "~" ) ,
"anes_timeseries_2016.dta"
)
anes2016_df <- anes_import_dta( anes2016_fn )
method2016_fn <-
file.path(
path.expand( "~" ) ,
"anes_timeseries_2016_methodology_dta.dta"
)
method2016_df <- anes_import_dta( method2016_fn )
before_nrow <- nrow( anes2016_df )
anes2016_df <- merge( anes2016_df , method2016_df , by = 'v160001' )
stopifnot( nrow( anes2016_df ) == before_nrow )
anes2016_df[ , 'age_categories' ] <-
factor(
findInterval(
anes2016_df[ , 'v161267' ] ,
c( 18 , seq( 30 , 70 , 10 ) )
) ,
levels = 1:6 ,
labels = c( '18-29' , '30-39' , '40-49' , '50-59' , '60-69' , '70 or older' )
)
anes2016_design <-
svrepdesign(
data = subset( anes2016_df , v160101f > 0 ) ,
weights = ~ v160101f ,
repweights = 'weight_ftf_rkwt([0-9]+)' ,
type = 'JK1' ,
scale = 32 / 33
)
( results <- svymean( ~ age_categories , anes2016_design , na.rm = TRUE ) )
published_results <- c( 0.21 , 0.158 , 0.156 , 0.2 , 0.147 , 0.129 )
published_standard_errors <- c( 0.0091 , 0.009 , 0.0094 , 0.0122 , 0.0069 , 0.0083 )
stopifnot( all( round( coef( results ) , 3 ) == published_results ) )
stopifnot( all( round( SE( results ) , 4 ) == published_standard_errors ) )
```
This example matches statistics and standard errors in the Age rows of the `Design-consistent, with published strata` column of `Table 1` from [How to Analyze ANES Survey Data](https://electionstudies.org/wp-content/uploads/2018/04/nes012492.pdf#page=25):
1. Log in to the ANES Data Center at https://electionstudies.org/
2. Choose `2004 Time Series Study`4
3. Download the `DTA` version of the Full Release August 16, 2005 file
4. Choose `2006 Pilot Study`
5. Download the `DTA` version of the April 26, 2007 file
```{r eval = FALSE , results = "hide" }
anes2004_fn <-
file.path(
path.expand( "~" ) ,
"anes2004TS.dta"
)
anes2004_df <- anes_import_dta( anes2004_fn )
pilot2006_fn <-
file.path(
path.expand( "~" ) ,
"anes2006pilot.dta"
)
pilot2006_df <- anes_import_dta( pilot2006_fn )
before_nrow <- nrow( pilot2006_df )
pilot2006_df <- merge( pilot2006_df , anes2004_df , by.x = 'v06p001' , by.y = 'v040001' )
stopifnot( nrow( pilot2006_df ) == before_nrow )
pilot2006_df[ , 'age_categories' ] <-
factor(
findInterval(
pilot2006_df[ , 'v043250' ] ,
c( 18 , seq( 30 , 70 , 10 ) )
) ,
levels = 1:6 ,
labels = c( '18-29' , '30-39' , '40-49' , '50-59' , '60-69' , '70 or older' )
)
pilot2006_design <-
svydesign(
id = ~v06p007b ,
strata = ~v06p007a ,
data = pilot2006_df ,
weights = ~v06p002 ,
nest = TRUE
)
( results <- svymean( ~ age_categories , pilot2006_design , na.rm = TRUE ) )
published_results <- c( 0.207 , 0.162 , 0.218 , 0.175 , 0.111 , 0.126 )
published_standard_errors <- c( 0.0251 , 0.024 , 0.022 , 0.0149 , 0.0125 , 0.0287 )
stopifnot( all( round( coef( results ) , 3 ) == published_results ) )
stopifnot( all( round( SE( results ) , 4 ) == published_standard_errors ) )
```
---
## Analysis Examples with `srvyr` \ {-}
The R `srvyr` library calculates summary statistics from survey data, such as the mean, total or quantile using [dplyr](https://github.com/tidyverse/dplyr/)-like syntax. [srvyr](https://github.com/gergness/srvyr) allows for the use of many verbs, such as `summarize`, `group_by`, and `mutate`, the convenience of pipe-able functions, the `tidyverse` style of non-standard evaluation and more consistent return types than the `survey` package. [This vignette](https://cran.r-project.org/web/packages/srvyr/vignettes/srvyr-vs-survey.html) details the available features. As a starting point for ANES users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(srvyr)
anes_srvyr_design <- as_survey( anes_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
anes_srvyr_design %>%
summarize( mean = survey_mean( republican_party_rating , na.rm = TRUE ) )
anes_srvyr_design %>%
group_by( undoc_kids ) %>%
summarize( mean = survey_mean( republican_party_rating , na.rm = TRUE ) )
```