Pesquisa Nacional por Amostra de Domicilios (PNAD)

Github Actions Badge

Brazil’s principal labor force survey, measuring education, employment, income, housing characteristics.

  • One consolidated table with one row per individual within each sampled household.

  • A complex sample survey designed to generalize to the civilian non-institutional population of Brazil.

  • Released quarterly since 2012, with microdata available both quarterly and annually.

  • Administered by the Instituto Brasileiro de Geografia e Estatistica.


Please skim before you begin:

  1. Conceitos e métodos

  2. Wikipedia Entry

  3. This human-composed haiku or a bouquet of artificial intelligence-generated limericks

# mineiro data
# love verdade gave to me
# twelve karaoke..

Download, Import, Preparation

Download and import the dictionary file:

dictionary_tf <- tempfile()

dictionary_url <-
    paste0(
        "https://ftp.ibge.gov.br/Trabalho_e_Rendimento/" ,
        "Pesquisa_Nacional_por_Amostra_de_Domicilios_continua/" ,
        "Trimestral/Microdados/Documentacao/Dicionario_e_input_20221031.zip"
    )

download.file( dictionary_url , dictionary_tf , mode = 'wb' )

dictionary_files <- unzip( dictionary_tf , exdir = tempdir() )

sas_fn <- grep( '\\.sas$' , dictionary_files , value = TRUE )

sas_lines <- readLines( sas_fn , encoding = 'latin1' )

Determine fixed-width file positions from the SAS import script:

sas_start <- grep( '@0001' , sas_lines )

sas_end <- grep( ';' , sas_lines )

sas_end <- sas_end[ sas_end > sas_start ][ 1 ]

sas_lines <- sas_lines[ seq( sas_start , sas_end - 1 ) ]

# remove SAS comments
sas_lines <- gsub( "\\/\\*(.*)" , "" , sas_lines )

# remove multiple spaces and spaces at the end of each string
sas_lines <- gsub( "( +)" , " " , sas_lines )
sas_lines <- gsub( " $" , "" , sas_lines )

sas_df <- 
    read.table( 
        textConnection( sas_lines ) , 
        sep = ' ' , 
        col.names = c( 'position' , 'column_name' , 'length' ) ,
        header = FALSE 
    )

sas_df[ , 'character' ] <- grepl( '\\$' , sas_df[ , 'length' ] )

sas_df[ , 'position' ] <- as.integer( gsub( "\\@" , "" , sas_df[ , 'position' ] ) )

sas_df[ , 'length' ] <- as.integer( gsub( "\\$" , "" , sas_df[ , 'length' ] ) )

stopifnot( 
    sum( sas_df[ , 'length' ] ) == 
    ( sas_df[ nrow( sas_df ) , 'position' ] + sas_df[ nrow( sas_df ) , 'length' ] - 1 ) 
)

Download the latest quarterly file:

this_tf <- tempfile()

this_url <-
    paste0(
        "https://ftp.ibge.gov.br/Trabalho_e_Rendimento/" ,
        "Pesquisa_Nacional_por_Amostra_de_Domicilios_continua/" ,
        "Trimestral/Microdados/2023/PNADC_012023.zip"
    )

download.file( this_url , this_tf , mode = 'wb' )

Import the latest quarterly file:

library(readr)

pnad_tbl <-
    read_fwf(
        this_tf ,
        fwf_widths( 
            widths = sas_df[ , 'length' ] , 
            col_names = sas_df[ , 'column_name' ] 
        ) ,
        col_types = 
            paste0( ifelse( sas_df[ , 'character' ] , "c" , "d" ) , collapse = '' )
    )

pnad_df <- data.frame( pnad_tbl )

names( pnad_df ) <- tolower( names( pnad_df ) )

pnad_df[ , 'one' ] <- 1

Save locally  

Save the object at any point:

# pnad_fn <- file.path( path.expand( "~" ) , "PNAD" , "this_file.rds" )
# saveRDS( pnad_df , file = pnad_fn , compress = FALSE )

Load the same object:

# pnad_df <- readRDS( pnad_fn )

Survey Design Definition

Construct a complex sample survey design:

library(survey)

pnad_design <-
    svrepdesign(
        data = pnad_df ,
        weight = ~ v1028 ,
        type = 'bootstrap' ,
        repweights = 'v1028[0-9]+' ,
        mse = TRUE ,
    )

Variable Recoding

Add new columns to the data set:

pnad_design <-
    update(
    
        pnad_design ,
        
        pia = as.numeric( v2009 >= 14 )
    
    )

pnad_design <-
    update(
    
        pnad_design ,
        
        ocup_c = ifelse( pia == 1 , as.numeric( vd4002 %in% 1 ) , NA ) ,

        desocup30 = ifelse( pia == 1 , as.numeric( vd4002 %in% 2 ) , NA )
    )

pnad_design <- 

    update( 

        pnad_design , 

        uf_name =
        
            factor(
            
                as.numeric( uf ) ,
                
                levels = 
                    c(11L, 12L, 13L, 14L, 15L, 16L, 17L, 21L, 22L, 23L, 24L, 25L, 
                    26L, 27L, 28L, 29L, 31L, 32L, 33L, 35L, 41L, 42L, 43L, 50L, 51L, 
                    52L, 53L) ,
                    
                labels =
                    c("Rondonia", "Acre", "Amazonas", "Roraima", "Para", "Amapa", 
                    "Tocantins", "Maranhao", "Piaui", "Ceara", "Rio Grande do Norte", 
                    "Paraiba", "Pernambuco", "Alagoas", "Sergipe", "Bahia", "Minas Gerais", 
                    "Espirito Santo", "Rio de Janeiro", "Sao Paulo", "Parana", "Santa Catarina", 
                    "Rio Grande do Sul", "Mato Grosso do Sul", "Mato Grosso", "Goias", 
                    "Distrito Federal")
                    
            ) ,

        age_categories = factor( 1 + findInterval( v2009 , seq( 5 , 60 , 5 ) ) ) ,

        male = as.numeric( v2007 == 1 ) ,

        region = substr( uf , 1 , 1 ) ,

        # calculate usual income from main job
        # (rendimento habitual do trabalho principal)
        vd4016n = ifelse( pia %in% 1 & vd4015 %in% 1 , vd4016 , NA ) ,

        # calculate effective income from main job
        # (rendimento efetivo do trabalho principal) 
        vd4017n = ifelse( pia %in% 1 & vd4015 %in% 1 , vd4017 , NA ) ,

        # calculate usual income from all jobs
        # (variavel rendimento habitual de todos os trabalhos)
        vd4019n = ifelse( pia %in% 1 & vd4015 %in% 1 , vd4019 , NA ) ,

        # calculate effective income from all jobs
        # (rendimento efetivo do todos os trabalhos) 
        vd4020n = ifelse( pia %in% 1 & vd4015 %in% 1 , vd4020 , NA ) ,

        # determine the potential labor force
        pea_c = as.numeric( ocup_c == 1 | desocup30 == 1 )
        
    )

Analysis Examples with the survey library  

Unweighted Counts

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

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

svyby( ~ one , ~ uf_name , pnad_design , unwtd.count )

Weighted Counts

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

svytotal( ~ one , pnad_design )

svyby( ~ one , ~ uf_name , pnad_design , svytotal )

Descriptive Statistics

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

svymean( ~ vd4020n , pnad_design , na.rm = TRUE )

svyby( ~ vd4020n , ~ uf_name , pnad_design , svymean , na.rm = TRUE )

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

svymean( ~ age_categories , pnad_design )

svyby( ~ age_categories , ~ uf_name , pnad_design , svymean )

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

svytotal( ~ vd4020n , pnad_design , na.rm = TRUE )

svyby( ~ vd4020n , ~ uf_name , pnad_design , svytotal , na.rm = TRUE )

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

svytotal( ~ age_categories , pnad_design )

svyby( ~ age_categories , ~ uf_name , pnad_design , svytotal )

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

svyquantile( ~ vd4020n , pnad_design , 0.5 , na.rm = TRUE )

svyby( 
    ~ vd4020n , 
    ~ uf_name , 
    pnad_design , 
    svyquantile , 
    0.5 ,
    ci = TRUE , na.rm = TRUE
)

Estimate a ratio:

svyratio( 
    numerator = ~ ocup_c , 
    denominator = ~ pea_c , 
    pnad_design ,
    na.rm = TRUE
)

Subsetting

Restrict the survey design to employed persons:

sub_pnad_design <- subset( pnad_design , ocup_c == 1 )

Calculate the mean (average) of this subset:

svymean( ~ vd4020n , sub_pnad_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( ~ vd4020n , pnad_design , na.rm = TRUE )

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

grouped_result <-
    svyby( 
        ~ vd4020n , 
        ~ uf_name , 
        pnad_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( pnad_design )

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

svyvar( ~ vd4020n , pnad_design , na.rm = TRUE )

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

# SRS without replacement
svymean( ~ vd4020n , pnad_design , na.rm = TRUE , deff = TRUE )

# SRS with replacement
svymean( ~ vd4020n , pnad_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( ~ male , pnad_design ,
    method = "likelihood" )

Regression Models and Tests of Association

Perform a design-based t-test:

svyttest( vd4020n ~ male , pnad_design )

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

svychisq( 
    ~ male + age_categories , 
    pnad_design 
)

Perform a survey-weighted generalized linear model:

glm_result <- 
    svyglm( 
        vd4020n ~ male + age_categories , 
        pnad_design 
    )

summary( glm_result )

Replication Example

This example matches statistics and coefficients of variation from Tabela 4092 - Pessoas de 14 anos ou mais de idade, por condição em relação à força de trabalho e condição de ocupação:

nationwide_adult_population <- svytotal( ~ pia , pnad_design , na.rm = TRUE )
    
stopifnot( round( coef( nationwide_adult_population ) / 1000000 , 3 ) == 174.228 )
stopifnot( round( cv( nationwide_adult_population ) / 1000000 , 3 ) == 0 )
    
nationwide_labor_force <- svytotal( ~ pea_c , pnad_design , na.rm = TRUE )

stopifnot( round( coef( nationwide_labor_force ) / 1000000 , 3 ) == 107.257 )
stopifnot( round( cv( nationwide_labor_force ) * 100 , 1 ) == 0.2 )
    
nationwide_employed <- svytotal( ~ ocup_c , pnad_design , na.rm = TRUE )

stopifnot( round( coef( nationwide_employed ) / 1000000 , 3 ) == 97.825 )
stopifnot( round( cv( nationwide_employed ) * 100 , 1 ) == 0.2 )
    
nationwide_unemployed <- svytotal( ~ desocup30 , pnad_design , na.rm = TRUE )

stopifnot( round( coef( nationwide_unemployed ) / 1000000 , 3 ) == 9.432 )
stopifnot( round( cv( nationwide_unemployed ) * 100 , 1 ) == 1.2 )
    
nationwide_not_in_labor_force <-
    svytotal( ~ as.numeric( pia & !pea_c ) , pnad_design , na.rm = TRUE )

stopifnot( round( coef( nationwide_not_in_labor_force ) / 1000000 , 3 ) == 66.972 )
stopifnot( round( cv( nationwide_not_in_labor_force ) * 100 , 1 ) == 0.3 )

Poverty and Inequality Estimation with convey  

The R convey library estimates measures of income concentration, poverty, inequality, and wellbeing. This textbook details the available features. As a starting point for PNAD users, this code calculates the gini coefficient on complex sample survey data:

library(convey)
pnad_design <- convey_prep( pnad_design )

svygini( ~ vd4020n , pnad_design , na.rm = TRUE )

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

library(srvyr)
pnad_srvyr_design <- as_survey( pnad_design )

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

pnad_srvyr_design %>%
    summarize( mean = survey_mean( vd4020n , na.rm = TRUE ) )

pnad_srvyr_design %>%
    group_by( uf_name ) %>%
    summarize( mean = survey_mean( vd4020n , na.rm = TRUE ) )