Pew Research Center (PEW)
Public opinion polling on U.S. Politics & Policy, Journalism & Media, Internet, Science & Tech, Religion & Public Life, Hispanic Trends, Global Attitudes & Trends, and Social & Demographic Trends.
Generally one table per survey, with one row per sampled respondent.
Complex samples generalizing to the noninstitutionalized adults in the nation(s) surveyed.
Varying publication dates for both American Trends Panel surveys of the United States and also for International Surveys. National Public Opinion Reference Survey released annually since 2020.
Administered by the Pew Research Center.
Please skim before you begin:
Country Specific Methodology, for example the 2022 Global Attitudes Survey
A haiku regarding this microdata:
Download, Import, Preparation
Register for a Pew Research Center account at https://www.pewresearch.org/profile/registration/.
DOWNLOAD THIS DATASET
at https://www.pewresearch.org/global/dataset/spring-2022-survey-data/.Download the SPSS dataset
Pew-Research-Center-Global-Attitudes-Spring-2022-Survey-Data.zip
:
library(haven)
pew_fn <-
file.path(
path.expand( "~" ) ,
"Pew Research Center Global Attitudes Spring 2022 Dataset.sav"
)
pew_tbl <- read_sav( pew_fn )
pew_label <- lapply( pew_tbl , function( w ) attributes( w )[['label']] )
pew_labels <- lapply( pew_tbl , function( w ) attributes( w )[['labels']] )
pew_tbl <- zap_labels( pew_tbl )
pew_df <- data.frame( pew_tbl )
names( pew_df ) <- tolower( names( pew_df ) )
Collapse country-specific cluster and strata variables into two all-country cluster and strata variables:
# create the constructed psu and strata variables from among the
# non-missing country-specific columns
pew_df[ , 'psu_constructed' ] <-
apply(
pew_df[ , grep( "^psu_" , names( pew_df ) ) ] ,
1 ,
function( w ) w[ which.min( is.na( w ) ) ]
)
pew_df[ , 'stratum_constructed' ] <-
apply(
pew_df[ , grep( "^stratum_" , names( pew_df ) ) ] ,
1 ,
function( w ) w[ which.min( is.na( w ) ) ]
)
# for countries without clustering variables, give every record a unique identifier for the psu..
pew_df[ is.na( pew_df[ , 'psu_constructed' ] ) , 'psu_constructed' ] <-
rownames( pew_df[ is.na( pew_df[ , 'psu_constructed' ] ) , ] )
# ..and zeroes for the stratum
pew_df[ is.na( pew_df[ , 'stratum_constructed' ] ) , 'stratum_constructed' ] <- 0
Save Locally
Save the object at any point:
# pew_fn <- file.path( path.expand( "~" ) , "PEW" , "this_file.rds" )
# saveRDS( pew_df , file = pew_fn , compress = FALSE )
Load the same object:
Variable Recoding
Add new columns to the data set:
pew_design <-
update(
pew_design ,
one = 1 ,
topcoded_respondent_age = ifelse( age >= 99 , NA , ifelse( age >= 97 , 97 , age ) ) ,
human_rights_priority_with_china =
ifelse(
china_humanrights_priority > 2 ,
NA ,
as.numeric( china_humanrights_priority == 1 )
) ,
favorable_unfavorable_one_to_four_us = ifelse( fav_us > 4 , NA , fav_us ) ,
favorable_unfavorable_one_to_four_un = ifelse( fav_un > 4 , NA , fav_un ) ,
country_name =
factor(
country ,
levels = as.integer( pew_labels[[ 'country' ]] ) ,
labels = names( pew_labels[['country']] )
) ,
econ_sit =
factor(
econ_sit ,
levels = 1:4 ,
labels = c( 'Very good' , 'Somewhat good' , 'Somewhat bad' , 'Very bad' )
)
)
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( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )
svyby( ~ topcoded_respondent_age , ~ country_name , pew_design , svymean , na.rm = TRUE )
Calculate the distribution of a categorical variable, overall and by groups:
svymean( ~ econ_sit , pew_design , na.rm = TRUE )
svyby( ~ econ_sit , ~ country_name , pew_design , svymean , na.rm = TRUE )
Calculate the sum of a linear variable, overall and by groups:
svytotal( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )
svyby( ~ topcoded_respondent_age , ~ country_name , pew_design , svytotal , na.rm = TRUE )
Calculate the weighted sum of a categorical variable, overall and by groups:
svytotal( ~ econ_sit , pew_design , na.rm = TRUE )
svyby( ~ econ_sit , ~ country_name , pew_design , svytotal , na.rm = TRUE )
Calculate the median (50th percentile) of a linear variable, overall and by groups:
svyquantile( ~ topcoded_respondent_age , pew_design , 0.5 , na.rm = TRUE )
svyby(
~ topcoded_respondent_age ,
~ country_name ,
pew_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE , na.rm.all = TRUE
)
Estimate a ratio:
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( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ topcoded_respondent_age ,
~ country_name ,
pew_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( ~ topcoded_respondent_age , pew_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ topcoded_respondent_age , pew_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(
topcoded_respondent_age ~ human_rights_priority_with_china + econ_sit ,
pew_design
)
summary( glm_result )
Replication Example
This matches statistics and standard errors from How to analyze Pew Research Center survey data in R:
DOWNLOAD THIS DATASET
at https://www.pewresearch.org/politics/dataset/april-2017-political-survey/.Download the SPSS dataset
Apr17-public-4.3-update.zip
dated 12/29/2017:
political_survey_2017_fn <- file.path( path.expand( "~" ) , "Apr17 public.sav" )
political_survey_2017_tbl <- read_sav( political_survey_2017_fn )
political_survey_2017_df <- data.frame( political_survey_2017_tbl )
names( political_survey_2017_df ) <- tolower( names( political_survey_2017_df ) )
Construct a complex sample survey design:
political_survey_2017_design <-
svydesign(
~ 0 ,
data = political_survey_2017_df ,
weights = ~ weight
)
Add new columns to the data set:
political_survey_2017_design <-
update(
political_survey_2017_design ,
q1 =
factor(
q1 ,
levels = c( 1 , 2 , 9 ) ,
labels = c( 'Approve' , 'Disapprove' , 'DK/RF' )
)
)
Reproduce statistics and standard errors shown under Estimating frequencies with survey weights
:
result <- svymean( ~ q1 , political_survey_2017_design , na.rm = TRUE )
stopifnot( round( coef( result ) , 4 ) == c( 0.3940 , 0.5424 , 0.0636 ) )
stopifnot( round( SE( result ) , 4 ) == c( 0.0144 , 0.0147 , 0.0078 ) )
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 PEW users, this code replicates previously-presented examples:
Calculate the mean (average) of a linear variable, overall and by groups: