Pew Research Center (PEW)

License: GPL v3 Local Testing Badge

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.


Download, Import, Preparation

  1. Register for a Pew Research Center account at https://www.pewresearch.org/profile/registration/.

  2. DOWNLOAD THIS DATASET at https://www.pewresearch.org/global/dataset/spring-2022-survey-data/.

  3. 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:

# pew_df <- readRDS( pew_fn )

Survey Design Definition

Construct a complex sample survey design:

library(survey)

options( survey.lonely.psu = "adjust" )

pew_design <- 
    svydesign(
        id = ~ psu_constructed ,
        strata = ~ interaction( country , stratum_constructed ) ,
        data = pew_df ,
        weights = ~ weight , 
        nest = TRUE
    )

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:

sum( weights( pew_design , "sampling" ) != 0 )

svyby( ~ one , ~ country_name , pew_design , unwtd.count )

Weighted Counts

Count the weighted size of the generalizable population, overall and by groups:

svytotal( ~ one , pew_design )

svyby( ~ one , ~ country_name , pew_design , svytotal )

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:

svyratio( 
    numerator = ~ favorable_unfavorable_one_to_four_un , 
    denominator = ~ favorable_unfavorable_one_to_four_us , 
    pew_design ,
    na.rm = TRUE
)

Subsetting

Restrict the survey design to :

sub_pew_design <- subset( pew_design , country_name == 'South Korea' )

Calculate the mean (average) of this subset:

svymean( ~ topcoded_respondent_age , sub_pew_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:

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:

degf( pew_design )

Calculate the complex sample survey-adjusted variance of any statistic:

svyvar( ~ topcoded_respondent_age , pew_design , na.rm = TRUE )

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:

svyciprop( ~ human_rights_priority_with_china , pew_design ,
    method = "likelihood" , na.rm = TRUE )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( topcoded_respondent_age ~ human_rights_priority_with_china , pew_design )

Perform a chi-squared test of association for survey data:

svychisq( 
    ~ human_rights_priority_with_china + econ_sit , 
    pew_design 
)

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:

  1. DOWNLOAD THIS DATASET at https://www.pewresearch.org/politics/dataset/april-2017-political-survey/.

  2. 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:

library(srvyr)
pew_srvyr_design <- as_survey( pew_design )

Calculate the mean (average) of a linear variable, overall and by groups:

pew_srvyr_design %>%
    summarize( mean = survey_mean( topcoded_respondent_age , na.rm = TRUE ) )

pew_srvyr_design %>%
    group_by( country_name ) %>%
    summarize( mean = survey_mean( topcoded_respondent_age , na.rm = TRUE ) )