-
Notifications
You must be signed in to change notification settings - Fork 0
/
0_wrangle_data.Rmd
1753 lines (1424 loc) · 82.4 KB
/
0_wrangle_data.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: "Data Wrangling"
output:
html_document:
toc: yes
toc_depth: 5
code_folding: "show"
---
```{r message=F,warning=F}
source("0_helpers.R")
library(tidylog)
knitr::opts_chunk$set(error = FALSE)
load("data/pretty_raw.rdata")
knit_print.alpha <- knitr:::knit_print.default
registerS3method("knit_print", "alpha", knit_print.alpha)
```
```{r nicer}
opts_chunk$set(message=T, warning = F)
```
## Weekdays
```{r weekdays, error=FALSE}
s3_daily$weekday = format(as.POSIXct(s3_daily$created), format = "%w")
s3_daily$weekend <- ifelse(s3_daily$weekday %in% c(0,5,6), 1, 0)
s3_daily$weekday <- car::Recode(s3_daily$weekday, "0='Sunday';1='Monday';2='Tuesday';3='Wednesday';4='Thursday';5='Friday';6='Saturday'",as.factor =T, levels = c('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'))
hour_string_to_period = function(hour_string) {
duration(as.numeric(stringr::str_sub(hour_string, 1,2)), units = "hours") + duration(as.numeric(stringr::str_sub(hour_string, 4,5)), units = "minutes")
}
s3_daily$sleep_awoke_time = hour_string_to_period(s3_daily$sleep_awoke_time)
s3_daily$sleep_fell_asleep_time = hour_string_to_period(s3_daily$sleep_fell_asleep_time)
s3_daily$sleep_duration = ifelse(
s3_daily$sleep_awoke_time >= s3_daily$sleep_fell_asleep_time,
s3_daily$sleep_awoke_time - s3_daily$sleep_fell_asleep_time,
dhours(24) - s3_daily$sleep_fell_asleep_time + s3_daily$sleep_awoke_time
) / 60 / 60
s3_daily = s3_daily %>%
mutate(created_date = as.Date(created - hours(10))) %>% # don't count night time as next day
group_by(session) %>%
mutate(first_diary_day = min(created_date)) %>%
ungroup()
stopifnot(s3_daily %>% drop_na(session, created_date) %>%
group_by(session, created_date) %>% filter(n()>1) %>% nrow() == 0)
```
## Menstrual phase
```{r menstrual.phase.calcs}
s1_demo = s1_demo %>% mutate(ended_date = as.Date(ended))
# s1_demo %>%
# filter(menstruation_last < ended_date - days(40)) %>%
# select(menstruation_last, ended_date, menstruation_last_certainty, contraception_method)
s1_menstruation_start = s1_demo %>% filter(!is.na(menstruation_last)) %>%
filter(menstruation_last >= ended_date - days(40)) %>% # only last menstruation that weren't ages ago
mutate(created_date = as.Date(created)) %>%
select(session, created_date, menstruation_last) %>% rename(menstrual_onset_date_inferred = menstruation_last)
s5_hadmenstruation = s5_hadmenstruation %>%
filter(!is.na(last_menstrual_onset_date)) %>%
mutate(created_date = as.Date(created)) %>%
select(session, created_date, last_menstrual_onset_date) %>% rename(menstrual_onset_date_inferred = last_menstrual_onset_date) %>%
filter(!duplicated(session))
table(duplicated(s5_hadmenstruation$session))
```
## Fertility estimation
### LH surges and sex hormones
```{r}
lab = readxl::read_xlsx("data/Datensatz_Zyklusstudie_Labor.xlsx")
lab = lab %>%
rename(created_date = `Datum Lab Session`) %>%
filter(!is.na(`VPN-CODE`), !is.na(created_date)) %>%
mutate(short = str_sub(Tagebuchcode, 1, 7),
lab_only_no_diary = is.na(short),
short = if_else(is.na(short), `VPN-CODE`, short),
created_date = as.Date(created_date),
`Date LH surge` = as.Date(if_else(`Date LH surge` == "xxx", NA_real_, as.numeric(`Date LH surge`)), origin = "1899-12-30")) # some excel problem, where nrs are repeated at end, so we shorten it
lab %>% mutate(n_women = n_distinct(`VPN-CODE`),
n_diary_participants = n_distinct(str_sub(Tagebuchcode, 1, 7), na.rm = T)) %>%
group_by(n_women,n_diary_participants, `VPN-CODE`) %>%
summarise(days = n(), surges = n_nonmissing(`Date LH surge`)) %>%
select(-`VPN-CODE`) %>%
summarise_all(mean)
setdiff(lab$short, s1_demo$short) %>% unique() %>% length() # 7 didnt enter online study at all
setdiff(lab$short, s3_daily$short) %>% unique() %>% length() # 18 didnt do diary
setdiff(s3_daily$short, lab$short) %>% unique() %>% length() # 1235 didnt do lab
# setdiff(lab$short, s1_demo$short_demo) %>% unique() # all codes found
lab <- lab %>% filter(!lab_only_no_diary) %>% select(-lab_only_no_diary)
get_long_sess = s3_daily %>% select(session, short) %>% na.omit() %>% unique()
testthat::expect_equal(lab %>% filter(is.na(created_date)) %>% nrow(), 0)
lab <- get_long_sess %>% inner_join(lab, by = "short")
testthat::expect_equal(lab %>% filter(is.na(created_date)) %>% nrow(), 0)
testthat::expect_equal(s3_daily %>% filter(is.na(created_date)) %>% nrow(), 0)
s3_daily <- s3_daily %>% full_join(lab %>% select(-`Date LH surge`, -`Menstrual Onset`), by = c("session", "short", "created_date"), suffixes = c("_diary", "_lab"))
s3_daily %>% select(ends_with("_lab")) %>% ncol()
s3_daily %>% select(`IBL_Estradiol pg/ml`, ended) %>% codebook::md_pattern(min_freq = 0)
s3_daily <- s3_daily %>%
full_join(
lab %>%
filter(!is.na(`Date LH surge`), exclude_luteal_too_long == 0) %>%
mutate(created_date = `Date LH surge`) %>%
select(session, short, created_date, `Date LH surge`),
by = c("session", "short", "created_date"), suffixes = c("_diary", "_lab"))
testthat::expect_equal(s3_daily %>% filter(is.na(created_date)) %>% nrow(), 0)
# because of the typos in the lab session codes, we have to merge the long ones back on
xtabs(~ is.na(session) + is.na(`VPN-CODE`), data = s3_daily)
xtabs(~ is.na(short) + is.na(`VPN-CODE`), data = s3_daily)
xtabs(~ is.na(ended) + is.na(`VPN-CODE`), data = s3_daily)
xtabs(~ is.na(ended) + is.na(`Progesterone pg/ml`), data = s3_daily)
#
# diary %>% filter(!is.na(Age)) %>%
# select(short, Age, age, relationship_status, Relationship_status, `MEAN Größe`, `MEAN Gewicht`, height, weight) %>%
# group_by(short) %>%
# summarise_all(first) %>%
# distinct() %>%
# mutate(age_diff = abs(Age - age),
# height_diff = abs(height- `MEAN Größe`),
# weight_diff = abs(weight - `MEAN Gewicht`),
# rel_diff = abs(relationship_status - Relationship_status)) %>%
# arrange(age_diff) %>% View
```
#### Center sex hormones
We remove outliers that are more than 3 SD from the mean and center within groups
(logged and non-logged).
```{r}
outliers_to_missing <- function(x, sd_multiplier = 3) {
if_else(x > (mean(x, na.rm = T) + sd_multiplier * sd(x, na.rm = T)) |
x < (mean(x, na.rm = T) - sd_multiplier * sd(x, na.rm = T)),
NA_real_, x)
}
s3_daily <- s3_daily %>%
ungroup() %>%
mutate(
`Progesterone pg/ml` = outliers_to_missing(`Progesterone pg/ml`),
`Estradiol pg/ml` = outliers_to_missing(`Estradiol pg/ml`),
`IBL_Estradiol pg/ml` = outliers_to_missing(`IBL_Estradiol pg/ml`),
`Testosterone pg/ml` = outliers_to_missing(`Testosterone pg/ml`),
`Cortisol nmol/l` = outliers_to_missing(`Cortisol nmol/l`)
) %>%
group_by(session) %>%
mutate(
progesterone_mean = mean(`Progesterone pg/ml`, na.rm = T),
`progesterone_diff` = `Progesterone pg/ml` - progesterone_mean,
progesterone_log_mean = mean(log(`Progesterone pg/ml`), na.rm = T),
progesterone_log_diff = log(`Progesterone pg/ml`) - progesterone_log_mean,
estradiol_mean = mean(`Estradiol pg/ml`, na.rm = T),
estradiol_diff = `Estradiol pg/ml` - estradiol_mean,
estradiol_log_mean = mean(log(`Estradiol pg/ml`), na.rm = T),
estradiol_log_diff = log(`Estradiol pg/ml`) - estradiol_log_mean,
ibl_estradiol_mean = mean(`IBL_Estradiol pg/ml`, na.rm = T),
ibl_estradiol_diff = `IBL_Estradiol pg/ml` - ibl_estradiol_mean,
ibl_estradiol_log_mean = mean(log(`IBL_Estradiol pg/ml`), na.rm = T),
ibl_estradiol_log_diff = log(`IBL_Estradiol pg/ml`) - ibl_estradiol_log_mean,
testosterone_mean = mean(`Testosterone pg/ml`, na.rm = T),
testosterone_diff = `Testosterone pg/ml` - testosterone_mean,
testosterone_log_mean = mean(log(`Testosterone pg/ml`), na.rm = T),
testosterone_log_diff = log(`Testosterone pg/ml`) - testosterone_log_mean,
cortisol_mean = mean(`Cortisol nmol/l`, na.rm = T),
cortisol_diff = `Cortisol nmol/l` - cortisol_mean,
cortisol_log_mean = mean(log(`Cortisol nmol/l`), na.rm = T),
cortisol_log_diff = log(`Cortisol nmol/l`) - cortisol_log_mean
) %>%
ungroup()
```
### Fertility awareness
```{r}
tracked_windows <- s4_followup %>% select(short, starts_with("aware_fertile"), -ends_with("block"), -aware_fertile_reason_unusual, -aware_fertile_effects) %>%
filter(aware_fertile_phases_number > 0) %>%
mutate_all(as.character) %>%
gather(cycle, date, -short, -aware_fertile_phases_number) %>%
tbl_df() %>%
mutate(cycle = str_sub(cycle, str_length("aware_fertile_") + 1)) %>%
separate(cycle, c("cycle", "startend")) %>%
mutate(date = as.Date(date)) %>%
spread(startend, date) %>%
mutate(window_length = end - start,
date_of_ovulation_awareness = end - days(1))
s3_daily <- s3_daily %>% left_join( tracked_windows %>%
select(short, window_length, date_of_ovulation_awareness) %>%
mutate(created_date = date_of_ovulation_awareness), by = c("short", "created_date"))
```
### Compute menstrual onsets
To compute menstrual onsets from the diary data, we have to clear a few hurdles:
- diaries could be filled out until 3 am (and later in special cases), but participants will tend to count backwards from the preceding day when asked when the last menstruation occurred
- we asked women only every ~3 days about menstruation (-> interpolate)
- women could report the same menstrual onset several times (-> use the report closest to the onset, more accurate)
- women reported a last menstrual onset in the demographic questionnaire preceding the diary and in the follow-up survey following the diary
- we need to count backward and forward from each menstrual onset
- we need to include the dates from the demographic and the follow-up questionnaire without overwriting more pertinent dates from the diary
- we want to "bridge gaps" between reports of menstruation that are at most 40 days wide (because wider gaps probably mean that there was something going on with the menstrual cycle such as a miscarriage, menopause, etc.)
Therefore we use a multi-step procedure:
1. Collect unique menstrual onsets reported by each woman from pre-survey, diary, and post-survey
2. Expand the onsets into time-series by participant.
3. "Merge"/prefer reports closer to the onset when several different reports were made
4. Count forward & backward.
5. Assign cycle numbers.
6. Merge on participant & created_date.
```{r menstrual_onsets}
# step 1
menstrual_onsets = s3_daily %>%
group_by(session) %>%
arrange(created) %>%
mutate(
menstrual_onset_date = as.Date(menstrual_onset_date),
menstrual_onset_date_inferred = as.Date(ifelse(!is.na(menstrual_onset_date),
menstrual_onset_date, # if date was given, take it
ifelse(!is.na(menstrual_onset), # if days ago was given
created_date - days(menstrual_onset - 1), # subtract them from current date
as.Date(NA))
), origin = "1970-01-01")
) %>%
select(session, created_date, menstrual_onset_date_inferred) %>%
filter(!is.na(menstrual_onset_date_inferred)) %>%
unique()
## add in the menstrual onsets we got from the pre and post survey and the lab
lab_onsets <- lab %>% select(session, created_date, menstrual_onset_date_inferred = `Menstrual Onset`) %>%
mutate(menstrual_onset_date_inferred = as.Date(menstrual_onset_date_inferred)) %>%
filter(!is.na(menstrual_onset_date_inferred))
mons = menstrual_onsets %>%
select(session, created_date, menstrual_onset_date_inferred) %>%
mutate(date_origin = "diary") %>%
bind_rows(
s1_menstruation_start %>% mutate(date_origin = "demo"),
s5_hadmenstruation %>% mutate(date_origin = "followup"),
lab_onsets %>% mutate(date_origin = "lab")
) %>%
filter( !is.na(menstrual_onset_date_inferred)) %>%
arrange(session, menstrual_onset_date_inferred, created_date) %>%
unique() %>%
group_by(session) %>%
# step 3: prefer reports closer to event if they conflict
mutate(
onset_diff = abs( as.double( lag(menstrual_onset_date_inferred) - menstrual_onset_date_inferred, units = "days")), # was there a change compared to the last reported menstrual onset (first one gets NA)
menstrual_onset_date_inferred = if_else(onset_diff < 7, # if last date is known, but is slightly different from current date
as.Date(NA), # attribute it to memory, not extremely short cycle, use fresher date
menstrual_onset_date_inferred, # if it's a big difference, use the current date
menstrual_onset_date_inferred # use current date if last date not known/first onset
) # if no date is assigned today, keep it like that
) %>% # carry the last MO forward
# mutate(created_date = menstrual_onset_date_inferred) %>%
filter(!is.na(menstrual_onset_date_inferred))
nrow(mons)
# mons %>% filter(created_date < menstrual_onset_date_inferred) %>% View
mons %>% group_by(session, created_date) %>% filter(n()> 1)
mons %>% distinct(session, created_date) %>% nrow()
mons %>% group_by(session) %>% filter("lab" %in% date_origin)
# mons %>% filter(session %starts_with% "2x-juq") %>% View()
# now turn our dataset of menstrual onsets into full time series
menstrual_days = mons %>% distinct(session, created_date) %>%
arrange(session, created_date) %>%
# step 2 expand into time-series for participant
full_join(s3_daily %>% select(session, created_date), by = c("session", "created_date")) %>%
full_join(mons %>% mutate(created_date = menstrual_onset_date_inferred), by = c("session", "created_date")) %>%
mutate(date_origin = if_else(is.na(date_origin), "not_onset", date_origin)) %>%
group_by(session) %>%
complete(created_date = full_seq(created_date, period = 1)) %>%
mutate(date_origin = if_else(is.na(date_origin), "unobserved_day", date_origin)) %>%
arrange(created_date) %>%
distinct(session, created_date, menstrual_onset_date_inferred, .keep_all = TRUE) %>%
arrange(session, created_date, menstrual_onset_date_inferred) %>%
distinct(session, created_date, .keep_all = TRUE)
table(menstrual_days$date_origin, exclude = NULL)
menstrual_days %>% filter(date_origin != "filledin") %>% group_by(session) %>% summarise(n = n()) %>% summarise(mean(n))
menstrual_days %>% group_by(session) %>% summarise(n = n()) %>% summarise(mean(n))
menstrual_days %>% group_by(session) %>% summarise(n = n()) %>% pull(n) %>% qplot()
menstrual_days %>% drop_na(session, created_date) %>%
group_by(session, created_date) %>% filter(n()>1) %>% nrow() %>% { . == 0} %>% stopifnot()
# menstrual_onsets %>% filter(session == "_2efChMgmsXAYmalYlRY9epxS_wse0ytWYttV6tLi6FUd2FRENkr9JgVnmtzaMCs")
# mons %>% filter(session %starts_with% "_2sufSUfIWjNXg6xfRzJaCid9jzkY") %>% View()
# menstrual_onsets %>% filter(session %starts_with% "_2sufSUfIWjNXg6xfRzJaCid9jzkY") %>% View()
menstrual_days = menstrual_days %>%
group_by(session) %>%
mutate(
# carry the last observation (the last observed menstrual onset) backward/forward (within person), but we don't do this if we'd bridge more than 40 days this way
# first we carry it backward (because reporting is retrospective)
next_menstrual_onset = rcamisc::repeat_last(menstrual_onset_date_inferred, forward = FALSE),
# then we carry it forward
last_menstrual_onset = rcamisc::repeat_last(menstrual_onset_date_inferred),
# in the next cycle, count to the next onset, not the last
next_menstrual_onset = if_else(next_menstrual_onset == last_menstrual_onset,
lead(next_menstrual_onset),
next_menstrual_onset),
# calculate the diff to current date
menstrual_onset_days_until = as.numeric(created_date - next_menstrual_onset),
menstrual_onset_days_since = as.numeric(created_date - last_menstrual_onset)
)
menstrual_days %>% drop_na(session, created_date) %>%
group_by(session, created_date) %>% filter(n()>1) %>% nrow() %>% { . == 0} %>% stopifnot()
avg_cycle_lengths = menstrual_days %>%
select(session, last_menstrual_onset, next_menstrual_onset) %>%
mutate(next_menstrual_onset_if_no_last = if_else(is.na(last_menstrual_onset), next_menstrual_onset, as.Date(NA_character_))) %>%
arrange(session, next_menstrual_onset_if_no_last, last_menstrual_onset) %>%
select(-next_menstrual_onset) %>%
distinct(session, last_menstrual_onset, next_menstrual_onset_if_no_last, .keep_all = TRUE) %>%
group_by(session) %>%
mutate(
number_of_cycles = n(),
cycle_nr = row_number(),
cycle_length = as.double(lead(last_menstrual_onset) - last_menstrual_onset, units = "days"),
cycle_nr_fully_observed = sum(!is.na(cycle_length)),
mean_cycle_length_diary = mean(cycle_length, na.rm = TRUE),
median_cycle_length_diary = median(cycle_length, na.rm = TRUE)) %>%
filter(!is.na(last_menstrual_onset) | !is.na(next_menstrual_onset_if_no_last))
# avg_cycle_lengths %>% filter(session %starts_with% "_sqtMf5") %>% View("cycles")
table(is.na(avg_cycle_lengths$cycle_nr))
# menstrual_onsets %>% filter(session %starts_with% "_2sufSUfIWjNXg6xfRzJaCid9jzkY") %>% View()
gaps <- s3_daily %>% filter(session %starts_with% "--_MgFd") %>% tbl_df() %>% pull(created_date) %>% diff() %>% as.numeric(.)
stopifnot(!all(gaps == 1))
s3_daily <- s3_daily %>%
group_by(session) %>%
complete(created_date = full_seq(created_date, period = 1)) %>% # include the gap days in the diary (happens by default in formr, this just to ensure)
ungroup() %>%
mutate(diary_day_observation = case_when(
is.na(created) ~ "interpolated",
is.na(modified) ~ "not_answered",
!is.na(expired) ~ "started_not_finished",
is.na(ended) ~ "not_finished",
!is.na(ended) ~ "finished"
)) %>%
left_join(menstrual_days %>%
select(session, created_date, next_menstrual_onset, last_menstrual_onset, menstrual_onset_days_until, menstrual_onset_days_since, date_origin),
by = c("session", "created_date")
) %>%
mutate(
menstruation_today = if_else(menstruation_since_last_entry == 1, as.numeric(menstruation_today), 0),
menstruation_labelled = factor(if_else(! is.na(menstruation_today),
if_else(menstruation_today == 1, "yes", "no"),
if_else(menstrual_onset_days_since <= 5,
if_else(menstrual_onset_days_since == 0, "yes", "probably", "no"),
"no", "no")),
levels = c('yes', 'probably', 'no'))
) %>%
mutate(next_menstrual_onset_if_no_last = if_else(is.na(last_menstrual_onset), next_menstrual_onset, as.Date(NA_character_)))
gaps <- s3_daily %>% filter(session %starts_with% "--_MgFd") %>% tbl_df() %>% pull(created_date) %>% diff() %>% as.numeric(.)
stopifnot(all(gaps == 1))
s3_daily <- s3_daily %>%
group_by(session) %>%
mutate(first_diary_day = first(na.omit(first_diary_day)),
day_number = round(as.numeric(as.Date(created_date) - first_diary_day, unit = 'days'))) %>%
ungroup()
# s3_daily %>% filter(is.na(day_number)) %>% select(session, short, created_date, ended, first_diary_day) %>% arrange(short, created_date) %>% View
table(s3_daily$day_number, exclude = NULL)
table(s3_daily %>% drop_na(ended) %>% pull(day_number), exclude = NULL)
testthat::expect_true(all(s3_daily %>% drop_na(ended) %>% pull(day_number) %in% 0:70))
stopifnot(s3_daily %>% drop_na(session, day_number) %>% group_by(session, day_number) %>% filter(n() > 1) %>% nrow() == 0)
gaps <- s3_daily %>%
drop_na(session) %>%
group_by(session) %>%
summarise(no_gaps = all(as.numeric(diff(created_date)) == 1),
n = n(),
range = paste(range(day_number), collapse = '-'))
stopifnot(all(gaps$no_gaps))
# sort(table(gaps$range))
```
### Estimate day of ovulation
```{r}
# s3_daily %>% filter(short == "_sqtMf5") %>% select(short, created_date, ended, menstruation_labelled, next_menstrual_onset_if_no_last, last_menstrual_onset) %>% View("days")
s3_daily <- s3_daily %>%
left_join(avg_cycle_lengths, by = c("session", "last_menstrual_onset", "next_menstrual_onset_if_no_last")) %>%
left_join(s1_demo %>% select(session, menstruation_length), by = 'session') %>%
mutate(
next_menstrual_onset_inferred = last_menstrual_onset + days(menstruation_length),
RCD_inferred = as.numeric(created_date - next_menstrual_onset_inferred)
)
s3_daily %>% filter(short == "_sqtMf5", created_date == "2016-08-25") %>% pull(cycle_nr) %>% is.na() %>% isFALSE() %>% stopifnot()
xtabs(~ s3_daily$diary_day_observation + is.na(s3_daily$cycle_nr))
s3_daily <- s3_daily %>%
group_by(session, cycle_nr) %>%
mutate(
luteal_BC = if_else(menstrual_onset_days_until >= -15, 1, 0),
follicular_FC = if_else(menstrual_onset_days_since <= 15, 1, 0)
) %>%
mutate(
day_lh_surge = if_else(created_date == `Date LH surge`, 1, 0),
day_of_ovulation = if_else(menstrual_onset_days_until == -15, 1, 0),
day_of_ovulation_inferred = if_else(RCD_inferred == -15, 1, 0),
day_of_ovulation_forward_counted = if_else(menstrual_onset_days_since == 14, 1, 0),
date_of_ovulation_BC = min(if_else(day_of_ovulation == 1, created_date, structure(NA_real_, class="Date")), na.rm = TRUE),
date_of_ovulation_inferred = min(if_else(day_of_ovulation_inferred == 1, created_date, structure(NA_real_, class="Date")), na.rm = TRUE),
date_of_ovulation_forward_counted = min(if_else(day_of_ovulation_forward_counted == 1, created_date, structure(NA_real_, class="Date")), na.rm = TRUE),
date_of_ovulation_LH = min(`Date LH surge` + days(1), na.rm = T),
DRLH = as.numeric(created_date - date_of_ovulation_LH),
DRLH = if_else(between(DRLH, -15, 15), DRLH, NA_real_)
) %>%
ungroup() %>%
mutate_at(vars(starts_with("date_of_ovulation_")), funs(if_else(is.infinite(.), as.Date(NA_character_),.)))
s3_daily <- s3_daily %>%
group_by(short, cycle_nr) %>%
mutate(date_of_ovulation_awareness_nr = n_nonmissing(date_of_ovulation_awareness),
date_of_ovulation_awareness = if_else(date_of_ovulation_awareness_nr == 1 &
window_length > 3 & window_length < 9,
first(na.omit(date_of_ovulation_awareness)), as.Date(NA_character_))) %>%
mutate(fertile_awareness = case_when(
is.na(date_of_ovulation_awareness) ~ NA_real_,
created_date < (date_of_ovulation_awareness + 1 - window_length) ~ 0,
created_date > (date_of_ovulation_awareness + 1) ~ 0,
TRUE ~ 1
)) %>%
ungroup()
table(!is.na(s3_daily$date_of_ovulation_awareness))
table(tracked_windows$window_length > 8)
qplot(tracked_windows$window_length)
s3_daily <- s3_daily %>%
left_join(s4_followup %>% select(session, follicular_phase_length, luteal_phase_length), by = 'session') %>%
mutate(
date_of_ovulation_avg_follicular = last_menstrual_onset + days(follicular_phase_length),
date_of_ovulation_avg_luteal = next_menstrual_onset - days(luteal_phase_length + 1),
date_of_ovulation_avg_luteal_inferred = next_menstrual_onset_inferred - days(luteal_phase_length)
) %>% select(
-luteal_phase_length, -follicular_phase_length
)
s3_daily %>%
group_by(short) %>%
summarise(surges = n_distinct(`Date LH surge`, na.rm = T)) %>%
filter(surges > 0) %>%
pull(surges) %>%
table()
# s3_daily %>%
# drop_na(session, cycle_nr) %>%
# group_by(short) %>%
# filter(4 == n_distinct(`Date LH surge`, na.rm = T)) %>% select(short, ended, DRLH, day_number, cycle_nr, created_date,menstrual_onset_days_until, menstrual_onset_days_since, `Date LH surge`) %>% View()
# s3_daily %>%
# drop_na(session, cycle_nr) %>%
# group_by(short, cycle_nr) %>%
# filter(2 == n_distinct(`Date LH surge`, na.rm = T)) %>% select(short, ended, day_number, DRLH, cycle_nr, created_date,menstrual_onset_days_until, menstrual_onset_days_since, `Date LH surge`) %>% View()
# one case of a woman who reported two surges (close together in one cycle, we use the first surge)
s3_daily %>%
group_by(short, cycle_nr) %>%
summarise(surges = n_distinct(`Date LH surge`, na.rm = T)) %>%
filter(surges > 0) %>%
pull(surges) %>%
table()
stopifnot(s3_daily %>% drop_na(session, created) %>%
group_by(session, created) %>% filter(n()>1) %>% nrow() == 0)
# s3_daily %>% filter(session %starts_with% "_2sufSUfIWjNXg6xfRzJaCid9jzkY") %>% select(created_date, menstrual_onset, menstrual_onset_date, menstrual_onset_days_until, menstrual_onset_days_since) %>% View()
# s3_daily %>% filter(session %starts_with% "2x-juq") %>% select(created_date, menstrual_onset, menstrual_onset_date, menstrual_onset_days_until, menstrual_onset_days_since) %>% View()
s3_daily %>% filter(is.na(cycle_nr), !is.na(next_menstrual_onset)) %>% select(short, cycle_nr, last_menstrual_onset, next_menstrual_onset) %>% nrow() %>% { . == 0 } %>% stopifnot()
s3_daily %>% filter(is.na(cycle_nr), !is.na(last_menstrual_onset)) %>% select(short, cycle_nr, last_menstrual_onset, next_menstrual_onset) %>% nrow() %>% { . == 0 } %>% stopifnot()
# There are some 56 days across women for whom we have a last menstrual onset, but no cycle info. This happens when a last menstrual onset was reported that was more than 40 days before the beginning of the diary
crosstabs(~ is.na(cycle_nr) + is.na(menstruation_length), s3_daily)
crosstabs(~ is.na(cycle_nr) + is.na(menstruation_length), s3_daily %>% filter(diary_day_observation == "finished"))
#
# s3_daily %>% filter(is.na(cycle_nr), !is.na(menstruation_length)) %>% select(short, created_date, ended, cycle_nr, last_menstrual_onset, next_menstrual_onset) %>% View
# s1_demo %>% filter(short=="-ontLSS") %>% select(ended, contains("menst"))
```
### Estimate fertile window probability
```{r fertility_estimation}
s3_daily = s3_daily %>%
mutate(
FCD = menstrual_onset_days_since + 1,
RCD = menstrual_onset_days_until,
DAL = created_date - date_of_ovulation_avg_luteal,
RCD_squished = if_else(
cycle_length - FCD < 14,
29 - (cycle_length - FCD),
((FCD/ (cycle_length - 14) ) * 15)),
RCD_squished = if_else(RCD_squished < 1, 1, RCD_squished),
RCD_squished = if_else(RCD < -40, NA_real_, RCD_squished) - 30,
RCD_squished_rounded = round(RCD_squished),
RCD_inferred_squished = if_else(
FCD > menstruation_length,
NA_real_,
if_else(
as.numeric(menstruation_length) - FCD < 14,
29 - (as.numeric(menstruation_length) - FCD),
round((FCD/ (as.numeric(menstruation_length) - 14) ) * 15))
),
RCD_inferred_squished = if_else(RCD_inferred_squished < 1, 1, RCD_inferred_squished),
RCD_inferred_squished = if_else(RCD_inferred < -40, NA_real_, RCD_inferred_squished) - 30,
# add 15 days to the reverse cycle days to arrive at the estimated day of ovulation
RCD_rel_to_ovulation = RCD + 15,
RCD_fab = RCD_squished
)
table(s3_daily$RCD_inferred_squished)
table(s3_daily$RCD_squished)
table(s3_daily$RCD)
table(s3_daily$RCD_inferred)
table(s3_daily$RCD_inferred_squished)
table(s3_daily$RCD_inferred > -1)
crosstabs(s3_daily$RCD_inferred[is.na(s3_daily$RCD_inferred_squished)]) %>% sort()
crosstabs(s3_daily$RCD[is.na(s3_daily$RCD_squished)]) %>% sort()
crosstabs(s3_daily$RCD_inferred_squished)
table(s3_daily$FCD)
days <- data.frame(
RCD = c(-28:-1, -29:-40),
FCD = c(1:40),
prc_stirn_b = c(.01, .01, .02, .03, .05, .09, .16, .27, .38, .48, .56, .58, .55, .48, .38, .28, .20, .14, .10, .07, .06, .04, .03, .02, .01, .01, .01, .01, rep(.01, times = 12)),
# rep(.01, times = 70)), # gangestad uses .01 here, but I think such cases are better thrown than kept, since we might simply have missed a mens
prc_wcx_b = c(.000, .000, .001, .002, .004, .009, .018, .032, .050, .069, .085, .094, .093, .085, .073, .059, .047, .036, .028, .021, .016, .013, .010, .008, .007, .006, .005, .005, rep(.005, times = 12))
)
# rep(NA_real_, times = 70)) # gangestad uses .005 here, but I think such cases are better thrown than kept, since we might simply have missed a mens
days = days %>% mutate(
fertile_narrow = if_else(between(RCD,-18, -14), mean(prc_stirn_b[between(RCD, -18, -14)], na.rm = T),
if_else(between(RCD, -11, -3), mean(prc_stirn_b[between(RCD,-11, -3)], na.rm = T), NA_real_)), # these days are likely infertile
fertile_broad = if_else(between(RCD,-21,-13), mean(prc_stirn_b[between(RCD,-21,-13)], na.rm = T),
if_else(between(RCD,-11,-3), mean(prc_stirn_b[between(RCD,-11,-3)], na.rm = T), NA_real_)), # these days are likely infertile
fertile_window = factor(if_else(fertile_broad > 0.1, if_else(!is.na(fertile_narrow), "narrow", "broad"),"infertile"), levels = c("infertile","broad", "narrow")),
premenstrual_phase = ifelse(between(RCD, -6, -1), TRUE, FALSE)
)
# lh_days = days %>% mutate(
# DRLH =
# FCD
# - 1 # because FCD starts counting at 1
# - 15 # because ovulation happens on ~14.6 days after menstrual onset
# # + 1 # we already added 1 to the date of the LH surge above, as it happens 24-48 hours before ovulation
# ) %>% select(-FCD, -RCD_for_merge)
# from Jünger/Stern et al. 2018 Supplementary Material
# Day relative to ovulation Schwartz et al., (1980) Wilcox et al., (1998) Colombo & Masarotto (2000) Weighted average
lh_days <- tibble(
conception_risk_lh = c(0.00, 0.01, 0.02, 0.06, 0.16, 0.20, 0.25, 0.24, 0.10, 0.02, 0.02 ),
DRLH = c(-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2)
) %>%
mutate(fertile_lh = conception_risk_lh/max(conception_risk_lh))
# from blake et al. supplement (unweighted)
blake_meta <- tibble::tibble(DRLH = -10:6, CR = c(0,0,0.00267,
0.00998,
0.02600,
0.06180,
0.11600,
0.15917,
0.20717,
0.21633,
0.15667,
0.06540,
0.05250,
0.01550,
0.00300,
0.00000, 0))
blake_meta %>% left_join(lh_days) %>%
mutate(conception_risk_lh = na_if(conception_risk_lh, 0)) %>%
summarise(cor(conception_risk_lh, CR, use = 'pairwise.complete.obs'))
# blake and juenger values are very close, I'll use Juenger
s3_daily = s3_daily %>% left_join(lh_days, by = "DRLH") %>%
mutate(fertile_lh = if_else(is.na(fertile_lh) &
between(DRLH, -15, 15), 0, fertile_lh))
rcd_days = days %>% select(-FCD)
s3_daily = left_join(s3_daily, rcd_days, by = "RCD")
rcd_squished = days %>% select(-FCD)
names(rcd_squished) = paste0(names(rcd_squished), "_squished")
s3_daily = left_join(s3_daily, rcd_squished, by = c("RCD_squished_rounded" = "RCD_squished"))
rcd_inferred_squished = days %>% select(-FCD)
names(rcd_inferred_squished) = paste0(names(rcd_inferred_squished), "_inferred_squished")
s3_daily = left_join(s3_daily, rcd_inferred_squished, by = "RCD_inferred_squished")
fcd_days = days %>% select(-RCD)
names(fcd_days) = paste0(names(fcd_days), "_forward_counted")
fcd_days = fcd_days %>% rename(FCD = FCD_forward_counted)
s3_daily = left_join(s3_daily, fcd_days, by = "FCD")
aware_luteal_squished = days %>% select(-FCD) %>% mutate(RCD = RCD + 15)
names(aware_luteal_squished) = paste0(names(aware_luteal_squished), "_aware_luteal")
s3_daily$DAL <- as.numeric(s3_daily$DAL)
s3_daily = left_join(s3_daily, aware_luteal_squished, by = c("DAL" = "RCD_aware_luteal"))
rcd_inferred_days = days %>% select(-FCD)
names(rcd_inferred_days) = paste0(names(rcd_inferred_days), "_inferred")
s3_daily = left_join(s3_daily, rcd_inferred_days, by = "RCD_inferred")
table(s3_daily$prc_stirn_b_inferred)
# s3_daily %>% filter(is.na(prc_stirn_b_inferred_squished), !is.na(prc_stirn_b_inferred)) %>% select(short, menstruation_length, cycle_nr, cycle_length, day_number, created_date, last_menstrual_onset, next_menstrual_onset, next_menstrual_onset_inferred, FCD, RCD, RCD_squished, RCD_inferred, RCD_inferred_squished, RCD_inferred, prc_stirn_b_inferred_squished, prc_stirn_b) %>% rcamisc::view_in_excel()
xtabs(~ is.na(prc_stirn_b_inferred_squished) + is.na(prc_stirn_b_inferred), data = s3_daily)
xtabs(~ is.na(prc_stirn_b_squished) + is.na(prc_stirn_b), data = s3_daily)
xtabs(~ is.na(prc_stirn_b_squished) + is.na(RCD_squished), data = s3_daily)
xtabs(~ is.na(prc_stirn_b_inferred_squished) + is.na(prc_stirn_b_squished), data = s3_daily)
s3_daily = s3_daily %>%
mutate(fertile_fab = prc_stirn_b,
premenstrual_phase_fab = premenstrual_phase
)
var_label(s3_daily$fertile_fab) <- "Est. fertile window prob. (BC+i)"
var_label(s3_daily$premenstrual_phase_fab) <- "Est. premenstrual phase (BC+i)"
s3_daily %>% select(fertile_fab, premenstrual_phase_fab, menstruation_labelled) %>% na.omit() %>% nrow()
s3_daily %>% select(fertile_fab, premenstrual_phase_fab, menstruation_labelled) %>% codebook::md_pattern()
# s3_daily %>% filter(is.na(menstruation_labelled), !is.na(fertile_fab)) %>% select(short, created_date, ended, menstruation_labelled, menstrual_onset, menstrual_onset_date, menstrual_onset_days_until, menstrual_onset_days_since) %>% View()
```
test some special corner cases
```{r}
# we did correctly infer FCDs from onset reported before the diary
s3_daily %>% filter(session %starts_with% "_2efChM") %>% slice(1) %>% pull(FCD) %>% is.na() %>% isFALSE %>% stopifnot()
# we did correctly add cycle nrs even when we didnt observe the cycle's end
s3_daily %>% filter(short == "_sqtMf5", created_date == "2016-08-25") %>% pull(cycle_nr) %>% is.na() %>% isFALSE() %>% stopifnot()
```
### Infer menstruation
We did not ask about menstruation on every day, so as not to give away the purpose of the study.
We can estimate the probability of menstruation quite well from other variables.
```{r}
infer_mens_df <- s3_daily %>% group_by(short) %>%
mutate(
premenstrual_phase = if_else(premenstrual_phase_fab == 1, "1", "0", "unknown"),
postmenstrual_phase = if_else(menstrual_onset_days_since < 6, "1", "0", "unknown"),
cycle_length = if_else(cycle_length > 34, "35+", as.character(cycle_length), "unknown"),
menstrual_onset_days_since = if_else(menstrual_onset_days_since > 9, "10+", as.character(menstrual_onset_days_since), "unknown"),
menstrual_pain = if_else(menstrual_pain == 1, "1", "0", "0"),
menstruation_lag3 = if_else(lag(menstruation_today, 3) == 1, "1", "0", "unknown"),
menstruation_lead3 = if_else(lead(menstruation_today, 3) == 1, "1", "0", "unknown")) %>%
ungroup() %>%
mutate_at(vars(menstruation_lag3, menstruation_lead3, menstrual_pain, premenstrual_phase, postmenstrual_phase), funs(factor)) %>%
mutate(menstrual_onset_days_since = factor(menstrual_onset_days_since, levels = c("unknown", 0:9, "10+"))) %>%
select(short, menstruation_today, menstrual_onset_days_since, cycle_length,
menstruation_lag3, menstruation_lead3, menstrual_pain, premenstrual_phase, postmenstrual_phase)
infer_mens_df %>% select(-menstruation_today, -short) %>% drop_na %>% nrow()
infer_mens_noran <- glm(menstruation_today ~ premenstrual_phase * menstrual_pain + menstrual_onset_days_since, data = infer_mens_df, family = binomial)
infer_mens_noran
DescTools::PseudoR2(infer_mens_noran, which = "Nagelkerke")
infer_mens <- lme4::glmer(menstruation_today ~ premenstrual_phase * menstrual_pain + menstrual_onset_days_since + (1 + menstrual_pain | short), data = infer_mens_df, family = binomial, na.action = na.exclude)
# plot(allEffects(infer_mens))
menstruation_imputed_noran <- predict(infer_mens_noran, newdata = infer_mens_df %>% select(-menstruation_today), type = "response", allow.new.levels = TRUE)
s3_daily$menstruation_imputed <- predict(infer_mens, newdata = infer_mens_df %>% select(-menstruation_today), type = "response", allow.new.levels = TRUE)
cor.test(s3_daily$menstruation_imputed, s3_daily$menstruation_today)
cor.test(menstruation_imputed_noran, s3_daily$menstruation_today)
s3_daily$menstruation <- if_else(is.na(s3_daily$menstruation_today), s3_daily$menstruation_imputed, as.double(s3_daily$menstruation_today))
sum(!is.na(s3_daily$menstruation_imputed))
qplot(s3_daily$menstruation_imputed, fill = if_else(s3_daily$menstruation_today == 1, "1", "0", "unknown"),) + scale_fill_colorblind("Actual")
qplot(menstrual_onset_days_since, menstruation, data = s3_daily, geom = "blank") + geom_smooth(stat = 'summary', fun.data = 'mean_se') + xlim(0,15)
s3_daily <- s3_daily %>% select(-menstruation_length)
var_label(s3_daily$menstruation) <- "Est. menstruation"
```
## Contraception
### Other hormonal contraception
For pills and other hormonal contraception that was not in our list.
```{r}
s1_demo %>%
filter(!is.na(other_pill_name) | !is.na(contraception_hormonal_other) | !is.na(contraception_method_other)) %>%
select(other_pill_name, contraception_hormonal_other, contraception_method_other) %>%
mutate(other_pill_name = str_to_lower(other_pill_name)) %>%
distinct() %>%
mutate(contraception_other_pill_estrogen = NA_real_,
contraception_other_pill_gestagen = NA_real_,
contraception_other_pill_gestagen_type = NA_real_
) ->
other_pill_name
rcamisc:::view_in_excel(other_pill_name)
rio::export(other_pill_name, "codings/other_pill_name.xlsx")
other_pill_name = readxl::read_xlsx("codings/other_pill_name_coded.xlsx",1)
s1_demo <- s1_demo %>% left_join(
other_pill_name %>% distinct())
```
```{r contraception}
s1_demo = s1_demo %>%
mutate(hormonal_contraception = if_else(contraception_method %contains% "hormonal", T, F, missing = F),
contraception_method_broad = stringr::str_split_fixed(contraception_method, "_", 2)[,1]
)
sort(table(s1_demo$contraception_method))
unique(s1_demo$contraception_method_other) # todo: code manually
sort(table(s1_demo$contraception_combi))
unique(s1_demo$contraception_method_combination_other)
s1_demo = s1_demo %>% mutate(
contraception_calendar_abstinence = stringr::str_replace(contraception_calendar_abstinence, "1", "abstinence"),
contraception_calendar_abstinence = stringr::str_replace(contraception_calendar_abstinence, "2", "no_penetration"),
contraception_calendar_abstinence = stringr::str_replace(contraception_calendar_abstinence, "3", "less_sex"),
contraception_calendar_abstinence = stringr::str_replace(contraception_calendar_abstinence, "4", "other_method")
)
choices <- rio::import("https://docs.google.com/spreadsheets/d/1tLQDVyYUAXLBkblTT8BXow_rcg5G6xK9Vi3xTGieN20/edit#gid=1116762580", which = 2)
pills <- choices %>%
slice(1:182) %>%
filter(!is.na(name), name != "") %>%
mutate(
list_name = na_if(list_name, ""),
list_name = zoo::na.locf(list_name)
) %>%
filter(list_name == "pills") %>%
select(contraception_hormonal_pill = name,
contraception_hormonal_pill_estrogen =
`Östrogenmikrogramm pro Zyklus`,
contraception_hormonal_pill_gestagen_type =
`Art des Gestagens`
) %>%
mutate(contraception_hormonal_pill_estrogen =
as.numeric(contraception_hormonal_pill_estrogen)/21)
s1_demo <- s1_demo %>%
left_join(pills, by = "contraception_hormonal_pill")
s1_demo <- s1_demo %>%
mutate(contraception_hormonal_pill_estrogen = if_na(contraception_hormonal_pill_estrogen, contraception_pill_estrogen),
contraception_hormonal_pill_gestagen_type = if_na(contraception_hormonal_pill_gestagen_type, contraception_pill_gestagen_type))
s1_demo <- s1_demo %>%
mutate(estrogen_progestogen = case_when(
contraception_hormonal_other == "depo_clinovir" ~ "progestogen_only",
contraception_hormonal_other == "implanon" ~ "progestogen_only",
contraception_hormonal_other_name %contains% "aydess" ~ "progestogen_only",
contraception_hormonal_other_name %contains% "Mirena" ~ "progestogen_only",
contraception_hormonal_other_name %contains% "Lisvy" ~ "progestogen_and_estrogen",
contraception_hormonal_other %contains% "mirena" ~ "progestogen_only",
contraception_hormonal_other != "mirena" ~ "progestogen_and_estrogen",
contraception_hormonal_pill %in% c("28_mini", "cerazette",
"cyprella", "damara",
"desirett",
"diamilla", "jubrele", "microlut",
"seculact") ~ "progestogen_only",
contraception_pill_estrogen == 0 & contraception_pill_gestagen > 0 ~ "progestogen_only",
contraception_method %contains% "hormonal_pill" ~ "progestogen_and_estrogen",
contraception_method %contains% "hormonal_morning_after_pill" ~ NA_character_,
TRUE ~ "non_hormonal"
)
)
crosstabs(~ estrogen_progestogen + hormonal_contraception, s1_demo)
# s1_demo %>% drop_na(other_pill_name) %>% select(other_pill_name, contraception_pill_estrogen,
# contraception_pill_gestagen, contraception_pill_gestagen_type) %>% View()
# s1_demo %>% drop_na(contraception_hormonal_other_name) %>% select(contraception_hormonal_other_name, contraception_other_estrogen,
# contraception_other_gestagen, contraception_other_gestagen_type) %>% View()
sort(table(s1_demo$estrogen_progestogen))
sort(table(s1_demo$contraception_calendar_abstinence))
sort(table(s1_demo$contraception_hormonal_other))
sort(table(s1_demo$contraception_hormonal_other_name))
table(s1_demo$contraception_app)
common_apps = table(tolower(stringr::str_trim(s1_demo$contraception_app_name)))
sort(common_apps[common_apps > 3])
table(s1_demo$pregnant_trying)
sort(table(s1_demo$wish_for_children))
```
## Singles vs couples
```{r singles}
s1_demo = s1_demo %>% mutate(hetero_relationship = as.numeric(hetero_relationship))
s1_demo %>%
count(hetero_relationship)
s1_demo %>%
left_join(s1_filter %>% select(session, gets_paid)) %>%
count(gets_paid, hetero_relationship) %>%
na.omit()
```
### SOI
```{r}
old_labels <- s2_initial$soi_r_desire_7 %>% val_labels()
new_labels <- 1:5
names(new_labels) <- names(old_labels)
s2_initial <- s2_initial %>% mutate_at(vars(soi_r_desire_7, soi_r_desire_9, soi_r_desire_8), funs(
recode(., "never" = 1, "rarely" = 2, "monthly" = 3, "weekly" = 4, "daily" = 5)
)) %>%
labelled::set_value_labels(soi_r_desire_7 = new_labels, soi_r_desire_9 = new_labels, soi_r_desire_8 = new_labels)
s2_initial$soi_r_desire = s2_initial %>% ungroup() %>% select(soi_r_desire_7, soi_r_desire_9, soi_r_desire_8) %>% aggregate_and_document_scale(fun = robust_rowmeans)
var_label(s2_initial$soi_r_desire_8) <- "Sociosexual inventory-revised: Desire Subscale"
cutpoints <- c("0" = 1,
"1" = 2,
"2-3" = 3,
"4-7" = 4,
"8 or more" = 5)
s2_initial <- s2_initial %>% mutate_at(vars(soi_r_behavior_1, soi_r_behavior_2, soi_r_behavior_3),
funs(discrete = case_when(
. == 0 ~ 1,
. == 1 ~ 2,
. %in% 2:3 ~ 3,
. %in% 4:7 ~ 4,
. %in% 8:1e4 ~ 5))) %>%
labelled::set_value_labels(soi_r_behavior_1_discrete = cutpoints, soi_r_behavior_2_discrete = cutpoints, soi_r_behavior_3_discrete = cutpoints)
var_label(s2_initial$soi_r_behavior_1_discrete) <- var_label(s2_initial$soi_r_behavior_1)
var_label(s2_initial$soi_r_behavior_2_discrete) <- var_label(s2_initial$soi_r_behavior_2)
var_label(s2_initial$soi_r_behavior_3_discrete) <- var_label(s2_initial$soi_r_behavior_3)
s2_initial$soi_r_behavior = s2_initial %>% ungroup() %>% select(soi_r_behavior_1_discrete, soi_r_behavior_2_discrete, soi_r_behavior_3_discrete) %>% aggregate_and_document_scale(fun = robust_rowmeans)
var_label(s2_initial$soi_r_behavior) <- "Sociosexual inventory-revised: Behaviour Subscale"
s2_initial$soi_r = s2_initial %>% ungroup() %>% select(soi_r_attitude_6r, soi_r_attitude_4, soi_r_attitude_5, soi_r_desire_7, soi_r_desire_9, soi_r_desire_8, soi_r_behavior_1_discrete, soi_r_behavior_2_discrete, soi_r_behavior_3_discrete) %>% aggregate_and_document_scale(fun = robust_rowmeans)
var_label(s2_initial$soi_r) <- "Sociosexual inventory-revised"
```
### Partner attractiveness items
```{r}
s2_initial <- s2_initial %>%
rename(partner_sexiness = attractiveness_sexy,
partner_attractiveness_body = attractiveness_body,
partner_attractiveness_face = attractiveness_face,
partner_attractiveness_shortterm = attractiveness_stp,
partner_attractiveness_longterm = attractiveness_ltp,
partner_attractiveness_trust = attractiveness_trustworthiness
) %>%
mutate(
spms_rel = spms_self - spms_partner
)
s2_initial$partner_attractiveness_sexual <- s2_initial %>% select(partner_sexiness, partner_attractiveness_shortterm, partner_attractiveness_face, partner_attractiveness_body) %>% aggregate_and_document_scale(fun = robust_rowmeans)
var_label(s2_initial$partner_attractiveness_sexual) <- "Partner's sexual attractiveness"
```
### Relationship satisfaction
```{r}
s2_initial$relationship_conflict_R = 6 - s2_initial$relationship_conflict
s2_initial$relationship_problems_R = 6 - s2_initial$relationship_problems
psych::alpha(s2_initial %>% select(relationship_problems_R, relationship_satisfaction_overall, relationship_conflict_R, relationship_satisfaction_2, relationship_satisfaction_3) %>% data.frame())
s2_initial$relationship_satisfaction = s2_initial %>% ungroup() %>% select(relationship_problems_R, relationship_satisfaction_overall, relationship_conflict_R, relationship_satisfaction_2, relationship_satisfaction_3) %>% aggregate_and_document_scale(fun = robust_rowmeans)
var_label(s2_initial$relationship_satisfaction) <- "Relationship satisfaction"
```
## Living situation
```{r}
s1_demo <- s1_demo %>%
mutate(
living_situation = case_when(
abode_alone == 1 ~ "alone",
abode_with_partner == 1 ~ "with partner",
abode_flat_share == 3 ~ "flatshare",
abode_flat_share == 2 ~ "with family",
nr_children > 1 ~ "with children",
hetero_relationship == 0 ~ "alone",
abode_flat_share == 1 ~ "other",
TRUE ~ "missing"
)
)
table(s1_demo$living_situation)
# s1_demo %>% filter(living_situation == "other") %>% select(starts_with("abode")) %>% View
```
## Merge surveys
```{r merge_xsect}
pre_surveys = s1_demo %>%
left_join(s2_initial, by = "session", suffix = c("_demo", "_initial")) # merge demo and personality stuff
all_surveys = pre_surveys %>%
left_join(s4_followup, by = "session") # add follow up survey
stopifnot(!any(duplicated(all_surveys$session)))
```
## Code open answer
### Guessed hypothesis?
```{r}
s4_followup %>% filter(!is.na(hypothesis_guess) & stringr::str_trim(hypothesis_guess) != "") %>% select(session, hypothesis_guess) %>%
mutate(
# if they mention hormones or menstruation or PMS, but not the cycle, fertile window, ovulation
hypothesis_hormones_mentioned = NA_real_,
# if they mention cycle/fertile window, but only generally or in combination with generics like "mood"
hypothesis_cycle_mentioned = 0,
# if they mention the cycle and sex/libido/attractiveness
hypothesis_cycle_sex = 0) %>%
data.frame() -> hypothesis_guessed
writexl::write_xlsx(hypothesis_guessed, "codings/hypothesis_guessed.xlsx")
hypothesis_guessed = readxl::read_xlsx("codings/hypothesis_guessed_coded.xlsx",1)
all_surveys = all_surveys %>% left_join(hypothesis_guessed %>% select(-hypothesis_guess), by = c("session"))
all_surveys$hypothesis_guess_topic <- 0
all_surveys <- all_surveys %>%
mutate(
hypothesis_guess_topic = replace(hypothesis_guess_topic, hypothesis_hormones_mentioned == 1, 1),
hypothesis_guess_topic = replace(hypothesis_guess_topic, hypothesis_cycle_mentioned == 1, 2),
hypothesis_guess_topic = replace(hypothesis_guess_topic, hypothesis_cycle_sex == 1, 3),
hypothesis_guess_topic = factor(hypothesis_guess_topic, level=c(0,1,2,3), labels=c('no_guess','hormones', 'cycle', 'cycle_sex'))
)
```
### app awareness
Did they use a menstrual cycle or pill app and was it one that would foster
awareness of the menstrual cycle?
```{r}
awareness <- rio::import("codings/awareness_coded.xlsx") %>% tbl_df()
rio::export(all_surveys %>% select(session, contraception_app_name, aware_fertile_reason_unusual, feedback_for_us) %>% full_join(awareness %>% select(session, cycle_awareness_app, cycle_awareness_other) %>%
distinct(), by = c("session")) %>% filter(contraception_app_name != "" | aware_fertile_reason_unusual != "" | feedback_for_us != "") %>% select(session, contraception_app_name, aware_fertile_reason_unusual, feedback_for_us, cycle_awareness_app, cycle_awareness_other), "codings/awareness.xlsx")