-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignWeights.Rmd
More file actions
616 lines (560 loc) · 17.6 KB
/
DesignWeights.Rmd
File metadata and controls
616 lines (560 loc) · 17.6 KB
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
---
title: "Design Weights"
author: "Jordan White, PhD"
date: "9/17/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(readr)
#library(survey)
setwd(paste("C:/Users/VBACOWhiteJ1/Documents"
,"/EDU_Docs/Survey-2021"
,sep = "")
)
```
# Goals
We've selected a sample for the survey, so now design weights can be calculated. To avoid too complex a series of calculations, let's focus on five variables: Gender, Benefit type, and Client Type, Race, and Era of service.
## Import prior data
```{r Data, echo=FALSE}
# import the eligible dataset and the sample index
# subset and create a separate table for the sample
data <- read_csv(#original data
"./PA&I_DataPulls/2022-02-28_processed.csv"
)
sample_data <- read_csv("Sample_Design_Weights.csv")
# sample_data1 <-
# read_csv("Main_sample-414survey.csv"
# ,col_types = "ccccccccccccccccccic")
# sample_data2 <-
# read_csv("Back_up_sample-414survey.csv"
# ,col_types = "ccccccccccccccccccic") %>%
# select(names(sample_data1))
# sample_data <- sample_data1 %>%
# bind_rows(sample_data2)
# Ran the below code once, to correct data
# # Import data to correct SV/Vet Race and Gender
# # It isn't complete, but this helps
# fix1 <- read_csv(paste(
# "./PA&I_DataPulls/VetsMissing-Gender-OR-"
# ,"Race_Output1.csv"
# ,sep=""))
# fix2 <- read_csv(paste(
# "./PA&I_DataPulls/VetsMissing-Gender-OR-"
# ,"Race_Output2.csv"
# ,sep=""))
# fix3 <- read_csv(paste(
# "./PA&I_DataPulls/VetsMissing-Gender-OR-"
# ,"Race_Output3.csv"
# ,sep=""))
# fix <- rbind(
# fix1, fix2, fix3
# )
# rm(
# fix1, fix2, fix3
# )
#
# # Push all to upper case
# data <- data %>%
# mutate(FIRST_NM = toupper(FIRST_NM),
# LAST_NM = toupper(LAST_NM))
# sample_data <- sample_data %>%
# mutate(FIRST_NM = toupper(FIRST_NM),
# LAST_NM = toupper(LAST_NM))
# fix <- fix %>%
# mutate(FIRST_NAME = toupper(FIRST_NAME),
# LAST_NAME = toupper(LAST_NAME))
# fix <- fix %>%
# select(SSN, FIRST_NAME, LAST_NAME, SEX, RACE)
#
# ### Match and fill values
# data <- data %>%
# left_join(fix, by = c(
# "SSN" = "SSN", "FIRST_NM" = "FIRST_NAME",
# "LAST_NM" = "LAST_NAME"
# ))
# sample_data <- sample_data %>%
# left_join(fix, by = c(
# "SSN" = "SSN", "FIRST_NM" = "FIRST_NAME",
# "LAST_NM" = "LAST_NAME"
# ))
#
# # Fill empty race values
# data$RACE <- NA
# data$RACE[!is.na(data$RACE.x)] <-
# data$RACE.x[!is.na(data$RACE.x)]
# data$RACE[!is.na(data$RACE.y)& #keep old if exists
# is.na(data$RACE)] <-
# data$RACE.y[!is.na(data$RACE.y)&
# is.na(data$RACE)]
# sample_data$RACE <- NA
# sample_data$RACE[!is.na(sample_data$RACE.x)] <-
# sample_data$RACE.x[!is.na(sample_data$RACE.x)]
# sample_data$RACE[!is.na(sample_data$RACE.y)&
# is.na(sample_data$RACE)] <-
# sample_data$RACE.y[!is.na(sample_data$RACE.y)&
# is.na(sample_data$RACE)]
#
# # Fill empty gender values
# data$GENDER[is.na(data$GENDER)] <-
# data$SEX[is.na(data$GENDER)]
# sample_data$GENDER[is.na(sample_data$GENDER)] <-
# sample_data$SEX[is.na(sample_data$GENDER)]
#
# # Remove unnecessary columns
# data <- data %>%
# select(-SEX, -RACE.x, -RACE.y)
# sample_data <- sample_data %>%
# select(-SEX, -RACE.x, -RACE.y)
# sample_data <- sample_data[!duplicated(
# sample_data
# ),]
# data <- data[!duplicated(
# data
# ),]
# # Save these files
# write_csv(data,
# "./PA&I_DataPulls/2021-10-14_Processed.csv")
# write_csv(sample_data,
# "Main_sample-414survey.csv")
#
# # Below code was used to correct most of the
# # missing gender codes
# x1 <- x1 %>% bind_rows(
# read_csv("WholePop_GenderCorrects9.csv"
# ,col_names =
# c("SSN","Gender","SelfGender")))
# data <- data %>% mutate(
# Social = if_else(is.na(CLIENT_SSN)
# ,SSN, CLIENT_SSN))
# )
# # Coerce some of the values to simplify
# gender_lookup <- c(
# "N" = "U"
# ,"O" = "U"
# ,"TF" = "F"
# ,"TM" = "M"
# ,"M" = "M"
# ,"F" = "F"
# ,"NULL" = NA
# )
#
# x1 <- x1 %>%
# mutate(SelfGender = gender_lookup[SelfGender]
# ) %>%
# mutate(Gender = gender_lookup[Gender]) %>%
# mutate(Gender = if_else(is.na(SelfGender)
# ,Gender
# ,SelfGender))
# data <- data %>%
# left_join(x1 %>%
# select(1,2)
# ,by = c("Social" = "SSN"))
# data$GENDER[(data$GENDER =="Z"|
# data$GENDER=="U"|
# is.na(data$GENDER))] <-
# data$Gender[(data$GENDER =="Z"|
# data$GENDER=="U"|
# is.na(data$GENDER))]
# # Move 57 U's to NA. This is to avoid weird weights
# data$GENDER[data$GENDER == "U"] <- NA
# data <- data %>% select(-Social)
# write_csv(data,
# "./PA&I_DataPulls/2022-02-28_processed.csv")
# # Apply to the samples
# sample_data1 <- sample_data1 %>%
# mutate(
# Social = if_else(is.na(CLIENT_SSN)
# ,SSN, CLIENT_SSN)) %>%
# left_join(x1 %>%
# select(1,2)
# ,by = c("Social" = "SSN"))
# sample_data2 <- sample_data2 %>%
# mutate(
# Social = if_else(is.na(CLIENT_SSN)
# ,SSN, CLIENT_SSN)) %>%
# left_join(x1 %>%
# select(1,2)
# ,by = c("Social" = "SSN"))
#
# sample_data1$GENDER[(sample_data1$GENDER =="Z"|
# sample_data1$GENDER=="U"|
# is.na(sample_data1$GENDER))] <-
# sample_data1$Gender[(sample_data1$GENDER =="Z"|
# sample_data1$GENDER=="U"|
# is.na(sample_data1$GENDER))]
#
# sample_data2$GENDER[(sample_data2$GENDER =="Z"|
# sample_data2$GENDER=="U"|
# is.na(sample_data2$GENDER))] <-
# sample_data2$Gender[(sample_data2$GENDER =="Z"|
# sample_data2$GENDER=="U"|
# is.na(sample_data2$GENDER))]
#
# sample_data1 <- sample_data1 %>% select(-Social
# ,Gender)
# sample_data2 <- sample_data2 %>% select(-Social
# ,Gender)
#
# write_csv(sample_data1, "Main_sample-414survey.csv")
# write_csv(sample_data2
# ,"Back_up_sample-414survey.csv")
# Change data types
sample_data <- sample_data %>%
mutate(RANK = as.factor(RANK)
,GENDER = as.factor(GENDER)
,ERA_OF_SERVICE = as.factor(ERA_OF_SERVICE)
,CLIENT_TYPE = as.factor(CLIENT_TYPE)
,RACE = as.factor(RACE)
,BENEFIT = as.factor(BENEFIT))
data <- data %>%
mutate(RANK = as.factor(RANK)
,GENDER = as.factor(GENDER)
,ERA_OF_SERVICE = as.factor(ERA_OF_SERVICE)
,CLIENT_TYPE = as.factor(CLIENT_TYPE)
,RACE = as.factor(RACE)
,BENEFIT = as.factor(BENEFIT))
sample_data <- sample_data[!duplicated(
sample_data$id
),]
```
A couple issues I noticed by cross-tabulating factors in the command line before building weights:
* NA's are ignored by default when tabulating. There are also "unknown" and "other" values in this dataset. I'll replace the inconsistent missing value types with NA and then coerce `table` to include the NA's
* The racial categories are splitting a bit too much. If I include it in the weighting, I need to combine the "Asian" and the "Pacific Islander" with the catchall category that already exists for anyone with ancestry East of the Caucasus.
* The Fry scholars are so few that it will create bizarre weights. Also, it is a Ch 33 benefit only and is technically a transfer of benefit, tragically (death). I'm going to collapse Fry with TOE for Client Type.
## Data Parsing and Weights
Replace unknown values with NA so that cross-tabulation can occur. After that, build design weights for Benefit, Gender, Race, and Type.
```{r Compression}
# Gender contains "U" that needs to be NA
# Race contains "Unknown" and "Other" for NAs
data$GENDER <- replace(
data$GENDER
,data$GENDER %in% c("U","Z")
,NA
)
sample_data$GENDER <- replace(
sample_data$GENDER
,sample_data$GENDER %in% c("U","Z")
,NA
)
data$RACE <- replace(
data$RACE
,data$RACE %in% c("Unknown","Other","Z")
,NA
)
sample_data$RACE <- replace(
sample_data$RACE
,sample_data$RACE %in% c("Unknown","Other","Z")
,NA
)
# Recode "FRY" as "TOE" in Client column
# Recode "Asian" and
# "Native Hawaiian/Pacific Islander" as
# "Asian or Pacific Islander Unspecified"
data[data$CLIENT_TYPE == "FRY", "CLIENT_TYPE"] <-
"TOE"
sample_data[sample_data$CLIENT_TYPE == "FRY"
,"CLIENT_TYPE"] <-
"TOE"
data[grep(
"Asian|Native Hawaiian/Pacific Islander"
,data$RACE)
,"RACE"] <-
"Asian or Pacific Islander Unspecified"
sample_data[grep(
"Asian|Native Hawaiian/Pacific Islander"
,sample_data$RACE)
,"RACE"] <-
"Asian or Pacific Islander Unspecified"
# Drop the now empty factor levels
data$GENDER <- droplevels(data$GENDER)
sample_data$GENDER <- droplevels(sample_data$GENDER)
data$RACE <- droplevels(data$RACE)
sample_data$RACE <- droplevels(sample_data$RACE)
data$CLIENT_TYPE <- droplevels(data$CLIENT_TYPE)
sample_data$CLIENT_TYPE <- droplevels(sample_data$CLIENT_TYPE)
```
Now let's build cross-tabulations and from that calculate the design weights.
```{r WeightsWithRace}
# These weights include race in the cross
# tabulation, which produces some categories with
# extremely low sample sizes and strong weights
# Also note that the multiracial category had to be
# dropped from the original data b/c there are none
# in the sample
data_cross <- as.data.frame(
table(data$BENEFIT
,data$CLIENT_TYPE
,data$GENDER
,data$RACE
,exclude = "Multiple race"
,useNA = "ifany")
)
sample_cross <- as.data.frame(
table(sample_data$BENEFIT
,sample_data$CLIENT_TYPE
,sample_data$GENDER
,sample_data$RACE
,useNA = "ifany")
)
# Build four functions
# One 'which' to detect all regular input
# One each for NA combos
# Use the output index to collect Freq on the fly
witcher1 <- function(cross_tab
,benefit
,client
,gender
,race){
which(
as.vector(cross_tab[,1]) == benefit &
as.vector(cross_tab[,2]) == client &
as.vector(cross_tab[,3]) == gender &
as.vector(cross_tab[,4]) == race
)
}
witcher2 <- function(cross_tab
,benefit
,client
,race){
which(
as.vector(cross_tab[,1]) == benefit &
as.vector(cross_tab[,2]) == client &
is.na(cross_tab[,3]) &
as.vector(cross_tab[,4]) == race
)
}
witcher3 <- function(cross_tab
,benefit
,client
,gender){
which(
as.vector(cross_tab[,1]) == benefit &
as.vector(cross_tab[,2]) == client &
as.vector(cross_tab[,3]) == gender &
is.na(cross_tab[,4])
)
}
witcher4 <- function(cross_tab
,benefit
,client){
which(
as.vector(cross_tab[,1]) == benefit &
as.vector(cross_tab[,2]) == client &
is.na(cross_tab[,3]) &
is.na(cross_tab[,4])
)
}
# Build and parse design weights,
# then use the Witchers to apply the design weights
# check for completeness
# make sure that if something has dropped from
# the sample frame, we aren't building a design
# weight for it
data_cross$Paste <- paste(# a pseudo-column of names
data_cross$Var1
,data_cross$Var2
,data_cross$Var3
,data_cross$Var4
)
sample_cross$Paste <- paste(
sample_cross$Var1
,sample_cross$Var2
,sample_cross$Var3
,sample_cross$Var4
)
# Now remove data rows without representation
# in the sample
data_cross <- data_cross %>%
filter(Paste %in% sample_cross$Paste)
# Calculate weights
sample_cross$D_weight_race <- data_cross$Freq /
sample_cross$Freq
# Deal with a few out of range issues
sample_cross[
is.infinite(sample_cross$D_weight_race)
,"D_weight_race"] <- NA
sample_cross[
is.nan(sample_cross$D_weight_race)
,"D_weight_race"] <- NA
# initialize empty column on the sample data frame
sample_data$D_weight_race <- NA
for(index in 1:dim(sample_data)[1]){
rows <- sample_data[index,]
if(!is.na(rows$GENDER) &
!is.na(rows$RACE)){
i <- witcher1(
sample_cross
,benefit = rows$BENEFIT
,client = rows$CLIENT_TYPE
,gender = rows$GENDER
,race = rows$RACE
)
sample_data$D_weight_race[index] <-
sample_cross$D_weight_race[i]
} else if(is.na(rows[,"GENDER"]) &
!is.na(rows[,"RACE"])){
i <- witcher2(
sample_cross
,benefit = rows$BENEFIT
,client = rows$CLIENT_TYPE
,race = rows$RACE
)
sample_data$D_weight_race[index] <-
sample_cross$D_weight_race[i]
} else if(!is.na(rows$GENDER) &
is.na(rows$RACE)){
i <- witcher3(
sample_cross
,benefit = rows$BENEFIT
,client = rows$CLIENT_TYPE
,gender = rows$GENDER
)
sample_data$D_weight_race[index] <-
sample_cross$D_weight_race[i]
} else if(is.na(rows$GENDER) &
is.na(rows$RACE)){
i <- witcher4(
sample_cross
,benefit = rows$BENEFIT
,client = rows$CLIENT_TYPE
)
sample_data$D_weight_race[index] <-
sample_cross$D_weight_race[i]
}
}
# For checking the weights:
# Note: there is a machine precision error that
# produced a FALSE comparison for one of the
# comparisons that should be TRUE. Check any
# FALSE's manually
# Check for NA's in sample weights
summary(sample_data$D_weight_race)
# Build Sums to verify population size weighting
Sample_weight_test <- sample_data %>%
group_by(BENEFIT, CLIENT_TYPE, GENDER, RACE) %>%
summarise(n = sum(D_weight_race)) # Sample
Data_weight_test <- data %>%
group_by(BENEFIT, CLIENT_TYPE, GENDER, RACE) %>%
summarise(N = n()) # Population
Data_weight_test <- Data_weight_test %>%
inner_join(Sample_weight_test)
# Check any FALSE values for machine precision
Data_weight_test$N == round(
Data_weight_test$n)
```
I also decided to make weights that disregard race. This will allow comparisons with the race-inclusive weights. If adding race reduces the variance of the weighted data, then it helps. If not, it would be better to use the simpler weights below.
```{r WeightsNoRace}
# Redo except exclude race from the weighting scheme
data_cross <- as.data.frame(
table(data$BENEFIT
,data$CLIENT_TYPE
,data$GENDER
,useNA = "ifany")
)
sample_cross <- as.data.frame(
table(sample_data$BENEFIT
,sample_data$CLIENT_TYPE
,sample_data$GENDER
,useNA = "ifany")
)
# Build two functions
# One 'which' to detect all regular input
# One for NA in GENDER
# Use the output index to collect Freq on the fly
witcher1 <- function(cross_tab
,benefit
,client
,gender){
which(
as.vector(cross_tab[,1]) == benefit &
as.vector(cross_tab[,2]) == client &
as.vector(cross_tab[,3]) == gender
)
}
witcher2 <- function(cross_tab
,benefit
,client){
which(
as.vector(cross_tab[,1]) == benefit &
as.vector(cross_tab[,2]) == client &
is.na(cross_tab[,3])
)
}
# check for completeness
# make sure that if something has dropped from
# the sample frame, we aren't building a design
# weight for it
data_cross$Paste <- paste(# a pseudo-column of names
data_cross$Var1
,data_cross$Var2
,data_cross$Var3
,data_cross$Var4
)
sample_cross$Paste <- paste(
sample_cross$Var1
,sample_cross$Var2
,sample_cross$Var3
,sample_cross$Var4
)
# Now remove data rows without representation
# in the sample
data_cross <- data_cross %>%
filter(Paste %in% sample_cross$Paste)
# Build and parse design weights,
# then use the Witchers to apply the design weights
sample_cross$D_weight <- data_cross$Freq /
sample_cross$Freq
sample_cross[
is.infinite(sample_cross$D_weight)
,"D_weight"] <- NA
sample_cross[
is.nan(sample_cross$D_weight)
,"D_weight"] <- NA
# initialize empty column
sample_data$D_weight <- NA
for(index in 1:dim(sample_data)[1]){
rows <- sample_data[index,]
if(!is.na(rows$GENDER)){
i <- witcher1(
sample_cross
,benefit = rows$BENEFIT
,client = rows$CLIENT_TYPE
,gender = rows$GENDER
)
sample_data$D_weight[index] <-
sample_cross$D_weight[i]
} else if(is.na(rows$GENDER)){
i <- witcher2(
sample_cross
,benefit = rows$BENEFIT
,client = rows$CLIENT_TYPE
)
sample_data$D_weight[index] <-
sample_cross$D_weight[i]
}
}
# For checking the weights:
# Note: there is a machine precision error that
# produced a FALSE comparison for one of the
# comparisons that should be TRUE. Check any
# FALSE's manually
# These values are more centered
summary(sample_data$D_weight)
Sample_weight_test <- sample_data %>%
group_by(BENEFIT, CLIENT_TYPE, GENDER) %>%
summarise(n = sum(D_weight))
Data_weight_test <- data %>%
group_by(BENEFIT, CLIENT_TYPE, GENDER) %>%
summarise(N = n())
Data_weight_test <- Data_weight_test %>%
inner_join(Sample_weight_test)
# If FALSE, check manually,
# potentially a machine precision issue
Data_weight_test$N == round(Data_weight_test$n)
```
The design weights all appear to sum to the population size, so everything is working normally.
```{r Output}
write_csv(sample_data,"Sample_Design_Weights.csv")
```