-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.Rmd
1722 lines (1311 loc) · 53.7 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Poverty and Inequality with Complex Survey Data"
author: "By Guilherme Jacob, Anthony Damico, and Djalma Pessoa. The authors received no external funding for the `convey` software and this accompanying textbook."
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::tufte_html_book:
toc: yes
css: toc.css
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apa
link-citations: yes
github-repo: guilhermejacob/context
description: "A book about the R convey package"
delete_merged_file: yes
---
```{r, include=FALSE}
knitr::opts_chunk$set(
cache=TRUE, cache.lazy=FALSE
)
```
```{r results='hide', echo=FALSE}
set.seed(2017)
```
# Intro
The R `convey` library estimates measures of poverty, inequality and richness/affluence. There are two other R libraries covering this subject, [vardpoor](https://CRAN.R-project.org/package=vardpoor) [@R-vardpoor] and [laeken](https://CRAN.R-project.org/package=laeken) [@R-laeken], however, only `convey` integrates seamlessly with the [R survey package](https://CRAN.R-project.org/package=survey) [@R-survey-article;@R-survey-book;@R-survey].
`convey` is free and open-source software that runs inside the [R environment for statistical computing](https://www.r-project.org/). Anyone can review and propose changes to [the source code](https://github.com/ajdamico/convey) for this software. Readers are welcome to [propose changes to this book](https://github.com/guilhermejacob/context/) as well.
As a companion guide, this flowchart clarifies the various options in this software:
[![](https://mermaid.ink/img/pako:eNrNWWlv20gS_SsNYQa2AUlraZL4wCQDyc41k8xmEy-yyXqxaJEtqcdkN4fdlCwn-e_7qg-SohXnmC8LA7ZE9vGq6tWr6vaHXqJT0TvtXapFyYsluzi_VPixk39f9s412-iKLflKMM4SnReZuGaG019mqnIlNiwVRi4U07M_RGJZUgpuRcrW0i6ZwC9RMrPa-EH7B0zT11IU9YNfLnv_CRuyweAR-_i7_sim2Ps1d5PtkivsrLBVn_4ambqngmVcXAlFK654mRYaH16zTM5KXkphht113wnzkZ01Rq25ssxqxhXPNjeCFXolSotNpBJ_VjyT9BlrljJZKmHM3_h8nlVCJaKG7H_smdvgHEu_8mvUW5_7N4_x6jlb6ypLAfBK0LbCWJnD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IG6NroTaG4O0BknsTqK64JOzGFrRI97bcvp6sPNa6qve7b2xvkTIgZyIMmhrWZJLQUd8txGb7S_c8ndSdldMjT5Rn0eFVAsv2fiijhmEgZYSH0kmp1WbaFrEshx5W5AdjYkdJX3Ol4rTuK1YmCKv617Qi5_E0p2p-xgzCQMolGvG5--0uhC5Q1vXb7QIV0laMX66DOat4Mmb-MFZ70NTWwuHcIhHY69dV1RK5FfJBowqdEF3tDLmGd2U4j_Rl48ZHtnS76HhAuNNb5dlXzF3X1fB5cziE1CB87o4iEX6JXYwjWolBdzxJbEyoY7J7cJDqN0gNfUgpkarFu5uWR0zcTwtgXTL1vw5OnFxahlQ_A326dLgQX7ma4SMWbg2rJvNupw2xrz7ea0-TLd5kvTFbfZsqJEbrL4i1SBdrkrQD4n4O5y8ivY0jh7GsFNvs7Z421nA2709aMv-7rxSdh1jE2f-sYcQnShdWZqZOM4ZnLr7pyE0N-eu2YMzlxJU7l7LgdsXvIkno5vn5nDAXTfCzfaigOkGPy0cK1pDbw-1wQ97_V7OY6cXKa9094HkunLHham68tTfEzFnFeZvexdqk8Yyiur32xU0ju1ZSX6vapIEfhzyXEOyeNDHMDQ2r_0_wNy_wrq9wqu3mtdD8HX3umH3nXvdHz0YDh-cHLv8GR0dPLg-Oh-v7fpnQ7Gh8fDwwejeyfjk9HJyXh0fPyp37txKxwN7z_46f7R8fHo3v2f7t07vv_pf82INM0)
Individuals getting started in the field of poverty and inequality statistics might find the number of techniques described in this textbook overwhelming, especially choosing which method might be most appropriate for each particular research question. The authors of this textbook consider Dr. Ija Trapeznikova's article [Measuring income inequality](https://wol.iza.org/articles/measuring-income-inequality/long) an important summary of how to approach selecting between available techniques.
## Installation {#install}
In order to work with the `convey` library, you will need to have R running on your machine. If you have never used R before, you will need to [install that software](https://www.r-project.org/) before `convey` can be accessed. Once you have R loaded on your machine, you can install..
* the latest released version from [CRAN](https://CRAN.R-project.org/package=convey) with
```R
install.packages("convey")
```
* the latest development version from github with
```R
remotes::install_github("ajdamico/convey")
```
In order to know how to cite this package, run `citation("convey")`.
## Complex surveys and statistical inference {#survey}
In this book, we demonstrate how to estimate poverty and inequality measures in a population using microdata collected from a complex survey sample. Most surveys administered by government agencies or larger research organizations utilize a sampling design that violates the assumption of simple random sampling (SRS), including:
1. Different units selection probabilities;
2. Clustering of units;
3. Stratification of clusters;
4. Reweighting to compensate for missing values and other adjustments.
Therefore, basic unweighted R commands such as `mean()` or `glm()` will not properly account for the weighting nor the measures of uncertainty (such as sampling variance estimates and confidence intervals) present in the dataset. For some examples of publicly-available complex survey data sets, see [http://asdfree.com]().
Unlike other software, the R `convey` package does not require that the user specify these parameters throughout the analysis. So long as the [svydesign object](http://r-survey.r-forge.r-project.org/survey/html/svydesign.html) or [svrepdesign object](http://r-survey.r-forge.r-project.org/survey/html/svrepdesign.html) has been constructed properly at the outset of the analysis, the `convey` package will incorporate the survey design automatically and produce statistics and variances that take the complex sample into account.
Survey analysts familiar with the R `dplyr` syntax implemented by the `survey` library's wrapper `srvyr` package might be interested in implementing specific `convey` functions by following the [`svygini()` example](http://gdfe.co/srvyr/articles/extending-srvyr.html) published by `srvyr` author Greg Freedman Ellis. Note that the full design stored by `convey_prep()` may in some cases complicate this extension.
## Usage Examples
In the following example, we've loaded the data set `eusilc` from the R library [laeken](https://CRAN.R-project.org/package=laeken) [@R-laeken].
```{r results='hide', message=FALSE, warning=FALSE}
library(laeken)
data(eusilc)
```
Next, we create an object of class `survey.design` using the function `svydesign` of the `survey` library:
```{r results='hide', message=FALSE, warning=FALSE}
library(survey)
des_eusilc <-
svydesign(
ids = ~ rb030,
strata = ~ db040,
weights = ~ rb050,
data = eusilc
)
```
Right after the creation of the design object `des_eusilc`, we should use the function `convey_prep` that adds an attribute to the survey design which saves information on the design object based upon the whole sample, needed to work with subsetted design objects.
```{r}
library(convey)
des_eusilc <- convey_prep(des_eusilc)
```
To estimate the at-risk-of-poverty rate, we use the function `svyarpt`:
```{r comment=NA}
svyarpr( ~ eqIncome, design = des_eusilc)
```
To estimate the at-risk-of-poverty rate across domains defined by the variable `db040` we use:
```{r comment=NA}
svyby(
~ eqIncome,
by = ~ db040,
design = des_eusilc,
FUN = svyarpr,
deff = FALSE
)
```
Using the same data set, we estimate the quintile share ratio:
```{r comment=NA}
# for the whole population
svyqsr( ~ eqIncome, design = des_eusilc, alpha1 = .20)
# for domains
svyby(
~ eqIncome,
by = ~ db040,
design = des_eusilc,
FUN = svyqsr,
alpha1 = .20,
deff = FALSE
)
```
These functions can be used as S3 methods for the classes `survey.design` and `svyrep.design`.
Let's create a design object of class `svyrep.design` and run the function `convey_prep` on it:
```{r}
des_eusilc_rep <- as.svrepdesign(des_eusilc, type = "bootstrap")
des_eusilc_rep <- convey_prep(des_eusilc_rep)
```
The function `svyarpr` produces matching coefficients and near-identical standard errors on the replication design:
```{r comment=NA}
svyarpr( ~ eqIncome, design = des_eusilc_rep)
svyby(
~ eqIncome,
by = ~ db040,
design = des_eusilc_rep,
FUN = svyarpr,
deff = FALSE
)
```
The functions of the convey `library` are called in a similar way to the functions in `survey` library.
It is also possible to discard missing values by using the argument `na.rm`:
```{r comment=NA}
# survey.design using a variable with missings
svygini( ~ py010n , design = des_eusilc)
svygini( ~ py010n , design = des_eusilc , na.rm = TRUE)
# svyrep.design using a variable with missings
svygini( ~ py010n , design = des_eusilc_rep)
svygini( ~ py010n , design = des_eusilc_rep , na.rm = TRUE)
```
## 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:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
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))]]
```
```{r results='hide', echo=FALSE}
rm( four_tbl , four_df ) ; gc()
```
Divide weights:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
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))
```
```{r results='hide', echo=FALSE}
rm( household_df , family_df , person_df , hh_fm_df , hh_fm_pr_df , repwgts_df ) ; gc()
# variables to keep
cps_df <- cps_df[ c( 'a_age' , 'a_sex' , 'moop' , 'a_maritl' , 'a_famrel' , 'gestfips' , 'ftotval' , 'htotval' , 'pearnval' , 'a_exprrp' , 'a_famtyp' , 'wewkrs' , 'marsupwt' , paste0( 'pwwgt' , 1:160 ) ) ] ; gc()
```
Construct a complex sample survey design:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
cps_design <- convey_prep(cps_design)
```
```{r results='hide', echo=FALSE}
rm( cps_df ) ; gc()
```
### Analysis Examples with the `survey` library \ {-}
Add new columns to the data set:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
svytotal( ~ one , cps_design )
svyby( ~ one , ~ state_name , cps_design , svytotal )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svymean( ~ pearnval , cps_design )
svyby( ~ pearnval , ~ state_name , cps_design , svymean )
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svymean( ~ a_maritl , cps_design )
svyby( ~ a_maritl , ~ state_name , cps_design , svymean )
```
Calculate the sum of a linear variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svytotal( ~ pearnval , cps_design )
svyby( ~ pearnval , ~ state_name , cps_design , svytotal )
```
Calculate the weighted sum of a categorical variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
svyquantile( ~ pearnval , cps_design , 0.5 )
svyby(
~ pearnval ,
~ state_name ,
cps_design ,
svyquantile ,
0.5 ,
ci = TRUE
)
```
Estimate a ratio:
```{r results='hide', message=FALSE, warning=FALSE}
svyratio(
numerator = ~ moop ,
denominator = ~ pearnval ,
cps_design
)
```
Restrict the survey design to persons aged 18-64:
```{r results='hide', message=FALSE, warning=FALSE}
sub_cps_design <- subset( cps_design , a_age %in% 18:64 )
```
Calculate the mean (average) of this subset:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
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:
```{r results='hide', message=FALSE, warning=FALSE}
degf( cps_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r results='hide', message=FALSE, warning=FALSE}
svyvar( ~ pearnval , cps_design )
```
Include the complex sample design effect in the result for a specific statistic:
```{r results='hide', message=FALSE, warning=FALSE}
# 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:
```{r results='hide', message=FALSE, warning=FALSE}
svyciprop( ~ male , cps_design ,
method = "likelihood" )
```
Perform a design-based t-test:
```{r results='hide', message=FALSE, warning=FALSE}
svyttest( pearnval ~ male , cps_design )
```
Perform a chi-squared test of association for survey data:
```{r results='hide', message=FALSE, warning=FALSE}
svychisq(
~ male + a_maritl ,
cps_design
)
```
Perform a survey-weighted generalized linear model:
```{r results='hide', message=FALSE, warning=FALSE}
glm_result <-
svyglm(
pearnval ~ male + a_maritl ,
cps_design
)
summary( glm_result )
```
### Household Income
Limit the CPS-ASEC person-level design to the household reference person, then calculate the Gini coefficient with total household income:
```{r}
cps_household_design <- subset(cps_design , a_exprrp %in% 1:2)
(cps_household_gini <- svygini( ~ htotval , cps_household_design))
# 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:
```{r}
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))
(
cps_household_atkinson <-
svyatk(
~ htotval_ones ,
cps_household_design ,
epsilon = 0.5 ,
na.rm = TRUE
)
)
# 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)
```
### 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:
```{r}
cps_family_design <-
subset(cps_design , a_famrel %in% 1 & a_famtyp %in% 1)
(cps_family_gini <- svygini( ~ ftotval , cps_family_design))
# 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)
```
### 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:
```{r}
cps_ftfy_worker_design <- subset(cps_design , wewkrs %in% 1)
```
Re-set the population of reference for poverty threshold estimation:
```{r}
cps_ftfy_worker_design <- convey_prep(cps_ftfy_worker_design)
```
Calculate the Gini coefficient with total earnings:
```{r}
svygini( ~ pearnval , cps_ftfy_worker_design)
```
```{r results='hide', echo=FALSE}
rm( cps_design ) ; gc()
```
## Pesquisa Nacional por Amostra de Domicílios Contínua (PNAD Contínua)
Administered by the Instituto Brasileiro de Geografia e Estatística (IBGE), the PNAD Contínua is the primary source of labor force statistics for the population of Brazil.
This section downloads, imports, and prepares the most current microdata for analysis, then reproduces some statistics and margins of error from IBGE.^[See [this link](https://agenciadenoticias.ibge.gov.br/agencia-noticias/2012-agencia-de-noticias/noticias/36857-em-2022-mercado-de-trabalho-e-auxilio-brasil-permitem-recuperacao-dos-rendimentos) for IBGE Gini coefficient estimates reproduced below.]
Download and import the 2022 5th interview file:
```{r results='hide', message=FALSE, warning=FALSE}
library(PNADcIBGE)
pnadc_df <-
get_pnadc(2022 ,
interview = 5 ,
design = FALSE ,
labels = FALSE)
names(pnadc_df) <- tolower(names(pnadc_df))
```
Recode a number of variables:
```{r results='hide', message=FALSE, warning=FALSE}
pnadc_df <-
transform(
pnadc_df ,
household_id = paste0(upa , v1008 , v1014) ,
deflated_labor_income = vd4019 * co2 ,
deflated_other_source_income = vd4048 * co2e
)
labor_income_sum_df <-
aggregate(
cbind(household_deflated_labor_income = deflated_labor_income) ~ household_id ,
data = pnadc_df[!(pnadc_df[, 'vd2002'] %in% 17:19) ,] ,
sum ,
na.rm = TRUE
)
other_income_sum_df <-
aggregate(
cbind(household_deflated_other_source_income = deflated_other_source_income) ~ household_id ,
data = pnadc_df[!(pnadc_df[, 'vd2002'] %in% 17:19) ,] ,
sum ,
na.rm = TRUE
)
before_nrow <- nrow(pnadc_df)
pnadc_df <- merge(pnadc_df , labor_income_sum_df , all.x = TRUE)
pnadc_df <- merge(pnadc_df , other_income_sum_df , all.x = TRUE)
stopifnot(nrow(pnadc_df) == before_nrow)
pnadc_df[is.na(pnadc_df[, 'household_deflated_labor_income']) , 'household_deflated_labor_income'] <-
0
pnadc_df[is.na(pnadc_df[, 'household_deflated_other_source_income']) , 'household_deflated_other_source_income'] <-
0
pnadc_df <-
transform(
pnadc_df ,
deflated_per_capita_income =
(
household_deflated_labor_income + household_deflated_other_source_income
) / vd2003 ,
sex = factor(
v2007 ,
levels = 1:2 ,
labels = c('male' , 'female')
)
)
```
```{r results='hide', echo=FALSE}
# variables to keep
pnadc_df <- pnadc_df[ c( 'vd4002' , 'uf' , 'v2009' , 'v2007' , 'vd4015' , 'vd4016' , 'vd4017' , 'vd4019' , 'vd4020' , 'deflated_per_capita_income' , 'deflated_labor_income' , 'sex' , 'v1032' , grep( 'v1032[0-9]{3}' , names( pnadc_df ) , value = TRUE ) ) ] ; gc()
```
Construct a complex sample survey design:
```{r results='hide', message=FALSE, warning=FALSE}
library(survey)
pnadc_design <-
svrepdesign(
data = pnadc_df ,
weight = ~ v1032,
type = "bootstrap" ,
repweights = "v1032[0-9]{3}" ,
mse = TRUE
)
```
Run the `convey_prep()` function on the full design:
```{r results='hide', message=FALSE, warning=FALSE}
pnadc_design <- convey_prep(pnadc_design)
```
### Analysis Examples with the `survey` library \ {-}
Add new columns to the data set:
```{r results='hide', message=FALSE, warning=FALSE}
pnadc_design <-
update(pnadc_design ,
pia = as.numeric(v2009 >= 14))
pnadc_design <-
update(
pnadc_design ,
ocup_c = ifelse(pia == 1 , as.numeric(vd4002 %in% 1) , NA) ,
desocup30 = ifelse(pia == 1 , as.numeric(vd4002 %in% 2) , NA)
)
pnadc_design <-
update(
pnadc_design ,
one = 1 ,
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)
)
```
Count the unweighted number of records in the survey sample, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
sum( weights( pnadc_design , "sampling" ) != 0 )
svyby( ~ one , ~ uf_name , pnadc_design , unwtd.count )
```
Count the weighted size of the generalizable population, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svytotal( ~ one , pnadc_design )
svyby( ~ one , ~ uf_name , pnadc_design , svytotal )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svymean( ~ vd4020n , pnadc_design , na.rm = TRUE )
svyby( ~ vd4020n , ~ uf_name , pnadc_design , svymean , na.rm = TRUE )
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svymean( ~ age_categories , pnadc_design )
svyby( ~ age_categories , ~ uf_name , pnadc_design , svymean )
```
Calculate the sum of a linear variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svytotal( ~ vd4020n , pnadc_design , na.rm = TRUE )
svyby( ~ vd4020n , ~ uf_name , pnadc_design , svytotal , na.rm = TRUE )
```
Calculate the weighted sum of a categorical variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svytotal( ~ age_categories , pnadc_design )
svyby( ~ age_categories , ~ uf_name , pnadc_design , svytotal )
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r results='hide', message=FALSE, warning=FALSE}
svyquantile( ~ vd4020n , pnadc_design , 0.5 , na.rm = TRUE )
svyby(
~ vd4020n ,
~ uf_name ,
pnadc_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE
)
```
Estimate a ratio:
```{r results='hide', message=FALSE, warning=FALSE}
svyratio(
numerator = ~ ocup_c ,
denominator = ~ pea_c ,
pnadc_design ,
na.rm = TRUE
)
```
Restrict the survey design to employed persons:
```{r results='hide', message=FALSE, warning=FALSE}
sub_pnadc_design <- subset( pnadc_design , ocup_c == 1 )
```
Calculate the mean (average) of this subset:
```{r results='hide', message=FALSE, warning=FALSE}
svymean( ~ vd4020n , sub_pnadc_design , na.rm = TRUE )