-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPHC-B-2021.Rmd
2782 lines (2178 loc) · 126 KB
/
SPHC-B-2021.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: "Self-reported sexual identity in Stockholm County in 2021"
author: Willi Zhang, Maya Mathur, Matteo Quartagno
email: [email protected]
output: html_notebook
editor_options:
chunk_output_type: console
---
### 1. Load Packages
```{r}
library(naniar)
library(tidyverse)
library(finalfit)
library(mitml)
library(mitools)
library(survey)
library(svyVGAM)
library(vcd)
library(rcompanion)
source("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Population Trends and Individual Fluidity of Sexual Identity in Stockholm County/Original Research/Temporal-Trends-in-Sexual-Identity-and-Sociodemographic-Disparities-in-Stockholm-County-2010-to-2021/Helper_functions.R") # helper function
```
### 2. Import and Prepare Data
```{r}
load("/Volumes/LGBT Project data/d_2021.RData")
# sampling strata
n_miss( d_2021$stratum )
d_2021$sampling_strata <- as.factor( d_2021$stratum )
length( unique( d_2021$sampling_strata ) ) # 38 strata
d_2021 <- d_2021 %>%
mutate( sampling_strata_region = recode( sampling_strata,
`114` = "Upplands-Väsby",
`115` = "Vallentuna",
`117` = "Österåker",
`120` = "Värmdö",
`123` = "Järfälla",
`125` = "Ekerö",
`126` = "Huddinge",
`127` = "Botkyrka",
`128` = "Salem",
`136` = "Haninge",
`138` = "Tyresö",
`139` = "Upplands-Bro",
`140` = "Nykvarn",
`160` = "Täby",
`162` = "Danderyd",
`163` = "Sollentuna",
`181` = "Södertälje",
`182` = "Nacka",
`183` = "Sundbyberg",
`184` = "Solna",
`186` = "Lidingö",
`187` = "Vaxholm",
`188` = "Norrtälje",
`191` = "Sigtuna",
`192` = "Nynäshamn",
`180103` = "Spånga-Tensta",
`180104` = "Hässelby-Vällingby",
`180106` = "Bromma",
`180108` = "Kungsholmen",
`180109` = "Norrmalm",
`180110` = "Östermalm",
`180115` = "Skarpnäck",
`180118` = "Farsta",
`180124` = "Skärholmen",
`180125` = "Rinkeby-Kista",
`180127` = "Södermalm",
`180128` = "Enskede-Årsta-Vantör",
`180129` = "Hägersten-Älvsjö"
) )
# sexual identity
table( d_2021$F21F45_Q57, useNA = "always" )
d_2021$sexual_identity_2021 <- factor( ifelse( d_2021$F21F45_Q57 == 1, "Heterosexual",
ifelse( d_2021$F21F45_Q57 == 2, "Homosexual",
ifelse( d_2021$F21F45_Q57 == 3, "Bisexual", "None of the above" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "None of the above" ) )
table( d_2021$sexual_identity_2021, useNA = "always" )
# sex
table( d_2021$kon, useNA = "always" )
d_2021$sex <- factor( ifelse( d_2021$kon == 1, "Male", "Female" ),
levels = c( "Male", "Female" ) )
table( d_2021$sex, useNA = "always" )
# age
summary( d_2021$F21alder )
d_2021$age <- d_2021$F21alder
d_2021$age_cat <- factor( ifelse( d_2021$age <= 29, "16-29",
ifelse( d_2021$age >=30 & d_2021$age <= 44, "30-44",
ifelse( d_2021$age >= 45 & d_2021$age <= 59, "45-59", ">=60" ) ) ),
levels = c( "16-29", "30-44", "45-59", ">=60" ) )
table( d_2021$age_cat, useNA = "always" )
# interaction term between age and sex
d_2021$age_sex <- interaction( d_2021$age_cat, d_2021$sex )
# generation
d_2021$birth_year <- 2021 - d_2021$age
d_2021 <- d_2021 %>%
mutate( generation = case_when(
birth_year >= 1997 & birth_year <= 2012 ~ "Generation Z (1997–2012)",
birth_year >= 1981 & birth_year <= 1996 ~ "Millennials (1981–1996)",
birth_year >= 1965 & birth_year <= 1980 ~ "Generation X (1965–1980)",
birth_year >= 1946 & birth_year <= 1964 ~ "Baby Boomers (1946–1964)",
birth_year >= 1928 & birth_year <= 1945 ~ "Silent Generation (1928–1945)",
birth_year >= 1901 & birth_year <= 1927 ~ "Greatest Generation (1901–1927)"
) )
d_2021$generation <- factor(
d_2021$generation,
levels = c( "Generation Z (1997–2012)", "Millennials (1981–1996)", "Generation X (1965–1980)", "Baby Boomers (1946–1964)", "Silent Generation (1928–1945)", "Greatest Generation (1901–1927)" ) )
summary( d_2021$generation )
# interaction term between generation and sex
d_2021$generation_sex <- interaction( d_2021$generation, d_2021$sex )
# country of birth
table( d_2021$fodelseland, useNA = "always" )
d_2021$country_of_birth <- factor( ifelse( d_2021$fodelseland == "Afrika", "Africa",
ifelse( d_2021$fodelseland == "Asien", "Asia",
ifelse( d_2021$fodelseland == "Europa", "Europe",
ifelse( d_2021$fodelseland == "Sverige", "Sweden", "Americas" ) ) ) ),
levels = c( "Africa", "Americas", "Asia", "Europe", "Sweden" ) )
table( d_2021$country_of_birth, useNA = "always" )
# education
table( d_2021$utbniva2021, useNA = "always" )
d_2021$education <- factor( ifelse( d_2021$utbniva2021 <= 2, "<=9 years",
ifelse( d_2021$utbniva2021 <= 4, "10-12 years", ">=13 years" ) ),
levels = c( "<=9 years", "10-12 years", ">=13 years" ) )
table( d_2021$education, useNA = "always" )
# disposable income
summary( d_2021$dispink2021, useNA = "always" )
d_2021$dispink2021[ d_2021$dispink2021 < 0 ] <- NA
d_2021$income <- factor( ifelse( d_2021$dispink2021 <= 2500, "<=2,500",
ifelse( d_2021$dispink2021 > 2500 & d_2021$dispink2021 <= 3500, "(2,500, 3,500]",
ifelse( d_2021$dispink2021 > 3500 & d_2021$dispink2021 <= 4500, "(3,500, 4,500]", ">4,500" ) ) ),
levels = c( "<=2,500", "(2,500, 3,500]", "(3,500, 4,500]", ">4,500" ) )
table( d_2021$income, useNA = "always" )
# marital status
table( d_2021$civil2021, useNA = "always" )
d_2021$marital_status <- factor( ifelse( d_2021$civil2021 == "G" | d_2021$civil2021 == "RP", "Currently married",
ifelse( d_2021$civil2021 == "OG", "Never married", "Other" ) ),
levels = c( "Never married", "Currently married", "Other" ) )
table( d_2021$marital_status, useNA = "always" )
# occupation
table( d_2021$SSYK_kl, useNA = "always" )
d_2021$occupation <- factor(
ifelse(
d_2021$SSYK_kl == "Yrken inom byggverksamhet och tillverkning" |
d_2021$SSYK_kl == "Yrken inom lantbruk, trädgård, skogsbruk och fiske" |
d_2021$SSYK_kl == "Yrken inom maskinell tillverkning och transport m.m.",
"Manual and field trades",
ifelse(
d_2021$SSYK_kl == "Service-, omsorgs- och försäljningsyrken" |
d_2021$SSYK_kl == "Yrken inom administration och kundtjänst" |
d_2021$SSYK_kl == "Yrken med krav på kortare utbildning eller introduktion",
"Service and support",
ifelse(
d_2021$SSYK_kl == "Yrken med krav på fördjupad högskolekompetens" |
d_2021$SSYK_kl == "Yrken med krav på högskolekompetens eller motsvarande" |
d_2021$SSYK_kl == "Chefsyrken",
"Expertise and leadership",
NA
)
)
),
levels = c(
"Manual and field trades",
"Service and support",
"Expertise and leadership"
)
)
table( d_2021$occupation, useNA = "always" )
##### demographic characteristics #####
# among respondents (n = 23,066)
# make characteristics table by sexual identity
explanatory = c( "sex", "age_cat", "country_of_birth", "education", "occupation", "income", "marital_status" )
dependent = "sexual_identity_2021"
d_2021_table <- d_2021 %>%
summary_factorlist( dependent,
explanatory,
na_include = TRUE,
na_include_dependent = TRUE,
total_col = TRUE,
add_col_totals = TRUE,
column = FALSE )
# Fisher's test
x1 <- table( d_2021$sex, d_2021$sexual_identity_2021 )
x1
format( round( fisher.test( x1[, c( 1:2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x1[, c( 1, 3 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x1[, c( 1, 4 ) ] )$p.value, 3 ), nsmall = 3 )
x2 <- table( d_2021$age_cat, d_2021$sexual_identity_2021 )
x2
format( round( fisher.test( x2[, c( 1:2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x2[, c( 1, 3 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x2[, c( 1, 4 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x3 <- table( d_2021$country_of_birth, d_2021$sexual_identity_2021 )
x3
format( round( fisher.test( x3[, c( 1:2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x3[, c( 1, 3 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x3[, c( 1, 4 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x4 <- table( d_2021$education, d_2021$sexual_identity_2021 )
x4
format( round( fisher.test( x4[, c( 1:2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x4[, c( 1, 3 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x4[, c( 1, 4 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x5 <- table( d_2021$income, d_2021$sexual_identity_2021 )
x5
format( round( fisher.test( x5[, c( 1:2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x5[, c( 1, 3 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x5[, c( 1, 4 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x6 <- table( d_2021$marital_status, d_2021$sexual_identity_2021 )
x6
format( round( fisher.test( x6[, c( 1:2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x6[, c( 1, 3 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x6[, c( 1, 4 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x7 <- table( d_2021$occupation, d_2021$sexual_identity_2021 )
x7
format( round( fisher.test( x7[, c( 1:2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x7[, c( 1, 3 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x7[, c( 1, 4 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
##### design weights #####
summary( d_2021$F21dvikt )
d_2021$design_weight <- d_2021$F21dvikt
##### calibrated weights #####
d_2021$calibrated_weight <- d_2021$F21kalvikt
##### non-response #####
# unit non-response
d_2021$design_weight_unit_nonresponse <- d_2021$F21dbvikt # weights calculated assuming Missing Completely At Random (MCAR) within each stratum
summary( d_2021$design_weight_unit_nonresponse )
sum( d_2021$design_weight_unit_nonresponse ) # No. of source population = 1,827,424
unitresponse_prob <- d_2021 %>%
group_by( sampling_strata_region ) %>%
summarise( unitresponse_prob = unique( design_weight ) / unique( design_weight_unit_nonresponse ),
no.of.population = sum( design_weight_unit_nonresponse ),
sample_size = unique( no.of.population )/unique( design_weight ) ) # calculate overall unit response rate, and no. of population and sample size within each stratum
d_2021 <- d_2021 %>%
left_join( unitresponse_prob, by = "sampling_strata_region" )
# item non-response
itemresponse_prob <- d_2021 %>%
group_by( sampling_strata_region ) %>%
summarise( itemresponse_prob = sum( !is.na( sexual_identity_2021 ) ) / n() ) # calculate item response rate
d_2021 <- d_2021 %>%
left_join( itemresponse_prob, by = "sampling_strata_region" ) %>%
mutate( itemresponse_prob = ifelse( is.na( sexual_identity_2021 ), 0, itemresponse_prob ) )
##### summary of stratified sampling #####
sampling_frame_2021 <- as.data.frame( d_2021 %>%
group_by( sampling_strata_region ) %>%
reframe( no.of.population = unique( no.of.population ),
sample_size = unique( no.of.population/design_weight ),
unitresponse = n(),
itemresponse = sum( itemresponse_prob != 0 ) ) )
sampling_frame_2021$unitresponse_rate <- sampling_frame_2021$unitresponse/sampling_frame_2021$sample_size
sampling_frame_2021$itemresponse_rate <- sampling_frame_2021$itemresponse/sampling_frame_2021$unitresponse
sampling_frame_2021$overallresponse_rate <- sampling_frame_2021$itemresponse/sampling_frame_2021$sample_size
sampling_frame_2021$unitresponse_label <- paste0( sampling_frame_2021$unitresponse, " (",
sprintf( "%.1f", sampling_frame_2021$unitresponse_rate*100 ), "%)" )
sampling_frame_2021$overallresponse_label <- paste0( sampling_frame_2021$itemresponse, " (",
sprintf( "%.1f", sampling_frame_2021$overallresponse_rate*100 ), "%)" )
round( sum( sampling_frame_2021$unitresponse )/( sum( sampling_frame_2021$sample_size ) ), 3 ) # overall unit response rate
round( sum( sampling_frame_2021$itemresponse )/( sum( sampling_frame_2021$unitresponse ) ), 3 ) # overall item response rate
round( sum( sampling_frame_2021$itemresponse )/( sum( sampling_frame_2021$sample_size ) ), 3 ) # overall response rate
writexl::write_xlsx( sampling_frame_2021, "sampling_frame_2021.xlsx" )
##### replicating non-respondent rows for sampling design #####
# because d_2021 includes data only for respondents (unit response), we need to duplicate rows for non-respondents
# so that the final dataset represents the sampling process
rows_to_add <- d_2021 %>%
group_by( sampling_strata_region ) %>%
summarise( to_add = unique( sample_size ) - n(),
no.of.population = unique( no.of.population ),
sample_size = unique( sample_size ),
design_weight = unique( design_weight ) )
sum( rows_to_add$to_add ) # 24,819 non-respondents
rows_to_add$to_add <- round( rows_to_add$to_add ) # to resolve floating-point precision issues
replicated_rows <- rows_to_add[ rep( row.names( rows_to_add ), rows_to_add$to_add ), c( 1, 3:5 ) ]
d_2021_complete <- bind_rows( d_2021, replicated_rows ) # 47,885 individuals in the original sample
```
### 3. Check Calibrated Weights and Proxy Auxiliary Variables
```{r}
survey_design_calibrated <- svydesign( ids = ~ 1,
strata = ~ sampling_strata_region,
weights = ~ calibrated_weight,
fpc = ~ no.of.population,
data = d_2021 )
# we were unable to re-calibrate the weights as recommended (https://cran.r-project.org/web/packages/survey/vignettes/precalibrated.pdf), because the original auxiliary variables used for calibration were not available anymore
svytable( ~ sampling_strata_region, survey_design_calibrated ) # calculate population totals for each sampling stratum
round( summary( d_2021$calibrated_weight ), 2 ) # check the distribution of calibrated weights
round( summary( d_2021$calibrated_weight / d_2021$design_weight ), 2 ) # check the ratios of weights
ggplot( d_2021, aes( x = calibrated_weight ) ) +
geom_histogram( binwidth = 1, fill = "steelblue", color = "steelblue" ) +
theme_classic() +
scale_x_continuous( breaks = c( 0, 100, 200, 300 ),
expand = c( 0.005, 0 ) ) +
scale_y_continuous( limits = c( 0, 360 ),
breaks = c( 0, 100, 200, 300 ),
expand = c( 0, 0 ) ) +
labs( x = "Calibrated Weights", y = "Frequency" ) +
theme( axis.text.y = element_text( family = "Arial", size = 11 ),
axis.title.y = element_text( family = "Arial", size = 11 ),
axis.text.x = element_text( family = "Arial", size = 11 ),
axis.title.x = element_text( family = "Arial", size = 11 )
)
# calculate strength of correlation between sexual identity in 2021 and proxy auxiliary variables
d_2021_correlation <- d_2021 %>%
rename( country_of_birth_auxiliary = fodelseland2 ) %>%
mutate(
age_auxiliary = factor(
case_when(
age <= 29 ~ "<=29",
age >=30 & age <= 44 ~ "30-44",
age >=45 & age <= 64 ~ "45-64",
age >=65 & age <= 79 ~ "65-79",
age >= 80 ~ ">=80" ),
levels = c( "<=29", "30-44", "45-64", "65-79", ">=80" ) ),
sex_auxiliary = sex,
country_of_birth_auxiliary = as.factor( country_of_birth_auxiliary ),
marital_status_auxiliary = as.factor(
ifelse( marital_status == "Currently married",
"Currently married",
"Other" ) ),
education_auxiliary = education,
sampling_strata_region_auxiliary = sampling_strata_region
)
summary( d_2021_correlation )
assocstats( table( d_2021_correlation$sexual_identity_2021, d_2021_correlation$sex_auxiliary ) )
assocstats( table( d_2021_correlation$sexual_identity_2021, d_2021_correlation$country_of_birth_auxiliary ) )
assocstats( table( d_2021_correlation$sexual_identity_2021, d_2021_correlation$marital_status_auxiliary ) )
assocstats( table( d_2021_correlation$sexual_identity_2021, d_2021_correlation$sampling_strata_region_auxiliary ) )
freemanTheta( table( d_2021_correlation$sexual_identity_2021, d_2021_correlation$age_auxiliary ), group = "row", digits = 2 )
freemanTheta( table( d_2021_correlation[ d_2021_correlation$sex == "Male", ]$sexual_identity_2021, d_2021_correlation[ d_2021_correlation$sex == "Male", ]$age_auxiliary ), group = "row", digits = 2 )
freemanTheta( table( d_2021_correlation[ d_2021_correlation$sex == "Female", ]$sexual_identity_2021, d_2021_correlation[ d_2021_correlation$sex == "Female", ]$age_auxiliary ), group = "row", digits = 2 )
freemanTheta( table( d_2021_correlation$sexual_identity_2021, d_2021_correlation$education_auxiliary ), group = "row", digits = 2 )
# references
freemanTheta( table( d_2021_correlation$age_auxiliary, d_2021_correlation$education_auxiliary ), group = "row", digits = 2 )
freemanTheta( table( d_2021_correlation$age_auxiliary, d_2021_correlation$income ), group = "row", digits = 2 )
freemanTheta( table( d_2021_correlation$education_auxiliary, d_2021_correlation$income ), group = "row", digits = 2 )
```
### 4. Multiple Imputation
#### 4.1. Obtain incomplete dataset
```{r}
summary( d_2021 )
selected_variables <- c( "sampling_strata_region", "calibrated_weight", "no.of.population", "sexual_identity_2021", "age", "sex", "country_of_birth", "education", "dispink2021", "marital_status" )
d_2021_incomplete <- d_2021 %>%
select( all_of( selected_variables ) ) %>%
rename( income = dispink2021 ) %>%
mutate(
country_of_birth = factor(
case_when(
country_of_birth == "Africa" | country_of_birth == "Americas" | country_of_birth == "Asia" ~ "Outside Europe",
country_of_birth == "Sweden" ~ "Sweden",
country_of_birth == "Europe" ~ "Europe" ),
levels = c( "Sweden", "Europe", "Outside Europe" ) )
)
# create weight strata (n = 25) for two-level multivariate normal imputation
d_2021_incomplete$weight_strata <- as.factor( cut( d_2021_incomplete$calibrated_weight,
breaks = quantile( d_2021_incomplete$calibrated_weight,
probs = seq( 0, 1, length.out = 26 ) ),
labels = 1:25,
include.lowest = TRUE,
right = TRUE ) )
ggplot( d_2021_incomplete %>%
group_by( weight_strata ) %>%
summarise( mean_weight = mean( calibrated_weight ), sd_weight = sd( calibrated_weight ) ),
aes( y = reorder( weight_strata, -mean_weight ), x = mean_weight ) ) +
geom_line( aes( group = 1 ) ) +
geom_point() +
geom_errorbarh( aes( xmin = mean_weight - sd_weight, xmax = mean_weight + sd_weight ), height = 0.4, alpha = 0.5 ) +
scale_x_continuous( limits = c( 0, 260 ),
breaks = c( 0, 50, 100, 150, 200, 250 ),
expand = c( 0, 0 ) ) +
labs( y = "Calibrated Weight Strata", x = "Mean and Standard Deviation of Calibrated Weights" ) +
theme_classic() +
theme( axis.text.y = element_text( family = "Arial", size = 11 ),
axis.title.y = element_text( family = "Arial", size = 11 ),
axis.text.x = element_text( family = "Arial", size = 11 ),
axis.title.x = element_text( family = "Arial", size = 11 )
)
summary( d_2021_incomplete )
sapply( d_2021_incomplete, class ) # all continuous variables are numeric, and all categorical variables are factor
# save( d_2021_incomplete, file = "/Volumes/LGBT Project data/Multiple Imputation/d_2021_incomplete.RData" )
```
#### 4.2. Visualize missing pattern
```{r}
# identify missing variables
vis_miss( d_2021_incomplete )
n_miss( d_2021_incomplete )
miss_var_summary( d_2021_incomplete ) # 2.2% missing in sexual_identity_2021, 2.1% in education, and 0.2% in income
```
#### 4.3. Two-level multivariate normal imputation
```{r}
# use multiple imputation to account for item non-response
# assume Missing At Random (MAR)
# specify imputation model
fml_imp_2021 <- sexual_identity_2021 + education + income ~ 1 + age*sex + country_of_birth + marital_status + ( 1 | weight_strata )
# perform a "dry run"
imp_ini_2021 <- jomoImpute( data = d_2021_incomplete, # incomplete dataset
formula = fml_imp_2021, # imputation model
random.L1 = "full", # random covariance matrix of residuals
n.burn = 2, # number of burn-in iterations
n.iter = 10, # number of between-imputation iterations
m = 2, # number of imputation
seed = 12345 # set the seed so that results can be reproduced
)
summary( imp_ini_2021 ) # check output
# increase the number of burn-in and between-imputation iterations
# to determine the number of iterations for the final imputation
imp_preliminary_2021 <- jomoImpute( data = d_2021_incomplete,
formula = fml_imp_2021,
random.L1 = "full",
n.burn = 5000,
n.iter = 1000,
m = 2,
seed = 12345
) # took around 30 minutes
# save( imp_preliminary_2021, file = "/Volumes/LGBT Project data/Multiple Imputation/imp_preliminary_2021.RData" )
summary( imp_preliminary_2021 ) # summarize model and display convergence statistics
plot( imp_preliminary_2021, trace = "all", print = "beta" ) # check trace and autocorrelation plots
# final imputation with the chosen number of iterations
imp_final_2021 <- jomoImpute( data = d_2021_incomplete,
formula = fml_imp_2021,
random.L1 = "full",
n.burn = 2000,
n.iter = 1000,
m = 20,
seed = 12345
) # took around 1 hour and a half
# save( imp_final_2021, file = "/Volumes/LGBT Project data/Multiple Imputation/imp_final_2021.RData" )
summary( imp_final_2021 )
plot( imp_final_2021, trace = "all", print = "beta" )
```
#### 4.4. Validate imputed data
```{r}
# extract imputed datasets
original_data_2021 <- mitmlComplete( imp_final_2021, print = 0 ) # extract original incomplete dataset
implist_2021 <- mitmlComplete( imp_final_2021, print = "all" ) # extract all imputed datasets
original_data_2021$imputation <- "0"
all_data_2021 <- bind_rows( original_data_2021,
bind_rows( implist_2021, .id = "imputation" ) ) # merge datasets
all_data_2021$imputation <- as.numeric( all_data_2021$imputation )
summary( all_data_2021 )
# sexual identity in 2021
ggplot( all_data_2021[ !is.na( all_data_2021$sexual_identity_2021 ), ],
aes( fill = sexual_identity_2021, x = imputation ) ) +
geom_bar( position = "fill" ) +
scale_y_continuous( labels = scales::percent ) +
scale_fill_discrete( name = "Sexual identity in 2021" ) +
labs(
x = "Imputation number",
y = "Proportion",
caption = "Notes: Imputation number 0 represents the original incomplete dataset." ) +
theme_classic() +
theme( axis.title.x = element_text( family = "Arial", size = 11 ),
axis.text.x = element_text( family = "Arial", size = 11 ),
axis.text.y = element_text( family = "Arial", size = 11 ),
axis.title.y = element_text( family = "Arial", size = 11 ),
legend.text = element_text( family = "Arial", size = 10 ),
legend.title = element_text( family = "Arial", size = 10 ),
legend.position = "bottom",
plot.caption = element_text( family = "Arial", size = 10, hjust = 0 )
)
# education
ggplot( all_data_2021[ !is.na( all_data_2021$education ), ],
aes( fill = education, x = imputation ) ) +
geom_bar( position = "fill" ) +
scale_y_continuous( labels = scales::percent ) +
scale_fill_discrete( name = "Level of education" ) +
labs(
x = "Imputation number",
y = "Proportion",
caption = "Notes: Imputation number 0 represents the original incomplete dataset." ) +
theme_classic() +
theme( axis.title.x = element_text( family = "Arial", size = 11 ),
axis.text.x = element_text( family = "Arial", size = 11 ),
axis.text.y = element_text( family = "Arial", size = 11 ),
axis.title.y = element_text( family = "Arial", size = 11 ),
legend.text = element_text( family = "Arial", size = 10 ),
legend.title = element_text( family = "Arial", size = 10 ),
legend.position = "bottom",
plot.caption = element_text( family = "Arial", size = 10, hjust = 0 )
)
# income
summary( all_data_2021$income )
nrow( all_data_2021[ all_data_2021$income < 0 & !is.na( all_data_2021$income ), ] ) # 152 imputed values are negative
```
#### 4.5. Prepare for survey analyses
```{r}
# transform certain variables in imputed datasets
implist_2021 <- mitmlComplete( imp_final_2021, print = "all" ) # extract all imputed datasets
summary( implist_2021[[1]] )
implist_2021_transformed <- lapply( implist_2021, function( df ) {
within( df, {
# age
age_exp <- factor(
case_when( age <= 29 ~ "16-29",
age >= 30 & age <= 44 ~ "30-44",
age >= 45 & age <= 59 ~ "45-59",
age >= 60 ~ ">=60" ),
levels = c( "45-59", "16-29", "30-44", ">=60" ) )
age_conf <- age
# age group
age_group <- cut( age,
breaks = c( seq( 15, 79, by = 2 ), 100 ),
include.lowest = TRUE,
right = TRUE )
# generation
birth_year <- 2021 - age
generation <- factor(
case_when(
birth_year >= 1997 & birth_year <= 2012 ~ "Generation Z (1997–2012)",
birth_year >= 1981 & birth_year <= 1996 ~ "Millennials (1981–1996)",
birth_year >= 1965 & birth_year <= 1980 ~ "Generation X (1965–1980)",
birth_year >= 1946 & birth_year <= 1964 ~ "Baby Boomers (1946–1964)",
birth_year >= 1928 & birth_year <= 1945 ~ "Silent Generation (1928–1945)",
birth_year >= 1901 & birth_year <= 1927 ~ "Greatest Generation (1901–1927)" ),
levels = c( "Generation Z (1997–2012)", "Millennials (1981–1996)", "Generation X (1965–1980)", "Baby Boomers (1946–1964)", "Silent Generation (1928–1945)", "Greatest Generation (1901–1927)" )
)
# sex
sex_exp <- sex
sex_conf <- sex
# interaction term between age and sex
age_sex <- interaction( age_exp, sex_exp )
# interaction term between generation and sex
generation_sex <- interaction( generation, sex_exp )
# interaction term between age group and sex
age_group_sex <- interaction( age_group, sex_exp )
# country of birth
country_of_birth_exp <- country_of_birth
country_of_birth_conf <- country_of_birth
# education
education_exp <- relevel( education, ref = ">=13 years" )
education_conf <- education
# disposable income
income <- ifelse( income < 0, 0, income )
income_exp <- factor(
case_when( income <= 2500 ~ "<=2,500",
income > 2500 & income <= 3500 ~ "(2,500, 3,500]",
income > 3500 & income <= 4500 ~ "(3,500, 4,500]",
income > 4500 ~ ">4,500" ),
levels = c( ">4,500", "<=2,500", "(2,500, 3,500]", "(3,500, 4,500]" ) )
income_conf <- income
# marital status
marital_status_exp <- relevel( marital_status, ref = "Currently married" )
marital_status_conf <- marital_status
# sexual identity in 2021
levels( sexual_identity_2021 )[ levels( sexual_identity_2021 ) == "None of the above" ] <- "Other"
sexual_identity_lgb <- factor(
case_when( sexual_identity_2021 == "Homosexual" | sexual_identity_2021 == "Bisexual" ~ "LGB",
sexual_identity_2021 == "Heterosexual" ~ "Heterosexual",
sexual_identity_2021 == "Other" ~ "Other" ),
levels = c( "Heterosexual", "LGB", "Other" ) )
# sexual minority
sexual_minority <- ifelse( sexual_identity_2021 == "Heterosexual", 0, 1 )
# homosexual
homosexual <- ifelse( sexual_identity_2021 == "Homosexual", 1, 0 )
# bisexual
bisexual <- ifelse( sexual_identity_2021 == "Bisexual", 1, 0 )
# other
other <- ifelse( sexual_identity_2021 == "Other", 1, 0 )
} )
} )
imputed_data_2021_transformed <- bind_rows( implist_2021_transformed, .id = "imputation" ) # merge datasets
imputed_data_2021_transformed$imputation <- as.numeric( imputed_data_2021_transformed$imputation )
summary( imputed_data_2021_transformed )
n_miss( imputed_data_2021_transformed ) # 0 missing
# use calibrated weights to account for unit non-response
# create survey design
survey_design_imp <- svydesign( ids = ~ 1,
strata = ~ sampling_strata_region,
weights = ~ calibrated_weight,
fpc = ~ no.of.population,
data = imputationList( implist_2021_transformed )
) # without pre-calibration
```
### 5. Estimation of Proportions of Sexual Identities
#### 5.1. Complete-case analysis
##### 5.1.1. Define survey design
```{r}
# prepare dataset
d_2021_complete_cc <- d_2021_complete
d_2021_complete_cc$country_of_birth <- factor(
ifelse( d_2021_complete_cc$country_of_birth == "Sweden", "Sweden",
ifelse( d_2021_complete_cc$country_of_birth == "Europe", "Europe", "Outside Europe" ) ),
levels = c( "Sweden", "Europe", "Outside Europe" )
)
# create survey design
survey_design_cc <- svydesign( ids = ~ 1,
strata = ~ sampling_strata_region,
weights = ~ design_weight,
fpc = ~ no.of.population,
data = d_2021_complete_cc )
categories <- c( "Heterosexual", "Homosexual", "Bisexual", "None of the above" )
# the following analyses assume MCAR
```
##### 5.1.2. By sex
```{r}
list_of_df <- list()
for ( cat in categories ) {
prop_cc_sex <- svyby( formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ sex,
design = subset( survey_design_cc, !is.na( sexual_identity_2021 ) & !is.na( sex ) ),
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( prop_cc_sex ) <- c( "subgroup", paste0( cat, "_point_estimate_2021" ), paste0( cat, "_lower_ci_2021" ), paste0( cat, "_upper_ci_2021" ) )
list_of_df[[cat]] <- prop_cc_sex
}
prop_cc_sex <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df )
prop_cc_sex <- left_join( prop_cc_sex,
d_2021_complete_cc[ !is.na( d_2021_complete_cc$sexual_identity_2021 ) &
!is.na( d_2021_complete_cc$sex ), ] %>%
group_by( subgroup = sex ) %>%
summarise( sample_size_2021 = n() ),
by = "subgroup" ) %>%
mutate( sample_size_2021 = prettyNum( sample_size_2021, big.mark = ",", preserve.width = "none" ) )
```
##### 5.1.3. By age group
###### 5.1.3.1. Calculate proportion by pre-specified age cut-off
```{r}
list_of_df <- list()
for ( cat in categories ) {
prop_cc_age <- svyby( formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ age_cat,
design = subset( survey_design_cc, !is.na( sexual_identity_2021 ) & !is.na( age_cat ) ),
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( prop_cc_age ) <- c( "subgroup", paste0( cat, "_point_estimate_2021" ), paste0( cat, "_lower_ci_2021" ), paste0( cat, "_upper_ci_2021" ) )
list_of_df[[cat]] <- prop_cc_age
}
prop_cc_age <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df )
prop_cc_age <- left_join( prop_cc_age,
d_2021_complete_cc[ !is.na( d_2021_complete_cc$sexual_identity_2021 ) &
!is.na( d_2021_complete_cc$age_cat ), ] %>%
group_by( subgroup = age_cat ) %>%
summarise( sample_size_2021 = n() ),
by = "subgroup" ) %>%
mutate( sample_size_2021 = prettyNum( sample_size_2021, big.mark = ",", preserve.width = "none" ) )
```
###### 5.1.3.2. Calculate proportion and proportion ratio by varying age cut-offs
```{r}
d_2021_iteration_age <- d_2021_complete
min_age <- min( d_2021_iteration_age$age, na.rm = TRUE )
results_df <- data.frame()
for ( cut_off in ( min_age ):100 ) {
d_2021_iteration_age$age_iteration <- ifelse( d_2021_iteration_age$age <= cut_off, "young", "old" )
survey_design_iteration_age <- svydesign( ids = ~ 1,
strata = ~ sampling_strata_region,
weights = ~ design_weight,
fpc = ~ no.of.population,
data = d_2021_iteration_age )
list_of_df <- list()
for ( cat in categories ) {
prop_cc_age_iteration <- svyby(
formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ age_iteration,
design = subset( survey_design_iteration_age, !is.na( sexual_identity_2021 ) & !is.na( age ) ),
FUN = svyciprop,
method = "beta" )
colnames( prop_cc_age_iteration ) <- c( "subgroup",
paste0( cat, "_point_estimate_2021" ),
paste0( cat, "_se_2021" ) )
list_of_df[[cat]] <- prop_cc_age_iteration
}
prop_cc_age_iteration_merged <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df
)
prop_cc_age_iteration_merged$cut_off <- cut_off
results_df <- rbind( results_df, prop_cc_age_iteration_merged )
}
results_wide <- results_df %>%
select( -ends_with( "_se_2021" ) ) %>%
pivot_wider( names_from = subgroup,
values_from = c( Heterosexual_point_estimate_2021,
Homosexual_point_estimate_2021,
Bisexual_point_estimate_2021,
`None of the above_point_estimate_2021` )
)
colnames( results_wide ) <- gsub( "_point_estimate_2021", "", colnames( results_wide ) )
results_wide$Heterosexual_pr_2021 <- results_wide$Heterosexual_young/results_wide$Heterosexual_old
results_wide$Homosexual_pr_2021 <- results_wide$Homosexual_young/results_wide$Homosexual_old
results_wide$Bisexual_pr_2021 <- results_wide$Bisexual_young/results_wide$Bisexual_old
results_wide$`None of the above_pr_2021` <- results_wide$`None of the above_young`/results_wide$`None of the above_old`
results_wide$Heterosexual_pr_2021[ is.infinite( results_wide$Heterosexual_pr_2021 ) ] <- NA
results_wide$Homosexual_pr_2021[ is.infinite( results_wide$Homosexual_pr_2021 ) ] <- NA
results_wide$Bisexual_pr_2021[ is.infinite( results_wide$Bisexual_pr_2021 ) ] <- NA
results_wide$`None of the above_pr_2021`[ is.infinite( results_wide$`None of the above_pr_2021` ) ] <- NA
writexl::write_xlsx( results_wide, "prop_cc_age_iteration_2021.xlsx" )
```
###### 5.1.3.3. Calculate proportion by generation
```{r}
list_of_df <- list()
for ( cat in categories ) {
prop_cc_generation_sex <- svyby( formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ generation_sex,
design = subset( survey_design_cc, !is.na( sexual_identity_2021 ) & !is.na( generation_sex ) ),
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( prop_cc_generation_sex ) <- c( "subgroup", paste0( cat, "_point_estimate_2021" ), paste0( cat, "_lower_ci_2021" ), paste0( cat, "_upper_ci_2021" ) )
list_of_df[[cat]] <- prop_cc_generation_sex
}
prop_cc_generation_sex <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df )
prop_cc_generation_sex <- left_join( prop_cc_generation_sex,
d_2021_complete_cc[ !is.na( d_2021_complete_cc$sexual_identity_2021 ) &
!is.na( d_2021_complete_cc$generation_sex ), ] %>%
group_by( subgroup = generation_sex ) %>%
summarise( sample_size_2021 = n() ),
by = "subgroup" ) %>%
mutate( sample_size_2021 = prettyNum( sample_size_2021, big.mark = ",", preserve.width = "none" ) )
```
###### 5.1.3.4. Calculate proportion by age groups
```{r}
d_2021_iteration_age_group <- d_2021_complete
d_2021_iteration_age_group$age_group <- cut( d_2021_iteration_age_group$age,
breaks = seq( 15, 79, by = 2 ),
include.lowest = TRUE,
right = TRUE )
table( d_2021_iteration_age_group$age_group )
d_2021_iteration_age_group$age_group_sex <- interaction( d_2021_iteration_age_group$age_group, d_2021_iteration_age_group$sex )
table( d_2021_iteration_age_group$age_group_sex, useNA = "always" )
survey_design_iteration_age_group <- svydesign( ids = ~ 1,
strata = ~ sampling_strata_region,
weights = ~ design_weight,
fpc = ~ no.of.population,
data = d_2021_iteration_age_group )
categories <- c( "Heterosexual", "Homosexual", "Bisexual", "None of the above" )
list_of_df <- list()
for ( cat in categories ) {
prop_cc_age_group_sex <- svyby( formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ age_group_sex,
design = subset( survey_design_iteration_age_group,
!is.na( sexual_identity_2021 ) & !is.na( age_group_sex ) ),
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( prop_cc_age_group_sex ) <- c( "subgroup", paste0( cat, "_point_estimate_2021" ), paste0( cat, "_lower_ci_2021" ), paste0( cat, "_upper_ci_2021" ) )
list_of_df[[cat]] <- prop_cc_age_group_sex
}
prop_cc_age_group_sex <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df )
writexl::write_xlsx( prop_cc_age_group_sex, "prop_cc_age_group_sex_2021.xlsx" )
```
##### 5.1.4. By sex*age group
```{r}
list_of_df <- list()
for ( cat in categories ) {
prop_cc_age_sex <- svyby( formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ age_sex,
design = subset( survey_design_cc, !is.na( sexual_identity_2021 ) & !is.na( age_sex ) ),
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( prop_cc_age_sex ) <- c( "subgroup", paste0( cat, "_point_estimate_2021" ), paste0( cat, "_lower_ci_2021" ), paste0( cat, "_upper_ci_2021" ) )
list_of_df[[cat]] <- prop_cc_age_sex
}
prop_cc_age_sex <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df )
prop_cc_age_sex <- left_join( prop_cc_age_sex,
d_2021_complete_cc[ !is.na( d_2021_complete_cc$sexual_identity_2021 ) &
!is.na( d_2021_complete_cc$age_sex ), ] %>%
group_by( subgroup = age_sex ) %>%
summarise( sample_size_2021 = n() ),
by = "subgroup" ) %>%
mutate( sample_size_2021 = prettyNum( sample_size_2021, big.mark = ",", preserve.width = "none" ) )
```
##### 5.1.5. By education
```{r}
list_of_df <- list()
for ( cat in categories ) {
prop_cc_education <- svyby( formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ education,
design = subset( survey_design_cc, !is.na( sexual_identity_2021 ) & !is.na( education ) ),
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( prop_cc_education ) <- c( "subgroup", paste0( cat, "_point_estimate_2021" ), paste0( cat, "_lower_ci_2021" ), paste0( cat, "_upper_ci_2021" ) )
list_of_df[[cat]] <- prop_cc_education
}
prop_cc_education <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df )
prop_cc_education <- left_join( prop_cc_education,
d_2021_complete_cc[ !is.na( d_2021_complete_cc$sexual_identity_2021 ) &
!is.na( d_2021_complete_cc$education ), ] %>%
group_by( subgroup = education ) %>%
summarise( sample_size_2021 = n() ),
by = "subgroup" ) %>%
mutate( sample_size_2021 = prettyNum( sample_size_2021, big.mark = ",", preserve.width = "none" ) )
```
##### 5.1.6. By occupation
```{r}
list_of_df <- list()
for ( cat in categories ) {
prop_cc_occupation <- svyby( formula = as.formula( paste0( "~ I( sexual_identity_2021 == '", cat, "' )" ) ),
by = ~ occupation,
design = subset( survey_design_cc, !is.na( sexual_identity_2021 ) & !is.na( occupation ) ),
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( prop_cc_occupation ) <- c( "subgroup", paste0( cat, "_point_estimate_2021" ), paste0( cat, "_lower_ci_2021" ), paste0( cat, "_upper_ci_2021" ) )
list_of_df[[cat]] <- prop_cc_occupation
}
prop_cc_occupation <- Reduce( function( df1, df2 ) {
merge( df1, df2, by = "subgroup" )
},
list_of_df )
prop_cc_occupation <- left_join( prop_cc_occupation,
d_2021_complete_cc[ !is.na( d_2021_complete_cc$sexual_identity_2021 ) &
!is.na( d_2021_complete_cc$occupation ), ] %>%
group_by( subgroup = occupation ) %>%
summarise( sample_size_2021 = n() ),
by = "subgroup" ) %>%