/
clean_labour_force_survey.R
369 lines (308 loc) · 12.5 KB
/
clean_labour_force_survey.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
#' Load and Clean Labour Force Survey Data
#'
#' @description This functions automatically detects Labour Force Survey data in the specified
#' directory and reads it into R. It then extracts the age, sex, UK birth status, and country.
#' From this it creates a tidy dataset. The data can be downloaded [here](https://discover.ukdataservice.ac.uk/catalogue/?sn=5461).
#' @inheritParams clean_demographics_uk
#' @param years A numeric vector specifying which years of data to clean
#' @param years_var A named list of character strings. Each character string should contain the variables
#' to extract from a given year and this should be named with the year of data to extract.
#' @return A tidy data frame of population broken down by country, age, sex and UK birth status
#' for 2000 to 2015.
#' @export
#' @importFrom dplyr mutate select rename
#' @importFrom haven read_stata
#' @importFrom purrr pmap
#' @import ggplot2
#' @examples
#'
clean_labour_force_survey <- function(data_path = "~/data/tb_data/LFS",
years = 2000:2016,
years_var = list( '2000' = c('age', 'sex', 'cry', 'govtof', 'pwt07'),
'2001' = c('age', 'sex', 'cry01', 'country', 'pwt07'),
'2002' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2003' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2004' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2005' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2006' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2007' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2008' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2009' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2010' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2011' = c('AGE', 'SEX', 'CRY01', 'COUNTRY', 'PWT14'),
'2012' = c('AGE', 'SEX', 'CRY12', 'COUNTRY', 'PWT14'),
'2013' = c('AGE', 'SEX', 'CRY12', 'COUNTRY', 'PWT16'),
'2014' = c('AGE', 'SEX', 'CRY12', 'COUNTRY', 'PWT16'),
'2015' = c('AGE', 'SEX', 'CRY12', 'COUNTRY', 'PWT16'),
'2016' = c('AGE', 'SEX', 'CRY12', 'COUNTRY', 'PWT16')),
return = TRUE,
save = TRUE,
save_name = "formatted_LFS_2000_2016",
save_path = "~/data/tb_data/tbinenglanddataclean",
save_format = "rds",
verbose = TRUE,
theme_set = theme_minimal) {
# Read in LFS data --------------------------------------------------------
## data notes
## NA and DNA are coded as -8 and -9
## list data folders in directory
LFS_folders <- list.files(path = data_path)
## ignore data
LFS_folders <- LFS_folders[grep('rds', LFS_folders, invert = TRUE)]
## For each data folder in the directory
LFS_data <- LFS_folders %>% lapply(function(x){
## Find path for folder
folder_dir <- file.path(data_path, x)
## list data folder contents
folder_folders <- list.files(path = folder_dir)
## find stata folder
data_folder <- folder_folders[grep('stata', folder_folders)]
## find data folder dir
data_sub_path <- file.path(folder_dir, data_folder)
## contents of data folder
stata_folder <- list.files(path = data_sub_path)
## dat path
full_path <- file.path(data_sub_path, stata_folder)
if (verbose) {
message("Data loaded from: ", full_path)
}
## read in the data
df <- read_stata(file = full_path, encoding = "latin1")
return(df)
})
## name data list
names(LFS_data) <- LFS_folders
# Extract key variables and combine ---------------------------------------
form_LFS_data <- pmap(list(LFS_data, years, years_var), function(x, year, year_var){
x %>%
select_(.dots = year_var) %>%
mutate(Year = year) -> x
if (verbose) {
message("Cleaning LFS data from: ", year)
message("Variables extracted from dataset: ", paste(year_var, collapse = ", "))
}
## Account for no avail of country in 2000 and format in R format
if (year == 2000)
{
## clean age with R style missing
x %>%
mutate(Age = replace(age, age %in% c(-8, -9), NA)) %>%
select(-age) -> x
## country of residence
x %>%
mutate(Country = ifelse(govtof %in% c(1,2,3,4,5,6,7,8,9,10), 'England',
ifelse(govtof %in% c(11), 'Wales', ifelse(govtof %in% c(12), 'Scotland',
ifelse(govtof %in% c(13), 'Wales', NA))))) %>%
select(-govtof) -> x
## country of birth (UK/not UK)
x %>%
mutate(CoB = ifelse(cry %in% c(-8,-9), NA,
ifelse(cry %in% c(1), 'UK born', 'Non-UK born'))) %>%
select(-cry) -> x
## formating of sex
x %>%
mutate(Sex = ifelse(sex %in% c(-8,-9), NA,
ifelse(sex %in% c(1), 'Male', 'Female'))) %>%
select(-sex) -> x
## standardise weight
x %>%
rename(Weight = pwt07) -> x
}else if (year == 2001)
{
## clean age with R style missing
x %>%
mutate(Age = replace(age, age %in% c(-8, -9), NA)) %>%
select(-age) -> x
## country of residence
x %>%
mutate(Country = ifelse(country %in% c(-9, -8), NA,
ifelse(country %in% c(1), 'England',
ifelse(country %in% c(2), 'Wales',
ifelse(country %in% c(3,4), 'Scotland',
'Northern Ireland'))))) %>%
select(-country) -> x
## country of birth (UK/not UK)
x %>%
mutate(CoB = ifelse(cry01 %in% c(-8,-9), NA,
ifelse(cry01 %in% c(1,2,3,4,5), 'UK born', 'Non-UK born'))) %>%
select(-cry01) -> x
## formating of sex
x %>%
mutate(Sex = ifelse(sex %in% c(-8,-9), NA,
ifelse(sex %in% c(1), 'Male', 'Female'))) %>%
select(-sex) -> x
## standardise weight
x %>%
rename(Weight = pwt07) -> x
}else if (year %in% 2002:2011)
{
## clean age with R style missing
x %>%
mutate(Age = replace(AGE, AGE %in% c(-8, -9), NA)) %>%
select(-AGE) -> x
## country of residence
x %>%
mutate(Country = ifelse(COUNTRY %in% c(-9, -8), NA,
ifelse(COUNTRY %in% c(1), 'England',
ifelse(COUNTRY %in% c(2), 'Wales',
ifelse(COUNTRY %in% c(3,4), 'Scotland', 'Northern Ireland'))))) %>%
select(-COUNTRY) -> x
## Split due to errors in variable encoding between 2002 and 2007
if (year %in% c(2002:2006))
{
## country of birth (UK/not UK)
x %>%
mutate(CoB = ifelse(CRY01 %in% c(-8,-9), NA,
ifelse(CRY01 %in% c(1,2,3,4,5), 'UK born', 'Non-UK born'))) %>%
select(-CRY01) -> x
}else{
x %>%
mutate(CoB = ifelse(CRY01 %in% c(-8,-9), NA,
ifelse(CRY01 %in% c(921, 922, 923, 924, 926), 'UK born', 'Non-UK born'))) %>%
select(-CRY01) -> x
}
## country of birth (UK/not UK)
## formating of sex
x %>%
mutate(Sex = ifelse(SEX %in% c(-8,-9), NA,
ifelse(SEX %in% c(1), 'Male', 'Female'))) %>%
select(-SEX) -> x
## standardise weight
x %>%
rename(Weight = PWT14) -> x
}else if (year %in% 2012:2016)
{
## clean age with R style missing
x %>%
mutate(Age = replace(AGE, AGE %in% c(-8, -9), NA)) %>%
select(-AGE) -> x
## country of residence
x %>%
mutate(Country = ifelse(COUNTRY %in% c(-9, -8), NA,
ifelse(COUNTRY %in% c(1), 'England', ifelse(COUNTRY %in% c(2), 'Wales',
ifelse(COUNTRY %in% c(3,4), 'Scotland', 'Northern Ireland'))))) %>%
select(-COUNTRY) -> x
## country of birth (UK/not UK)
x %>%
mutate(CoB = ifelse(CRY12 %in% c(-8,-9), NA,
ifelse(CRY12 %in% c(921, 922, 923, 924, 926), 'UK born', 'Non-UK born'))) %>%
select(-CRY12) -> x
## formating of sex
x %>%
mutate(Sex = ifelse(SEX %in% c(-8,-9), NA,
ifelse(SEX %in% c(1), 'Male', 'Female'))) %>%
select(-SEX) -> x
#Set weights based on the variable
if (year %in% c(2012))
{
## standardise weight
x %>%
rename(Weight = PWT14) -> x
}else{
## standardise weight
x %>%
rename(Weight = PWT16) -> x
}
}else{
stop('Year has no defined variables, or cleaning process; check year_var')
}
return(x)
}) %>% bind_rows
## Set variables as factors
form_LFS_data <- form_LFS_data %>%
mutate(Country = factor(Country),
CoB = factor(CoB),
Sex = factor(Sex))
## standidise age with other data formats
form_LFS_data <- form_LFS_data %>%
mutate(Age = replace(Age, Age >= 90, '90+')) %>%
mutate(Age = factor(Age, levels = c(as.character(0:89), '90+')))
if (verbose) {
# Simple plots of the data ------------------------------------------------
## 2000 distribution by UK birth status
form_LFS_data %>%
filter(Year %in% 2000) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~CoB, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p
p
## 2005 distribution by UK birth status
form_LFS_data %>%
filter(Year %in% 2005) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~CoB, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p1
p1
## 2010 distribution by UK birth status
form_LFS_data %>%
filter(Year %in% 2010) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~CoB, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p2
p2
## 2015 distribution by UK birth status
form_LFS_data %>%
filter(Year %in% 2015) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~CoB, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p3
interactive_plot(p3, interactive)
## 2000 distribution by Country
form_LFS_data %>%
filter(Year %in% 2000) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~Country, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p4
p4
## 2005 distribution by Country
form_LFS_data %>%
filter(Year %in% 2005) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~Country, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p5
p5
## 2010 distribution by Country
form_LFS_data %>%
filter(Year %in% 2010) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~Country, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p6
p6
## 2015 distribution by Country
form_LFS_data %>%
filter(Year %in% 2015) %>%
ggplot(aes(x = Age)) +
geom_bar(alpha = 0.4) +
facet_wrap(~Country, scales = 'free', nrow = 3) +
theme_set() +
theme(axis.text.x = element_text(angle = 90)) -> p7
p7
}
## save formatted LFS data
if (save) {
save_data(form_LFS_data,
name = save_name,
path = save_path,
format = save_format,
message = "Cleaded LFS data has been saved to: ",
verbose = verbose
)
}
if(return) {
return(form_LFS_data)
}
}