-
Notifications
You must be signed in to change notification settings - Fork 449
/
nssrn.Rmd
362 lines (271 loc) · 10.7 KB
/
nssrn.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
# National Sample Survey of Registered Nurses (NSSRN) {-}
[![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/nssrn/actions"><img src="https://github.com/asdfree/nssrn/actions/workflows/r.yml/badge.svg" alt="Github Actions Badge"></a>
The employment, education, and demographics of the RN, NP, and APRN workforce in the United States.
* One table with one row per surveyed registered nurse (RN) or nurse practitioner (NP).
* A complex sample designed to generalize to RNs and NPs at both state and national levels.
* Released every four to ten years since 1977.
* Administered by the [Health Services and Resources Administration](http://www.hrsa.gov/), in partnership with [Census](https://www.census.gov/).
---
Please skim before you begin:
1. [2022 NSSRN Methodology Report](https://data.hrsa.gov/DataDownload/NSSRN/GeneralPUF22/Documentation.zip)
2. [Frequently Asked Questions](https://www.census.gov/programs-surveys/nssrn/about/faq.html)
3. A haiku regarding this microdata:
```{r}
# florence nightingale
# founder of modern nursing
# a statistician
```
---
## Download, Import, Preparation {-}
Download and import the state file:
```{r eval = FALSE , results = "hide" }
library(haven)
nssrn_tf <- tempfile()
nssrn_url <-
"https://data.hrsa.gov/DataDownload/NSSRN/GeneralPUF22/2022_NSSRN_PUF_Stata_Package.zip"
download.file( nssrn_url , nssrn_tf , mode = 'wb' )
nssrn_files <- unzip( nssrn_tf , exdir = tempdir() )
nssrn_dta <- grep( "\\.dta$" , nssrn_files , ignore.case = TRUE , value = TRUE )
nssrn_tbl <- read_dta( nssrn_dta )
nssrn_df <- data.frame( nssrn_tbl )
names( nssrn_df ) <- tolower( names( nssrn_df ) )
nssrn_df[ , 'one' ] <- 1
```
### Save Locally \ {-}
Save the object at any point:
```{r eval = FALSE , results = "hide" }
# nssrn_fn <- file.path( path.expand( "~" ) , "NSSRN" , "this_file.rds" )
# saveRDS( nssrn_df , file = nssrn_fn , compress = FALSE )
```
Load the same object:
```{r eval = FALSE , results = "hide" }
# nssrn_df <- readRDS( nssrn_fn )
```
### Survey Design Definition {-}
Construct a complex sample survey design:
```{r eval = FALSE , results = "hide" }
library(survey)
nssrn_design <-
svrepdesign(
weight = ~rkrnwgta ,
repweights = 'rkrnwgta[0-9]+' ,
scale = 4 / 80 ,
rscales = rep( 1 , 80 ) ,
mse = TRUE ,
type = 'JK1' ,
data = nssrn_df
)
```
### Variable Recoding {-}
Add new columns to the data set:
```{r eval = FALSE , results = "hide" }
nssrn_design <-
update(
nssrn_design ,
# all advanced practice registered nurses
# (including nurse practitioners)
all_aprn = as.numeric( ed_lcrn == 2 ) ,
age_group =
factor(
findInterval( age_gp_puf , c( 0 , 3 , 5 , 7 , 9 ) ) ,
levels = 1:5 ,
labels =
c(
'34 or younger' ,
'35 to 44' ,
'45 to 54' ,
'55 to 64' ,
'65 or older'
)
) ,
primary_position_state =
factor(
as.numeric( pn_loc_code_puf ) ,
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, 72L,
# note collapsed geographies from codebook
500L, 800L) ,
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", "Puerto Rico",
# note collapsed geographies from codebook
"District of Columbia & Delaware", "Montana & Wyoming")
)
)
```
---
## Analysis Examples with the `survey` library \ {-}
### Unweighted Counts {-}
Count the unweighted number of records in the survey sample, overall and by groups:
```{r eval = FALSE , results = "hide" }
sum( weights( nssrn_design , "sampling" ) != 0 )
svyby( ~ one , ~ age_group , nssrn_design , unwtd.count )
```
### Weighted Counts {-}
Count the weighted size of the generalizable population, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ one , nssrn_design )
svyby( ~ one , ~ age_group , nssrn_design , svytotal )
```
### Descriptive Statistics {-}
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ pn_earn_puf , nssrn_design , na.rm = TRUE )
svyby( ~ pn_earn_puf , ~ age_group , nssrn_design , svymean , na.rm = TRUE )
```
Calculate the distribution of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svymean( ~ primary_position_state , nssrn_design , na.rm = TRUE )
svyby( ~ primary_position_state , ~ age_group , nssrn_design , svymean , na.rm = TRUE )
```
Calculate the sum of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ pn_earn_puf , nssrn_design , na.rm = TRUE )
svyby( ~ pn_earn_puf , ~ age_group , nssrn_design , svytotal , na.rm = TRUE )
```
Calculate the weighted sum of a categorical variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svytotal( ~ primary_position_state , nssrn_design , na.rm = TRUE )
svyby( ~ primary_position_state , ~ age_group , nssrn_design , svytotal , na.rm = TRUE )
```
Calculate the median (50th percentile) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
svyquantile( ~ pn_earn_puf , nssrn_design , 0.5 , na.rm = TRUE )
svyby(
~ pn_earn_puf ,
~ age_group ,
nssrn_design ,
svyquantile ,
0.5 ,
ci = TRUE , na.rm = TRUE
)
```
Estimate a ratio:
```{r eval = FALSE , results = "hide" }
svyratio(
numerator = ~ pn_earn_puf ,
denominator = ~ hrs_yr_puf ,
nssrn_design ,
na.rm = TRUE
)
```
### Subsetting {-}
Restrict the survey design to individuals working as RNs or APRNs (excluding RNs working as LPNs):
```{r eval = FALSE , results = "hide" }
sub_nssrn_design <- subset( nssrn_design , pn_lcreq_none == 2 )
```
Calculate the mean (average) of this subset:
```{r eval = FALSE , results = "hide" }
svymean( ~ pn_earn_puf , sub_nssrn_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:
```{r eval = FALSE , results = "hide" }
this_result <- svymean( ~ pn_earn_puf , nssrn_design , na.rm = TRUE )
coef( this_result )
SE( this_result )
confint( this_result )
cv( this_result )
grouped_result <-
svyby(
~ pn_earn_puf ,
~ age_group ,
nssrn_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:
```{r eval = FALSE , results = "hide" }
degf( nssrn_design )
```
Calculate the complex sample survey-adjusted variance of any statistic:
```{r eval = FALSE , results = "hide" }
svyvar( ~ pn_earn_puf , nssrn_design , na.rm = TRUE )
```
Include the complex sample design effect in the result for a specific statistic:
```{r eval = FALSE , results = "hide" }
# SRS without replacement
svymean( ~ pn_earn_puf , nssrn_design , na.rm = TRUE , deff = TRUE )
# SRS with replacement
svymean( ~ pn_earn_puf , nssrn_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:
```{r eval = FALSE , results = "hide" }
svyciprop( ~ all_aprn , nssrn_design ,
method = "likelihood" )
```
### Regression Models and Tests of Association {-}
Perform a design-based t-test:
```{r eval = FALSE , results = "hide" }
svyttest( pn_earn_puf ~ all_aprn , nssrn_design )
```
Perform a chi-squared test of association for survey data:
```{r eval = FALSE , results = "hide" }
svychisq(
~ all_aprn + primary_position_state ,
nssrn_design
)
```
Perform a survey-weighted generalized linear model:
```{r eval = FALSE , results = "hide" }
glm_result <-
svyglm(
pn_earn_puf ~ all_aprn + primary_position_state ,
nssrn_design
)
summary( glm_result )
```
---
## Replication Example {-}
This example matches statistics and relative standard errors from the "Demographics" tab of [Nursing Workforce 2022 NSSRN Dashboard Data](https://data.hrsa.gov//DataDownload/DD_Files/Nursing_Workforce_2022_NSSRN_Dashboard_Data.xlsx):
```{r eval = FALSE , results = "hide" }
unwtd_count_result <- svyby( ~ one , ~ age_group , nssrn_design , unwtd.count )
# cells L398 thru L402
stopifnot( coef( unwtd_count_result ) == c( 6693 , 12268 , 10804 , 10538 , 8811 ) )
wtd_n_result <- svytotal( ~ age_group , nssrn_design )
# cells J398 thru J402
stopifnot( round( coef( wtd_n_result ) , 0 ) == c( 861060 , 1078187 , 935778 , 834939 , 639412 ) )
share_result <- svymean( ~ age_group , nssrn_design )
# cells K398 thru K402
stopifnot( round( coef( share_result ) , 3 ) == c( 0.198 , 0.248 , 0.215 , 0.192 , 0.147 ) )
# cells M398 thru M402
stopifnot(
round( SE( share_result ) / coef( share_result ) , 4 ) ==
c( 0.0206 , 0.0155 , 0.0192 , 0.0187 , 0.0125 )
)
```
---
## Analysis Examples with `srvyr` \ {-}
The R `srvyr` library calculates summary statistics from survey data, such as the mean, total or quantile using [dplyr](https://github.com/tidyverse/dplyr/)-like syntax. [srvyr](https://github.com/gergness/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](https://cran.r-project.org/web/packages/srvyr/vignettes/srvyr-vs-survey.html) details the available features. As a starting point for NSSRN users, this code replicates previously-presented examples:
```{r eval = FALSE , results = "hide" }
library(srvyr)
nssrn_srvyr_design <- as_survey( nssrn_design )
```
Calculate the mean (average) of a linear variable, overall and by groups:
```{r eval = FALSE , results = "hide" }
nssrn_srvyr_design %>%
summarize( mean = survey_mean( pn_earn_puf , na.rm = TRUE ) )
nssrn_srvyr_design %>%
group_by( age_group ) %>%
summarize( mean = survey_mean( pn_earn_puf , na.rm = TRUE ) )
```