-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRFSS_Data_Cleaning.R
371 lines (281 loc) · 9.01 KB
/
BRFSS_Data_Cleaning.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
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
###################################################
##### BRFSS Health Data Decriptive Analysis #####
########### Data Cleaning ###########
###################################################
# Import "foreign" package for reading the data
library(foreign)
# Setting working directory
getwd()
setwd("C:/...")
# Reading the data
brfss <- read.xport("data/MMSA2014.xpt")
dim(brfss)
colnames(brfss)
head(brfss)
# Define the object list of variables to be kept
varList <- c("VETERAN3", "ALCDAY5", "SLEPTIM1", "ASTHMA3", "X_AGE_G", "SMOKE100",
"SMOKDAY2", "SEX", "X_HISPANC", "X_MRACE1", "MARITAL", "GENHLTH",
"HLTHPLN1", "EDUCA", "INCOME2", "X_BMI5CAT", "EXERANY2")
# Create a subset of data by the variable list
brfss <- brfss[varList]
dim(brfss)
### Data Cleaning ###
# Remove observations before generating new variables
# Remove obs not in the subpopulation
# Subset the dataset for only veterans
brfss <- subset(brfss, VETERAN3==1)
# Remove obs does not have a valid value for exposure or outcome
# Only keep rows with with valid alcohol/exposure variable
brfss <- subset(brfss, ALCDAY5 < 777 | ALCDAY5==888)
# Only keep rows with valid sleep data
brfss <- subset(brfss, SLEPTIM1 < 77)
# Only keep rows with valid asthma data
brfss <- subset(brfss, ASTHMA3 < 7)
dim(brfss)
## Generating exposures variables
# Add the categorical variable set to 9 to the dataset
brfss$ALCGRP <- 9
# Update according to the data dictionary
# Drink Weekly = 3
brfss$ALCGRP[brfss$ALCDAY5 <200 ] <- 3
# Drink Monthly = 2
brfss$ALCGRP[brfss$ALCDAY5 >=200 & brfss$ALCDAY5 <777] <- 2
# No drink in past 30 days = 1
brfss$ALCGRP[brfss$ALCDAY5 == 888] <- 1
# Check the new variable
table(brfss$ALCGRP, brfss$ALCDAY5)
# Create dummy variable for each category
brfss$DRKMONTHLY <- 0
brfss$DRKMONTHLY[brfss$ALCGRP == 2] <- 1
brfss$DRKWEEKLY <- 0
brfss$DRKWEEKLY[brfss$ALCGRP == 3] <- 1
# Check the new dummy variables
table(brfss$ALCGRP, brfss$DRKMONTHLY)
table(brfss$ALCGRP, brfss$DRKWEEKLY)
## Create outcome variables
# Create the sleep variable
brfss$SLEPTIM2 <- NA
brfss$SLEPTIM2[!is.na(brfss$SLEPTIM1) & brfss$SLEPTIM1 !=77
& brfss$SLEPTIM1 !=99] <- brfss$SLEPTIM1
# Check the new sleep variable
table(brfss$SLEPTIM1, brfss$SLEPTIM2)
# Create the asthma variable
brfss$ASTHMA4 <- 9
brfss$ASTHMA4[brfss$ASTHMA3 == 1] <- 1
brfss$ASTHMA4[brfss$ASTHMA3 == 2] <- 0
# Check the new asthma variable
table(brfss$ASTHMA3, brfss$ASTHMA4)
# Create Age dummy variables (baseline group Age 18 to 24)
brfss$AGE2 <- 0
brfss$AGE3 <- 0
brfss$AGE4 <- 0
brfss$AGE5 <- 0
brfss$AGE6 <- 0
# Age 25 to 34
brfss$AGE2[brfss$X_AGE_G == 2] <- 1
# Age 35 to 44
brfss$AGE3[brfss$X_AGE_G == 3] <- 1
# Age 45 to 54
brfss$AGE4[brfss$X_AGE_G == 4] <- 1
# Age 55 to 64
brfss$AGE5[brfss$X_AGE_G == 5] <- 1
# Age 65 or above
brfss$AGE6[brfss$X_AGE_G == 6] <- 1
# Check the new age dummy variables
table(brfss$X_AGE_G, brfss$AGE2)
# Create Smoke dummy variables
# Dummy for never smoker
brfss$NEVERSMK <- 0
brfss$NEVERSMK [brfss$SMOKE100 == 2] <- 1
# Check the non-smoker dummy
table(brfss$SMOKE100, brfss$NEVERSMK)
# Smoker group, 1 who still smoke, 2 who no longer smoke
brfss$SMOKGRP <- 9
brfss$SMOKGRP[brfss$SMOKDAY2 == 1 | brfss$SMOKDAY2 == 2] <- 1
brfss$SMOKGRP[brfss$SMOKDAY2 == 3 | brfss$NEVERSMK == 1] <- 2
table(brfss$SMOKGRP, brfss$SMOKDAY2)
table(brfss$SMOKGRP, brfss$SMOKE100)
# Dummy for smoker
brfss$SMOKER <- 0
brfss$SMOKER[brfss$SMOKGRP == 1] <- 1
# Check the smoker dummy
table(brfss$SMOKGRP, brfss$SMOKER)
## Create other categorical variables for analysis:
# Create sex dummy variable
brfss$MALE <- 0
brfss$MALE[brfss$SEX == 1] <- 1
# Check the new sex dummy
table(brfss$MALE, brfss$SEX)
# Create new Hispanic dummy variable
brfss$HISPANIC <- 0
brfss$HISPANIC[brfss$X_HISPANC == 1] <- 1
# Check the new Hispanic dummy
table(brfss$HISPANIC, brfss$X_HISPANC)
# Create new race category variables
# Baseline category 9 = Don't know
brfss$RACEGRP <- 9
# 1 = White
brfss$RACEGRP[brfss$X_MRACE1 == 1] <- 1
# 2 = Black
brfss$RACEGRP[brfss$X_MRACE1 == 2] <- 2
# 3 = American Indian
brfss$RACEGRP[brfss$X_MRACE1 == 3] <- 3
# 4 = Asian
brfss$RACEGRP[brfss$X_MRACE1 == 4] <- 4
# 5 = Native Hawaiian / Pacific Islander
brfss$RACEGRP[brfss$X_MRACE1 == 5] <- 5
# 6 = Other race / multiracial
brfss$RACEGRP[brfss$X_MRACE1 == 6 | brfss$X_MRACE1 == 7] <- 6
# Check the new race variable
table(brfss$RACEGRP , brfss$X_MRACE1)
# Create now columns for Black, Asian and Others
brfss$BLACK <- 0
brfss$ASIAN <- 0
brfss$OTHRACE <- 0
# Create dummies for Black, Asian, and others
brfss$BLACK[brfss$RACEGRP == 2] <- 1
table(brfss$RACEGRP, brfss$BLACK)
brfss$ASIAN[brfss$RACEGRP == 4] <- 1
table(brfss$RACEGRP, brfss$ASIAN)
brfss$OTHRACE[brfss$RACEGRP == 3 | brfss$RACEGRP == 5 | brfss$RACEGRP == 6 | brfss$RACEGRP == 7] <- 1
table(brfss$RACEGRP, brfss$OTHRACE)
# Create the marital status category variables
# Baseline category: 9 = Don't know
brfss$MARGRP <- 9
# 1 = Married / Member of an unmarried couple
brfss$MARGRP[brfss$MARITAL == 1 | brfss$MARITAL == 5] <- 1
# 2 = Divorced / Widowed
brfss$MARGRP[brfss$MARITAL == 2 | brfss$MARITAL == 3 ] <- 2
# 3 = Never married
brfss$MARGRP[brfss$MARITAL == 4] <- 3
# Check the new categorical variable
table(brfss$MARGRP, brfss$MARITAL)
# Create dummy variables for each category
brfss$NEVERMAR <- 0
brfss$FORMERMAR <- 0
brfss$NEVERMAR[brfss$MARGRP == 3] <- 1
table(brfss$MARGRP, brfss$NEVERMAR)
brfss$FORMERMAR[brfss$MARGRP == 2] <- 1
table(brfss$MARGRP, brfss$FORMERMAR)
# Create the Genhealth category variables
# Baseline category: 9 = Refused or Don't know
brfss$GENHLTH2 <- 9
# 1 = Excellent
brfss$GENHLTH2[brfss$GENHLTH == 1] <- 1
# 2 = Very Good
brfss$GENHLTH2[brfss$GENHLTH == 2] <- 2
# 3 = Good
brfss$GENHLTH2[brfss$GENHLTH == 3] <- 3
# 4 = Fair
brfss$GENHLTH2[brfss$GENHLTH == 4] <- 4
# 5 = Poor
brfss$GENHLTH2[brfss$GENHLTH == 5] <- 5
# Check the new categorical variable
table(brfss$GENHLTH2, brfss$GENHLTH)
# Create Fair and Poor dummy
brfss$FAIRHLTH <- 0
brfss$POORHLTH <- 0
brfss$FAIRHLTH [brfss$GENHLTH2 == 4] <- 1
table(brfss$FAIRHLTH, brfss$GENHLTH2)
brfss$POORHLTH [brfss$GENHLTH2 == 5] <- 1
table(brfss$POORHLTH, brfss$GENHLTH2)
# Create health plan variables
brfss$HLTHPLN2 <- 9
# 1 = Yes
brfss$HLTHPLN2[brfss$HLTHPLN1 == 1] <- 1
# 2 = No
brfss$HLTHPLN2[brfss$HLTHPLN1 == 2] <- 2
table(brfss$HLTHPLN1, brfss$HLTHPLN2)
# Create dummy for no health plan
brfss$NOPLAN <- 0
brfss$NOPLAN [brfss$HLTHPLN2== 2] <- 1
table(brfss$NOPLAN, brfss$HLTHPLN2)
# Create education category variables
# Baseline category: 9 = refused
brfss$EDGROUP <- 9
# 1 = Below Grade 12
brfss$EDGROUP[brfss$EDUCA == 1 | brfss$EDUCA == 2 | brfss$EDUCA == 3] <- 1
# 2 = Grade 12 or GED
brfss$EDGROUP[brfss$EDUCA == 4] <- 2
# 3 = College 1 to 3 years
brfss$EDGROUP[brfss$EDUCA == 5] <- 3
# 4 = College 4 years or more
brfss$EDGROUP[brfss$EDUCA == 6] <- 4
table(brfss$EDGROUP, brfss$EDUCA)
# Create dummy for lowed education group and some college group
brfss$LOWED <- 0
brfss$SOMECOLL <- 0
brfss$LOWED[brfss$EDGROUP == 1 | brfss$EDGROUP == 2 ] <- 1
table(brfss$LOWED, brfss$EDGROUP)
brfss$SOMECOLL [brfss$EDGROUP == 3] <- 1
table(brfss$SOMECOLL, brfss$EDGROUP)
# Create income categorical variables
brfss$INCOME3 <- brfss$INCOME2
# Baseline category: 9 = Don't know or Refused
brfss$INCOME3[brfss$INCOME2 >=77] <- 9
table(brfss$INCOME2, brfss$INCOME3)
# Create dummies for each income group
brfss$INC1 <- 0
brfss$INC2 <- 0
brfss$INC3 <- 0
brfss$INC4 <- 0
brfss$INC5 <- 0
brfss$INC6 <- 0
brfss$INC7 <- 0
# <$10k
brfss$INC1[brfss$INCOME3 == 1] <- 1
table(brfss$INC1, brfss$INCOME3)
# <$15k
brfss$INC2[brfss$INCOME3 == 2] <- 1
table(brfss$INC2, brfss$INCOME3)
# <$20
brfss$INC3[brfss$INCOME3 == 3] <- 1
table(brfss$INC3, brfss$INCOME3)
# <$25
brfss$INC4[brfss$INCOME3 == 4] <- 1
table(brfss$INC4, brfss$INCOME3)
# <$35
brfss$INC5[brfss$INCOME3 == 5] <- 1
table(brfss$INC5, brfss$INCOME3)
# <$50
brfss$INC6[brfss$INCOME3 == 6] <- 1
table(brfss$INC6, brfss$INCOME3)
# <$75
brfss$INC7[brfss$INCOME3 == 7] <- 1
table(brfss$INC7, brfss$INCOME3)
# Create BMI categorical variables
# Basline category: 9 = Missing
brfss$BMICAT<- 9
# 1 = Underweight
brfss$BMICAT[brfss$X_BMI5CAT ==1] <- 1
# 2 = Normal
brfss$BMICAT[brfss$X_BMI5CAT ==2] <- 2
# 3 = Overweight
brfss$BMICAT[brfss$X_BMI5CAT ==3] <- 3
# 4 = Obese
brfss$BMICAT[brfss$X_BMI5CAT ==4] <- 4
table(brfss$BMICAT, brfss$X_BMI5CAT)
# Create dummies
brfss$UNDWT <- 0
brfss$OVWT <- 0
brfss$OBESE <- 0
brfss$UNDWT[brfss$BMICAT== 1] <- 1
table(brfss$UNDWT, brfss$BMICAT)
brfss$OVWT[brfss$BMICAT== 3] <- 1
table(brfss$OVWT, brfss$BMICAT)
brfss$OBESE[brfss$BMICAT== 4] <- 1
table(brfss$OBESE, brfss$BMICAT)
# Create exercise categorical variables
brfss$EXERANY3<- 9
brfss$EXERANY3[brfss$EXERANY2 ==1] <- 1
brfss$EXERANY3[brfss$EXERANY2 ==2] <- 2
table(brfss$EXERANY3, brfss$EXERANY2)
brfss$NOEXER <- 0
brfss$NOEXER[brfss$EXERANY3 ==2] <- 1
table(brfss$NOEXER, brfss$EXERANY3)
# Drop all NAs in the dataset
brfss_final <- na.omit(brfss)
# Check the data frame
dim(brfss_final)
# Write the clean dataset into csv file
write.csv(brfss_final, file="data/brfss_clean.csv")