1.4 Current Population Survey - Annual Social and Economic Supplement (CPS-ASEC)

Sponsored jointly by the U.S. Census Bureau and the U.S. Bureau of Labor Statistics (BLS), the CPS-ASEC is the primary source of labor force statistics for the population of the United States.

This section downloads, imports, and prepares the most current microdata for analysis, then reproduces some statistics and margin of error terms from the U.S. Census Bureau.

Download and unzip the 2023 file:

library(httr)

tf <- tempfile()

this_url <-
  "https://www2.census.gov/programs-surveys/cps/datasets/2023/march/asecpub23sas.zip"

GET(this_url , write_disk(tf), progress())
unzipped_files <- unzip(tf , exdir = tempdir())

Import all four files:

library(haven)

four_tbl <- lapply(unzipped_files , read_sas)

four_df <- lapply(four_tbl , data.frame)

four_df <-
  lapply(four_df , function(w) {
    names(w) <- tolower(names(w))
    w
  })

household_df <-
  four_df[[grep('hhpub' , basename(unzipped_files))]]
family_df <-
  four_df[[grep('ffpub' , basename(unzipped_files))]]
person_df <-
  four_df[[grep('pppub' , basename(unzipped_files))]]
repwgts_df <-
  four_df[[grep('repwgt' , basename(unzipped_files))]]

Divide weights:

household_df[, 'hsup_wgt'] <- household_df[, 'hsup_wgt'] / 100
family_df[, 'fsup_wgt'] <- family_df[, 'fsup_wgt'] / 100
for (j in c('marsupwt' , 'a_ernlwt' , 'a_fnlwgt'))
  person_df[, j] <- person_df[, j] / 100

Merge these four files:

names(family_df)[names(family_df) == 'fh_seq'] <- 'h_seq'
names(person_df)[names(person_df) == 'ph_seq'] <- 'h_seq'
names(person_df)[names(person_df) == 'phf_seq'] <- 'ffpos'

hh_fm_df <- merge(household_df , family_df)
hh_fm_pr_df <- merge(hh_fm_df , person_df)
cps_df <- merge(hh_fm_pr_df , repwgts_df)

stopifnot(nrow(cps_df) == nrow(person_df))

Construct a complex sample survey design:

library(survey)

cps_design <-
  svrepdesign(
    weights = ~ marsupwt ,
    repweights = "pwwgt[1-9]" ,
    type = "Fay" ,
    rho = (1 - 1 / sqrt(4)) ,
    data = cps_df ,
    combined.weights = TRUE ,
    mse = TRUE
  )

Add a sex variable:

cps_design <-
  update(cps_design, sex = factor(
    a_sex ,
    levels = 1:2 ,
    labels = c('male' , 'female')
  ))

Run the convey_prep() function on the full design:

cps_design <- convey_prep(cps_design)

Analysis Examples with the survey library  

Add new columns to the data set:

cps_design <-
  update(
    cps_design ,
    
    one = 1 ,
    
    a_maritl =
      factor(
        a_maritl ,
        labels =
          c(
            "married - civilian spouse present" ,
            "married - AF spouse present" ,
            "married - spouse absent" ,
            "widowed" ,
            "divorced" ,
            "separated" ,
            "never married"
          )
      ) ,
    
    state_name =
      factor(
        gestfips ,
        levels =
          c(
            1L,
            2L,
            4L,
            5L,
            6L,
            8L,
            9L,
            10L,
            11L,
            12L,
            13L,
            15L,
            16L,
            17L,
            18L,
            19L,
            20L,
            21L,
            22L,
            23L,
            24L,
            25L,
            26L,
            27L,
            28L,
            29L,
            30L,
            31L,
            32L,
            33L,
            34L,
            35L,
            36L,
            37L,
            38L,
            39L,
            40L,
            41L,
            42L,
            44L,
            45L,
            46L,
            47L,
            48L,
            49L,
            50L,
            51L,
            53L,
            54L,
            55L,
            56L
          ) ,
        labels =
          c(
            "Alabama",
            "Alaska",
            "Arizona",
            "Arkansas",
            "California",
            "Colorado",
            "Connecticut",
            "Delaware",
            "District of Columbia",
            "Florida",
            "Georgia",
            "Hawaii",
            "Idaho",
            "Illinois",
            "Indiana",
            "Iowa",
            "Kansas",
            "Kentucky",
            "Louisiana",
            "Maine",
            "Maryland",
            "Massachusetts",
            "Michigan",
            "Minnesota",
            "Mississippi",
            "Missouri",
            "Montana",
            "Nebraska",
            "Nevada",
            "New Hampshire",
            "New Jersey",
            "New Mexico",
            "New York",
            "North Carolina",
            "North Dakota",
            "Ohio",
            "Oklahoma",
            "Oregon",
            "Pennsylvania",
            "Rhode Island",
            "South Carolina",
            "South Dakota",
            "Tennessee",
            "Texas",
            "Utah",
            "Vermont",
            "Virginia",
            "Washington",
            "West Virginia",
            "Wisconsin",
            "Wyoming"
          )
      ) ,
    
    male = as.numeric(a_sex == 1)
  )

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

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

svyby( ~ one , ~ state_name , cps_design , unwtd.count )

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

svytotal( ~ one , cps_design )

svyby( ~ one , ~ state_name , cps_design , svytotal )

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

svymean( ~ pearnval , cps_design )

svyby( ~ pearnval , ~ state_name , cps_design , svymean )

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

svymean( ~ a_maritl , cps_design )

svyby( ~ a_maritl , ~ state_name , cps_design , svymean )

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

svytotal( ~ pearnval , cps_design )

svyby( ~ pearnval , ~ state_name , cps_design , svytotal )

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

svytotal( ~ a_maritl , cps_design )

svyby( ~ a_maritl , ~ state_name , cps_design , svytotal )

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

svyquantile( ~ pearnval , cps_design , 0.5 )

svyby( 
    ~ pearnval , 
    ~ state_name , 
    cps_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE 
)

Estimate a ratio:

svyratio( 
    numerator = ~ moop , 
    denominator = ~ pearnval , 
    cps_design 
)

Restrict the survey design to persons aged 18-64:

sub_cps_design <- subset( cps_design , a_age %in% 18:64 )

Calculate the mean (average) of this subset:

svymean( ~ pearnval , sub_cps_design )

Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:

this_result <- svymean( ~ pearnval , cps_design )

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

grouped_result <-
    svyby( 
        ~ pearnval , 
        ~ state_name , 
        cps_design , 
        svymean 
    )
    
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )

Calculate the degrees of freedom of any survey design object:

degf( cps_design )

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

svyvar( ~ pearnval , cps_design )

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

# SRS without replacement
svymean( ~ pearnval , cps_design , deff = TRUE )

# SRS with replacement
svymean( ~ pearnval , cps_design , deff = "replace" )

Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See ?svyciprop for alternatives:

svyciprop( ~ male , cps_design ,
    method = "likelihood" )

Perform a design-based t-test:

svyttest( pearnval ~ male , cps_design )

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

svychisq( 
    ~ male + a_maritl , 
    cps_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        pearnval ~ male + a_maritl , 
        cps_design 
    )

summary( glm_result )

1.4.1 Household Income

Limit the CPS-ASEC person-level design to the household reference person, then calculate the Gini coefficient with total household income:

cps_household_design <- subset(cps_design , a_exprrp %in% 1:2)

(cps_household_gini <- svygini( ~ htotval , cps_household_design))
##            gini    SE
## htotval 0.48846 0.002
# match 2022 household gini coefficient
# https://www2.census.gov/programs-surveys/cps/tables/time-series/historical-income-households/h04.xlsx
stopifnot(round(coef(cps_household_gini) , 3) == 0.488)

# match 2022 household gini margin of error
# https://www.census.gov/content/dam/Census/newsroom/press-kits/2023/iphi/20230912-iphi-slides-income.pdf#page=13
stopifnot(round(
  coef(cps_household_gini) - confint(cps_household_gini , level = 0.9)[1] ,
  4
) == 0.0033) 

Calculate matching Theil and Atkinson measures:

cps_household_design <-
  update(cps_household_design , htotval_ones = ifelse(htotval < 0 , NA , htotval))
cps_household_design <-
  update(cps_household_design ,
         htotval_ones = ifelse(htotval_ones < 1 , 1 , htotval_ones))

(cps_household_theil <-
    svygei(~ htotval_ones , cps_household_design , na.rm = TRUE))
##                  gei     SE
## htotval_ones 0.44013 0.0051
(
  cps_household_atkinson <-
    svyatk(
      ~ htotval_ones ,
      cps_household_design ,
      epsilon = 0.5 ,
      na.rm = TRUE
    )
)
##              atkinson     SE
## htotval_ones  0.20737 0.0017
# https://www2.census.gov/programs-surveys/cps/tables/time-series/historical-income-households/h04.xlsx
stopifnot(round(coef(cps_household_theil) , 3) == 0.440)
stopifnot(round(coef(cps_household_atkinson), 3) == 0.207)

1.4.2 Family Income

Limit the CPS-ASEC person-level design to the family reference person of primary families, then calculate the Gini coefficient with total family income:

cps_family_design <-
  subset(cps_design , a_famrel %in% 1 & a_famtyp %in% 1)

(cps_family_gini <- svygini( ~ ftotval , cps_family_design))
##            gini     SE
## ftotval 0.45816 0.0023
# match 2022 family gini coefficient
# https://www2.census.gov/programs-surveys/cps/tables/time-series/historical-income-families/f04.xlsx
stopifnot(round(coef(cps_family_gini) , 3) == 0.458)

1.4.3 Worker Earnings

The convey_prep() function sets the population of reference for poverty threshold estimation. For relative poverty measures, it is important to think what should be the reference population for the poverty threshold. For instance, if one is interested is computing poverty rates across regions using a poverty line computed at a nationwide level (represented by a full sample design object), it is important to run convey_prep() immediately after creating the full object. However, imagine that one wants to compute the poverty line using the annual earnings among only full-time, full year workers. In this case, subsetting the full object after running convey_prep() will compute the poverty line using the entire sample, including the earnings of part-time workers, part year workers, and also zeroes for the non-working population. So in this (non-standard) case, we suggest subsetting the main object and then running convey_prep() again to re-set the reference population:

Limit the CPS-ASEC person-level design to full-time, full year workers:

cps_ftfy_worker_design <- subset(cps_design , wewkrs %in% 1)

Re-set the population of reference for poverty threshold estimation:

cps_ftfy_worker_design <- convey_prep(cps_ftfy_worker_design)

Calculate the Gini coefficient with total earnings:

svygini( ~ pearnval , cps_ftfy_worker_design)
##           gini     SE
## pearnval 0.412 0.0026