-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_dataclean_panel.R
319 lines (226 loc) · 12.9 KB
/
path_dataclean_panel.R
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
# Merge Data
adult_panel <- adult_w1 %>%
full_join(y = adult_w2, by = c('PERSONID')) %>%
full_join(y= adult_w3, by = c('PERSONID')
)
adult_panel %>% filter(wave_2 == 1, wave_3==1, wave_1 ==1) %>% count()
##### W1 -> W2: Transition Status Quit Categories ####
#Note: quit_w1_w2 doesn't have category for Wave 1 non-smoker/former-smokers?
# Note: smoker must have quit for year in order to be considered to have quit
adult_panel <- adult_panel %>%
mutate(quit_w1_w2 = case_when(
cig_use_now_w1 == 1 & cig_use_now_w2 == 1 ~ 'No',
cig_use_now_w1 == 1 & (cig_use_past12M_w2 == 0 | cig_use_now_w2 == 0) ~ 'Yes'),
quit_cat_w1_w2 = case_when(
cig_use_now_w1 == 1 & cig_use_now_w2 == 1 ~ 'No',
cig_use_now_w1 == 1 & (cig_use_past12M_w2 == 2 | cig_use_now_w2 == 0) ~ 'Yes',
cig_use_now_w1 == 0 & (cig_use_past12M_w2 == 2 | cig_use_now_w2 == 0) ~ 'Stayed Non-Smoker')
)
# W1 -> W3: Quit Status at W3 for 'W1 Current Smokers'
adult_panel <- adult_panel %>%
mutate(
quit_w1_w3 = case_when(
cig_use_now_w1 == 1 & cig_use_now_w3 == 1 ~ 'No',
cig_use_now_w1 == 1 & (cig_use_past12M_w3 == 0 | cig_use_now_w3 == 0) ~ 'Yes'),
quit_cat_w1_w3 = case_when(
cig_use_now_w1 == 1 & cig_use_now_w3 == 1 ~ 'No',
cig_use_now_w1 == 1 & (cig_use_past12M_w3 == 1 | cig_use_now_w3 == 0) ~ 'Yes',
cig_use_now_w1 == 0 & (cig_use_past12M_w3 == 1 | cig_use_now_w3 == 0) ~ 'Stayed Non-Smoker')
)
##### W2 -> W3: Quit Status at W3 for 'W2 Current Smokers' ####
adult_panel <- adult_panel %>%
mutate(
quit_w2_w3 = case_when(
cig_use_now_w2 == 1 & cig_use_now_w3 == 1 ~ 'No',
cig_use_now_w2 == 1 & (cig_use_past12M_w3 == 0 | cig_use_now_w3 == 0) ~ 'Yes'),
quit_cat_w2_w3 = case_when(
cig_use_now_w2 == 1 & cig_use_now_w3 == 1 ~ 'No',
cig_use_now_w2 == 1 & (cig_use_past12M_w3 == 1 | cig_use_now_w3 == 0) ~ 'Yes',
cig_use_now_w2 == 0 & (cig_use_past12M_w3 == 1 | cig_use_now_w3 == 0) ~ 'Stayed Non-Smoker'),
)
#### Convert to Factors ####
adult_panel <- adult_panel %>%
mutate(
quit_cat_w1_w2 = factor(quit_cat_w1_w3, levels = c('Yes', 'No', 'Stayed Non-Smoker')),
quit_cat_w1_w3 = factor(quit_cat_w1_w3, levels = c('Yes', 'No', 'Stayed Non-Smoker')),
quit_cat_w2_w3 = factor(quit_cat_w2_w3, levels = c('Yes', 'No', 'Stayed Non-Smoker'))
)
#### What is this? ####
# WAVE 3: P30D Quit Status at W3 for 'W2 Current Smokers'
# adult_panel <- adult_panel %>%
# mutate( quit_p30d_w2_w3 = case_when(
# cig_use_now_w2==1 & cig_use_now_w3==1 ~ 'No',
# cig_use_now_w2==1 & (cig_use_past12M_w3==0 | cig_use_now_w3== 0) ~ 'Yes')
# )
#### Recode NAs as Zero f####
#For obs. that was not included in a given wave
adult_panel <- adult_panel %>%
mutate(wave_1 = ifelse(is.na(wave_1), 0, wave_1),
wave_2 = ifelse(is.na(wave_2), 0, wave_2),
wave_3 = ifelse(is.na(wave_3), 0, wave_3)
)
# Note: recode -8, -7, -1 as NA?
adult_panel %>% group_by(cig_use_past12M_w2) %>% count
adult_panel %>% group_by(cig_use_now_w2) %>% count
adult_panel %>% group_by(cig_use_now_w1) %>% count
# Note: Some inconsistencies (n= 3)
# 1 adult in wave 1 and wave 2 but marked as NA for wave 2
# 2 adults in all three waves but marked as NA for waves 2, 3
# Note: quit_nonresp_reason_w2 == 1 are recanters
# Note: Maybe make current/former category and establish category then use these categories to create 4 sub categories
adult_panel <- adult_panel %>%
mutate( est_smoker_w2 = case_when(cig_num_life_w2== 6 | est_smoker_w1 == 1 ~1,
cig_num_life_w2 >= 1 & cig_num_life_w2 <= 5 ~ 0),
smoking_status_full_w2 = case_when(cig_current_freq_w2 %in% c(1,2) & est_smoker_w2 == 1 ~ 'current_est_smoker',
(cig_use_now_w2 == 0 | cig_use_past12M_w2 ==2) & est_smoker_w2 == 1 & quit_nonresp_reason_w2 != 1 ~ 'former_est_smoker',
cig_use_now_w2 == 1 & est_smoker_w2 == 0 ~ 'current_exp_smoker',
(cig_use_now_w2 == 0 | cig_use_past12M_w2 ==2) & est_smoker_w2 == 0 & quit_nonresp_reason_w2 != 1 ~ 'former_exp_smoker',
cig_use_ever_w2 == 0 ~'never_smoker'),
smoking_status_full_w2 = as.factor(smoking_status_full_w2),
smoking_status_w2 = fct_collapse(smoking_status_full_w2,
'current' = c('current_est_smoker', 'current_exp_smoker'),
'former' = c('former_est_smoker', 'former_exp_smoker')),
current_est_smoker_w2 = if_else(smoking_status_full_w2 == 'current_est_smoker', 1, 0),
former_est_smoker_w2 = if_else(smoking_status_full_w2 == 'former_est_smoker', 1, 0),
current_exp_smoker_w2 = if_else(smoking_status_full_w2 == 'current_exp_smoker', 1, 0),
former_exp_smoker_w2 = if_else(smoking_status_full_w2 == 'former_exp_smoker', 1, 0),
never_smoker_w2 = if_else(smoking_status_full_w2 == 'never_smoker', 1, 0)
)
#### New Variable Codes ####
# R02_AC1003: cig_current_freq_w2
# R02_AC1005: cig_num_life_w2
# R02_AC1002_12M: cig_use_past12M_w2
# R01_YC1005: from youth survey
# R02R_A_EVR_CIGS: cig_use_ever_w2
# R02_AC1132: quit_nonresp_reason_w2
#### My Derived Vars ####
adult_panel %>%
filter(wave_1 == 1, wave_2 == 1) %>%
count(smoking_status_full_w2, adult_cont_w2 )
#### Paths Vars ####
adult_panel %>%
filter( adult_cont_w2 == 1) %>%
count(R02R_A_CUR_ESTD_CIGS, R02R_A_CUR_EXPR_CIGS, R02R_A_FMR_ESTD_CIGS_REV, R02R_A_FMR_EXPR_CIGS_REV )
# Note: Maybe make current/former category and establish category then use these categories to create 4 sub categories
adult_panel <- adult_panel %>%
mutate( est_smoker_w3 = case_when(cig_num_life_w3== 6 | est_smoker_w2 == 1 ~1,
cig_num_life_w3 >= 1 & cig_num_life_w3 <= 5 ~ 0),
smoking_status_full_w3 = case_when(cig_current_freq_w3 %in% c(1,2) & est_smoker_w3 == 1 ~ 'current_est_smoker',
(cig_use_now_w3 == 0 | cig_use_past12M_w3 ==2) & est_smoker_w3 == 1 & quit_nonresp_reason_w3 != 3 ~ 'former_est_smoker',
cig_use_now_w3 == 1 & est_smoker_w3 == 0 ~ 'current_exp_smoker',
(cig_use_now_w3 == 0 | cig_use_past12M_w3 ==2) & est_smoker_w3 == 0 & quit_nonresp_reason_w3 != 3 ~ 'former_exp_smoker',
cig_use_ever_w3 == 0 ~'never_smoker'),
smoking_status_full_w3 = as.factor(smoking_status_full_w3),
smoking_status_w3 = fct_collapse(smoking_status_full_w3,
'current' = c('current_est_smoker', 'current_exp_smoker'),
'former' = c('former_est_smoker', 'former_exp_smoker')),
current_est_smoker_w3 = if_else(smoking_status_full_w3 == 'current_est_smoker', 1, 0),
former_est_smoker_w3 = if_else(smoking_status_full_w3 == 'former_est_smoker', 1, 0),
current_exp_smoker_w3 = if_else(smoking_status_full_w3 == 'current_exp_smoker', 1, 0),
former_exp_smoker_w3 = if_else(smoking_status_full_w3 == 'former_exp_smoker', 1, 0),
never_smoker_w3 = if_else(smoking_status_full_w3 == 'never_smoker', 1, 0)
)
#### My Derived Vars ####
adult_panel %>%
filter(wave_1 == 1, wave_2 == 1, wave_3== 1) %>%
count(smoking_status_full_w3)
#### Paths Vars ####
adult_panel %>%
filter( adult_cont_w2 == 1, adult_cont_w3== 1) %>%
count(R03R_A_CUR_ESTD_CIGS, R03R_A_CUR_EXPR_CIGS, R03R_A_FMR_ESTD_CIGS_REV, R03R_A_FMR_EXPR_CIGS_REV )
#### Write to Output File ####
# Note: not sure about CASEID, VARPSU, and VARSTAT
cols <- names(adult_panel) %>% str_subset("[a-z]")
cols <- cols[!str_detect(cols, '\\.x|\\.y') ]
cols
derived_vars <- c( "R01R_A_NVR_CIGS",
"R01R_A_CUR_ESTD_CIGS",
"R01R_A_CUR_EXPR_CIGS",
"R01R_A_FMR_EXPR_CIGS" ,
"R01R_A_FMR_EXPR_CIGS",
"R02R_A_CUR_ESTD_CIGS",
"R02R_A_CUR_EXPR_CIGS",
"R02R_A_FMR_ESTD_CIGS_REV",
"R02R_A_FMR_EXPR_CIGS_REV",
"R03R_A_CUR_ESTD_CIGS" ,
"R03R_A_CUR_EXPR_CIGS",
"R03R_A_FMR_ESTD_CIGS_REV",
"R03R_A_FMR_EXPR_CIGS_REV")
# R01R_A RO2R_A , RO3R_A : Derived Variables
adult_panel_final <- adult_panel %>% select(PERSONID, all_of(cols), all_of(derived_vars))
object.size(adult_panel_final) # 25.6 mb
write.csv(adult_panel_final, 'data/Output/adult_panel.csv', row.names =FALSE)
# note make sure git attributes includes csv files in lfs git system
# git lfs track "*.csv"
# git lfs ls-files -> see files being tracked
#### Check Representation across waves ####
adult_panel %>% glimpse
adult_panel %>% group_by(wave_1, wave_2, wave_3, adult_cont_w2 , adult_cont_w3) %>% count
# Derived Variable
adult_panel %>% dplyr::select(starts_with('R02R_A'))
## Matching tabel
# Five Cateories
adult_panel$smoking_status_full_w1 %>% table
# wave 1
adult_panel %>% count(R01R_A_NVR_CIGS)
adult_panel %>% count(R01R_A_CUR_ESTD_CIGS)
adult_panel %>% count( R01R_A_FMR_ESTD_CIGS)
adult_panel %>% count(R01R_A_FMR_EXPR_CIGS) # different number
adult_panel %>% count(R01R_A_CUR_EXPR_CIGS) # different number
# wave 2
adult_panel$smoking_status_full_w2 %>% table
adult_panel %>% count(R02R_A_EVR_CIGS) # Difference var name
adult_panel %>% count( R02R_A_FMR_ESTD_CIGS_REV ) # Difference var name
adult_panel %>% count(R02R_A_CUR_ESTD_CIGS)
adult_panel %>% count(R02R_A_FMR_EXPR_CIGS_REV) # Different var name
adult_panel %>% count(R02R_A_CUR_EXPR_CIGS)
#wave 3
adult_panel$smoking_status_full_w3 %>% table
adult_panel%>% filter( adult_cont_w3 ==1 ) %>% count(smoking_status_full_w3)
#adult_panel %>% select(starts_with('R03R_A_EVR')) %>% View # Cann't finn ever cigarette
adult_panel %>% count( R03R_A_FMR_ESTD_CIGS_REV )
adult_panel %>% count(R03R_A_CUR_ESTD_CIGS)
adult_panel %>% count(R03R_A_FMR_EXPR_CIGS_REV)
adult_panel %>% count(R03R_A_CUR_EXPR_CIGS)
#### checkes ####
# adult_panel %>%
# count(est_smoker_w2, cig_num_life_w1, cig_num_life_w2) %>%
# filter(est_smoker_w2 == 1) %>% View
adult_panel %>%
count(R02R_A_CUR_ESTD_CIGS)
## Wave1, Wave2 adult numbers match
## Does not include est. smokers from youth wave 1
## Does include wave 1 youths who became established smokers between waves 1 & 2
adult_panel %>%
filter(R02R_A_CUR_ESTD_CIGS== 1) %>%
count( adult_cont_w2 )
adult_panel %>%
filter(wave_1 == 1, wave_2 == 1) %>%
filter(smoking_status_full_w2 == 'current_est_smoker') %>% count
#
# adult_panel %>%
# filter( adult_cont_w2 == 1, cig_num_life_w2 >= 0, cig_use_now_w2 >= 0) %>%
# count(R02R_A_CUR_EXPR_CIGS, cig_num_life_w2,cig_use_now_w2, est_smoker_w2, smoking_status_full_w2 ) %>%
# View
adult_panel %>%
filter( adult_cont_w2 == 1, smoking_status_full_w2 == 'current_exp_smoker') %>%
count(cig_num_life_w2,cig_use_now_w2, est_smoker_w2, smoking_status_full_w2)
adult_panel %>%
filter( adult_cont_w2 == 1, smoking_status_full_w2 == 'former_est_smoker') %>%
count( R02R_A_FMR_ESTD_CIGS_REV, smoking_status_full_w2, est_smoker_w2, cig_use_now_w2, cig_use_past12M_w2 )
# adult_panel %>%
# count(cig_current_freq_w2, est_smoker_w2) %>%
# filter_all(all_vars(. >= 0)) %>%
# View
adult_panel %>% count(cig_use_ever_w2)
adult_panel %>% count(R01R_A_CUR_ESTD_CIGS)
adult_panel %>% count(cig_use_now_w1, quit_w1_w2)
adult_panel %>% count(cig_use_now_w1, quit_cat_w1_w2)
# Think I missing youths who reached 100 lifetime cigarette during youth survey
# Also think there are some new adults in wave 3
# adult_panel %>%
# count(R03R_A_THRSHLD_CIGS, adult_cont_w3, adult_cont_w2 ,est_smoker_w3, est_smoker_w2, est_smoker_w1) %>%
# View
#remove(list = c('adult_w1', 'adult_w2', 'adult_w3'))
#remove(list = c('adult_w1', 'adult_w2', 'adult_w3', 'adult_panel'))
#