-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-cleandata.Rmd
188 lines (154 loc) · 7.59 KB
/
02-cleandata.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
---
title: "clean/join young and mature women datasets"
author: "Paloma Cárcamo"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse)
```
## Load datasets
Raw datasets downloaded from https://www.nlsinfo.org/investigator/pages/search#
Tagsets in data/maturewomen-full.MW and data/youngwomen-full2.MW
Downloaded data formatted using scripts 1a and 1b
```{r}
young_raw <- read_rds("data/young-women.rds")
mature_raw <- read_rds("data/mature-women.rds")
```
## Format young women dataset
```{r}
young <- young_raw |>
# Excluding HBP in 1995 or before and MI in or before 1997
filter((hbp_ever_91 != 1 | is.na(hbp_ever_91)) &
(hbp_curr_95 != 1 | is.na(hbp_curr_95)) &
(hbp_dx_95 != 1 | is.na(hbp_dx_95)) &
(heartprob_95 != 1 | is.na(heartprob_95)) &
(heartprob_97 != 1 | is.na(heartprob_97)) &
(mi_ever_95 != 1 | is.na(mi_ever_95)) &
(mi_ever_97 != 1 | is.na(mi_ever_97)) &
(mi_year_95 > 1997 | is.na(mi_year_95)) &
(mi_year_97 > 1997 | is.na(mi_year_97)) &
(mi_year_99 > 1997 | is.na(mi_year_99)) &
(mi_year_01 > 1997 | is.na(mi_year_01)) &
(mi_year_03 > 1997 | is.na(mi_year_03))) |>
# Excluding OC start after 1997 and no data on OC use
mutate(year_birth = 1968 - age_68,
oc_yearstart = oc_agestart_summary_03 + year_birth) |>
filter(oc_yearstart < 1997 | is.na(oc_yearstart)) |>
filter(!is.na(oc_ever_95) | !is.na(oc_agestart_summary_03) | !is.na(oc_agestop_summary_03)) |>
# Defining exposure
mutate(oc_use = if_else(oc_ever_95 == 1 | !is.na(oc_agestart_summary_03) | !is.na(oc_agestop_summary_03), 1, 0)) |>
# Defining mediator
rowwise() |>
mutate(year_mi = if (all(is.na(c(mi_year_95, mi_year_97, mi_year_99, mi_year_01, mi_year_03)))) {NA}
else {min(c(mi_year_95, mi_year_97, mi_year_99, mi_year_01, mi_year_03), na.rm = TRUE)}) |>
ungroup() |>
mutate(year_hbp = case_when(hbp_curr_03 == 1 ~ 2003,
hbp_dx_03 == 1 ~ 2003,
hbp_curr_01 == 1 ~ 2001,
hbp_dx_01 == 1 ~ 2001,
hbp_curr_99 == 1 ~ 1999,
hbp_dx_95 == 1 ~ 1999,
hbp_curr_97 == 1 ~ 1997,
hbp_dx_97 == 1 ~ 1997,
.default = NA),
hbp = case_when(year_hbp < year_mi ~ 1,
year_hbp > year_mi ~ 0,
hbp_dx_97 == 1 ~ 1,
hbp_curr_97 == 1 ~ 1,
hbp_dx_97 == 0 ~ 0,
hbp_curr_97 == 0 ~ 0,
.default = NA)) |>
# Defining outcome
mutate(mi = as.integer(if_any(c(mi_ever_99, mi_ever_01, mi_ever_03, heartprob_99, heartprob_01, heartprob_03), ~ . == 1 & !is.na(.)))) |>
# Excluding no data on mediator or outcome
filter(!is.na(mi) & !is.na(hbp)) |>
# Defining confounders
mutate(age = 1995-year_birth,
weight = if_else(weight_lb_91 < 100, NA, weight_lb_91),
weight_kg = weight*0.45,
height1_cm = height_ft_91*30.48,
height2_cm = height_in_91*2.54,
height_cm = height1_cm + height2_cm,
bmi = weight_kg/((height_cm/100)^2),
smoking = if_else(smoking_ever_91 == 1 | smoking_curr_93 == 1 | smoking_curr_95 == 1, 1, 0),
hbp_med = if_else(hbp_med_95 == 1 | hbp_med_97 == 1 | hbp_med_99 == 1 | hbp_med_01 == 1 | hbp_med_03 == 1, 1, 0),
hbp_med = if_else(is.na(hbp_med) & !is.na(hbp), 0, hbp)) |>
# Selecting final variables
select(id, year_birth, age, smoking, income = income_family_03, children = children_roster_03, bmi, oc_use, oc_yearstart = oc_agestart_summary_03, oc_yearstop = oc_agestop_summary_03, hbp, hbp_year = year_hbp, hbp_med, mi, mi_year = year_mi) |>
mutate(cohort = "young")
summary(young)
table(young$mi, young$oc_use, dnn = c("MI", "OC"))
# write_rds(young, "data/young-clean.rds")
```
## Format mature women dataset
```{r}
mature <- mature_raw |>
# Excluding HBP in 1995 or before and MI in or before 1997
filter((hbp_curr_95 != 1 | is.na(hbp_curr_95)) &
(hbp_dx_95 != 1 | is.na(hbp_dx_95)) &
(mi_ever_95 != 1 | is.na(mi_ever_95)) &
(mi_ever_97 != 1 | is.na(mi_ever_97)) &
(heartprob_95 != 1 | is.na(heartprob_95)) &
(heartprob_97 != 1 | is.na(heartprob_97)) &
(mi_year_95 > 1997 | is.na(mi_year_95)) &
(mi_year_97 > 1997 | is.na(mi_year_97)) &
(mi_year_99 > 1997 | is.na(mi_year_99)) &
(mi_year_01 > 1997 | is.na(mi_year_01)) &
(mi_year_03 > 1997 | is.na(mi_year_03))) |>
# Excluding OC start after 1997 and no data on OC use
mutate(year_birth = 1967 - age_67,
oc_yearstart = oc_agestart_summary_03 + year_birth) |>
filter(oc_yearstart < 1997 | is.na(oc_yearstart)) |>
filter(!is.na(oc_ever_95) | !is.na(oc_agestart_summary_03) | !is.na(oc_agestop_summary_03)) |>
# Defining exposure
mutate(oc_use = if_else(oc_ever_95 == 1 | !is.na(oc_agestart_summary_03) | !is.na(oc_agestop_summary_03), 1, 0)) |>
# Defining mediator
rowwise() |>
mutate(year_mi = if (all(is.na(c(mi_year_95, mi_year_97, mi_year_99, mi_year_01, mi_year_03)))) {NA}
else {min(c(mi_year_95, mi_year_97, mi_year_99, mi_year_01, mi_year_03), na.rm = TRUE)}) |>
ungroup() |>
mutate(year_hbp = case_when(hbp_curr_03 == 1 ~ 2003,
hbp_dx_03 == 1 ~ 2003,
hbp_curr_01 == 1 ~ 2001,
hbp_dx_01 == 1 ~ 2001,
hbp_curr_99 == 1 ~ 1999,
hbp_dx_95 == 1 ~ 1999,
hbp_curr_97 == 1 ~ 1997,
hbp_dx_97 == 1 ~ 1997,
.default = NA),
hbp = case_when(year_hbp < year_mi ~ 1,
year_hbp > year_mi ~ 0,
hbp_dx_97 == 1 ~ 1,
hbp_curr_97 == 1 ~ 1,
hbp_dx_97 == 0 ~ 0,
hbp_curr_97 == 0 ~ 0,
.default = NA)) |>
# Defining outcome
mutate(mi = as.integer(if_any(c(mi_ever_99, mi_ever_01, mi_ever_03, heartprob_99, heartprob_01, heartprob_03), ~ . == 1 & !is.na(.)))) |>
# Excluding no data on mediator or outcome
filter(!is.na(mi) & !is.na(hbp)) |>
# Defining confounders
mutate(age = 1995-year_birth,
weight = if_else(weight_lb_92 < 100, NA, weight_lb_92),
weight_kg = weight*0.45,
height1_cm = height_ft_92*30.48,
height2_cm = height_in_92*2.54,
height_cm = height1_cm + height2_cm,
bmi = weight_kg/((height_cm/100)^2),
smoking = if_else(smoking_curr_95 == 1, 1, 0),
hbp_med = if_else(hbp_med_95 == 1 | hbp_med_97 == 1 | hbp_med_99 == 1 | hbp_med_01 == 1 | hbp_med_03 == 1, 1, 0),
hbp_med = if_else(is.na(hbp_med) & !is.na(hbp), 0, hbp)) |>
# Selecting final variables
select(id, year_birth, age, smoking, income = income_family_03, children = children_roster_03, bmi, oc_use, oc_yearstart = oc_agestart_summary_03, oc_yearstop = oc_agestop_summary_03, hbp, hbp_year = year_hbp, hbp_med, mi, mi_year = year_mi) |>
mutate(cohort = "mature")
summary(mature)
table(mature$mi, mature$oc_use, dnn = c("MI", "OC"))
# write_rds(mature, "data/mature-clean.rds")
```
## Join datasets
```{r}
full_db <- rbind(young, mature)
# write_rds(full_db, "data/joined-db-clean.rds")
```