This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from ajdamico/asdfree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
56-sbo.Rmd
267 lines (203 loc) · 7.51 KB
/
56-sbo.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
# Survey of Business Owners (SBO) {-}
[![Build Status](https://travis-ci.org/asdfree/sbo.svg?branch=master)](https://travis-ci.org/asdfree/sbo) [![Build status](https://ci.appveyor.com/api/projects/status/github/asdfree/sbo?svg=TRUE)](https://ci.appveyor.com/project/ajdamico/sbo)
The Survey of Business Owners tracks nearly every tax-filing sole proprietorship, partnership, and corporation in the nation.
* One table with one row per firm per state per industry.
* A complex sample survey designed to generalize to all firms in the United States, however the public use microdata only includes [classifiable (non-identifiable) firms](https://www2.census.gov/econ/sbo/07/pums/2007_sbo_pums_users_guide.pdf#page=17) which comprise nearly all businesses but only about half of workers.
* Released as part of the U.S. Census Bureau's Economic Census, every year ending in 2 or 7.
* Administered by the [U.S. Census Bureau](http://www.census.gov/).
## Simplified Download and Importation {-}
The R `lodown` package easily downloads and imports all available SBO microdata by simply specifying `"sbo"` 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.
```{r eval = FALSE }
library(lodown)
lodown( "sbo" , output_dir = file.path( path.expand( "~" ) , "SBO" ) )
```
## Analysis Examples with the `survey` library \ {-}
Construct a multiply-imputed, complex sample survey design:
```{r eval = FALSE }
```
```{r eval = FALSE }
gc()
options( survey.lonely.psu = "adjust" )
library(survey)
library(mitools)
sbo_design <-
readRDS( file.path( path.expand( "~" ) , "SBO" , "2007 main.rds" ) )
# keep only the variables you need
variables_to_keep <-
c(
"one" ,
"newwgt" ,
"tabwgt" ,
"receipts_noisy" ,
"employment_noisy" ,
"n07_employer" ,
"established" ,
"healthins" ,
"husbwife"
)
# keep only columns used in this analysis
sbo_design$coef$variables <-
sbo_design$coef$variables[ variables_to_keep ]
sbo_design$var <-
lapply(
sbo_design$var ,
function( w ){
w$variables <- w$variables[ variables_to_keep ]
w
}
)
gc()
# this step conserves RAM
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE }
sbo_design <-
sbo_update(
sbo_design ,
established_before_2000 =
ifelse( established %in% c( '0' , 'A' ) , NA , as.numeric( established < 4 ) ) ,
healthins =
factor( healthins , levels = 1:2 ,
labels = c( "offered health insurance" , "did not offer health insurance" )
)
)
gc()
```
### Unweighted Counts {-}
Count the unweighted number of records in the survey sample, overall and by groups:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design , svyby( ~ one , ~ one , unwtd.count ) ) )
sbo_MIcombine( sbo_with( sbo_design , svyby( ~ one , ~ healthins , unwtd.count ) ) )
```
### Weighted Counts {-}
Count the weighted size of the generalizable population, overall and by groups:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design , svytotal( ~ one ) ) )
sbo_MIcombine( sbo_with( sbo_design ,
svyby( ~ one , ~ healthins , svytotal )
) )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design , svymean( ~ receipts_noisy ) ) )
sbo_MIcombine( sbo_with( sbo_design ,
svyby( ~ receipts_noisy , ~ healthins , svymean )
) )
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design , svymean( ~ n07_employer , na.rm = TRUE ) ) )
sbo_MIcombine( sbo_with( sbo_design ,
svyby( ~ n07_employer , ~ healthins , svymean , na.rm = TRUE )
) )
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design , svytotal( ~ receipts_noisy ) ) )
sbo_MIcombine( sbo_with( sbo_design ,
svyby( ~ receipts_noisy , ~ healthins , svytotal )
) )
```
Calculate the weighted sum of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design , svytotal( ~ n07_employer , na.rm = TRUE ) ) )
sbo_MIcombine( sbo_with( sbo_design ,
svyby( ~ n07_employer , ~ healthins , svytotal , na.rm = TRUE )
) )
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design ,
svyquantile(
~ receipts_noisy ,
0.5 , se = TRUE
) ) )
sbo_MIcombine( sbo_with( sbo_design ,
svyby(
~ receipts_noisy , ~ healthins , svyquantile ,
0.5 , se = TRUE ,
keep.var = TRUE , ci = TRUE
) ) )
```
Estimate a ratio:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design ,
svyratio( numerator = ~ receipts_noisy , denominator = ~ employment_noisy )
) )
```
### Subsetting {-}
Restrict the survey design to jointly owned by husband and wife:
```{r eval = FALSE , results = "hide" }
sub_sbo_design <- sbo_subset( sbo_design , husbwife %in% 1:3 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sub_sbo_design , svymean( ~ receipts_noisy ) ) ) ; rm( sub_sbo_design ) ; gc()
```
### Measures of Uncertainty {-}
Extract the coefficient, standard error, confidence interval, and coefficient of variation from any descriptive statistics function result, overall and by groups:
```{r eval = FALSE , results = "hide" }
this_result <-
sbo_MIcombine( sbo_with( sbo_design ,
svymean( ~ receipts_noisy )
) )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
sbo_MIcombine( sbo_with( sbo_design ,
svyby( ~ receipts_noisy , ~ healthins , svymean )
) )
coef( grouped_result )
SE( grouped_result )
confint( grouped_result )
cv( grouped_result )
```
Calculate the degrees of freedom of any survey design object:
```{r eval = FALSE , results = "hide" }
sbo_degf( sbo_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
sbo_MIcombine( sbo_with( sbo_design , svyvar( ~ receipts_noisy ) ) )
```
Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# SRS without replacement
sbo_MIcombine( sbo_with( sbo_design ,
svymean( ~ receipts_noisy , deff = TRUE )
) )
# SRS with replacement
sbo_MIcombine( sbo_with( sbo_design ,
svymean( ~ receipts_noisy , deff = "replace" )
) )
```
Compute confidence intervals for proportions using methods that may be more accurate near 0 and 1. See `?svyciprop` for alternatives:
```{r eval = FALSE , results = "hide" }
sbo_MIsvyciprop( ~ established_before_2000 , sbo_design ,
method = "likelihood" , na.rm = TRUE ) ; gc()
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
# not implemented sbo_MIsvyttest( receipts_noisy ~ established_before_2000 , sbo_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
# not implemented sbo_MIsvychisq( ~ established_before_2000 + n07_employer , sbo_design )
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
sbo_MIcombine( sbo_with( sbo_design ,
svyglm( receipts_noisy ~ established_before_2000 + n07_employer )
) )
glm_result
```
---
## Replication Example {-}
```{r eval = FALSE , results = "hide" }
```