American National Election Studies (ANES)
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.
Please skim before you begin:
ANES 2020 Time Series Study Full Release: User Guide and Codebook
A haiku regarding this microdata:
Function Definitions
Define a function to import a stata file as a data.frame:
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
Register for the ANES Data Center at https://electionstudies.org/
Choose
2020 Time Series Study
Download the
STATA
version of the February 10, 2022 file:
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:
# anes_fn <- file.path( path.expand( "~" ) , "ANES" , "this_file.rds" )
# saveRDS( anes_df , file = anes_fn , compress = FALSE )
Load the same object:
Variable Recoding
Add new columns to the data set:
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:
Descriptive Statistics
Calculate the mean (average) of a linear variable, overall and by groups:
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:
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:
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:
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:
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:
Subsetting
Restrict the survey design to party id: independent:
Calculate the mean (average) of this subset:
Measures of Uncertainty
Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
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:
Calculate the complex sample survey-adjusted variance of any statistic:
Include the complex sample design effect in the result for a specific statistic:
# 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:
Regression Models and Tests of Association
Perform a design-based t-test:
Perform a chi-squared test of association for survey data:
Perform a survey-weighted generalized linear model:
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:
Log in to the ANES Data Center at https://electionstudies.org/
Choose
2016 Time Series Study
.Download the
DTA
version of the September 4, 2019 fileDownload the
DTA
version of theMethodology File December 10, 2018
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:
Log in to the ANES Data Center at https://electionstudies.org/
Choose
2004 Time Series Study
4Download the
DTA
version of the Full Release August 16, 2005 fileChoose
2006 Pilot Study
Download the
DTA
version of the April 26, 2007 file
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-like syntax. 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 details the available features. As a starting point for ANES users, this code replicates previously-presented examples:
Calculate the mean (average) of a linear variable, overall and by groups: