Current Population Survey - Basic Monthly (CPSBASIC)

Build Status Build status

The Current Population Survey - Basic Monthly is the monthly labor force survey of the United States.

  • One table with one row per sampled youth respondent.

  • A complex sample survey designed to generalize to the civilian non-institutional population of the United States

  • Released monthly since 1994.

  • Administered jointly by the US Census Bureau and the Bureau of Labor Statistics.

Simplified Download and Importation

The R lodown package easily downloads and imports all available CPSBASIC microdata by simply specifying "cpsbasic" 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( "cpsbasic" , output_dir = file.path( path.expand( "~" ) , "CPSBASIC" ) )

lodown also provides a catalog of available microdata extracts with the get_catalog() function. After requesting the CPSBASIC catalog, you could pass a subsetted catalog through the lodown() function in order to download and import specific extracts (rather than all available extracts).

library(lodown)
# examine all available CPSBASIC microdata files
cpsbasic_cat <-
    get_catalog( "cpsbasic" ,
        output_dir = file.path( path.expand( "~" ) , "CPSBASIC" ) )

# march 2017 only
cpsbasic_cat <- subset( cpsbasic_cat , year == 2017 & month == 3 )
# download the microdata to your local computer
lodown( "cpsbasic" , cpsbasic_cat )

Analysis Examples with the survey library

Construct a complex sample survey design:

library(survey)

cpsbasic_df <- 
    readRDS( file.path( path.expand( "~" ) , "CPSBASIC" , "2017 03 cps basic.rds" ) )

# construct a fake survey design
warning( "this survey design produces correct point estimates
but incorrect standard errors." )
cpsbasic_design <- 
    svydesign( 
        ~ 1 , 
        data = cpsbasic_df , 
        weights = ~ pwsswgt
    )

Variable Recoding

Add new columns to the data set:

cpsbasic_design <- 
    update( 
        cpsbasic_design , 
        
        one = 1 ,
        
        pesex = factor( pesex , levels = 1:2 , labels = c( 'male' , 'female' ) ) ,
        
        weekly_earnings = ifelse( prernwa == -.01 , NA , prernwa ) ,
        
        # exclude anyone whose hours vary
        weekly_hours = ifelse( pehrusl1 < 0 , NA , pehrusl1 ) ,
        
        class_of_worker =
            factor( peio1cow , levels = 1:8 ,
                labels = 
                    c( "government - federal" , "government - state" ,
                    "government - local" , "private, for profit" ,
                    "private, nonprofit" , "self-employed, incorporated" ,
                    "self-employed, unincorporated" , "without pay" )
            ) ,
            
        part_time = ifelse( pemlr == 1 , as.numeric( pehruslt < 35 ) , NA )
    )

Unweighted Counts

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

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

svyby( ~ one , ~ pesex , cpsbasic_design , unwtd.count )

Weighted Counts

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

svytotal( ~ one , cpsbasic_design )

svyby( ~ one , ~ pesex , cpsbasic_design , svytotal )

Descriptive Statistics

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

svymean( ~ weekly_earnings , cpsbasic_design , na.rm = TRUE )

svyby( ~ weekly_earnings , ~ pesex , cpsbasic_design , svymean , na.rm = TRUE )

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

svymean( ~ class_of_worker , cpsbasic_design , na.rm = TRUE )

svyby( ~ class_of_worker , ~ pesex , cpsbasic_design , svymean , na.rm = TRUE )

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

svytotal( ~ weekly_earnings , cpsbasic_design , na.rm = TRUE )

svyby( ~ weekly_earnings , ~ pesex , cpsbasic_design , svytotal , na.rm = TRUE )

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

svytotal( ~ class_of_worker , cpsbasic_design , na.rm = TRUE )

svyby( ~ class_of_worker , ~ pesex , cpsbasic_design , svytotal , na.rm = TRUE )

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

svyquantile( ~ weekly_earnings , cpsbasic_design , 0.5 , na.rm = TRUE )

svyby( 
    ~ weekly_earnings , 
    ~ pesex , 
    cpsbasic_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE ,
    keep.var = TRUE ,
    na.rm = TRUE
)

Estimate a ratio:

svyratio( 
    numerator = ~ weekly_earnings , 
    denominator = ~ weekly_hours , 
    cpsbasic_design ,
    na.rm = TRUE
)

Subsetting

Restrict the survey design to california residents:

sub_cpsbasic_design <- subset( cpsbasic_design , gestfips == 6 )

Calculate the mean (average) of this subset:

svymean( ~ weekly_earnings , sub_cpsbasic_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( ~ weekly_earnings , cpsbasic_design , na.rm = TRUE )

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

grouped_result <-
    svyby( 
        ~ weekly_earnings , 
        ~ pesex , 
        cpsbasic_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( cpsbasic_design )

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

svyvar( ~ weekly_earnings , cpsbasic_design , na.rm = TRUE )

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

# SRS without replacement
svymean( ~ weekly_earnings , cpsbasic_design , na.rm = TRUE , deff = TRUE )

# SRS with replacement
svymean( ~ weekly_earnings , cpsbasic_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( ~ part_time , cpsbasic_design ,
    method = "likelihood" , na.rm = TRUE )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( weekly_earnings ~ part_time , cpsbasic_design )

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

svychisq( 
    ~ part_time + class_of_worker , 
    cpsbasic_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        weekly_earnings ~ part_time + class_of_worker , 
        cpsbasic_design 
    )

summary( glm_result )

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

library(srvyr)
cpsbasic_srvyr_design <- as_survey( cpsbasic_design )

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

cpsbasic_srvyr_design %>%
    summarize( mean = survey_mean( weekly_earnings , na.rm = TRUE ) )

cpsbasic_srvyr_design %>%
    group_by( pesex ) %>%
    summarize( mean = survey_mean( weekly_earnings , na.rm = TRUE ) )

Replication Example