General Social Survey (GSS)

Build Status Build status

The General Social Survey (GSS) has captured political beliefs and social attitudes since 1972. In contrast to non-trendable tracking polls that capture newspaper headlines, the GSS has sustained a set of questions over four decades.

  • One table with one row per sampled respondent.

  • A complex sample survey designed to generalize to the non-institutional population of adults (18+) in the United States.

  • Updated biennially since 1972.

  • Funded by the National Science Foundation and administered by the National Opinion Research Center.

Simplified Download and Importation

The R lodown package easily downloads and imports all available GSS microdata by simply specifying "gss" with an output_dir = parameter in the lodown() function. Depending on your internet connection and computer processing speed, you might prefer to run this step overnight.

library(lodown)
lodown( "gss" , output_dir = file.path( path.expand( "~" ) , "GSS" ) )

Analysis Examples with the survey library

Construct a complex sample survey design:

library(survey)

gss_df <- 
    readRDS( file.path( path.expand( "~" ) , "GSS" , 
        "gss 1972 2016 cross sectional cumulative data release 2 september 29 2017.rds" ) )

gss_df <- 
    transform( 
        gss_df , 
        
        # the calculation for compwt comes from
        # http://sda.berkeley.edu/D3/GSS10/Doc/gs100195.htm#COMPWT
        compwt = oversamp * formwt * wtssall , 
        
        # the calculation for samplerc comes from
        # http://sda.berkeley.edu/D3/GSS10/Doc/gs100195.htm#SAMPLERC
        samplerc = 
            # if sample is a three or a four, samplerc should be a three
            ifelse( sample %in% 3:4 , 3 , 
            # if sample is a six or a seven, samplerc should be a six
            ifelse( sample %in% 6:7 , 6 , 
            # otherwise, samplerc should just be set to sample
                sample ) )

    )

# keep only the variables you need
keep_vars <- 
    c( "sampcode" , "samplerc" , "compwt" , "polviews" , 
        "born" , "adults" , "hompop" , "race" , "region" ,
        "age" , "sex" , "one" )
        
gss_df <- gss_df[ keep_vars ] ; gc()
# this step conserves RAM

gss_design <- 
    svydesign( 
        ~sampcode , 
        strata = ~samplerc , 
        data = subset( gss_df , !is.na( sampcode ) ) , 
        weights = ~compwt , 
        nest = TRUE 
    )

Variable Recoding

Add new columns to the data set:

gss_design <- 
    update( 
        gss_design , 

        polviews = 
            factor( polviews ,
                labels = c( "Extremely liberal" , "Liberal" ,
                "Slightly liberal" , "Moderate, middle of the road" ,
                "Slightly conservative" , "Conservative" ,
                "Extremely conservative" )
            ) ,
        
        born_in_usa = ifelse( born %in% 1:2 , as.numeric( born == 1 ) , NA ) ,
        
        adults_in_hh = ifelse( adults > 8 , NA , adults ) ,
        
        persons_in_hh = ifelse( hompop > 11 , NA , hompop ) ,
        
        race = factor( race , labels = c( "white" , "black" , "other" ) ) ,
        
        region = 
            factor( region , 
                labels = c( "New England" , "Middle Atlantic" ,
                    "East North Central" , "West North Central" ,
                    "South Atlantic" , "East South Central" ,
                    "West South Central" , "Mountain" , "Pacific" )
            )

    )

Unweighted Counts

Count the unweighted number of records in the survey sample, overall and by groups:

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

svyby( ~ one , ~ region , gss_design , unwtd.count )

Weighted Counts

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

svytotal( ~ one , gss_design )

svyby( ~ one , ~ region , gss_design , svytotal )

Descriptive Statistics

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

svymean( ~ age , gss_design , na.rm = TRUE )

svyby( ~ age , ~ region , gss_design , svymean , na.rm = TRUE )

Calculate the distribution of a categorical variable, overall and by groups:

svymean( ~ race , gss_design , na.rm = TRUE )

svyby( ~ race , ~ region , gss_design , svymean , na.rm = TRUE )

Calculate the sum of a linear variable, overall and by groups:

svytotal( ~ age , gss_design , na.rm = TRUE )

svyby( ~ age , ~ region , gss_design , svytotal , na.rm = TRUE )

Calculate the weighted sum of a categorical variable, overall and by groups:

svytotal( ~ race , gss_design , na.rm = TRUE )

svyby( ~ race , ~ region , gss_design , svytotal , na.rm = TRUE )

Calculate the median (50th percentile) of a linear variable, overall and by groups:

svyquantile( ~ age , gss_design , 0.5 , na.rm = TRUE )

svyby( 
    ~ age , 
    ~ region , 
    gss_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE ,
    keep.var = TRUE ,
    na.rm = TRUE
)

Estimate a ratio:

svyratio( 
    numerator = ~ adults_in_hh , 
    denominator = ~ persons_in_hh , 
    gss_design ,
    na.rm = TRUE
)

Subsetting

Restrict the survey design to females:

sub_gss_design <- subset( gss_design , sex == 2 )

Calculate the mean (average) of this subset:

svymean( ~ age , sub_gss_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( ~ age , gss_design , na.rm = TRUE )

coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )

grouped_result <-
    svyby( 
        ~ age , 
        ~ region , 
        gss_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( gss_design )

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

svyvar( ~ age , gss_design , na.rm = TRUE )

Include the complex sample design effect in the result for a specific statistic:

# SRS without replacement
svymean( ~ age , gss_design , na.rm = TRUE , deff = TRUE )

# SRS with replacement
svymean( ~ age , gss_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( ~ born_in_usa , gss_design ,
    method = "likelihood" , na.rm = TRUE )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( age ~ born_in_usa , gss_design )

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

svychisq( 
    ~ born_in_usa + race , 
    gss_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        age ~ born_in_usa + race , 
        gss_design 
    )

summary( glm_result )

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 GSS users, this code replicates previously-presented examples:

library(srvyr)
gss_srvyr_design <- as_survey( gss_design )

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

gss_srvyr_design %>%
    summarize( mean = survey_mean( age , na.rm = TRUE ) )

gss_srvyr_design %>%
    group_by( region ) %>%
    summarize( mean = survey_mean( age , na.rm = TRUE ) )

Replication Example