-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPHC-2010.Rmd
1702 lines (1367 loc) · 68.7 KB
/
SPHC-2010.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: "Sexual identity and health outcomes in Stockholm County, SPHC 2010"
author: Guoqiang Zhang
email: [email protected]
output: html_notebook
editor_options:
chunk_output_type: console
---
### 1. Load Packages
```{r}
library(haven)
library(tidyr)
library(tidyverse)
library(dplyr)
library(finalfit)
library(stringr)
library(ggplot2)
library(naniar)
library(jomo)
library(mitml)
library(mitools)
library(survey)
library(svyVGAM)
library(vcd)
library(rcompanion)
source("/Users/guoqiang.zhang/Library/CloudStorage/OneDrive-KarolinskaInstitutet/Karolinska Institutet/Research Projects/Sexual Identity and Health Outcomes in Stockholm County/Sexual_identity_and_health_outcomes_in_Stockholm_County/Helper_functions.R") # helper function
```
### 2. Import and Prepare Data
```{r}
load("/Volumes/LGBT Project data/d_2010.RData")
# sampling strata
n_miss( d_2010$stratum )
d_2010$sampling_strata <- as.factor( d_2010$stratum )
length( unique( d_2010$sampling_strata ) ) # 39 strata
d_2010 <- d_2010 %>%
mutate( sampling_strata_region = recode( sampling_strata,
`2101` = "Upplands-Väsby",
`2102` = "Vallentuna",
`2103` = "Österåker",
`2104` = "Värmdö",
`2105` = "Järfälla",
`2106` = "Ekerö",
`2107` = "Huddinge",
`2108` = "Botkyrka",
`2109` = "Salem",
`2110` = "Haninge",
`2111` = "Tyresö",
`2112` = "Upplands-Bro",
`2113` = "Nykvarn",
`2114` = "Täby",
`2115` = "Danderyd",
`2116` = "Sollentuna",
`2117` = "Södertälje",
`2118` = "Nacka",
`2119` = "Sundbyberg",
`2120` = "Solna",
`2121` = "Lidingö",
`2122` = "Vaxholm",
`2123` = "Norrtälje",
`2124` = "Sigtuna",
`2125` = "Nynäshamn",
`2201` = "Rinkeby-Kista",
`2202` = "Spånga-Tensta",
`2203` = "Hässelby-Vällingby",
`2204` = "Bromma",
`2205` = "Kungsholmen",
`2206` = "Norrmalm",
`2207` = "Östermalm",
`2208` = "Södermalm",
`2209` = "Enskede-Årsta-Vantör",
`2210` = "Skarpnäck",
`2211` = "Farsta",
`2212` = "Älvsjö",
`2213` = "Hägersten-Liljeholmen",
`2214` = "Skärholmen"
) )
# sexual identity in 2010
table( d_2010$F10U87G78, useNA = "always" )
d_2010$F10U87G78[ d_2010$F10U87G78 == 9 ] <- NA
d_2010$sexual_identity_2010 <- factor( ifelse( d_2010$F10U87G78 == 1, "Heterosexual",
ifelse( d_2010$F10U87G78 == 2, "Homosexual",
ifelse( d_2010$F10U87G78 == 3, "Bisexual", "Uncertain" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Uncertain" ) )
table( d_2010$sexual_identity_2010, useNA = "always" )
# sexual identity in 2014
table( d_2010$F14F103, useNA = "always" )
d_2010$sexual_identity_2014 <- factor( ifelse( d_2010$F14F103 == 1, "Heterosexual",
ifelse( d_2010$F14F103 == 2, "Homosexual",
ifelse( d_2010$F14F103 == 3, "Bisexual", "None of the above" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "None of the above" ) )
table( d_2010$sexual_identity_2014, useNA = "always" )
# sexual identity in 2021
table( d_2010$F21F91, useNA = "always" )
d_2010$sexual_identity_2021 <- factor( ifelse( d_2010$F21F91 == 1, "Heterosexual",
ifelse( d_2010$F21F91 == 2, "Homosexual",
ifelse( d_2010$F21F91 == 3, "Bisexual", "None of the above" ) ) ),
levels = c( "Heterosexual", "Homosexual", "Bisexual", "None of the above" ) )
table( d_2010$sexual_identity_2021, useNA = "always" )
# any change in sexual identity in 2010-2021
d_2010 <- d_2010 %>%
mutate(
sexual_identity_fluidity = ifelse(
replace( as.character( sexual_identity_2010 ), sexual_identity_2010 == "Uncertain", "Other" ) !=
replace( as.character( sexual_identity_2014 ), sexual_identity_2014 == "None of the above", "Other" ) |
replace( as.character( sexual_identity_2010 ), sexual_identity_2010 == "Uncertain", "Other" ) !=
replace( as.character( sexual_identity_2021 ), sexual_identity_2021 == "None of the above", "Other" ) |
replace( as.character( sexual_identity_2014 ), sexual_identity_2014 == "None of the above", "Other" ) !=
replace( as.character( sexual_identity_2021 ), sexual_identity_2021 == "None of the above", "Other" ),
1,
0
)
)
# sex
table( d_2010$kon, useNA = "always" )
d_2010$sex <- factor( ifelse( d_2010$kon == 1, "Male", "Female" ),
levels = c( "Male", "Female" ) )
table( d_2010$sex, useNA = "always" )
# age
summary( d_2010$F10alder )
d_2010$age <- d_2010$F10alder
d_2010$age_cat <- factor( ifelse( d_2010$age <= 29, "18-29",
ifelse( d_2010$age >=30 & d_2010$age <= 44, "30-44",
ifelse( d_2010$age >= 45 & d_2010$age <= 59, "45-59", ">=60" ) ) ),
levels = c( "18-29", "30-44", "45-59", ">=60" ) )
table( d_2010$age_cat, useNA = "always" )
# interaction term between age and sex
d_2010$age_sex <- interaction( d_2010$age_cat, d_2010$sex )
# generation
d_2010$birth_year <- 2010 - d_2010$age
d_2010 <- d_2010 %>%
mutate( generation = case_when(
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_2010$generation <- factor(
d_2010$generation,
levels = c( "Millennials (1981–1996)", "Generation X (1965–1980)", "Baby Boomers (1946–1964)", "Silent Generation (1928–1945)", "Greatest Generation (1901–1927)" ) )
summary( d_2010$generation )
# country of birth
table( d_2010$fodelseland, useNA = "always" )
d_2010$country_of_birth <- factor( ifelse( d_2010$fodelseland == "Sverige", "Sweden",
ifelse( d_2010$fodelseland == "Europa", "Europe", "Outside Europe" ) ),
levels = c( "Sweden", "Europe", "Outside Europe" ) )
table( d_2010$country_of_birth, useNA = "always" )
# education
table( d_2010$utbniva2010, useNA = "always" )
d_2010$education <- factor( ifelse( d_2010$utbniva2010 <= 2, "<=9 years",
ifelse( d_2010$utbniva2010 <= 4, "10-12 years", ">=13 years" ) ),
levels = c( "<=9 years", "10-12 years", ">=13 years" ) )
table( d_2010$education, useNA = "always" )
# occupation
table( d_2010$SSYK_kl, useNA = "always" )
d_2010$occupation <- factor(
ifelse(
d_2010$SSYK_kl == "Yrken inom byggverksamhet och tillverkning" |
d_2010$SSYK_kl == "Yrken inom lantbruk, trädgård, skogsbruk och fiske" |
d_2010$SSYK_kl == "Yrken inom maskinell tillverkning och transport m.m.",
"Manual and field trades",
ifelse(
d_2010$SSYK_kl == "Service-, omsorgs- och försäljningsyrken" |
d_2010$SSYK_kl == "Yrken inom administration och kundtjänst" |
d_2010$SSYK_kl == "Yrken med krav på kortare utbildning eller introduktion",
"Service and support",
ifelse(
d_2010$SSYK_kl == "Yrken med krav på fördjupad högskolekompetens" |
d_2010$SSYK_kl == "Yrken med krav på högskolekompetens eller motsvarande" |
d_2010$SSYK_kl == "Chefsyrken" |
d_2010$SSYK_kl == "Militära yrken",
"Expertise and leadership",
NA
)
)
),
levels = c(
"Manual and field trades",
"Service and support",
"Expertise and leadership"
)
)
table( d_2010$occupation, useNA = "always" )
# disposable income
summary( d_2010$dispink2010, useNA = "always" )
d_2010$dispink2010 <- d_2010$dispink2010*( 1960/1733 ) # Consumer Price Index (CPI)-adjusted income
d_2010$income <- factor( ifelse( d_2010$dispink2010 <= 2500, "<=2,500",
ifelse( d_2010$dispink2010 > 2500 & d_2010$dispink2010 <= 3500, "(2,500, 3,500]",
ifelse( d_2010$dispink2010 > 3500 & d_2010$dispink2010 <= 4500, "(3,500, 4,500]", ">4,500" ) ) ),
levels = c( "<=2,500", "(2,500, 3,500]", "(3,500, 4,500]", ">4,500" ) )
table( d_2010$income, useNA = "always" )
# marital status
table( d_2010$civil2010, useNA = "always" )
d_2010$marital_status <- factor( ifelse( d_2010$civil2010 == "G" | d_2010$civil2010 == "RP", "Currently married",
ifelse( d_2010$civil2010 == "OG", "Never married", "Other" ) ),
levels = c( "Never married", "Currently married", "Other" ) )
table( d_2010$marital_status, useNA = "always" )
# living alone
table( d_2010$F10U53aG57a, useNA = "always" )
d_2010$F10U53aG57a[ d_2010$F10U53aG57a == 9 ] <- NA
d_2010$living_alone <- factor( ifelse( d_2010$F10U53aG57a == 1, "living_alone_no", "living_alone_yes" ),
levels = c( "living_alone_yes", "living_alone_no" ) )
table( d_2010$living_alone, useNA = "always" )
# personal support
table( d_2010$F10U57G62, useNA = "always" )
d_2010$F10U57G62[ d_2010$F10U57G62 == 9 ] <- NA
d_2010$personal_support <- factor( ifelse( d_2010$F10U57G62 <= 2, "personal_support_yes", "personal_support_no" ),
levels = c( "personal_support_yes", "personal_support_no" ) )
table( d_2010$personal_support, useNA = "always" )
##### demographic characteristics #####
# among respondents (n = 30,767)
# make characteristics table by sexual identity
explanatory = c( "sex", "age_cat", "country_of_birth", "education", "occupation", "income", "marital_status", "living_alone", "personal_support" )
dependent = "sexual_identity_2010"
d_2010_table_1 <- d_2010 %>%
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_2010$sex, d_2010$sexual_identity_2010 )
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_2010$age_cat, d_2010$sexual_identity_2010 )
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 ) ] )$p.value, 3 ), nsmall = 3 )
x3 <- table( d_2010$country_of_birth, d_2010$sexual_identity_2010 )
x3
format( round( fisher.test( x3[, c( 1, 2 ) ], simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x3[, c( 1, 3 ) ], simulate.p.value = TRUE )$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_2010$education, d_2010$sexual_identity_2010 )
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 ) ] )$p.value, 3 ), nsmall = 3 )
x5 <- table( d_2010$income, d_2010$sexual_identity_2010 )
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_2010$marital_status, d_2010$sexual_identity_2010 )
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_2010$living_alone, d_2010$sexual_identity_2010 )
x7
format( round( fisher.test( x7[, c( 1, 2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x7[, c( 1, 3 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x7[, c( 1, 4 ) ] )$p.value, 3 ), nsmall = 3 )
x8 <- table( d_2010$personal_support, d_2010$sexual_identity_2010 )
x8
format( round( fisher.test( x8[, c( 1, 2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x8[, c( 1, 3 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x8[, c( 1, 4 ) ] )$p.value, 3 ), nsmall = 3 )
x9 <- table( d_2010$occupation, d_2010$sexual_identity_2010 )
x9
format( round( fisher.test( x9[, c( 1, 2 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x9[, c( 1, 3 ) ] )$p.value, 3 ), nsmall = 3 )
format( round( fisher.test( x9[, c( 1, 4 ) ] )$p.value, 3 ), nsmall = 3 )
# make characteristics table by change of sexual identity
explanatory = c( "sex", "age_cat", "country_of_birth", "education", "occupation", "income", "marital_status", "living_alone", "personal_support", "sexual_identity_2010" )
dependent = "sexual_identity_fluidity"
d_2010_table_2 <- d_2010 %>%
mutate( sexual_identity_fluidity = factor( sexual_identity_fluidity, levels = c( 0, 1 ), labels = c( "no change", "change" ) ) ) %>%
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_2010$sex, d_2010$sexual_identity_fluidity )
x1
format( round( fisher.test( x1 )$p.value, 3 ), nsmall = 3 )
x2 <- table( d_2010$age_cat, d_2010$sexual_identity_fluidity )
x2
format( round( fisher.test( x2, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x3 <- table( d_2010$country_of_birth, d_2010$sexual_identity_fluidity )
x3
format( round( fisher.test( x3, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x4 <- table( d_2010$education, d_2010$sexual_identity_fluidity )
x4
format( round( fisher.test( x4, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x5 <- table( d_2010$occupation, d_2010$sexual_identity_fluidity )
x5
format( round( fisher.test( x5, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x6 <- table( d_2010$income, d_2010$sexual_identity_fluidity )
x6
format( round( fisher.test( x6, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x7 <- table( d_2010$marital_status, d_2010$sexual_identity_fluidity )
x7
format( round( fisher.test( x7, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x8 <- table( d_2010$living_alone, d_2010$sexual_identity_fluidity )
x8
format( round( fisher.test( x8, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x9 <- table( d_2010$personal_support, d_2010$sexual_identity_fluidity )
x9
format( round( fisher.test( x9, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
x10 <- table( d_2010$sexual_identity_2010, d_2010$sexual_identity_fluidity )
x10
format( round( fisher.test( x10, simulate.p.value = TRUE )$p.value, 3 ), nsmall = 3 )
##### design weights #####
summary( d_2010$F10dvikt )
d_2010$design_weight <- d_2010$F10dvikt
##### calibrated weights #####
d_2010$calibrated_weight <- d_2010$F10kalvikt
##### non-response #####
# unit non-response
d_2010$design_weight_unit_nonresponse <- d_2010$F10dbvikt # weights calculated assuming Missing Completely At Random (MCAR) within each stratum
summary( d_2010$design_weight_unit_nonresponse )
sum( d_2010$design_weight_unit_nonresponse ) # No. of source population = 1,602,868
unitresponse_prob <- d_2010 %>%
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_2010 <- d_2010 %>%
left_join( unitresponse_prob, by = "sampling_strata_region" )
# item non-response
itemresponse_prob <- d_2010 %>%
group_by( sampling_strata_region ) %>%
summarise( itemresponse_prob = sum( !is.na( sexual_identity_2010 ) ) / n() ) # calculate item response rate
d_2010 <- d_2010 %>%
left_join( itemresponse_prob, by = "sampling_strata_region" ) %>%
mutate( itemresponse_prob = ifelse( is.na( sexual_identity_2010 ), 0, itemresponse_prob ) )
##### summary of stratified sampling #####
sampling_frame_2010 <- as.data.frame( d_2010 %>%
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_2010$unitresponse_rate <- sampling_frame_2010$unitresponse/sampling_frame_2010$sample_size
sampling_frame_2010$itemresponse_rate <- sampling_frame_2010$itemresponse/sampling_frame_2010$unitresponse
sampling_frame_2010$overallresponse_rate <- sampling_frame_2010$itemresponse/sampling_frame_2010$sample_size
sampling_frame_2010$unitresponse_label <- paste0( sampling_frame_2010$unitresponse, " (",
sprintf( "%.1f", sampling_frame_2010$unitresponse_rate*100 ), "%)" )
sampling_frame_2010$overallresponse_label <- paste0( sampling_frame_2010$itemresponse, " (",
sprintf( "%.1f", sampling_frame_2010$overallresponse_rate*100 ), "%)" )
round( sum( sampling_frame_2010$unitresponse )/( sum( sampling_frame_2010$sample_size ) ), 3 ) # overall unit response rate
round( sum( sampling_frame_2010$itemresponse )/( sum( sampling_frame_2010$unitresponse ) ), 3 ) # overall item response rate
round( sum( sampling_frame_2010$itemresponse )/( sum( sampling_frame_2010$sample_size ) ), 3 ) # overall response rate
writexl::write_xlsx( sampling_frame_2010, "sampling_frame_2010.xlsx" )
##### replicating non-respondent rows for sampling design #####
# because d_2010 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_2010 %>%
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 ) )
rows_to_add$to_add <- round( rows_to_add$to_add )
sum( rows_to_add$to_add ) # 24,573 non-respondents
replicated_rows <- rows_to_add[ rep( row.names( rows_to_add ), rows_to_add$to_add ), c( 1, 3:5 ) ]
d_2010_complete <- bind_rows( d_2010, replicated_rows ) # 55,340 individuals in the original sample
```
### 3. Self-Reported Health Outcomes
#### 3.1. Complete-case analysis
##### 3.1.1. Prepare dataset
```{r}
##### mental health outcomes #####
# recent psychological distress
summary( d_2010_complete$F10GHQ12 )
d_2010_complete$psychological_distress_continuous <- d_2010_complete$F10GHQ12
d_2010_complete$psychological_distress_binary <- factor( ifelse( d_2010_complete$F10GHQ12 < 3, "No", "Yes" ),
levels = c( "Yes", "No" ) ) # psychological distress defined as a score >= 3
table( d_2010_complete$psychological_distress_binary, useNA = "always" )
# self-reported doctor-diagnosed depression
table( d_2010_complete$F10U16G19, useNA = "always" )
d_2010_complete$F10U16G19[ d_2010_complete$F10U16G19 == 9 ] <- NA
d_2010_complete$depression_self_report <- factor( ifelse( d_2010_complete$F10U16G19 == 1, "Never", "Ever" ),
levels = c( "Never", "Ever" ) )
table( d_2010_complete$depression_self_report, useNA = "always" )
# suicidal thoughts
table( d_2010_complete$F10U51G55, useNA = "always" )
d_2010_complete$F10U51G55[ d_2010_complete$F10U51G55 == 9 ] <- NA
d_2010_complete$suicidal_thoughts <- factor(
case_when(
d_2010_complete$F10U51G55 == 1 ~ "Never",
d_2010_complete$F10U51G55 %in% c( 2, 3, 4 ) ~ "Ever"
),
levels = c( "Never", "Ever" ) )
table( d_2010_complete$suicidal_thoughts, useNA = "always" )
# suicidal attempts
table( d_2010_complete$F10U52G56, useNA = "always" )
d_2010_complete$F10U52G56[ d_2010_complete$F10U52G56 == 9 ] <- NA
d_2010_complete$suicidal_attempts <- factor(
case_when(
d_2010_complete$F10U52G56 == 1 ~ "Never",
d_2010_complete$F10U52G56 %in% c( 2, 3, 4 ) ~ "Ever"
),
levels = c( "Never", "Ever" ) )
table( d_2010_complete$suicidal_attempts, useNA = "always" )
```
```{r}
##### physical health outcomes #####
```
##### 3.1.2. Calculate prevalence by sexual identity
```{r}
# create survey design
d_2010_complete_cc <- d_2010_complete
survey_design_cc <- svydesign( ids = ~ 1,
strata = ~ sampling_strata_region,
weights = ~ design_weight,
fpc = ~ no.of.population,
data = d_2010_complete_cc )
# the following analyses assume MCAR
variables_list <- list(
list( variable = "psychological_distress_binary", condition = "Yes", name = "psychological_distress" ),
list( variable = "depression_self_report", condition = "Ever", name = "depression_self_report" ),
list( variable = "suicidal_thoughts", condition = "Ever", name = "suicidal_thoughts" ),
list( variable = "suicidal_attempts", condition = "Ever", name = "suicidal_attempts" )
)
# by sexual identity
results_list <- calculate_svyby_proportion( variables_list = variables_list,
design = survey_design_cc,
year = 2010,
group_var = "sexual_identity_2010" )
self_report_health_outcomes_2010 <- Reduce( function( x, y ){
merge( x, y, by = "sexual_identity_2010" )
},
results_list )
# overall sample
results_list_overall <- calculate_svyby_proportion_overall(
variables_list = variables_list,
design = survey_design_cc,
year = 2010 )
self_report_health_outcomes_2010_overall <- Reduce( function( x, y ){
cbind( x, y )
},
results_list_overall ) %>%
as.data.frame() %>%
rownames_to_column( var = "sexual_identity_2010" )
self_report_health_outcomes_2010_overall[ 1, 1 ] <- "Stockholm County"
writexl::write_xlsx( rbind( self_report_health_outcomes_2010, self_report_health_outcomes_2010_overall ), "prevalence_cc_self_report_health_outcomes_2010.xlsx" )
# by sex
results_list_by_sex <- calculate_svyby_proportion_subgroup(
variables_list = variables_list,
design = survey_design_cc,
year = 2010,
group_var = "sexual_identity_2010 + sex" )
self_report_health_outcomes_2010_by_sex <- Reduce( function( x, y ){
merge( x, y, by = c( "sexual_identity_2010", "sex" ) )
},
results_list_by_sex )
writexl::write_xlsx( self_report_health_outcomes_2010_by_sex, "prevalence_cc_self_report_health_outcomes_2010_by_sex.xlsx" )
# by age
results_list_by_age <- calculate_svyby_proportion_subgroup(
variables_list = variables_list,
design = survey_design_cc,
year = 2010,
group_var = "sexual_identity_2010 + age_cat" )
self_report_health_outcomes_2010_by_age <- Reduce( function( x, y ){
merge( x, y, by = c( "sexual_identity_2010", "age_cat" ) )
},
results_list_by_age )
writexl::write_xlsx( self_report_health_outcomes_2010_by_age, "prevalence_cc_self_report_health_outcomes_2010_by_age.xlsx" )
# by country of birth
results_list_by_country_of_birth <- calculate_svyby_proportion_subgroup(
variables_list = variables_list,
design = survey_design_cc,
year = 2010,
group_var = "sexual_identity_2010 + country_of_birth" )
self_report_health_outcomes_2010_by_country_of_birth <- Reduce( function( x, y ){
merge( x, y, by = c( "sexual_identity_2010", "country_of_birth" ) )
},
results_list_by_country_of_birth )
writexl::write_xlsx( self_report_health_outcomes_2010_by_country_of_birth, "prevalence_cc_self_report_health_outcomes_2010_by_country_of_birth.xlsx" )
```
### 4. Registry-Based Health Outcomes
#### 4.1. Prescribed Drug Register
##### 4.1.1. Complete-case analysis
###### 4.1.1.1. Prepare dataset
```{r}
pdr_regitry <- read_sas("/Volumes/LGBT Project data/Mental health outcomes/atc_2002_2014.sas7bdat") # include participants who HAD at least one prescription record of N05B or N06A from 2005-06-30 to 2021-12-31
d_pdr_regitry <- pdr_regitry %>%
filter( ATC != "N05CD" ) %>%
mutate(
presence_N06A = if_else( grepl( "N06A", ATC ), "Yes", "No" ),
presence_N05B = if_else( grepl( "N05B", ATC ), "Yes", "No" ),
year = year( EDATUM )
)
n_miss( d_pdr_regitry ) # 0
unique( d_pdr_regitry$ATC )
summary( d_pdr_regitry$EDATUM ) # from 2005-06-30 to 2021-12-31
summary( d_pdr_regitry$year ) # from 2005 to 2021
count_ATC <- d_pdr_regitry %>%
group_by( lopnr, year ) %>%
summarise(
presence_N06A_Yes = sum( presence_N06A == "Yes" ),
presence_N05B_Yes = sum( presence_N05B == "Yes" ),
.groups = "drop"
) # calculate no. of prescriptions per year
sum( count_ATC$presence_N06A_Yes )
sum( count_ATC$presence_N05B_Yes )
table( d_pdr_regitry$presence_N06A )
table( d_pdr_regitry$presence_N05B ) # all numbers match
count_ATC_wide <- count_ATC %>%
# transform to wide format
pivot_wider(
names_from = year,
values_from = c( presence_N06A_Yes, presence_N05B_Yes ),
names_sep = "_",
values_fill = 0 # NOTE: assume that among participants who HAD at least one prescription record during 2005-2021, those who did not have any prescription during a specific year did not receive a prescription during that year, i.e., assume complete follow-up during 2005-2021 through register
)
ever_ATC <- d_pdr_regitry %>%
group_by( lopnr ) %>%
summarise(
total_N06A = sum( presence_N06A == "Yes" ),
total_N05B = sum( presence_N05B == "Yes" ),
ever_use_N06A = ifelse( any( presence_N06A == "Yes" ), "Yes", "No" ),
ever_use_N05B = ifelse( any( presence_N05B == "Yes" ), "Yes", "No" ),
.groups = "drop"
) # calculate total no. of prescriptions and determine if ever use; the same assumption as above
ATC_data <- full_join( count_ATC_wide, ever_ATC, by = "lopnr" )
n_miss( ATC_data ) # 0
sum( ATC_data$total_N06A )
sum( ATC_data$total_N05B )
table( d_pdr_regitry$presence_N06A )
table( d_pdr_regitry$presence_N05B ) # all numbers match
d_2010_complete_merged <- left_join( d_2010_complete, ATC_data, by = "lopnr" )
inner_join( d_2010_complete, ATC_data, by = "lopnr" ) %>%
summarize( count = n() ) # 12,645 matched
d_2010_complete_merged_cleaned <- d_2010_complete_merged %>%
mutate( across( contains( c( "N06A", "N05B" ) ),
~ ifelse( is.na( lopnr ), .,
ifelse( is.na(.), 0, . ) ) )
) # NOTE: assume that among participants who did NOT have any prescription record during 2005-2021, they did not receive a prescription during the period, i.e., assume complete follow-up during 2005-2021 through register
d_2010_complete_merged_cleaned <- d_2010_complete_merged_cleaned %>%
mutate(
# replace 0 with "No"
ever_use_N05B = ifelse( ever_use_N05B == 0, "No", ever_use_N05B ),
ever_use_N06A = ifelse( ever_use_N06A == 0, "No", ever_use_N06A ),
# create new columns
across(
starts_with( "presence_N06A_Yes_" ),
~ ifelse(. > 0, "Yes", "No" ),
.names = "ever_use_N06A_{str_sub(.col, -4)}"
),
across(
starts_with( "presence_N05B_Yes_" ),
~ ifelse(. > 0, "Yes", "No" ),
.names = "ever_use_N05B_{str_sub(.col, -4)}"
),
# convert to factor
across(
starts_with( "ever_use" ),
~ factor(., levels = c( "Yes", "No" ) )
)
)
summary( d_2010_complete_merged_cleaned[ , ( 60:131 ) ] )
# in summary, the final merged dataset assumes that all participants have complete follow-up through register from 2005 to 2021; therefore, those who do not have any prescription record are assumed not to have a prescription
d_2010_complete_cc <- d_2010_complete_merged_cleaned
# create survey design
survey_design_cc <- svydesign( ids = ~ 1,
strata = ~ sampling_strata_region,
weights = ~ design_weight,
fpc = ~ no.of.population,
data = d_2010_complete_cc )
# the following analyses assume MCAR
```
###### 4.1.1.2. Use of antidepressants and anxiolytics
```{r}
##### among all participants (prevalence) #####
# calculate year-specific prevalence by sexual identity
# antidepressants
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svyby( formula = as.formula( paste0( "~ I(ever_use_N06A_", year, " == 'Yes')" ) ),
by = ~ sexual_identity_2010,
design = survey_design_cc,
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( svyby_result )[-1] <- c( paste0( "ever_use_N06A_", year, "_point_estimate" ),
paste0( "ever_use_N06A_", year, "_lower_ci" ),
paste0( "ever_use_N06A_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N06A <- Reduce( function( x, y ){
merge( x, y, by = "sexual_identity_2010" )
},
results_list )
# anxiolytics
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svyby( formula = as.formula( paste0( "~ I(ever_use_N05B_", year, " == 'Yes')" ) ),
by = ~ sexual_identity_2010,
design = survey_design_cc,
FUN = svyciprop,
vartype = "ci",
method = "beta" )
colnames( svyby_result )[-1] <- c( paste0( "ever_use_N05B_", year, "_point_estimate" ),
paste0( "ever_use_N05B_", year, "_lower_ci" ),
paste0( "ever_use_N05B_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N05B <- Reduce( function( x, y ){
merge( x, y, by = "sexual_identity_2010" )
},
results_list )
# calculate overall year-specific prevalence
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svyciprop( formula = as.formula( paste0( "~ I(ever_use_N06A_", year, " == 'Yes')" ) ), # antidepressants
design = subset( survey_design_cc, !is.na( get( paste0( "ever_use_N06A_", year ) ) ) ),
method = "beta" )
svyby_result <- data.frame( svyby_result[ 1 ], confint( svyby_result )[ 1 ], confint( svyby_result )[ 2 ] )
colnames( svyby_result ) <- c( paste0( "ever_use_N06A_", year, "_point_estimate" ),
paste0( "ever_use_N06A_", year, "_lower_ci" ),
paste0( "ever_use_N06A_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N06A_overall <- Reduce( function( x, y ){
cbind( x, y )
},
results_list ) %>%
as.data.frame() %>%
rownames_to_column( var = "sexual_identity_2010" )
results_N06A_overall[ 1, 1 ] <- "Stockholm County"
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svyciprop( formula = as.formula( paste0( "~ I(ever_use_N05B_", year, " == 'Yes')" ) ), # anxiolytics
design = subset( survey_design_cc, !is.na( get( paste0( "ever_use_N05B_", year ) ) ) ),
vartype = "ci",
method = "beta" )
svyby_result <- data.frame( svyby_result[ 1 ], confint( svyby_result )[ 1 ], confint( svyby_result )[ 2 ] )
colnames( svyby_result ) <- c( paste0( "ever_use_N05B_", year, "_point_estimate" ),
paste0( "ever_use_N05B_", year, "_lower_ci" ),
paste0( "ever_use_N05B_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N05B_overall <- Reduce( function( x, y ){
cbind( x, y )
},
results_list ) %>%
as.data.frame() %>%
rownames_to_column( var = "sexual_identity_2010" )
results_N05B_overall[ 1, 1 ] <- "Stockholm County"
# combine datasets
results_N06A_all <- bind_rows( results_N06A, results_N06A_overall )
results_N05B_all <- bind_rows( results_N05B, results_N05B_overall )
results_combined <- full_join( results_N06A_all, results_N05B_all, by = "sexual_identity_2010" )
str( results_combined )
results_combined_long <- results_combined %>%
pivot_longer(
cols = starts_with( "ever_use_" ),
names_to = c( "drug_class", "year", ".value" ),
names_pattern = "ever_use_(.*)_(\\d{4})_(.*)$" )
str( results_combined_long )
results_combined_long$sexual_identity_2010 <- factor( results_combined_long$sexual_identity_2010,
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Uncertain", "Stockholm County" ) )
results_combined_long$drug_class <- factor( results_combined_long$drug_class, levels = c( "N06A", "N05B" ) )
results_combined_long$year <- as.numeric( results_combined_long$year )
# plot
color_values <- c( "Heterosexual" = "red",
"Homosexual" = "#00BFC4",
"Bisexual" = "#C77CFF",
"Stockholm County" = "grey20" )
ggplot(
results_combined_long %>%
filter( sexual_identity_2010 %in% c( "Heterosexual", "Homosexual", "Bisexual", "Stockholm County" ) ),
aes( x = year, y = point_estimate, color = sexual_identity_2010, group = sexual_identity_2010 ) ) +
geom_line( linewidth = 0.6 ) +
geom_ribbon( aes( ymin = lower_ci, ymax = upper_ci, fill = sexual_identity_2010 ),
alpha = 0.2,
color = NA ) +
scale_x_continuous( breaks = seq( 2005, 2021, by = 4 ),
labels = seq( 2005, 2021, by = 4 ),
expand = c( 0.02, 0 ) ) +
scale_color_manual( values = color_values ) +
scale_fill_manual( values = color_values ) +
labs(
x = "Calendar Year",
y = "Prevalence of Dispensed Prescription"
) +
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_blank(),
legend.position = "bottom",
strip.background = element_blank(),
strip.text = element_text( family = "Arial", size = 12 ),
panel.spacing = unit( 0.5, "lines" ) ) +
facet_wrap( ~ drug_class,
ncol = 1,
scales = "free_y",
labeller = labeller( drug_class = c( "N05B" = "Anxiolytics",
"N06A" = "Antidepressants" ) ) ) +
ggh4x::facetted_pos_scales( y = list(
drug_class == "N05B" ~ scale_y_continuous( labels = scales::percent, limits = c( 0, 0.3 ), breaks = c( 0, 0.1, 0.2, 0.3 ) ),
drug_class == "N06A" ~ scale_y_continuous( labels = scales::percent, limits = c( 0, 0.3 ), breaks = c( 0, 0.1, 0.2, 0.3 ) )
) )
```
```{r}
##### among all participants (number of prescriptions) #####
# calculate average number of prescriptions by sexual identity
# antidepressants
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svyby( formula = as.formula( paste0( "~presence_N06A_Yes_", year ) ),
by = ~ sexual_identity_2010,
design = survey_design_cc,
FUN = svymean,
vartype = "ci" )
colnames( svyby_result )[-1] <- c( paste0( "presence_N06A_Yes_", year, "_point_estimate" ),
paste0( "presence_N06A_Yes_", year, "_lower_ci" ),
paste0( "presence_N06A_Yes_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N06A <- Reduce( function( x, y ){
merge( x, y, by = "sexual_identity_2010" )
},
results_list )
# anxiolytics
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svyby( formula = as.formula( paste0( "~presence_N05B_Yes_", year ) ),
by = ~ sexual_identity_2010,
design = survey_design_cc,
FUN = svymean,
vartype = "ci" )
colnames( svyby_result )[-1] <- c( paste0( "presence_N05B_Yes_", year, "_point_estimate" ),
paste0( "presence_N05B_Yes_", year, "_lower_ci" ),
paste0( "presence_N05B_Yes_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N05B <- Reduce( function( x, y ){
merge( x, y, by = "sexual_identity_2010" )
},
results_list )
# calculate overall average number of prescriptions
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svymean( x = as.formula( paste0( "~presence_N06A_Yes_", year ) ), # antidepressants
design = subset( survey_design_cc, !is.na( get( paste0( "presence_N06A_Yes_", year ) ) ) ) )
svyby_result <- data.frame( svyby_result[ 1 ], confint( svyby_result )[ 1 ], confint( svyby_result )[ 2 ] )
colnames( svyby_result ) <- c( paste0( "presence_N06A_Yes_", year, "_point_estimate" ),
paste0( "presence_N06A_Yes_", year, "_lower_ci" ),
paste0( "presence_N06A_Yes_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N06A_overall <- Reduce( function( x, y ){
cbind( x, y )
},
results_list ) %>%
as.data.frame() %>%
rownames_to_column( var = "sexual_identity_2010" )
results_N06A_overall[ 1, 1 ] <- "Stockholm County"
results_list <- list()
for ( year in 2005:2021 ) {
svyby_result <- svymean( x = as.formula( paste0( "~presence_N05B_Yes_", year ) ), # anxiolytics
design = subset( survey_design_cc, !is.na( get( paste0( "presence_N05B_Yes_", year ) ) ) ) )
svyby_result <- data.frame( svyby_result[ 1 ], confint( svyby_result )[ 1 ], confint( svyby_result )[ 2 ] )
colnames( svyby_result ) <- c( paste0( "presence_N05B_Yes_", year, "_point_estimate" ),
paste0( "presence_N05B_Yes_", year, "_lower_ci" ),
paste0( "presence_N05B_Yes_", year, "_upper_ci" ) )
results_list[[ as.character( year ) ]] <- svyby_result %>%
remove_rownames()
}
results_N05B_overall <- Reduce( function( x, y ){
cbind( x, y )
},
results_list ) %>%
as.data.frame() %>%
rownames_to_column( var = "sexual_identity_2010" )
results_N05B_overall[ 1, 1 ] <- "Stockholm County"
# combine datasets
results_N06A_all <- bind_rows( results_N06A, results_N06A_overall )
results_N05B_all <- bind_rows( results_N05B, results_N05B_overall )
results_combined <- full_join( results_N06A_all, results_N05B_all, by = "sexual_identity_2010" )
str( results_combined )
results_combined_long <- results_combined %>%
pivot_longer(
cols = starts_with( "presence_" ),
names_to = c( "drug_class", "year", ".value" ),
names_pattern = "presence_(.*)_Yes_(\\d{4})_(.*)$" )
str( results_combined_long )
results_combined_long$sexual_identity_2010 <- factor( results_combined_long$sexual_identity_2010,
levels = c( "Heterosexual", "Homosexual", "Bisexual", "Uncertain", "Stockholm County" ) )
results_combined_long$drug_class <- factor( results_combined_long$drug_class, levels = c( "N06A", "N05B" ) )
results_combined_long$year <- as.numeric( results_combined_long$year )
# plot
ggplot(
results_combined_long %>%
filter( sexual_identity_2010 %in% c( "Heterosexual", "Homosexual", "Bisexual", "Stockholm County" ) ),
aes( x = year, y = point_estimate, color = sexual_identity_2010, group = sexual_identity_2010 ) ) +
geom_line( linewidth = 0.6 ) +
geom_ribbon( aes( ymin = lower_ci, ymax = upper_ci, fill = sexual_identity_2010 ),
alpha = 0.2,
color = NA ) +
scale_x_continuous( breaks = seq( 2005, 2021, by = 4 ),
labels = seq( 2005, 2021, by = 4 ),