-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathssa.Rmd
380 lines (271 loc) · 11.9 KB
/
ssa.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
# Social Security Public-Use Data Files (SSA) {-}
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) <a href="https://github.com/asdfree/ssa/actions"><img src="https://github.com/asdfree/ssa/actions/workflows/r.yml/badge.svg" alt="Github Actions Badge"></a>
Microdata from administrative sources like the Master Beneficiary Record, Supplemental Security Record.
* Tables contain either one record per person or one record per person per year.
* A systematic sample of either social security number holders (most americans) or program recipients (current beneficiaries). Multiply 1% samples by 100 to get weighted statistics, 5% samples by 20.
* [No expected release timeline](https://www.ssa.gov/policy/pub_schedule.html).
* Released by the [Office of Research, Evaluation, and Statistics](https://www.ssa.gov/policy/index.html), US [Social Security Administration](http://www.ssa.gov/).
---
## Recommended Reading {-}
Two Methodology Documents:
> [The 2006 Earnings Public-Use Microdata File: An Introduction](https://www.ssa.gov/policy/docs/ssb/v71n4/v71n4p33.html)
> [Comparing Earnings Estimates from the 2006 Public-Use File and the Annual Statistical Supplement](https://www.ssa.gov/policy/docs/rsnotes/rsn2012-01.html)
<br>
One Haiku:
```{r}
# annual earnings.
# for pensioner payouts, see
# the '04 extract
```
---
## Download, Import, Preparation {-}
Download and import the 1951-2006 one percent files with one record per person and per person-year:
```{r eval = FALSE , results = "hide" }
library(haven)
library(httr)
tf <- tempfile()
ssa_url <- "https://www.ssa.gov/policy/docs/microdata/epuf/epuf2006_sas_files.zip"
GET( ssa_url , write_disk( tf ) , progress() )
ssa_files <- unzip( tf , exdir = tempdir() )
ssa_fn <- grep( 'demographic' , ssa_files , value = TRUE )
annual_fn <- grep( 'annual' , ssa_files , value = TRUE )
ssa_tbl <- read_sas( ssa_fn )
annual_tbl <- read_sas( annual_fn )
ssa_df <- data.frame( ssa_tbl )
annual_df <- data.frame( annual_tbl )
names( ssa_df ) <- tolower( names( ssa_df ) )
names( annual_df ) <- tolower( names( annual_df ) )
```
Sum up 1951-1952 and 1953-2006 earnings, and also 1953-2006 credits, copying the naming convention:
```{r eval = FALSE , results = "hide" }
summed_earnings_5152 <-
with(
subset( annual_df , year_earn %in% 1951:1952 ) ,
aggregate( annual_earnings , list( id ) , sum )
)
names( summed_earnings_5152 ) <- c( 'id' , 'tot_cov_earn5152' )
summed_earnings_5306 <-
with(
subset( annual_df , year_earn > 1952 ) ,
aggregate( annual_earnings , list( id ) , sum )
)
names( summed_earnings_5306 ) <- c( 'id' , 'tot_cov_earn5306' )
summed_quarters_5306 <-
with(
subset( annual_df , year_earn > 1952 ) ,
aggregate( annual_qtrs , list( id ) , sum )
)
names( summed_quarters_5306 ) <- c( 'id' , 'qc5306' )
```
Isolate a single year of earnings:
```{r eval = FALSE , results = "hide" }
earnings_2006 <- annual_df[ annual_df[ , 'year_earn' ] == 2006 , c( 'id' , 'annual_earnings' ) ]
names( earnings_2006 ) <- c( 'id' , 'tot_cov_earn06' )
```
Merge each new column on to the person-level table, then add zeroes to person-years without earnings:
```{r eval = FALSE , results = "hide" }
stopifnot( all( !is.na( ssa_df ) ) )
before_nrow <- nrow( ssa_df )
ssa_df <- merge( ssa_df , summed_earnings_5152 , all.x = TRUE )
ssa_df <- merge( ssa_df , summed_earnings_5306 , all.x = TRUE )
ssa_df <- merge( ssa_df , summed_quarters_5306 , all.x = TRUE )
ssa_df <- merge( ssa_df , earnings_2006 , all.x = TRUE )
ssa_df[ is.na( ssa_df ) ] <- 0
stopifnot( nrow( ssa_df ) == before_nrow )
```
### Save Locally \ {-}
Save the object at any point:
```{r eval = FALSE , results = "hide" }
# ssa_fn <- file.path( path.expand( "~" ) , "SSA" , "this_file.rds" )
# saveRDS( ssa_df , file = ssa_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# ssa_df <- readRDS( ssa_fn )
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
ssa_df <-
transform(
ssa_df ,
decade_of_birth = floor( yob / 10 ) * 10 ,
sex = factor( sex , levels = 1:2 , labels = c( 'male' , 'female' ) ) ,
tot_cov_earn3706 = ( tot_cov_earn3750 + tot_cov_earn5152 + tot_cov_earn5306 ) ,
qc3706 = ( qc3750 + qc5152 + qc5306 ) ,
any_earnings_2006 = ( tot_cov_earn06 > 0 ) ,
earnings_periods =
factor(
ifelse( ( tot_cov_earn5152 + tot_cov_earn5306 > 0 ) & tot_cov_earn3750 > 0 , 1 ,
ifelse( tot_cov_earn5152 > 0 | tot_cov_earn5306 > 0 , 2 ,
ifelse( tot_cov_earn3750 > 0 , 3 , 4 ) ) ) ,
levels = 1:4 ,
labels =
c( 'Earnings in both periods' , 'Earnings during 1951-2006 only' ,
'Earnings during 1937-1950 only' , 'No earnings' ) )
)
```
---
## Analysis Examples with base R \ {-}
### Unweighted Counts {-}
Count the unweighted number of records in the table, overall and by groups:
```{r eval = FALSE , results = "hide" }
nrow( ssa_df )
table( ssa_df[ , "sex" ] , useNA = "always" )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
mean( ssa_df[ , "tot_cov_earn3706" ] )
tapply(
ssa_df[ , "tot_cov_earn3706" ] ,
ssa_df[ , "sex" ] ,
mean
)
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
prop.table( table( ssa_df[ , "decade_of_birth" ] ) )
prop.table(
table( ssa_df[ , c( "decade_of_birth" , "sex" ) ] ) ,
margin = 2
)
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
sum( ssa_df[ , "tot_cov_earn3706" ] )
tapply(
ssa_df[ , "tot_cov_earn3706" ] ,
ssa_df[ , "sex" ] ,
sum
)
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
quantile( ssa_df[ , "tot_cov_earn3706" ] , 0.5 )
tapply(
ssa_df[ , "tot_cov_earn3706" ] ,
ssa_df[ , "sex" ] ,
quantile ,
0.5
)
```
### Subsetting {-}
Limit your `data.frame` to individuals with at least forty lifetime credits:
```{r eval = FALSE , results = "hide" }
sub_ssa_df <- subset( ssa_df , qc3706 >= 40 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
mean( sub_ssa_df[ , "tot_cov_earn3706" ] )
```
### Measures of Uncertainty {-}
Calculate the variance, overall and by groups:
```{r eval = FALSE , results = "hide" }
var( ssa_df[ , "tot_cov_earn3706" ] )
tapply(
ssa_df[ , "tot_cov_earn3706" ] ,
ssa_df[ , "sex" ] ,
var
)
```
### Regression Models and Tests of Association {-}
Perform a t-test:
```{r eval = FALSE , results = "hide" }
t.test( tot_cov_earn3706 ~ any_earnings_2006 , ssa_df )
```
Perform a chi-squared test of association:
```{r eval = FALSE , results = "hide" }
this_table <- table( ssa_df[ , c( "any_earnings_2006" , "decade_of_birth" ) ] )
chisq.test( this_table )
```
Perform a generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
glm(
tot_cov_earn3706 ~ any_earnings_2006 + decade_of_birth ,
data = ssa_df
)
summary( glm_result )
```
---
## Replication Example {-}
This example matches statistics in [The 2006 Earnings Public-Use Microdata File: An Introduction](https://www.ssa.gov/policy/docs/ssb/v71n4/v71n4p33.html):
Chart 5. Percentage distribution of individuals in EPUF, by capped Social Security taxable earnings status:
```{r eval = FALSE , results = "hide" }
chart_five_results <- prop.table( table( ssa_df[ , 'earnings_periods' ] ) )
chart_five_results <- round( 100 * chart_five_results )
stopifnot( chart_five_results[ 'Earnings in both periods' ] == 16 )
stopifnot( chart_five_results[ 'Earnings during 1951-2006 only' ] == 55 )
stopifnot( chart_five_results[ 'Earnings during 1937-1950 only' ] == 4 )
stopifnot( chart_five_results[ 'No earnings' ] == 25 )
```
Table 4. Average and median Social Security taxable earnings in EPUF, by sex, 1951–2006 (in dollars):
```{r eval = FALSE , results = "hide" }
nonzero_2006_earners <- ssa_df[ ssa_df[ , 'tot_cov_earn06' ] > 0 , 'tot_cov_earn06' ]
stopifnot( round( mean( nonzero_2006_earners ) , 0 ) == 30953 )
stopifnot( round( quantile( nonzero_2006_earners )[ 3 ] , 0 ) == 24000 )
```
Table A1. Number and percentage distribution of individuals with Social Security taxable earnings records in EPUF, by sex, 1951–2006:
```{r eval = FALSE , results = "hide" }
nonzero_2006_earners <- ssa_df[ ssa_df[ , 'tot_cov_earn06' ] > 0 , ]
stopifnot( round( mean( nonzero_2006_earners[ , 'tot_cov_earn06' ] ) , 0 ) == 30953 )
stopifnot( round( quantile( nonzero_2006_earners[ , 'tot_cov_earn06' ] )[ 3 ] , 0 ) == 24000 )
```
This example matches statistics in [Comparing Earnings Estimates from the 2006 Earnings Public-Use File and the Annual Statistical Supplement](https://www.ssa.gov/policy/docs/rsnotes/rsn2012-01.html):
Table 4. Comparing Supplement and EPUF estimates: Number of all, male, and female workers with any earnings during the year, 1951–2006:
```{r eval = FALSE , results = "hide" }
stopifnot( round( nrow( nonzero_2006_earners ) * 100 , -3 ) == 156280000 )
earners_in_2006_by_sex <- table( nonzero_2006_earners[ , 'sex' ] ) * 100
stopifnot( round( earners_in_2006_by_sex[ 'male' ] , -3 ) == 81576000 )
stopifnot( round( earners_in_2006_by_sex[ 'female' ] , -3 ) == 74681000 )
```
---
## Analysis Examples with `dplyr` \ {-}
The R `dplyr` library offers an alternative grammar of data manipulation to base R and SQL syntax. [dplyr](https://github.com/tidyverse/dplyr/) offers many verbs, such as `summarize`, `group_by`, and `mutate`, the convenience of pipe-able functions, and the `tidyverse` style of non-standard evaluation. [This vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html) details the available features. As a starting point for SSA users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(dplyr)
ssa_tbl <- as_tibble( ssa_df )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
ssa_tbl %>%
summarize( mean = mean( tot_cov_earn3706 ) )
ssa_tbl %>%
group_by( sex ) %>%
summarize( mean = mean( tot_cov_earn3706 ) )
```
---
## Analysis Examples with `data.table` \ {-}
The R `data.table` library provides a high-performance version of base R's data.frame with syntax and feature enhancements for ease of use, convenience and programming speed. [data.table](https://r-datatable.com) offers concise syntax: fast to type, fast to read, fast speed, memory efficiency, a careful API lifecycle management, an active community, and a rich set of features. [This vignette](https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html) details the available features. As a starting point for SSA users, this code replicates previously-presented examples:
```{r eval = FALSE , results = 'hide' }
library(data.table)
ssa_dt <- data.table( ssa_df )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = 'hide' }
ssa_dt[ , mean( tot_cov_earn3706 ) ]
ssa_dt[ , mean( tot_cov_earn3706 ) , by = sex ]
```
---
## Analysis Examples with `duckdb` \ {-}
The R `duckdb` library provides an embedded analytical data management system with support for the Structured Query Language (SQL). [duckdb](https://duckdb.org) offers a simple, feature-rich, fast, and free SQL OLAP management system. [This vignette](https://duckdb.org/docs/api/r) details the available features. As a starting point for SSA users, this code replicates previously-presented examples:
```{r eval = FALSE , results = 'hide' }
library(duckdb)
con <- dbConnect( duckdb::duckdb() , dbdir = 'my-db.duckdb' )
dbWriteTable( con , 'ssa' , ssa_df )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = 'hide' }
dbGetQuery( con , 'SELECT AVG( tot_cov_earn3706 ) FROM ssa' )
dbGetQuery(
con ,
'SELECT
sex ,
AVG( tot_cov_earn3706 )
FROM
ssa
GROUP BY
sex'
)
```