Introduction

The General Social Survey, or GSS, is one of the cornerstones of American social science and one of the most-analyzed datasets in Sociology. It is routinely used in research, in teaching, and as a reference point in discussions about changes in American society since the early 1970s. It is also a model of open, public data. The National Opinion Research Center already provides many excellent tools for working with the data, and has long made it freely available to researchers. Casual users of the GSS can examine the GSS Data Explorer, and social scientists can download complete datasets directly. At present, the GSS is provided to researchers in a choice of two commercial formats, Stata (.dta) and SPSS (.sav). It’s not too difficult to get the data into R (especially now that the Haven package is pretty reliable), but it can be a little annoying to have to do it repeatedly. After doing it one too many times, I got tired of it and I made a package instead. The gssr package provides the GSS Cumulative Data File (1972-2018) and three GSS Three Wave Panel Data Files (for panels beginning in 2006, 2008, and 2010, respectively), together with GSS codebooks, in a format that makes it straightforward to get started working with them in R. The gssr package makes the GSS a little more accessible to users of R, the free software environment for statistical computing, and thus helps in a small way to make the GSS even more open than it already is.

Load the gssr package and its data

We will begin with the Cumulative Data file (1972-2018). As the startup message notes, the data objects are not automatically loaded. That is, we do not use R’s “lazy loading” functionality. This is because the main GSS dataset is rather large. Instead we load it manually with data(). For the purposes of this vignette, because the full Cumulative Data object is big, we will use just a few columns of it stored in an object called gss_sub. But all the code here will also work with the full dataset object, gss_all, which you can load with the command data(gss_all). We will also load the tibble that contains the codebook for the Cumulative Data File. This is called gss_doc.

The GSS data comes in a labelled format, mirroring the way it is encoded for Stata and SPSS platforms. The numeric codes are the content of the column cells. The labeling information is stored as an attribute of the column.

We will use the label information later when recoding the variables into, say, character or factor variables. The labels and values are reflected in the codebook tibble. To see them, pull out the marginals list-column:

Alternatively, use the function gss_get_marginals() to see a tibble for one or more categorical variables:

A similar function lets you peek at the codebook’s properties for any variable:

The description and any additional codebook text can be extracted directly:

Descriptive analysis of the data: an example

The GSS is a complex survey. When working with it, we need to take its structure into account in order to properly calculate statistics such as the population mean for a variable in some year, its standard error, and so on. For these tasks we use the survey and srvyr packages. For details on survey, see Lumley (2010). We will also do some recoding prior to analyzing the data, so we load several additional tidyverse packages to assist us.

We will examine a topic that was the subject of recent media attention, in the New York Times and elsewhere, regarding the beliefs of young men about gender roles. Some surveys seemed to point to some recent increasing conservatism on this front amongst young men. As it happens, the GSS has a longstanding question named fefam, where respondents are asked to give their opinion on the following statement:

It is much better for everyone involved if the man is the achiever outside the home and the woman takes care of the home and family.

Respondents may answer that they Strongly Agree, Agree, Disagree, or Strongly Disagree with the statement (as well as refusing to answer, or saying they don’t know).

Subset the Data

The GSS data retains labeling information (as it was originally imported via the haven package). When working with the data in an analysis, we will probably want to convert the labeled variables to data types such as factors. This should be done with care (and not on the whole dataset all at once). Typically, we will want to focus on some relatively small subset of variables and examine those. For example, let’s say we want to explore the fefam question. We will subset the data and then prepare that for analysis. Here we are going to subset gss_sub into an object called gss_fam containing just the variables we want to examine, along with core measures that identify respondents (such as id and year) and variables necessary for the survey weighting later (such as wtssall).

Recode the Subsetted Data

Next, some recoding, along with creating some new variables. We will clean up gss_fam a bit, discarding some of the label and missing value information we don’t need. We also create some new variables: age quintiles, a variable flagging whether a respondent is 25 or younger, recoded fefam to binary “Agree” or “Disagree” (with non-responses dropped).

qrts <- quantile(as.numeric(gss_fam$age), 
                 na.rm = TRUE)
qrts
#>   0%  25%  50%  75% 100% 
#>   18   31   44   59   89

quintiles <- quantile(as.numeric(gss_fam$age), 
                      probs = seq(0, 1, 0.2), na.rm = TRUE)

quintiles
#>   0%  20%  40%  60%  80% 100% 
#>   18   29   38   49   63   89

## Recoding
## The convert_agegrp() and capwords() functions seen here are defined
## at the top of the Rmd file used to produce this document.

gss_fam <- gss_fam %>%
  purrr::modify_at(vars(), haven::zap_missing) %>%
  purrr::modify_at(wt_vars, as.numeric) %>%
  purrr::modify_at(cat_vars, forcats::as_factor) %>%
  purrr::modify_at(cat_vars, forcats::fct_relabel, capwords, strict = TRUE) %>%
  mutate(ageq = cut(x = age, breaks = unique(qrts), include.lowest = TRUE),
           ageq =  forcats::fct_relabel(ageq, convert_agegrp), 
           agequint = cut(x = age, breaks = unique(quintiles), include.lowest = TRUE),
           agequint = forcats::fct_relabel(agequint, convert_agegrp),
           year_f = droplevels(factor(year)),
           young = ifelse(age < 26, "Yes", "No"),
           fefam = forcats::fct_recode(fefam, NULL = "IAP", NULL = "DK", NULL = "NA"),
           fefam_d = forcats::fct_recode(fefam,
                                Agree = "Strongly Agree",
                                Disagree = "Strongly Disagree"),
           fefam_n = recode(fefam_d, "Agree" = 0, "Disagree" = 1))

gss_fam <- gss_fam %>% 
  mutate(compwt = oversamp * formwt * wtssall, 
         samplerc = case_when(sample %in% c(3:4) ~ 3, 
                              sample %in% c(6:7) ~ 6,
                              TRUE ~ sample))

gss_fam
#> # A tibble: 64,814 x 22
#>     year    id ballot   age race  sex   fefam  vpsu vstrat oversamp formwt
#>    <dbl> <dbl> <dbl+> <dbl> <fct> <fct> <fct> <dbl>  <dbl>    <dbl>  <dbl>
#>  1  1972     1     NA    23 White Fema… <NA>     NA     NA        1      1
#>  2  1972     2     NA    70 White Male  <NA>     NA     NA        1      1
#>  3  1972     3     NA    48 White Fema… <NA>     NA     NA        1      1
#>  4  1972     4     NA    27 White Fema… <NA>     NA     NA        1      1
#>  5  1972     5     NA    61 White Fema… <NA>     NA     NA        1      1
#>  6  1972     6     NA    26 White Male  <NA>     NA     NA        1      1
#>  7  1972     7     NA    28 White Male  <NA>     NA     NA        1      1
#>  8  1972     8     NA    27 White Male  <NA>     NA     NA        1      1
#>  9  1972     9     NA    21 Black Fema… <NA>     NA     NA        1      1
#> 10  1972    10     NA    30 Black Fema… <NA>     NA     NA        1      1
#> # … with 64,804 more rows, and 11 more variables: wtssall <dbl>,
#> #   sampcode <dbl>, sample <dbl>, ageq <fct>, agequint <fct>,
#> #   year_f <fct>, young <chr>, fefam_d <fct>, fefam_n <dbl>, compwt <dbl>,
#> #   samplerc <dbl>

Integrate the Survey Weights

Nexr, we set up the data so we can properly calculate population means and errors and so on. We use svyr’s wrappers to survey for this.


options(survey.lonely.psu = "adjust")
options(na.action="na.pass")

gss_svy <- gss_fam %>%
    filter(year > 1974) %>%
    tidyr::drop_na(fefam_d, young) %>%
    mutate(stratvar = interaction(year, vstrat)) %>%
    as_survey_design(id = vpsu,
                     strata = stratvar,
                     weights = wtssall,
                     nest = TRUE)

The gss_svy object contains the same data as gss_fam, but incorporates information about the sampling structure in a way that the survey package’s functions can work with:

Plot the Results

We finish with a polished plot of the trends in fefam over time, for men and women in two (recoded) age groups over time.

The GSS Three Wave Panels

In addition to the Cumulative Data File, the gssr package also includes the GSS’s panel data. The current rotating panel design began in 2006. A panel of respondents were interviewed that year and followed up on for further interviews in 2008 and 2010. A second panel was interviewed beginning in 2008, and was followed up on for further interviews in 2010 and 2012. And a third panel began in 2010, with follow-up interviews in 2012 and 2014. The gssr package provides three datasets, one for each of three-wave panels. They are gss_panel06_long, gss_panel08_long, and gss_panel10_long. The datasets are provided by the GSS in wide format but (as their names suggest) are packaged here in long format. The conversion was carried out using the panelr package and its long_panel() function. Conversion from long back to wide format is possible with the tools provided in panelr.

We load the panel data as before. For example:

data(gss_panel06_long)

gss_panel06_long
#> # A tibble: 6,000 x 1,572
#>    firstid  wave  ballot    form formwt oversamp sampcode  sample
#>    <fct>   <dbl> <dbl+l> <dbl+l>  <dbl>    <dbl> <dbl+lb> <dbl+l>
#>  1 9           1 3 [BAL… 2 [ALT…      1        1      501 9 [200…
#>  2 9           2 3 [BAL… 2 [ALT…      1        1      501 9 [200…
#>  3 9           3 3 [BAL… 2 [ALT…      1        1      501 9 [200…
#>  4 10          1 1 [BAL… 1 [STA…      1        1      501 9 [200…
#>  5 10          2 1 [BAL… 1 [STA…      1        1      501 9 [200…
#>  6 10          3 1 [BAL… 1 [STA…      1        1      501 9 [200…
#>  7 11          1 3 [BAL… 2 [ALT…      1        1      501 9 [200…
#>  8 11          2 3 [BAL… 2 [ALT…      1        1      501 9 [200…
#>  9 11          3 3 [BAL… 2 [ALT…      1        1      501 9 [200…
#> 10 12          1 1 [BAL… 2 [ALT…      1        1      501 9 [200…
#> # … with 5,990 more rows, and 1,564 more variables: samptype <dbl+lbl>,
#> #   vstrat <dbl+lbl>, vpsu <dbl+lbl>, wtpan12 <dbl+lbl>,
#> #   wtpan123 <dbl+lbl>, wtpannr12 <dbl+lbl>, wtpannr123 <dbl+lbl>,
#> #   letin1a <dbl+lbl>, abany <dbl+lbl>, abdefect <dbl+lbl>,
#> #   abhlth <dbl+lbl>, abnomore <dbl+lbl>, abpoor <dbl+lbl>,
#> #   abrape <dbl+lbl>, absingle <dbl+lbl>, accntsci <dbl+lbl>,
#> #   acqasian <dbl+lbl>, acqattnd <dbl+lbl>, acqblack <dbl+lbl>,
#> #   acqbrnda <dbl+lbl>, acqchild <dbl+lbl>, acqcohab <dbl+lbl>,
#> #   acqcon <dbl+lbl>, acqcops <dbl+lbl>, acqdems <dbl+lbl>,
#> #   acqelecs <dbl+lbl>, acqfmasn <dbl+lbl>, acqfmblk <dbl+lbl>,
#> #   acqfmcoh <dbl+lbl>, acqfmcon <dbl+lbl>, acqfmgay <dbl+lbl>,
#> #   acqfmgo <dbl+lbl>, acqfmhme <dbl+lbl>, acqfmhsp <dbl+lbl>,
#> #   acqfmlib <dbl+lbl>, acqfmlin <dbl+lbl>, acqfmmrk <dbl+lbl>,
#> #   acqfmno <dbl+lbl>, acqfmpri <dbl+lbl>, acqfmune <dbl+lbl>,
#> #   acqfmwht <dbl+lbl>, acqgay <dbl+lbl>, acqgoatt <dbl+lbl>,
#> #   acqhisp <dbl+lbl>, acqhome <dbl+lbl>, acqjans <dbl+lbl>,
#> #   acqjose <dbl+lbl>, acqkaren <dbl+lbl>, acqkeith <dbl+lbl>,
#> #   acqkevin <dbl+lbl>, acqlaws <dbl+lbl>, acqlib <dbl+lbl>,
#> #   acqlinda <dbl+lbl>, acqmaria <dbl+lbl>, acqmark <dbl+lbl>,
#> #   acqmils <dbl+lbl>, acqmyrac <dbl+lbl>, acqnhasn <dbl+lbl>,
#> #   acqnhblk <dbl+lbl>, acqnhcoh <dbl+lbl>, acqnhcon <dbl+lbl>,
#> #   acqnhgay <dbl+lbl>, acqnhgo <dbl+lbl>, acqnhhme <dbl+lbl>,
#> #   acqnhhsp <dbl+lbl>, acqnhlib <dbl+lbl>, acqnhlin <dbl+lbl>,
#> #   acqnhmrk <dbl+lbl>, acqnhno <dbl+lbl>, acqnhpri <dbl+lbl>,
#> #   acqnhune <dbl+lbl>, acqnhwht <dbl+lbl>, acqnoatt <dbl+lbl>,
#> #   acqntsex <dbl+lbl>, acqprisn <dbl+lbl>, acqrachl <dbl+lbl>,
#> #   acqreps <dbl+lbl>, acqshawn <dbl+lbl>, acqsocs <dbl+lbl>,
#> #   acqunemp <dbl+lbl>, acqvaasn <dbl+lbl>, acqvablk <dbl+lbl>,
#> #   acqvacoh <dbl+lbl>, acqvacon <dbl+lbl>, acqvagay <dbl+lbl>,
#> #   acqvago <dbl+lbl>, acqvahme <dbl+lbl>, acqvahsp <dbl+lbl>,
#> #   acqvalib <dbl+lbl>, acqvalin <dbl+lbl>, acqvamrk <dbl+lbl>,
#> #   acqvano <dbl+lbl>, acqvapri <dbl+lbl>, acqvaune <dbl+lbl>,
#> #   acqvawht <dbl+lbl>, acqwhite <dbl+lbl>, acqwkasn <dbl+lbl>,
#> #   acqwkblk <dbl+lbl>, acqwkcoh <dbl+lbl>, acqwkcon <dbl+lbl>, …

The panel data objects were created by panelr but are regular tibbles. The column names in long format do not have wave identifiers. Rather, firstid and wave variables track the cases. The firstid variable is unique for every row and has no missing values. The id variable is from the GSS and tracks individuals within waves.

We can look at attrition across waves with, e.g.:

The documentation tibble for the panel data is called gss_panel_doc.

Because it was created from the main GSS codebook, it is in wide format and the time-varying variables have wave identifiers. The identifiers are the suffixes _1, _2, and _3, for the first, second, and third waves. The variable names are capitalized. The categorical variables in the panel codebook can be queried in the same way as those in the cumulative codebook. We specify that we want to look at gss_panel_doc rather than gss_doc.

References

Lumley, Thomas (2010). Complex Surveys: A Guide to Analysis Using R. New York: Wiley.