-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPre_Processing.R
More file actions
2681 lines (1824 loc) · 85 KB
/
Pre_Processing.R
File metadata and controls
2681 lines (1824 loc) · 85 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
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
# Data pre-processing
#### Load the packages #######
library(probably) # Calibration analysis
library(haven) # Open SAS files
library(tidyverse) # Data Manipulation
library(lubridate) # Dates Manipulation
library(janitor) # Fast statistics calculations (cross-tabs)
library(tidymodels) # Modelling
library(finetune) # Fine tuning of models
library(xgboost) # Extreme Gradient Boosting Trees
library(vip) # Variable Importance Tools
library(riskRegression) # Some evaluation and calibration tools
library(dcurves) # Decision curves
library(patchwork) # Merging Plots
library(DALEX) # Model Explainability
library(DALEXtra) # Additional Exlainability
library(shapviz) # Shapley Values
library(rms) # For Classic Logistic Regression
library(DALEX) # For explainability
library(patchwork) # For figure merging
library(yardstick) #Metrics
library(dcurves) # Decision Curve Analysis
library(missRanger) # Imputation
library(ingredients) # PDP plots
library(DALEX) # Explainability of the model
library(DataExplorer) # Data exploration
library(SHAPforxgboost) # Shapleys for XGBoost
library(hstats) # Interaction Statistics
library(tidylog) # Logs of pre-processing
library(arrow) # Writing parquet files
#########################
################################
####### Data extraction ########
################################
# Access the directory for the data
# Overall population
nogle23 <- read_sas("nogle2023.sas7bdat")
# 11-year follow-up data for children
dnbc_11_children <- read_sas("y11_samlet_barn.sas7bdat")
# 11-year follow-up data for the adults
dnbc_11_adults <- read_sas("y11_samlet_voksen.sas7bdat")
# Read the 18-year follow-up data (That's where we get the outcome)
dnbc_18 <- read_sas("y18_samlet.sas7bdat")
# First interview of mather
adults_i1 <- read_sas("i1_samlet.sas7bdat")
# Children's stress scale
stress_11 <- read_sas("sic_y11.sas7bdat")
# Parity
parents <- read_dta("mat_char_birth.dta")
# Various eating disorders & behaviours
ED_11 <- read_sas("ed_11y_fu.sas7bdat")
ED_18 <- read_sas("ed_18y_fu.sas7bdat")
# BMI data at 7 & 11 years follow-up
BMI_7 <- read_sas("bmi_7y_fu.sas7bdat") #BMI at 7-year follow up
BMI_11 <- read_sas("BMI_11y_fu.sas7bdat") # BMI at 11-year follow up
# Parental Education (11-y follow up)
# Parental Education
parental_education <- read_sas("hh_ha_edu.sas7bdat")
# Paternal Education
paternal_education <- read_sas("pat_ha_edu.sas7bdat")
# Maternal Education
maternal_education <- read_sas("mat_ha_edu.sas7bdat")
# Income at 11years of age
income_11 <- read_sas("hh_inc_quart_age11.sas7bdat")
# Feeding disorders
feeding_disorders <- read_sas("indexchild_feedingillness.sas7bdat")
# Pre-Conceptional Folic Acid
folic <- read_sas("dnbc_folin.sas7bdat")
# Grab urbanicity at age 11
urbanicity <- read_sas("urb_year.sas7bdat")
########### Childhood adversities ###########
parental_somatic_illness <- read_dta("A_1_Parental_somatic_illness.dta")
sibling_somatic_illness <- read_dta("A_2_sibling_somatic_illness.dta")
parental_psychiatric_illness <- read_dta("A_3_parental_psychiatric_illness.dta")
sibling_psychiatric_illness <- read_dta("A_4_sibling_psychiatric_illness.dta")
foster_care <- read_dta("A_5_foster_care.dta")
death_of_parent <- read_dta("A_6_death_of_parent.dta")
death_of_sibling <- read_dta("A_7_death_of_sibling.dta")
parental_separation <- read_dta("A_8_parental_separation.dta")
parental_long_term_unemployment <- read_dta("A_9_parental_long-term_unemployment.dta")
poverty <- read_dta("A_10_poverty.dta")
alcohol_abuse <- read_dta("A_11_parental_alcohol_abuse.dta")
drug_abuse <- read_dta("A_12_parental_drug_abuse.dta")
#################################
# Eating disorders in the registers
ed_lpr <- read_sas("eating_disorders.sas7bdat")
# Maternal drinking
mat_drink <- read_sas("mat_drink_i1.sas7bdat")
# Maternal smoking
mat_smoke <- read_sas("mat_smok_i1.sas7bdat")
# Parental psychiatric disorders
diagn_parental_psychiatric <- read_sas("parent_psych_noed.sas7bdat")
# Parental eating disorders
diagn_parental_ed <- read_sas("parent_ed.sas7bdat")
# Child's psychiatric disorders
diagn_children_psychiatric <- read_sas("children_psych_noed.sas7bdat")
# Autoimmune Diseases Diagnosed
diagn_autoimmune <- read_sas("autoimmune_diseases.sas7bdat")
# Autoinflammatory Conditions
diagn_autoinfl <- read_sas("autoinflammatory_conditions.sas7bdat")
# Implementing the SDQ scales also at 7 and 11-y follow up
sdq11_child <- read_sas("sdqchild_y11.sas7bdat")
sdq11_parent <- read_sas("sdqparents_y11.sas7bdat")
sdq7_parent <- read_sas("sdq_y7.sas7bdat")
############################################################################
# Initial restriction of the population
# We should only keep the children that have
# agreed to participate and those who answered the 11-year
# follow up along with their mothers
nogle23 <- nogle23 |>
filter(y11_besvar_v == 1, y11_besvar_b == 1,
outcom_f == 1 |
outcom_f == 18 | outcom_f == 22 | (outcom_f == 21 & udfald_barn != 0))
# After restricting to children (alive) and parents answered in 11-y follow up:
# These are people for whom we have consent to analyze the data and have answered the items
dnbc_11_children <- dnbc_11_children |>
filter(lbgravff %in% nogle23$lbgravff)
# Now match the DNBC-11 adults with the children above
dnbc_11_adults <- dnbc_11_adults |>
filter(lbgravff %in% dnbc_11_children$lbgravff)
# Put the cpr of these children in the original dataframe
cprs <- nogle23 |> select(lbgravff,bcpr)
# Merge the CPR numbers to our existing DNBC database
dnbc_11_children <- dnbc_11_children |>
left_join(cprs,by = "lbgravff") |>
relocate(bcpr,.after = lbgravff)
#############################################
# Re-coding of some predictors
################ Age variable ##########################
dnbc_11_children |>
tabyl(E001) |>
mutate(percent = round(percent,3))
# Let's change age to be either 11,12 or something else
dnbc_11_children <- dnbc_11_children |>
mutate(Age = case_when(E001 == 99 ~ NA,
E001 == 2 ~ "11 years old",
E001 == 3 ~ "12 years old",
.default = "Else")) |>
mutate_at(vars(Age),as.factor) |>
relocate(Age,.before = E001) |>
select(-E001)
################## Sex ########################
dnbc_11_children |>
tabyl(E_SEX)
# Let's convert it also into a factor
dnbc_11_children <- dnbc_11_children |>
mutate(Sex = if_else(E_SEX == 1, "Male", "Female")) |>
mutate_at(vars(Sex), as.factor) |>
relocate(Sex, .before = E_SEX) |>
select(-E_SEX)
############### Family structure ####################
# With whom are you living with
dnbc_11_children |>
tabyl(E003) |>
mutate(percent = round(percent,2))
# Reconstruct into a factor
dnbc_11_children <- dnbc_11_children |>
mutate(Family_Living = case_when(E003 == 99 ~ NA,
E003 == 1 ~ "Both of my parents",
.default = "Other")) |>
mutate_at(vars(Family_Living),as.factor) |>
relocate(Family_Living,.before = E003) |>
select(-E003)
############### Age at Parental Divorce ##################
dnbc_11_children |>
tabyl(E005) |>
mutate(percent = round(percent,3))
dnbc_11_children <- dnbc_11_children |>
mutate(Divorce_Age = case_when(E005 == 100 ~ "Not Divorced",
E005 == 99 ~ NA,
E005 %in% 1:5 ~ "Younger than 5 years old",
E005 %in% 6:10 ~ "Between 6 to 10 years old",
E005 %in% 11:14 ~ "Older than 10 years old")) |>
mutate_at(vars(Divorce_Age),as.factor) |>
relocate(Divorce_Age, .before = E005) |>
select(-E005)
############### Biological Siblings ######################
dnbc_11_children |>
tabyl(E007)
dnbc_11_children <- dnbc_11_children |>
mutate(Bio_Sibling = case_when(E007 == 99 ~ NA,
E007 == 1 ~ "Have a biological sibling",
.default = "No")) |>
mutate_at(vars(Bio_Sibling), as.factor) |>
relocate(Bio_Sibling, .before = E007) |>
select(-E007)
############## Room-Share ################
dnbc_11_children |>
tabyl(E010) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Room_Share = case_when(E010 == 99 ~ NA,
E010 == 1 ~"I do not share room",
.default = "I share")) |>
mutate_at(vars(Room_Share), as.factor) |>
relocate(Room_Share, .before = E010) |>
select(-E010)
#################### Social relations and well-being ##################
# How many of your close friends are boys
dnbc_11_children |>
tabyl(E011_1) |>
mutate(percent = round(percent,3))
dnbc_11_children <- dnbc_11_children |>
mutate(Close_Male_Friends = case_when(E011_1 == 1 ~ "None",
E011_1 == 2 ~ "One",
E011_1 == 3 ~ "Two",
E011_1 == 4 ~ "Three or more",
E011_1 == 99 ~ NA)) |>
mutate_at(vars(Close_Male_Friends), as.factor) |>
relocate(Close_Male_Friends, .before = E011_1) |>
select(-E011_1)
# How many of your close friends are females
dnbc_11_children |>
tabyl(E011_2) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Close_Female_Friends = case_when(E011_2 == 1 ~ "None",
E011_2 == 2 ~ "One",
E011_2 == 3 ~ "Two",
E011_2 == 4 ~ "Three or more",
E011_2 == 99 ~ NA)) |>
mutate_at(vars(Close_Female_Friends), as.factor) |>
relocate(Close_Female_Friends, .before = E011_2) |>
select(-E011_2)
# How easy or difficult to make new friends
dnbc_11_children |>
tabyl(E012) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Friendship_Creation = case_when(
E012 == 1 | E012 == 2 ~ "Easy or Very Easy",
E012 == 99 ~ NA,
.default = "Difficult or Very Difficult")) |>
mutate_at(vars(Friendship_Creation),as.factor) |>
relocate(Friendship_Creation, .before = E012) |>
select(-E012)
##################### Bullying #######################
dnbc_11_children |>
tabyl(E014_1)
dnbc_11_children <- dnbc_11_children |>
mutate(Bullying_Receive = case_when(
E014_1 == 1 | E014_1 == 2 ~ "Bullied",
E014_1 == 99 ~ NA,
.default = "Not Bullied")) |>
mutate_at(vars(Bullying_Receive),as.factor) |>
relocate(Bullying_Receive, .before = E014_1) |>
select(-E014_1)
dnbc_11_children |>
tabyl(Bullying_Receive)
###################### Bullying Others #######################
dnbc_11_children |>
tabyl(E014_2)
dnbc_11_children <- dnbc_11_children |>
mutate(Bullying_Given = case_when(
E014_2 == 1 | E014_2 == 2 ~ "Have Bullied",
E014_2 == 99 ~ NA,
.default = "Have not Bullied")) |>
mutate_at(vars(Bullying_Given),as.factor) |>
relocate(Bullying_Given, .before = E014_2) |>
select(-E014_2)
########################### Schooling ########################
# Rating of own school achievements
dnbc_11_children |>
tabyl(E015_1) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(School_Performance = case_when(
E015_1 == 99 ~ NA,
E015_1 == 1 ~ "Excellent",
E015_1 == 2 ~ "Good",
E015_1 == 3 | E015_1 == 4 ~ "Not Good")) |>
mutate_at(vars(School_Performance),as.factor) |>
relocate(School_Performance, .before = E015_1) |>
select(-E015_1)
# Sports performance
dnbc_11_children |>
tabyl(E015_4) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Sports_Performance = case_when(
E015_4 == 99 ~ NA,
E015_4 == 1 ~ "Excellent",
E015_4 == 2 ~ "Good",
E015_4 == 3 | E015_4 == 4 | E015_4 == 99 ~ "Not Good")) |>
mutate_at(vars(Sports_Performance),as.factor) |>
relocate(Sports_Performance, .before = E015_4) |>
select(-E015_4)
####################### General Well-Being #########################
# Stomach Pains
dnbc_11_children |>
tabyl(E022_5) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Stomach_Pains = case_when(
E022_5 == 1 | E022_5 == 2 ~ "Very Often or Often",
E022_5 == 3 ~ "Sometimes",
E022_5 == 4 ~ "Never",
E022_5 == 99 ~ NA)) |>
mutate_at(vars(Stomach_Pains),as.factor) |>
relocate(Stomach_Pains, .before = E022_5) |>
select(-E022_5)
# Loneliness
dnbc_11_children |>
tabyl(E022_6) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Loneliness_Feeling = case_when(
E022_6 == 1 | E022_6 == 2 ~ "Very Often or Often",
E022_6 == 3 ~ "Sometimes",
E022_6 == 4 ~ "Never",
E022_6 == 99 ~ NA)) |>
mutate_at(vars(Loneliness_Feeling),as.factor) |>
relocate(Loneliness_Feeling, .before = E022_6) |>
select(-E022_6)
# Do you express your sadness to the rest
dnbc_11_children |>
tabyl(E022_17) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Showing_Sadness = case_when(
E022_17 == 1 | E022_17 == 2 ~ "Very Often or Often",
E022_17 == 3 ~ "Sometimes",
E022_17 == 4 ~ "Never",
E022_17 == 99 ~ NA)) |>
mutate_at(vars(Showing_Sadness),as.factor) |>
relocate(Showing_Sadness, .before = E022_17) |>
select(-E022_17)
# Is there an adult to talk to in hard times
dnbc_11_children |>
tabyl(E022_19) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Adult_To_Talk_To = case_when(
E022_19 == 1 | E022_19 == 2 ~ "Very Often or Often",
E022_19 == 3 ~ "Sometimes",
E022_19 == 4 ~ "Never",
E022_19 == 99 ~ NA)) |>
mutate_at(vars(Adult_To_Talk_To),as.factor) |>
relocate(Adult_To_Talk_To, .before = E022_19) |>
select(-E022_19)
# Negative events in children's life: they start with E023_1 :E023_15
# We need this to be factors
dnbc_11_children <- dnbc_11_children |>
mutate_at(vars(E023_1:E023_15),as.factor) |>
mutate_at(vars(starts_with("E023_")), ~case_match(., "1" ~ "Yes", "2" ~ "No")) |>
mutate_at(vars(E023_1:E023_15),as.factor)
################# Sleeping related questions #####################
# Do you feel that you don't get enough sleep
dnbc_11_children |>
tabyl(E028_1) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Lack_Of_Sleep = case_when(
E028_1 == 1 ~ "Almost Always or Always",
E028_1 == 2 ~ "Sometimes",
E028_1 == 3 ~ "Rarely/Never",
E028_1 == 99 ~ NA)) |>
mutate_at(vars(Lack_Of_Sleep),as.factor) |>
relocate(Lack_Of_Sleep, .before = E028_1) |>
select(-E028_1)
# Do you find it difficult to wake up in the morning?
dnbc_11_children |>
tabyl(E028_8) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Difficult_To_WakeUp = case_when(
E028_8 == 99 ~ NA,
E028_8 == 1 ~ "Almost Always or Always",
E028_8 == 2 ~ "Sometimes",
E028_8 == 3 ~ "Rarely/Never")) |>
mutate_at(vars(Difficult_To_WakeUp),as.factor) |>
relocate(Difficult_To_WakeUp, .before = E028_8) |>
select(-E028_8)
# Create a variable to measure amount of sleep in children
sleep_df <- dnbc_11_children |>
select(lbgravff,E024,E025,E026,E027) |>
mutate(E024 = case_when(E024 == 1 ~ ("19:00"),
E024 == 2 ~ ("19:30"),
E024 == 3 ~ ("20:00"),
E024 == 4 ~ ("20:30"),
E024 == 5 ~ ("21:00"),
E024 == 6 ~ ("21:30"),
E024 == 7 ~ ("22:00"),
E024 == 8 ~ ("22:30"),
E024 == 9 ~ ("23:00"),
E024 == 10 ~ ("23:30"),
E024 == 11 ~ ("24:00"),
E024 == 12 ~ ("24:30"),
E024 == 13 ~ ("01:00"),
E024 == 14 ~ ("02:00"),
E024 == 15 ~ ("03:00"),
E024 == 99 ~ NA)) |>
mutate(E024 = hm(E024)) |>
mutate(E025 = case_when(E025 == 1 ~ ("19:00"),
E025 == 2 ~ ("19:30"),
E025 == 3 ~ ("20:00"),
E025 == 4 ~ ("20:30"),
E025 == 5 ~ ("21:00"),
E025 == 6 ~ ("21:30"),
E025 == 7 ~ ("22:00"),
E025 == 8 ~ ("22:30"),
E025 == 9 ~ ("23:00"),
E025 == 10 ~ ("23:30"),
E025 == 11 ~ ("24:00"),
E025 == 12 ~ ("24:30"),
E025 == 13 ~ ("01:00"),
E025 == 14 ~ ("02:00"),
E025 == 15 ~ ("03:00"),
E025 == 99 ~ NA)) |>
mutate(E025 = hm(E025)) |>
mutate(E026 = case_when(E026 == 1 ~ ("5:00"),
E026 == 2 ~ ("5:30"),
E026 == 3 ~ ("6:00"),
E026 == 4 ~ ("6:30"),
E026 == 5 ~ ("7:00"),
E026 == 6 ~ ("7:30"),
E026 == 7 ~ ("8:00"),
E026 == 8 ~ ("8:30"),
E026 == 9 ~ ("9:00"),
E026 == 10 ~ ("9:30"),
E026 == 11 ~ ("10:00"),
E026 == 12 ~ ("11:00"),
E026 == 13 ~ ("12:00"),
E026 == 14 ~ ("13:00"),
E026 == 99 ~ NA)) |>
mutate(E026 = hm(E026)) |>
mutate(E027 = case_when(E027 == 1 ~ ("5:00"),
E027 == 2 ~ ("5:30"),
E027 == 3 ~ ("6:00"),
E027 == 4 ~ ("6:30"),
E027 == 5 ~ ("7:00"),
E027 == 6 ~ ("7:30"),
E027 == 7 ~ ("8:00"),
E027 == 8 ~ ("8:30"),
E027 == 9 ~ ("9:00"),
E027 == 10 ~ ("9:30"),
E027 == 11 ~ ("10:00"),
E027 == 12 ~ ("11:00"),
E027 == 13 ~ ("12:00"),
E027 == 14 ~ ("13:00"),
E027 == 99 ~ NA)) |>
mutate(E027 = hm(E027)) |>
rename(weekday_bed = "E024",
weekend_bed = "E025",
weekday_wake = "E026",
weekend_wake = "E027") |>
mutate(sleep_weekday = as.numeric(weekday_wake - weekday_bed)/3600,
sleep_weekend = as.numeric(weekend_wake - weekend_bed)/3600) |>
mutate(sleep_weekday = if_else(sleep_weekday < 0, sleep_weekday + 24, sleep_weekday)) |>
mutate(sleep_weekend = if_else(sleep_weekend < 0, sleep_weekend + 24, sleep_weekend)) |>
mutate(avg_sleep = (5*sleep_weekday + 2*sleep_weekend)/7)
sleep_df <- sleep_df |>
rename(Sleeping_Hours = "avg_sleep")
# Let's put the Sleeping hours into dnbc_11_children
dnbc_11_children <- dnbc_11_children |>
left_join(sleep_df |> select(lbgravff,Sleeping_Hours),by = "lbgravff")
##################### Dietary Habits ####################
# How ofter do you have breakfast
dnbc_11_children |>
tabyl(E030_1) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Breakfast = case_when(E030_1 == 1 ~ "Every Day",
E030_1 == 2 ~ "6 Days a week",
E030_1 == 99 ~ NA,
.default = "Less than 6 days a week")) |>
mutate_at(vars(Breakfast), as.factor) |>
relocate(Breakfast, .before = E030_1) |>
select(-E030_1)
# How often do you have lunch
dnbc_11_children |>
tabyl(E030_2) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Lunch = case_when(E030_2 == 1 ~ "Every Day",
E030_2 == 2 ~ "6 Days a week",
E030_2 == 99 ~ NA,
.default = "Less than 6 days a week")) |>
mutate_at(vars(Lunch), as.factor) |>
relocate(Lunch, .before = E030_2) |>
select(-E030_2)
# Dinner
dnbc_11_children |>
tabyl(E030_3) |>
mutate(percent = round(percent,3))
dnbc_11_children <- dnbc_11_children |>
mutate(Dinner = case_when(E030_3 == 1 ~ "Every Day",
E030_3 == 2 ~ "6 Days a week",
E030_3 == 99 ~ NA,
.default = "Less than 6 days a week")) |>
mutate_at(vars(Dinner), as.factor) |>
relocate(Dinner, .before = E030_3) |>
select(-E030_3)
# Snack Frequency
dnbc_11_children |>
tabyl(E031) |>
mutate(percent = round(percent,3))
dnbc_11_children <- dnbc_11_children |>
mutate(Snack = case_when(E031 == 1 | E031 == 2 | E031 ==3 ~ "3 to 5 times a day",
E031 == 4 | E031 == 5 ~ "1 to 2 times a day",
E031 == 6 ~ "Not every day",
E031 == 99 ~ NA)) |>
mutate_at(vars(Snack), as.factor) |>
relocate(Snack, .before = E031) |>
select(-E031)
###### Fat Feeling #########
dnbc_11_children |>
tabyl(E033_1)
dnbc_11_children <- dnbc_11_children |>
mutate(Feeling_Fat = case_when(E033_1 == 1 ~ "Every Day",
E033_1 == 2 |
E033_1 == 3 ~ "Often or Sometimes",
E033_1 == 99 ~ NA,
.default = "Rarely or Never")) |>
mutate_at(vars(Feeling_Fat), as.factor) |>
relocate(Feeling_Fat, .before = E033_1) |>
select(-E033_1)
###### How often you felt you wannted to be thinner ###########
dnbc_11_children |>
tabyl(E033_2)
dnbc_11_children <- dnbc_11_children |>
mutate(Want_For_Thin = case_when(E033_2 == 1 ~ "Every Day",
E033_2 == 2 |
E033_2 == 3 ~ "Often or Sometimes",
E033_2 == 99 ~ NA,
.default = "Rarely or Never")) |>
mutate_at(vars(Want_For_Thin), as.factor) |>
relocate(Want_For_Thin, .before = E033_2) |>
select(-E033_2)
# Have you often gone to an eating binge
dnbc_11_children |>
tabyl(E037) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Binge_Eating = case_when(E037 == 1 ~ "Never",
E037 == 2 ~ "1-3 times per month",
E037 == 99 ~ NA,
.default = "Once or more in a week")) |>
mutate_at(vars(Binge_Eating), as.factor) |>
relocate(Binge_Eating, .before = E037) |>
select(-E037)
# Did you eat until your stomach hurt ?
dnbc_11_children |>
tabyl(E038_2)
dnbc_11_children <- dnbc_11_children |>
mutate(Stomach_Binge = case_when(E038_2 == 1 ~ "Yes",
E038_2 == 99 ~ NA,
E038_2 == 2 ~ "No",
.default = "Not Applicable")) |>
mutate_at(vars(Stomach_Binge), as.factor) |>
relocate(Stomach_Binge, .before = E038_2) |>
select(-E038_2)
############### Tried smoking #################
dnbc_11_children |>
tabyl(E071) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Smoking = case_when(E071 == 1 ~ "Smoked",
E071 == 2 ~ "Not Smoked",
E071 == 99 ~ NA)) |>
mutate_at(vars(Smoking),as.factor) |>
select(-E071)
############# Health-related questions ###############
# Self-Rated Health
dnbc_11_children |>
tabyl(E083)
dnbc_11_children <- dnbc_11_children |>
mutate(Self_Rated_Health = case_when(E083 == 1 ~ "Excellent",
E083 == 2 ~ "Good",
E083 == 3 | E083 == 4 ~ "Not so Good or Poor",
E083 == 99 ~ NA)) |>
mutate_at(vars(Self_Rated_Health), as.factor) |>
relocate(Self_Rated_Health, .before = E083) |>
select(-E083)
# Physical Condition
dnbc_11_children |>
tabyl(E084)
dnbc_11_children <- dnbc_11_children |>
mutate(Self_Physical_Condition = case_when(E084 == 1 ~ "Excellent",
E084 == 2 ~ "Good",
E084 == 3 | E084 == 4 | E084 == 5 ~ "Not so Good or Poor or Very Poor",
E084 == 99 ~ NA)) |>
mutate_at(vars(Self_Physical_Condition), as.factor) |>
relocate(Self_Physical_Condition, .before = E084) |>
select(-E084)
################### Questions of Athleticism and Activity ##################
# School Break Movement
dnbc_11_children |>
tabyl(E059)
dnbc_11_children <- dnbc_11_children |>
mutate(Movement_School = if_else(E059 == 99, NA, E059)) |>
mutate_at(vars(Movement_School),as.factor) |>
select(-E059)
levels(dnbc_11_children$Movement_School) <- c("Very Active", "Active", "Normal", "Not Active")
#Leisure Movement
dnbc_11_children |>
tabyl(E060)
dnbc_11_children <- dnbc_11_children |>
mutate(Movement_Leisure = if_else(E060 == 99, NA, E060)) |>
mutate_at(vars(Movement_Leisure),as.factor) |>
select(-E060)
levels(dnbc_11_children$Movement_Leisure) <- c("Very Active", "Active", "Normal", "Not Active")
# Gymnastics
dnbc_11_children <- dnbc_11_children |>
mutate(Gymnastics_Dancing = case_when(
E062_7 == 99 | E062_8 == 99 ~ NA,
E062_7 == 1 | E062_8 == 1 ~ "Does Gymnastics or Dancing",
.default = "Does not do Gymnastics or Dancing")) |>
mutate(Frequency_Gymnastics_Dancing = case_when(
E062_7A == 3 | E062_7A == 4 |
E062_7A == 5 | E062_8A == 3 |
E062_8A == 4 | E062_8A == 5 ~ "Practicing at least 3 times per week",
E062_7A == 99 | E062_8A == 99 ~ NA,
.default = "Not Practicing or Practicing Rarely")) |>
mutate_at(vars(Gymnastics_Dancing,Frequency_Gymnastics_Dancing),as.factor)
# Body Dissatisfaction Score
# Body Disatisfaction
dnbc_11_children <-dnbc_11_children |>
mutate(E125_1 = if_else(E125_1 == 99 | E125_1 == 100, NA, E125_1)) |>
mutate(E125_2 = if_else(E125_2 == 99 | E125_2 == 100, NA, E125_2)) |>
mutate(Body_Score = E125_2 - E125_1)
# The way you want to look - The way you look
dnbc_11_children <-dnbc_11_children |>
mutate(E126_1 = if_else(E126_1 == 99 | E126_1 == 100, NA, E126_1)) |>
mutate(E126_2 = if_else(E126_2 == 99 | E126_2 == 100, NA, E126_2)) |>
mutate(Body_Score_Males = E126_2 - E126_1)
dnbc_11_children <- dnbc_11_children |>
mutate(Body_Score = if_else(is.na(Body_Score) & !is.na(Body_Score_Males), Body_Score_Males, Body_Score ))
dnbc_11_children <- dnbc_11_children |>
select(-Body_Score_Males)
################### Self Harm #####################
dnbc_11_children <- dnbc_11_children |>
mutate(Self_Harm = case_when(E116_3 == 1 ~ "Yes",
E116_3 == 99 ~ NA,
E116_3 == 2 ~ "No")) |>
mutate_at(vars(Self_Harm),as.factor) |>
select(-E116_3)
####################### Depression Proxy ########################
dnbc_11_children <- dnbc_11_children |>
mutate(Depression_Feelings = case_when(E112 == 1 ~ "Yes",
E112 == 99 ~ NA,
E112 == 2 ~ "No")) |>
mutate(Loss_Of_Interest = case_when(E114 == 1 ~ "Yes",
E114 == 99 ~ NA,
E114 == 2 ~ "No")) |>
mutate_at(vars(Depression_Feelings, Loss_Of_Interest),as.factor) |>
select(-E112,-E114)
################### Perfectionist Related Questions ##################
## We will create a composite variable of Obsessive Compulsive Disorder Items
# E086_1 until E086_5
dnbc_11_children <- dnbc_11_children |>
mutate(OCD_Symptoms = case_when(
E086_1 == 1 | E086_2 == 1 | E086_3 == 1 |
E086_4 == 1 | E086_5 == 1 ~ "Frequent OCD Symptoms",
E086_1 == 2 | E086_2 == 2 | E086_3 == 2 |
E086_4 == 2 | E086_5 == 2 ~ "Occasional OCD Symptoms",
E086_1 == 3 | E086_2 == 3 | E086_3 == 3 |
E086_4 == 3 | E086_5 == 3 ~ "Abscence of OCD Symptoms")) |>
mutate_at(vars(OCD_Symptoms),as.factor) |>
relocate(OCD_Symptoms, .before = E086_1)
############ Musculosceletal Pain ################
dnbc_11_children |>
tabyl(E089) |>
mutate(percent = round(percent,2))
dnbc_11_children <- dnbc_11_children |>
mutate(Neck_Pain = case_when(E089 == 1 | E089 == 2 ~
"Often or occasionally",
E089 == 3 ~ "Rarely",
E089 == 4 ~ "Never",
E089 == 99 ~ NA)) |>
mutate_at(vars(Neck_Pain),as.factor) |>
relocate(Neck_Pain, .before = E089) |>
select(-E089)
dnbc_11_children <- dnbc_11_children |>
mutate(Middle_Back_Pain = case_when(E090 == 1 | E090 == 2 ~
"Often or occasionally",
E090 == 3 ~ "Rarely",
E090 == 4 ~ "Never",
E090 == 99 ~ NA)) |>
mutate_at(vars(Middle_Back_Pain),as.factor) |>
relocate(Middle_Back_Pain, .before = E090) |>
select(-E090)
dnbc_11_children <- dnbc_11_children |>
mutate(Low_Back_Pain = case_when(E091 == 1 | E091 == 2 ~
"Often or occasionally",
E091 == 3 ~ "Rarely",
E091 == 4 ~ "Never",
E091 == 99 ~ NA)) |>
mutate_at(vars(Low_Back_Pain),as.factor) |>
relocate(Low_Back_Pain, .before = E091) |>
select(-E091)
########### Gender Identity ###########